DesignTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\View;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Store\Model\ScopeInterface;
  9. /**
  10. * @magentoComponentsDir Magento/Theme/Model/_files/design
  11. * @magentoDbIsolation enabled
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. */
  14. class DesignTest extends \PHPUnit\Framework\TestCase
  15. {
  16. /**
  17. * @var \Magento\Framework\View\DesignInterface
  18. */
  19. protected $_model;
  20. /**
  21. * @var \Magento\Framework\View\FileSystem
  22. */
  23. protected $_viewFileSystem;
  24. /**
  25. * @var \Magento\Framework\View\ConfigInterface
  26. */
  27. protected $_viewConfig;
  28. public static function setUpBeforeClass()
  29. {
  30. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  31. /** @var \Magento\Framework\Filesystem $filesystem */
  32. $filesystem = $objectManager->get(\Magento\Framework\Filesystem::class);
  33. $themeDir = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
  34. $themeDir->delete('theme/frontend');
  35. $themeDir->delete('theme/_merged');
  36. $libDir = $filesystem->getDirectoryWrite(DirectoryList::LIB_WEB);
  37. $libDir->copyFile('prototype/prototype.js', 'prototype/prototype.min.js');
  38. }
  39. public static function tearDownAfterClass()
  40. {
  41. /** @var \Magento\Framework\Filesystem $filesystem */
  42. $filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  43. ->get(\Magento\Framework\Filesystem::class);
  44. $libDir = $filesystem->getDirectoryWrite(DirectoryList::LIB_WEB);
  45. $libDir->delete('prototype/prototype.min.js');
  46. }
  47. protected function setUp()
  48. {
  49. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  50. /** @var \Magento\Theme\Model\Theme\Registration $registration */
  51. $registration = $objectManager->get(
  52. \Magento\Theme\Model\Theme\Registration::class
  53. );
  54. $registration->register();
  55. $this->_model = $objectManager->create(\Magento\Framework\View\DesignInterface::class);
  56. $this->_viewFileSystem = $objectManager->create(\Magento\Framework\View\FileSystem::class);
  57. $this->_viewConfig = $objectManager->create(\Magento\Framework\View\ConfigInterface::class);
  58. $objectManager->get(\Magento\Framework\App\State::class)->setAreaCode('frontend');
  59. }
  60. /**
  61. * Emulate fixture design theme
  62. *
  63. * @param string $themePath
  64. */
  65. protected function _emulateFixtureTheme($themePath = 'Test_FrameworkThemeTest/default')
  66. {
  67. \Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea('frontend');
  68. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  69. $objectManager->get(\Magento\Framework\View\DesignInterface::class)->setDesignTheme($themePath);
  70. $this->_viewFileSystem = $objectManager->create(\Magento\Framework\View\FileSystem::class);
  71. $this->_viewConfig = $objectManager->create(\Magento\Framework\View\ConfigInterface::class);
  72. }
  73. public function testSetGetArea()
  74. {
  75. $this->assertEquals(\Magento\Framework\View\DesignInterface::DEFAULT_AREA, $this->_model->getArea());
  76. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\App\State::class)
  77. ->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);
  78. $this->assertEquals(\Magento\Framework\App\Area::AREA_ADMINHTML, $this->_model->getArea());
  79. }
  80. public function testSetDesignTheme()
  81. {
  82. $this->_model->setDesignTheme('Magento/blank', 'frontend');
  83. $this->assertEquals('Magento/blank', $this->_model->getDesignTheme()->getThemePath());
  84. }
  85. public function testGetDesignTheme()
  86. {
  87. $this->assertInstanceOf(\Magento\Framework\View\Design\ThemeInterface::class, $this->_model->getDesignTheme());
  88. }
  89. /**
  90. * @magentoConfigFixture current_store design/theme/theme_id 0
  91. */
  92. public function testGetConfigurationDesignThemeDefaults()
  93. {
  94. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  95. $themes = ['frontend' => 'test_f', 'adminhtml' => 'test_a'];
  96. $design = $objectManager->create(\Magento\Theme\Model\View\Design::class, ['themes' => $themes]);
  97. $objectManager->addSharedInstance($design, \Magento\Theme\Model\View\Design::class);
  98. $model = $objectManager->get(\Magento\Theme\Model\View\Design::class);
  99. $this->assertEquals('test_f', $model->getConfigurationDesignTheme());
  100. $this->assertEquals('test_f', $model->getConfigurationDesignTheme('frontend'));
  101. $this->assertEquals('test_f', $model->getConfigurationDesignTheme('frontend', ['store' => 0]));
  102. $this->assertEquals('test_f', $model->getConfigurationDesignTheme('frontend', ['store' => null]));
  103. $this->assertEquals('test_a', $model->getConfigurationDesignTheme('adminhtml'));
  104. $this->assertEquals('test_a', $model->getConfigurationDesignTheme('adminhtml', ['store' => uniqid()]));
  105. }
  106. /**
  107. * @magentoConfigFixture current_store design/theme/theme_id one
  108. * @magentoDataFixture Magento/Store/_files/core_fixturestore.php
  109. */
  110. public function testGetConfigurationDesignThemeStore()
  111. {
  112. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  113. /** @var \Magento\Framework\App\Config\MutableScopeConfigInterface $mutableConfig */
  114. $mutableConfig = $objectManager->get(\Magento\Framework\App\Config\MutableScopeConfigInterface::class);
  115. $mutableConfig->setValue('design/theme/theme_id', 'two', ScopeInterface::SCOPE_STORE, 'fixturestore');
  116. $storeId = $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class)
  117. ->getStore()
  118. ->getId();
  119. $this->assertEquals('one', $this->_model->getConfigurationDesignTheme());
  120. $this->assertEquals('one', $this->_model->getConfigurationDesignTheme(null, ['store' => $storeId]));
  121. $this->assertEquals('one', $this->_model->getConfigurationDesignTheme('frontend', ['store' => $storeId]));
  122. $this->assertEquals('two', $this->_model->getConfigurationDesignTheme(null, ['store' => 'fixturestore']));
  123. $this->assertEquals(
  124. 'two',
  125. $this->_model->getConfigurationDesignTheme('frontend', ['store' => 'fixturestore'])
  126. );
  127. }
  128. /**
  129. * @dataProvider getFilenameDataProvider
  130. * @magentoAppIsolation enabled
  131. */
  132. public function testGetFilename($file, $params)
  133. {
  134. $this->_emulateFixtureTheme();
  135. $this->assertFileExists($this->_viewFileSystem->getFilename($file, $params));
  136. }
  137. /**
  138. * @return array
  139. */
  140. public function getFilenameDataProvider()
  141. {
  142. return [
  143. ['theme_file.txt', ['module' => 'Magento_Catalog']],
  144. ['Magento_Catalog::theme_file.txt', []],
  145. ['Magento_Catalog::theme_file_with_2_dots..txt', []],
  146. ['Magento_Catalog::theme_file.txt', ['module' => 'Overridden_Module']]
  147. ];
  148. }
  149. /**
  150. * @magentoAppIsolation enabled
  151. */
  152. public function testGetViewConfig()
  153. {
  154. $this->_emulateFixtureTheme();
  155. $config = $this->_viewConfig->getViewConfig();
  156. $this->assertInstanceOf(\Magento\Framework\Config\View::class, $config);
  157. $this->assertEquals(['var1' => 'value1', 'var2' => 'value2'], $config->getVars('Namespace_Module'));
  158. }
  159. /**
  160. * @magentoAppIsolation enabled
  161. */
  162. public function testGetConfigCustomized()
  163. {
  164. $this->_emulateFixtureTheme();
  165. /** @var $theme \Magento\Framework\View\Design\ThemeInterface */
  166. $theme = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  167. \Magento\Framework\View\DesignInterface::class
  168. )->getDesignTheme();
  169. $customConfigFile = $theme->getCustomization()->getCustomViewConfigPath();
  170. /** @var $filesystem \Magento\Framework\Filesystem */
  171. $filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  172. ->create(\Magento\Framework\Filesystem::class);
  173. $directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
  174. $relativePath = $directory->getRelativePath($customConfigFile);
  175. try {
  176. $directory->writeFile(
  177. $relativePath,
  178. '<?xml version="1.0" encoding="UTF-8"?>
  179. <view><vars module="Namespace_Module"><var name="customVar">custom value</var></vars></view>'
  180. );
  181. $config = $this->_viewConfig->getViewConfig();
  182. $this->assertInstanceOf(\Magento\Framework\Config\View::class, $config);
  183. $this->assertEquals(['customVar' => 'custom value'], $config->getVars('Namespace_Module'));
  184. } catch (\Exception $e) {
  185. $directory->delete($relativePath);
  186. throw $e;
  187. }
  188. $directory->delete($relativePath);
  189. }
  190. }