Skip to content

Commit bfd1fd1

Browse files
merge magento/2.3-develop into magento-epam/EPAM-PR-83
2 parents 6993fce + 5f1a803 commit bfd1fd1

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

dev/tests/static/testsuite/Magento/Test/Legacy/PhtmlTemplateTest.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88
namespace Magento\Test\Legacy;
99

10+
use Magento\Framework\Component\ComponentRegistrar;
11+
1012
/**
1113
* Static test for phtml template files.
1214
*/
@@ -105,4 +107,84 @@ function ($file) {
105107
\Magento\Framework\App\Utility\Files::init()->getPhtmlFiles()
106108
);
107109
}
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+
}
108190
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
/**
7+
* List of templates with data-mage-init attribute where JS component is not correctly called.
8+
*
9+
* JS component is initialized in php here. These templates cannot be refactored easily. This list consists of
10+
* module name and template path within module.
11+
*/
12+
return [
13+
['Magento_Braintree', 'view/frontend/templates/paypal/button_shopping_cart.phtml']
14+
];

0 commit comments

Comments
 (0)