TemplateFilesTest.php 5.49 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Test\Integrity\Theme;

class TemplateFilesTest extends \Magento\TestFramework\TestCase\AbstractIntegrity
{
    /**
     * Note that data provider is not used in conventional way in order to not overwhelm test statistics
     */
    public function testTemplates()
    {
        $invalidTemplates = [];
        foreach ($this->templatesDataProvider() as $template) {
            list($area, $themeId, $module, $file, $xml) = $template;
            $params = ['area' => $area, 'themeId' => $themeId, 'module' => $module];
            try {
                $templateFilename = \Magento\TestFramework\Helper\Bootstrap::getObjectmanager()
                    ->get(\Magento\Framework\View\FileSystem::class)
                    ->getTemplateFileName($file, $params);
                $this->assertFileExists($templateFilename);
            } catch (\PHPUnit\Framework\ExpectationFailedException $e) {
                $invalidTemplates[] = "File \"{$templateFilename}\" does not exist." .
                    PHP_EOL .
                    "Parameters: {$area}/{$themeId} {$module}::{$file}" .
                    PHP_EOL .
                    'Layout update: ' .
                    $xml;
            }
        }

        $this->assertEmpty(
            $invalidTemplates,
            "Invalid templates found:\n\n" . implode("\n-----\n", $invalidTemplates)
        );
    }

    public function templatesDataProvider()
    {
        $templates = [];

        $themes = $this->_getDesignThemes();
        foreach ($themes as $theme) {
            /** @var \Magento\Framework\View\Layout\ProcessorInterface $layoutUpdate */
            $layoutUpdate = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
                \Magento\Framework\View\Layout\ProcessorInterface::class,
                ['theme' => $theme]
            );
            $layoutTemplates = $this->_getLayoutTemplates($layoutUpdate->getFileLayoutUpdatesXml());
            foreach ($layoutTemplates as $templateData) {
                $templates[] = array_merge([$theme->getArea(), $theme->getId()], $templateData);
            }
        }

        return $templates;
    }

    /**
     * Get templates list that are defined in layout
     *
     * @param  \SimpleXMLElement $layoutXml
     * @return array
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     */
    protected function _getLayoutTemplates($layoutXml)
    {
        $templates = [];

        $blocks = $layoutXml->xpath('//block');
        foreach ($blocks as $block) {
            $attributes = $block->attributes();
            if (isset($attributes['template'])) {
                $module = $this->_getBlockModule($block);
                if (!$this->_isTemplateForDisabledModule($module, (string)$attributes['template'])) {
                    $templates[] = [$module, (string)$attributes['template'], $block->asXML()];
                }
            }
        }

        $layoutTemplates = $layoutXml->xpath('//template');
        foreach ($layoutTemplates as $template) {
            $action = $template->xpath("parent::*");
            $attributes = $action[0]->attributes();
            switch ($attributes['method']) {
                case 'setTemplate':
                    $parent = $action[0]->xpath("parent::*");
                    $attributes = $parent[0]->attributes();
                    $referenceName = (string)$attributes['name'];
                    $block = $layoutXml->xpath(
                        "//block[@name='{$referenceName}'] | //referenceBlock[@name='{$referenceName}']"
                    );
                    $module = $this->_getBlockModule($block[0]);
                    if (!$template->attributes() && !$this->_isTemplateForDisabledModule($module, (string)$template)) {
                        $templates[] = [$module, (string)$template, $parent[0]->asXml()];
                    }
                    break;
                case 'addInformationRenderer':
                case 'addMergeSettingsBlockType':
                    $blockType = $action[0]->xpath('block');
                    $module = $this->_getBlockModule($blockType[0]);
                    if (!$this->_isTemplateForDisabledModule($module, (string)$template)) {
                        $templates[] = [$module, (string)$template, $action[0]->asXml()];
                    }
                    break;
                default:
                    break;
            }
        }
        return $templates;
    }

    /**
     * Get module name based on block definition in xml layout
     *
     * @param  \SimpleXMLElement $xmlNode
     * @return string
     */
    protected function _getBlockModule($xmlNode)
    {
        $attributes = $xmlNode->attributes();
        if (isset($attributes['type'])) {
            $class = (string)$attributes['type'];
        } else {
            $class = (string)$xmlNode;
        }
        $blockModule = substr($class, 0, strpos($class, '_Block'));
        return $blockModule;
    }

    /**
     * Returns whether template belongs to a disabled module
     *
     * @param string $blockModule Module of a block that will render this template
     * @param string $template
     * @return bool
     */
    protected function _isTemplateForDisabledModule($blockModule, $template)
    {
        $enabledModules = $this->_getEnabledModules();

        if (!isset($enabledModules[$blockModule])) {
            return true;
        }
        return $this->_isFileForDisabledModule($template);
    }
}