Skip to content

Commit cd47cca

Browse files
committed
MAGETWO-66675: Fix relative template references in individual Magento modules #1895
- Corrected templates referencing the module of the Virtual type versus the module of the base - Add static test for the Blocks virtual types
1 parent d0639be commit cd47cca

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

app/code/Magento/CatalogSearch/view/frontend/layout/catalogsearch_advanced_result.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</referenceBlock>
1616
<referenceContainer name="content">
1717
<block class="Magento\CatalogSearch\Block\Advanced\Result" name="catalogsearch_advanced_result" template="Magento_CatalogSearch::advanced/result.phtml">
18-
<block class="Magento\CatalogSearch\Block\SearchResult\ListProduct" name="search_result_list" template="Magento_CatalogSearch::product/list.phtml">
18+
<block class="Magento\CatalogSearch\Block\SearchResult\ListProduct" name="search_result_list" template="Magento_Catalog::product/list.phtml">
1919
<block class="Magento\Catalog\Block\Product\ProductList\Toolbar" name="product_list_toolbar" template="Magento_Catalog::product/list/toolbar.phtml">
2020
<block class="Magento\Theme\Block\Html\Pager" name="product_list_toolbar_pager"/>
2121
</block>

app/code/Magento/CatalogSearch/view/frontend/layout/catalogsearch_result_index.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<attribute name="class" value="page-products"/>
1111
<referenceContainer name="content">
1212
<block class="Magento\CatalogSearch\Block\Result" name="search.result" template="Magento_CatalogSearch::result.phtml" cacheable="false">
13-
<block class="Magento\CatalogSearch\Block\SearchResult\ListProduct" name="search_result_list" template="Magento_CatalogSearch::product/list.phtml" cacheable="false">
13+
<block class="Magento\CatalogSearch\Block\SearchResult\ListProduct" name="search_result_list" template="Magento_Catalog::product/list.phtml" cacheable="false">
1414
<arguments>
1515
<!-- If argument's position depends on image size changeable in VDE:
1616
positions:list-secondary,grid-secondary,list-actions,grid-actions,list-primary,grid-primary

dev/tests/static/testsuite/Magento/Test/Integrity/Layout/TemplatesTest.php

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,24 @@ class TemplatesTest extends \PHPUnit_Framework_TestCase
1616
*/
1717
protected static $templates = [];
1818

19+
/**
20+
* @var array
21+
*/
22+
protected static $blockVirtualTypes = [];
23+
1924
/**
2025
* Collect declarations of containers per layout file that have aliases
2126
*/
2227
public static function setUpBeforeClass()
2328
{
2429
$count = 0;
30+
self::getBlockVirtualTypesWithDifferentModule();
2531
foreach (Files::init()->getLayoutFiles([], false) as $file) {
2632
$xml = simplexml_load_file($file);
27-
$templateElements = $xml->xpath('//block[@template]') ?: [];
33+
$blocks = $xml->xpath('//block[@template]') ?: [];
2834
$fileTemplates = [];
29-
foreach ($templateElements as $node) {
30-
$fileTemplates[] = (string)$node['template'];
35+
foreach ($blocks as $block) {
36+
$fileTemplates[] = ['class' => (string)$block['class'], 'file' => (string)$block['template']];
3137
}
3238
if (!empty($fileTemplates)) {
3339
self::$templates[$file] = $fileTemplates;
@@ -45,13 +51,18 @@ public static function setUpBeforeClass()
4551
public function testTemplateFollowsCanonicalName()
4652
{
4753
$errors = [];
54+
$warnings = [];
4855
foreach (self::$templates as $file => $templates) {
49-
foreach ($templates as $template) {
50-
if (!preg_match('/[A-Za-z0-9]_[A-Za-z0-9]+\:\:[A-Za-z0-9\\_\.]+/', $template)) {
56+
foreach ($templates as $templatePair) {
57+
if (!preg_match('/[A-Za-z0-9]_[A-Za-z0-9]+\:\:[A-Za-z0-9\\_\-\.]+/', $templatePair['file'])) {
5158
if (!isset($errors[$file])) {
5259
$errors[$file] = [];
5360
}
54-
$errors[$file][] = $template;
61+
$errors[$file][] = $templatePair['file'];
62+
} else {
63+
if (isset(self::$blockVirtualTypes[$templatePair['class']])) {
64+
$warnings[$file][] = $templatePair;
65+
}
5566
}
5667
}
5768
}
@@ -66,4 +77,32 @@ public function testTemplateFollowsCanonicalName()
6677
$this->fail($message);
6778
}
6879
}
80+
81+
/**
82+
* Initialize array with the Virtual types for blocks
83+
*
84+
* Contains just those occurrences where base type and virtual type are located in different modules
85+
*/
86+
private static function getBlockVirtualTypesWithDifferentModule()
87+
{
88+
$virtual = \Magento\Framework\App\Utility\Classes::getVirtualClasses();
89+
foreach ($virtual as $className => $resolvedName) {
90+
if (strpos($resolvedName, 'Block') !== false) {
91+
$matches = [];
92+
preg_match('/([A-Za-z0-9]+\\\\[A-Za-z0-9]+).*/', $className, $matches);
93+
if (count($matches) > 1) {
94+
$oldModule = $matches[1];
95+
} else {
96+
$oldModule = $className;
97+
}
98+
99+
$matches = [];
100+
preg_match('/([A-Za-z0-9]+\\\\[A-Za-z0-9]+).*/', $resolvedName, $matches);
101+
$newModule = $matches[1];
102+
if ($oldModule != $newModule) {
103+
self::$blockVirtualTypes[$className] = $resolvedName;
104+
}
105+
}
106+
}
107+
}
69108
}

0 commit comments

Comments
 (0)