CustomizationTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Test theme customization config model
  8. */
  9. namespace Magento\Theme\Test\Unit\Model\Config;
  10. use Magento\Framework\App\Area;
  11. class CustomizationTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Store\Model\StoreManagerInterface
  15. */
  16. protected $storeManager;
  17. /**
  18. * @var \Magento\Framework\View\DesignInterface|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $designPackage;
  21. /**
  22. * @var \Magento\Theme\Model\ResourceModel\Theme\Collection
  23. */
  24. protected $themeCollection;
  25. /**
  26. * @var \Magento\Theme\Model\Config\Customization
  27. */
  28. protected $model;
  29. /**
  30. * @var \Magento\Theme\Model\Theme\ThemeProvider|\PHPUnit\Framework\MockObject_MockBuilder
  31. */
  32. protected $themeProviderMock;
  33. protected function setUp()
  34. {
  35. $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)->getMock();
  36. $this->designPackage = $this->getMockBuilder(\Magento\Framework\View\DesignInterface::class)->getMock();
  37. $this->themeCollection = $this->getMockBuilder(\Magento\Theme\Model\ResourceModel\Theme\Collection::class)
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $collectionFactory = $this->getMockBuilder(\Magento\Theme\Model\ResourceModel\Theme\CollectionFactory::class)
  41. ->disableOriginalConstructor()
  42. ->setMethods(['create'])
  43. ->getMock();
  44. $collectionFactory->expects($this->any())->method('create')->will($this->returnValue($this->themeCollection));
  45. $this->themeProviderMock = $this->getMockBuilder(\Magento\Theme\Model\Theme\ThemeProvider::class)
  46. ->disableOriginalConstructor()
  47. ->setMethods(['getThemeCustomizations', 'getThemeByFullPath'])
  48. ->getMock();
  49. $this->model = new \Magento\Theme\Model\Config\Customization(
  50. $this->storeManager,
  51. $this->designPackage,
  52. $this->themeProviderMock
  53. );
  54. }
  55. /**
  56. * @covers \Magento\Theme\Model\Config\Customization::getAssignedThemeCustomizations
  57. * @covers \Magento\Theme\Model\Config\Customization::hasThemeAssigned
  58. * @covers \Magento\Theme\Model\Config\Customization::_prepareThemeCustomizations
  59. * @covers \Magento\Theme\Model\Config\Customization::__construct
  60. */
  61. public function testGetAssignedThemeCustomizations()
  62. {
  63. $this->designPackage->expects($this->once())
  64. ->method('getConfigurationDesignTheme')
  65. ->willReturn($this->getAssignedTheme()->getId());
  66. $this->storeManager->expects($this->once())
  67. ->method('getStores')
  68. ->willReturn([$this->getStore()]);
  69. $this->themeProviderMock->expects($this->once())
  70. ->method('getThemeCustomizations')
  71. ->with(Area::AREA_FRONTEND)
  72. ->willReturn([$this->getAssignedTheme(), $this->getUnassignedTheme()]);
  73. $assignedThemes = $this->model->getAssignedThemeCustomizations();
  74. $this->assertArrayHasKey($this->getAssignedTheme()->getId(), $assignedThemes);
  75. $this->assertTrue($this->model->hasThemeAssigned());
  76. }
  77. /**
  78. * @covers \Magento\Theme\Model\Config\Customization::getUnassignedThemeCustomizations
  79. * @covers \Magento\Theme\Model\Config\Customization::__construct
  80. */
  81. public function testGetUnassignedThemeCustomizations()
  82. {
  83. $this->storeManager->expects($this->once())
  84. ->method('getStores')
  85. ->willReturn([$this->getStore()]);
  86. $this->designPackage->expects($this->once())
  87. ->method('getConfigurationDesignTheme')
  88. ->willReturn($this->getAssignedTheme()->getId());
  89. $this->themeProviderMock->expects($this->once())
  90. ->method('getThemeCustomizations')
  91. ->with(Area::AREA_FRONTEND)
  92. ->willReturn([$this->getAssignedTheme(), $this->getUnassignedTheme()]);
  93. $unassignedThemes = $this->model->getUnassignedThemeCustomizations();
  94. $this->assertArrayHasKey($this->getUnassignedTheme()->getId(), $unassignedThemes);
  95. }
  96. /**
  97. * @covers \Magento\Theme\Model\Config\Customization::getStoresByThemes
  98. * @covers \Magento\Theme\Model\Config\Customization::__construct
  99. */
  100. public function testGetStoresByThemes()
  101. {
  102. $this->storeManager->expects($this->once())
  103. ->method('getStores')
  104. ->willReturn([$this->getStore()]);
  105. $this->designPackage->expects($this->once())
  106. ->method('getConfigurationDesignTheme')
  107. ->willReturn($this->getAssignedTheme()->getId());
  108. $stores = $this->model->getStoresByThemes();
  109. $this->assertArrayHasKey($this->getAssignedTheme()->getId(), $stores);
  110. }
  111. /**
  112. * @covers \Magento\Theme\Model\Config\Customization::isThemeAssignedToStore
  113. * @covers \Magento\Theme\Model\Config\Customization::_getConfigurationThemeId
  114. * @covers \Magento\Theme\Model\Config\Customization::__construct
  115. */
  116. public function testIsThemeAssignedToDefaultStore()
  117. {
  118. $this->storeManager->expects($this->once())
  119. ->method('getStores')
  120. ->willReturn([$this->getStore()]);
  121. $this->designPackage->expects($this->once())
  122. ->method('getConfigurationDesignTheme')
  123. ->willReturn($this->getAssignedTheme()->getId());
  124. $this->themeProviderMock->expects($this->once())
  125. ->method('getThemeCustomizations')
  126. ->with(Area::AREA_FRONTEND)
  127. ->willReturn([$this->getAssignedTheme(), $this->getUnassignedTheme()]);
  128. $themeAssigned = $this->model->isThemeAssignedToStore($this->getAssignedTheme());
  129. $this->assertEquals(true, $themeAssigned);
  130. }
  131. /**
  132. * @covers \Magento\Theme\Model\Config\Customization::isThemeAssignedToStore
  133. * @covers \Magento\Theme\Model\Config\Customization::_isThemeAssignedToSpecificStore
  134. */
  135. public function testIsThemeAssignedToConcreteStore()
  136. {
  137. $this->designPackage->expects($this->once())
  138. ->method('getConfigurationDesignTheme')
  139. ->willReturn($this->getAssignedTheme()->getId());
  140. $themeUnassigned = $this->model->isThemeAssignedToStore($this->getUnassignedTheme(), $this->getStore());
  141. $this->assertEquals(false, $themeUnassigned);
  142. }
  143. /**
  144. * @return \Magento\Framework\DataObject
  145. */
  146. protected function getAssignedTheme()
  147. {
  148. return new \Magento\Framework\DataObject(['id' => 1, 'theme_path' => 'Magento/luma']);
  149. }
  150. /**
  151. * @return \Magento\Framework\DataObject
  152. */
  153. protected function getUnassignedTheme()
  154. {
  155. return new \Magento\Framework\DataObject(['id' => 2, 'theme_path' => 'Magento/blank']);
  156. }
  157. /**
  158. * @return \Magento\Framework\DataObject
  159. */
  160. protected function getStore()
  161. {
  162. return new \Magento\Framework\DataObject(['id' => 55]);
  163. }
  164. }