Skip to content

Commit

Permalink
MAGETWO-66675: Fix relative template references in individual Magento…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
vrann committed Mar 24, 2017
1 parent d0639be commit cd47cca
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</referenceBlock>
<referenceContainer name="content">
<block class="Magento\CatalogSearch\Block\Advanced\Result" name="catalogsearch_advanced_result" template="Magento_CatalogSearch::advanced/result.phtml">
<block class="Magento\CatalogSearch\Block\SearchResult\ListProduct" name="search_result_list" template="Magento_CatalogSearch::product/list.phtml">
<block class="Magento\CatalogSearch\Block\SearchResult\ListProduct" name="search_result_list" template="Magento_Catalog::product/list.phtml">
<block class="Magento\Catalog\Block\Product\ProductList\Toolbar" name="product_list_toolbar" template="Magento_Catalog::product/list/toolbar.phtml">
<block class="Magento\Theme\Block\Html\Pager" name="product_list_toolbar_pager"/>
</block>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<attribute name="class" value="page-products"/>
<referenceContainer name="content">
<block class="Magento\CatalogSearch\Block\Result" name="search.result" template="Magento_CatalogSearch::result.phtml" cacheable="false">
<block class="Magento\CatalogSearch\Block\SearchResult\ListProduct" name="search_result_list" template="Magento_CatalogSearch::product/list.phtml" cacheable="false">
<block class="Magento\CatalogSearch\Block\SearchResult\ListProduct" name="search_result_list" template="Magento_Catalog::product/list.phtml" cacheable="false">
<arguments>
<!-- If argument's position depends on image size changeable in VDE:
positions:list-secondary,grid-secondary,list-actions,grid-actions,list-primary,grid-primary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@ class TemplatesTest extends \PHPUnit_Framework_TestCase
*/
protected static $templates = [];

/**
* @var array
*/
protected static $blockVirtualTypes = [];

/**
* Collect declarations of containers per layout file that have aliases
*/
public static function setUpBeforeClass()
{
$count = 0;
self::getBlockVirtualTypesWithDifferentModule();
foreach (Files::init()->getLayoutFiles([], false) as $file) {
$xml = simplexml_load_file($file);
$templateElements = $xml->xpath('//block[@template]') ?: [];
$blocks = $xml->xpath('//block[@template]') ?: [];
$fileTemplates = [];
foreach ($templateElements as $node) {
$fileTemplates[] = (string)$node['template'];
foreach ($blocks as $block) {
$fileTemplates[] = ['class' => (string)$block['class'], 'file' => (string)$block['template']];
}
if (!empty($fileTemplates)) {
self::$templates[$file] = $fileTemplates;
Expand All @@ -45,13 +51,18 @@ public static function setUpBeforeClass()
public function testTemplateFollowsCanonicalName()
{
$errors = [];
$warnings = [];
foreach (self::$templates as $file => $templates) {
foreach ($templates as $template) {
if (!preg_match('/[A-Za-z0-9]_[A-Za-z0-9]+\:\:[A-Za-z0-9\\_\.]+/', $template)) {
foreach ($templates as $templatePair) {
if (!preg_match('/[A-Za-z0-9]_[A-Za-z0-9]+\:\:[A-Za-z0-9\\_\-\.]+/', $templatePair['file'])) {
if (!isset($errors[$file])) {
$errors[$file] = [];
}
$errors[$file][] = $template;
$errors[$file][] = $templatePair['file'];
} else {
if (isset(self::$blockVirtualTypes[$templatePair['class']])) {
$warnings[$file][] = $templatePair;
}
}
}
}
Expand All @@ -66,4 +77,32 @@ public function testTemplateFollowsCanonicalName()
$this->fail($message);
}
}

/**
* Initialize array with the Virtual types for blocks
*
* Contains just those occurrences where base type and virtual type are located in different modules
*/
private static function getBlockVirtualTypesWithDifferentModule()
{
$virtual = \Magento\Framework\App\Utility\Classes::getVirtualClasses();
foreach ($virtual as $className => $resolvedName) {
if (strpos($resolvedName, 'Block') !== false) {
$matches = [];
preg_match('/([A-Za-z0-9]+\\\\[A-Za-z0-9]+).*/', $className, $matches);
if (count($matches) > 1) {
$oldModule = $matches[1];
} else {
$oldModule = $className;
}

$matches = [];
preg_match('/([A-Za-z0-9]+\\\\[A-Za-z0-9]+).*/', $resolvedName, $matches);
$newModule = $matches[1];
if ($oldModule != $newModule) {
self::$blockVirtualTypes[$className] = $resolvedName;
}
}
}
}
}

0 comments on commit cd47cca

Please sign in to comment.