|
7 | 7 | */
|
8 | 8 | namespace Magento\Test\Legacy;
|
9 | 9 |
|
| 10 | +use Magento\Framework\Component\ComponentRegistrar; |
| 11 | + |
10 | 12 | /**
|
11 | 13 | * Static test for phtml template files.
|
12 | 14 | */
|
@@ -105,4 +107,84 @@ function ($file) {
|
105 | 107 | \Magento\Framework\App\Utility\Files::init()->getPhtmlFiles()
|
106 | 108 | );
|
107 | 109 | }
|
| 110 | + |
| 111 | + public function testJsComponentsAreProperlyInitializedInDataMageInitAttribute() |
| 112 | + { |
| 113 | + $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this); |
| 114 | + $invoker( |
| 115 | + /** |
| 116 | + * JS components in data-mage-init attributes should be initialized not in php. |
| 117 | + * JS components should be initialized in templates for them to be properly statically analyzed for bundling. |
| 118 | + * |
| 119 | + * @param string $file |
| 120 | + */ |
| 121 | + function ($file) { |
| 122 | + $whiteList = $this->getWhiteList(); |
| 123 | + if (!in_array($file, $whiteList, true) |
| 124 | + && (strpos($file, '/view/frontend/templates/') !== false |
| 125 | + || strpos($file, '/view/base/templates/') !== false) |
| 126 | + ) { |
| 127 | + self::assertNotRegExp( |
| 128 | + '/data-mage-init=(?:\'|")(?!\s*{\s*"[^"]+")/', |
| 129 | + file_get_contents($file), |
| 130 | + 'Please do not initialize JS component in php. Do it in template.' |
| 131 | + ); |
| 132 | + } |
| 133 | + }, |
| 134 | + \Magento\Framework\App\Utility\Files::init()->getPhtmlFiles() |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @return array |
| 140 | + */ |
| 141 | + private function getWhiteList() |
| 142 | + { |
| 143 | + $whiteListFiles = []; |
| 144 | + $componentRegistrar = new ComponentRegistrar(); |
| 145 | + foreach ($this->getFilesData('data_mage_init/whitelist.php') as $fileInfo) { |
| 146 | + $whiteListFiles[] = $componentRegistrar->getPath(ComponentRegistrar::MODULE, $fileInfo[0]) |
| 147 | + . DIRECTORY_SEPARATOR . $fileInfo[1]; |
| 148 | + } |
| 149 | + return $whiteListFiles; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @param string $filePattern |
| 154 | + * @return array |
| 155 | + */ |
| 156 | + private function getFilesData($filePattern) |
| 157 | + { |
| 158 | + $result = []; |
| 159 | + foreach (glob(__DIR__ . '/_files/initialize_javascript/' . $filePattern) as $file) { |
| 160 | + $fileData = include $file; |
| 161 | + $result = array_merge($result, $fileData); |
| 162 | + } |
| 163 | + return $result; |
| 164 | + } |
| 165 | + |
| 166 | + public function testJsComponentsAreProperlyInitializedInXMagentoInitAttribute() |
| 167 | + { |
| 168 | + $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this); |
| 169 | + $invoker( |
| 170 | + /** |
| 171 | + * JS components in x-magento-init attributes should be initialized not in php. |
| 172 | + * JS components should be initialized in templates for them to be properly statically analyzed for bundling. |
| 173 | + * |
| 174 | + * @param string $file |
| 175 | + */ |
| 176 | + function ($file) { |
| 177 | + if (strpos($file, '/view/frontend/templates/') !== false |
| 178 | + || strpos($file, '/view/base/templates/') !== false |
| 179 | + ) { |
| 180 | + self::assertNotRegExp( |
| 181 | + '@x-magento-init.>(?!\s*+{\s*"[^"]+"\s*:\s*{\s*"[\w/-]+")@i', |
| 182 | + file_get_contents($file), |
| 183 | + 'Please do not initialize JS component in php. Do it in template.' |
| 184 | + ); |
| 185 | + } |
| 186 | + }, |
| 187 | + \Magento\Framework\App\Utility\Files::init()->getPhtmlFiles() |
| 188 | + ); |
| 189 | + } |
108 | 190 | }
|
0 commit comments