WhitelistDeclarationTest.php 5.5 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Magento\Setup\Declaration;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\App\Utility\Files;
use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Component\ComponentRegistrarInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Setup\Declaration\Schema\Dto\Constraint;
use Magento\Framework\Setup\Declaration\Schema\Dto\Index;
use Magento\Framework\Setup\Declaration\Schema\SchemaConfigInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\ObjectManager;

/**
 * Class WhitelistDeclarationTest
 */
class WhitelistDeclarationTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var ComponentRegistrarInterface
     */
    private $componentRegistrar;

    /**
     * @var SchemaConfigInterface
     */
    private $schemaConfig;

    public function setUp()
    {
        /** @var ObjectManagerInterface|ObjectManager $objectManager */
        $objectManager = Bootstrap::getObjectManager();
        $resourceConnection = $objectManager->create(ResourceConnection::class);
        $objectManager->removeSharedInstance(ResourceConnection::class);
        $objectManager->addSharedInstance($resourceConnection, ResourceConnection::class);
        $this->componentRegistrar = $objectManager->get(ComponentRegistrarInterface::class);
        $this->schemaConfig = $objectManager->create(SchemaConfigInterface::class);
    }

    /**
     * Checks that all declared table elements also declared into whitelist declaration.
     *
     * @magentoAppIsolation enabled
     * @throws \Exception
     */
    public function testConstraintsAndIndexesAreWhitelisted()
    {
        $undeclaredElements = [];
        $resultMessage = "New table elements that do not exist in the whitelist declaration:\n";
        $whitelistTables = $this->getWhiteListTables();
        $declarativeSchema = $this->schemaConfig->getDeclarationConfig();

        foreach ($declarativeSchema->getTables() as $schemaTable) {
            $tableNameWithoutPrefix = $schemaTable->getNameWithoutPrefix();
            foreach ($schemaTable->getConstraints() as $constraint) {
                $constraintNameWithoutPrefix = $constraint->getNameWithoutPrefix();
                if (isset($whitelistTables[$tableNameWithoutPrefix][Constraint::TYPE][$constraintNameWithoutPrefix])) {
                    continue;
                }

                $undeclaredElements[$tableNameWithoutPrefix][Constraint::TYPE][] = $constraintNameWithoutPrefix;
            }

            foreach ($schemaTable->getIndexes() as $index) {
                $indexNameWithoutPrefix = $index->getNameWithoutPrefix();
                if (isset($whitelistTables[$tableNameWithoutPrefix][Index::TYPE][$indexNameWithoutPrefix])) {
                    continue;
                }

                $undeclaredElements[$tableNameWithoutPrefix][Index::TYPE][] = $indexNameWithoutPrefix;
            }
        }

        $undeclaredElements = $this->filterUndeclaredElements($undeclaredElements);

        if (!empty($undeclaredElements)) {
            $resultMessage .= json_encode($undeclaredElements, JSON_PRETTY_PRINT);
        }

        $this->assertEmpty($undeclaredElements, $resultMessage);
    }

    /**
     * Excludes ignored elements from the list of undeclared table elements.
     *
     * @param array $undeclaredElements
     * @return array
     */
    private function filterUndeclaredElements(array $undeclaredElements): array
    {
        $files = Files::getFiles([__DIR__ . '/_files/ignore_whitelisting'], '*.json');
        $ignoredElements = [];
        foreach ($files as $filePath) {
            $ignoredElements = array_merge_recursive(
                $ignoredElements,
                json_decode(file_get_contents($filePath), true)
            );
        }

        return $this->arrayRecursiveDiff($undeclaredElements, $ignoredElements);
    }

    /**
     * Performs a recursive comparison of two arrays.
     *
     * @param array $array1
     * @param array $array2
     * @return array
     */
    private function arrayRecursiveDiff(array $array1, array $array2): array
    {
        $diffResult = [];

        foreach ($array1 as $key => $value) {
            if (array_key_exists($key, $array2)) {
                if (is_array($value)) {
                    $recursiveDiffResult = $this->arrayRecursiveDiff($value, $array2[$key]);
                    if (count($recursiveDiffResult)) {
                        $diffResult[$key] = $recursiveDiffResult;
                    }
                } else {
                    if (!in_array($value, $array2)) {
                        $diffResult[] = $value;
                    }
                }
            } else {
                $diffResult[$key] = $value;
            }
        }

        return $diffResult;
    }

    /**
     * @return array
     */
    private function getWhiteListTables(): array
    {
        $whiteListTables = [];

        foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $path) {
            $whiteListPath = $path . DIRECTORY_SEPARATOR . 'etc' .
                DIRECTORY_SEPARATOR . 'db_schema_whitelist.json';

            if (file_exists($whiteListPath)) {
                $whiteListTables = array_replace_recursive(
                    $whiteListTables,
                    json_decode(file_get_contents($whiteListPath), true)
                );
            }
        }

        return $whiteListTables;
    }
}