|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test layout declaration and usage of block elements |
| 4 | + * |
| 5 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 6 | + * See COPYING.txt for license details. |
| 7 | + */ |
| 8 | +namespace Magento\Test\Integrity\Layout; |
| 9 | + |
| 10 | +use Magento\Framework\App\Utility\Files; |
| 11 | + |
| 12 | +class TemplatesTest extends \PHPUnit_Framework_TestCase |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @var array |
| 16 | + */ |
| 17 | + protected static $templates = []; |
| 18 | + |
| 19 | + /** |
| 20 | + * Collect declarations of containers per layout file that have aliases |
| 21 | + */ |
| 22 | + public static function setUpBeforeClass() |
| 23 | + { |
| 24 | + $count = 0; |
| 25 | + foreach (Files::init()->getLayoutFiles([], false) as $file) { |
| 26 | + $xml = simplexml_load_file($file); |
| 27 | + $templateElements = $xml->xpath('//block[@template]') ?: []; |
| 28 | + $fileTemplates = []; |
| 29 | + foreach ($templateElements as $node) { |
| 30 | + $fileTemplates[] = (string)$node['template']; |
| 31 | + } |
| 32 | + if (!empty($fileTemplates)) { |
| 33 | + self::$templates[$file] = $fileTemplates; |
| 34 | + $count += count($fileTemplates); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Test that references to template files follows canonical Vendor_Module::path/to/template.phtml format. |
| 41 | + * |
| 42 | + * path/to/template.phtml Format is prohibited. |
| 43 | + * @return void |
| 44 | + */ |
| 45 | + public function testTemplateFollowsCanonicalName() |
| 46 | + { |
| 47 | + $errors = []; |
| 48 | + 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)) { |
| 51 | + if (!isset($errors[$file])) { |
| 52 | + $errors[$file] = []; |
| 53 | + } |
| 54 | + $errors[$file][] = $template; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + if (count($errors) > 0) { |
| 59 | + $message = 'Failed to assert that the template reference follows the canonical format ' |
| 60 | + . 'Vendor_Module::path/to/template.phtml. Following files haven\'t pass verification:' . PHP_EOL; |
| 61 | + foreach ($errors as $file => $wrongTemplates) { |
| 62 | + $message .= $file . ':' . PHP_EOL; |
| 63 | + $message .= '- ' . implode(PHP_EOL . '- ', $wrongTemplates) . PHP_EOL; |
| 64 | + } |
| 65 | + $this->fail($message); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments