PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/Theme/Test/Unit/Model/ThemeTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 489 lines | 301 code | 46 blank | 142 comment | 0 complexity | 1ff3f1c948f4ca43adf3fca9cf1fb0c1 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Test theme model
  8. */
  9. namespace Magento\Theme\Test\Unit\Model;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  11. use Magento\Framework\View\Design\ThemeInterface;
  12. use Magento\Theme\Model\Theme;
  13. /**
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class ThemeTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * @var \Magento\Theme\Model\Theme|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $_model;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $_imageFactory;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Design\Theme\FlyweightFactory
  28. */
  29. protected $themeFactory;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Theme\Model\ResourceModel\Theme\Collection
  32. */
  33. protected $resourceCollection;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Design\Theme\Domain\Factory
  36. */
  37. protected $domainFactory;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Design\Theme\Validator
  40. */
  41. protected $validator;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Design\Theme\CustomizationFactory
  44. */
  45. protected $customizationFactory;
  46. /**
  47. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\State
  48. */
  49. protected $appState;
  50. protected function setUp()
  51. {
  52. $customizationConfig = $this->getMock(\Magento\Theme\Model\Config\Customization::class, [], [], '', false);
  53. $this->customizationFactory = $this->getMock(
  54. \Magento\Framework\View\Design\Theme\CustomizationFactory::class,
  55. ['create'],
  56. [],
  57. '',
  58. false
  59. );
  60. $this->resourceCollection = $this->getMock(
  61. \Magento\Theme\Model\ResourceModel\Theme\Collection::class,
  62. [],
  63. [],
  64. '',
  65. false
  66. );
  67. $this->_imageFactory = $this->getMock(
  68. \Magento\Framework\View\Design\Theme\ImageFactory::class,
  69. ['create'],
  70. [],
  71. '',
  72. false
  73. );
  74. $this->themeFactory = $this->getMock(
  75. \Magento\Framework\View\Design\Theme\FlyweightFactory::class,
  76. ['create'],
  77. [],
  78. '',
  79. false
  80. );
  81. $this->domainFactory = $this->getMock(
  82. \Magento\Framework\View\Design\Theme\Domain\Factory::class,
  83. ['create'],
  84. [],
  85. '',
  86. false
  87. );
  88. $this->validator = $this->getMock(\Magento\Framework\View\Design\Theme\Validator::class, [], [], '', false);
  89. $this->appState = $this->getMock(\Magento\Framework\App\State::class, [], [], '', false);
  90. $objectManagerHelper = new ObjectManager($this);
  91. $arguments = $objectManagerHelper->getConstructArguments(
  92. \Magento\Theme\Model\Theme::class,
  93. [
  94. 'customizationFactory' => $this->customizationFactory,
  95. 'customizationConfig' => $customizationConfig,
  96. 'imageFactory' => $this->_imageFactory,
  97. 'resourceCollection' => $this->resourceCollection,
  98. 'themeFactory' => $this->themeFactory,
  99. 'domainFactory' => $this->domainFactory,
  100. 'validator' => $this->validator,
  101. 'appState' => $this->appState,
  102. ]
  103. );
  104. $this->_model = $objectManagerHelper->getObject(\Magento\Theme\Model\Theme::class, $arguments);
  105. }
  106. protected function tearDown()
  107. {
  108. $this->_model = null;
  109. }
  110. /**
  111. * @covers \Magento\Theme\Model\Theme::getThemeImage
  112. */
  113. public function testThemeImageGetter()
  114. {
  115. $this->_imageFactory->expects($this->once())->method('create')->with(['theme' => $this->_model]);
  116. $this->_model->getThemeImage();
  117. }
  118. /**
  119. * @dataProvider isVirtualDataProvider
  120. * @param int $type
  121. * @param string $isVirtual
  122. * @covers \Magento\Theme\Model\Theme::isVirtual
  123. */
  124. public function testIsVirtual($type, $isVirtual)
  125. {
  126. /** @var $themeModel \Magento\Theme\Model\Theme */
  127. $themeModel = $this->getMock(\Magento\Theme\Model\Theme::class, ['__wakeup'], [], '', false);
  128. $themeModel->setType($type);
  129. $this->assertEquals($isVirtual, $themeModel->isVirtual());
  130. }
  131. /**
  132. * @return array
  133. */
  134. public function isVirtualDataProvider()
  135. {
  136. return [
  137. ['type' => ThemeInterface::TYPE_VIRTUAL, 'isVirtual' => true],
  138. ['type' => ThemeInterface::TYPE_STAGING, 'isVirtual' => false],
  139. ['type' => ThemeInterface::TYPE_PHYSICAL, 'isVirtual' => false]
  140. ];
  141. }
  142. /**
  143. * @dataProvider isPhysicalDataProvider
  144. * @param int $type
  145. * @param string $isPhysical
  146. * @covers \Magento\Theme\Model\Theme::isPhysical
  147. */
  148. public function testIsPhysical($type, $isPhysical)
  149. {
  150. /** @var $themeModel \Magento\Theme\Model\Theme */
  151. $themeModel = $this->getMock(\Magento\Theme\Model\Theme::class, ['__wakeup'], [], '', false);
  152. $themeModel->setType($type);
  153. $this->assertEquals($isPhysical, $themeModel->isPhysical());
  154. }
  155. /**
  156. * @return array
  157. */
  158. public function isPhysicalDataProvider()
  159. {
  160. return [
  161. ['type' => ThemeInterface::TYPE_VIRTUAL, 'isPhysical' => false],
  162. ['type' => ThemeInterface::TYPE_STAGING, 'isPhysical' => false],
  163. ['type' => ThemeInterface::TYPE_PHYSICAL, 'isPhysical' => true]
  164. ];
  165. }
  166. /**
  167. * @dataProvider isVisibleDataProvider
  168. * @param int $type
  169. * @param string $isVisible
  170. * @covers \Magento\Theme\Model\Theme::isVisible
  171. */
  172. public function testIsVisible($type, $isVisible)
  173. {
  174. /** @var $themeModel \Magento\Theme\Model\Theme */
  175. $themeModel = $this->getMock(\Magento\Theme\Model\Theme::class, ['__wakeup'], [], '', false);
  176. $themeModel->setType($type);
  177. $this->assertEquals($isVisible, $themeModel->isVisible());
  178. }
  179. /**
  180. * @return array
  181. */
  182. public function isVisibleDataProvider()
  183. {
  184. return [
  185. ['type' => ThemeInterface::TYPE_VIRTUAL, 'isVisible' => true],
  186. ['type' => ThemeInterface::TYPE_STAGING, 'isVisible' => false],
  187. ['type' => ThemeInterface::TYPE_PHYSICAL, 'isVisible' => true]
  188. ];
  189. }
  190. /**
  191. * Test id deletable
  192. *
  193. * @dataProvider isDeletableDataProvider
  194. * @param string $themeType
  195. * @param bool $isDeletable
  196. * @covers \Magento\Theme\Model\Theme::isDeletable
  197. */
  198. public function testIsDeletable($themeType, $isDeletable)
  199. {
  200. $themeModel = $this->getMock(\Magento\Theme\Model\Theme::class, ['getType', '__wakeup'], [], '', false);
  201. $themeModel->expects($this->once())->method('getType')->will($this->returnValue($themeType));
  202. /** @var $themeModel \Magento\Theme\Model\Theme */
  203. $this->assertEquals($isDeletable, $themeModel->isDeletable());
  204. }
  205. /**
  206. * @return array
  207. */
  208. public function isDeletableDataProvider()
  209. {
  210. return [
  211. [ThemeInterface::TYPE_VIRTUAL, true],
  212. [ThemeInterface::TYPE_STAGING, true],
  213. [ThemeInterface::TYPE_PHYSICAL, false]
  214. ];
  215. }
  216. /**
  217. * @param mixed $originalCode
  218. * @param string $expectedCode
  219. * @dataProvider getCodeDataProvider
  220. */
  221. public function testGetCode($originalCode, $expectedCode)
  222. {
  223. $this->_model->setCode($originalCode);
  224. $this->assertSame($expectedCode, $this->_model->getCode());
  225. }
  226. /**
  227. * @return array
  228. */
  229. public function getCodeDataProvider()
  230. {
  231. return [
  232. 'string code' => ['theme/code', 'theme/code'],
  233. 'null code' => [null, ''],
  234. 'number code' => [10, '10']
  235. ];
  236. }
  237. /**
  238. * @test
  239. * @return void
  240. */
  241. public function testGetInheritedThemes()
  242. {
  243. $inheritedTheme = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)->getMock();
  244. $this->_model->setParentId(10);
  245. $this->themeFactory->expects($this->once())
  246. ->method('create')
  247. ->with(10)
  248. ->willReturn($inheritedTheme);
  249. $this->assertContainsOnlyInstancesOf(
  250. \Magento\Framework\View\Design\ThemeInterface::class,
  251. $this->_model->getInheritedThemes()
  252. );
  253. $this->assertCount(2, $this->_model->getInheritedThemes());
  254. }
  255. /**
  256. * @test
  257. * @return void
  258. */
  259. public function testAfterDelete()
  260. {
  261. $expectId = 101;
  262. $theme = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)
  263. ->setMethods(['delete', 'getId'])
  264. ->getMockForAbstractClass();
  265. $theme->expects($this->once())
  266. ->method('getId')
  267. ->willReturn($expectId);
  268. $theme->expects($this->once())
  269. ->method('delete')
  270. ->willReturnSelf();
  271. $this->_model->setId(1);
  272. $this->resourceCollection->expects($this->at(0))
  273. ->method('addFieldToFilter')
  274. ->with('parent_id', 1)
  275. ->willReturnSelf();
  276. $this->resourceCollection->expects($this->at(1))
  277. ->method('addFieldToFilter')
  278. ->with('type', Theme::TYPE_STAGING)
  279. ->willReturnSelf();
  280. $this->resourceCollection->expects($this->once())
  281. ->method('getFirstItem')
  282. ->willReturn($theme);
  283. $this->resourceCollection->expects($this->once())
  284. ->method('updateChildRelations')
  285. ->with($this->_model);
  286. $this->assertInstanceOf(get_class($this->_model), $this->_model->afterDelete());
  287. }
  288. /**
  289. * @test
  290. * @return void
  291. */
  292. public function testGetStagingVersion()
  293. {
  294. $theme = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)
  295. ->setMethods(['getId'])
  296. ->getMockForAbstractClass();
  297. $theme->expects($this->once())
  298. ->method('getId')
  299. ->willReturn(null);
  300. $this->_model->setId(1);
  301. $this->resourceCollection->expects($this->at(0))
  302. ->method('addFieldToFilter')
  303. ->with('parent_id', 1)
  304. ->willReturnSelf();
  305. $this->resourceCollection->expects($this->at(1))
  306. ->method('addFieldToFilter')
  307. ->with('type', Theme::TYPE_STAGING)
  308. ->willReturnSelf();
  309. $this->resourceCollection->expects($this->once())
  310. ->method('getFirstItem')
  311. ->willReturn($theme);
  312. $this->assertNull($this->_model->getStagingVersion());
  313. }
  314. /**
  315. * @test
  316. * @return void
  317. */
  318. public function testGetStagingVersionWithoutTheme()
  319. {
  320. $this->assertNull($this->_model->getStagingVersion());
  321. }
  322. /**
  323. * @test
  324. * @return void
  325. */
  326. public function testGetDomainModel()
  327. {
  328. $result = 'res';
  329. $this->domainFactory->expects($this->once())
  330. ->method('create')
  331. ->with($this->_model)
  332. ->willReturn($result);
  333. $this->assertEquals($result, $this->_model->getDomainModel());
  334. }
  335. /**
  336. * @test
  337. * @expectedException \InvalidArgumentException
  338. * @return void
  339. */
  340. public function testGetDomainModelWithIncorrectType()
  341. {
  342. $this->_model->getDomainModel('bla-bla-bla');
  343. }
  344. /**
  345. * @test
  346. * @expectedException \Magento\Framework\Exception\LocalizedException
  347. * @expectedExceptionMessage testMessage
  348. * @return void
  349. */
  350. public function testValidate()
  351. {
  352. $this->validator->expects($this->once())
  353. ->method('validate')
  354. ->with($this->_model)
  355. ->willReturn(false);
  356. $this->validator->expects($this->once())
  357. ->method('getErrorMessages')
  358. ->willReturn([[__('testMessage')]]);
  359. $this->assertInstanceOf(get_class($this->_model), $this->_model->beforeSave());
  360. }
  361. /**
  362. * @test
  363. * @return void
  364. */
  365. public function testValidatePass()
  366. {
  367. $this->validator->expects($this->once())
  368. ->method('validate')
  369. ->with($this->_model)
  370. ->willReturn(true);
  371. $this->assertInstanceOf(get_class($this->_model), $this->_model->beforeSave());
  372. }
  373. /**
  374. * @test
  375. * @return void
  376. */
  377. public function testHasChildThemes()
  378. {
  379. $this->_model->setId(1);
  380. $this->resourceCollection->expects($this->once())
  381. ->method('addTypeFilter')
  382. ->with(Theme::TYPE_VIRTUAL)
  383. ->willReturnSelf();
  384. $this->resourceCollection->expects($this->once())
  385. ->method('addFieldToFilter')
  386. ->with('parent_id', ['eq' => 1])
  387. ->willReturnSelf();
  388. $this->resourceCollection->expects($this->once())
  389. ->method('getSize')
  390. ->willReturn(10);
  391. $this->assertTrue($this->_model->hasChildThemes());
  392. }
  393. /**
  394. * @test
  395. * @return void
  396. */
  397. public function testGetCustomization()
  398. {
  399. $this->customizationFactory->expects($this->once())
  400. ->method('create')
  401. ->willReturn(
  402. $this->getMockBuilder(\Magento\Framework\View\Design\Theme\CustomizationInterface::class)->getMock()
  403. );
  404. $this->assertInstanceOf(
  405. \Magento\Framework\View\Design\Theme\CustomizationInterface::class,
  406. $this->_model->getCustomization()
  407. );
  408. }
  409. /**
  410. * @test
  411. * @return void
  412. */
  413. public function testIsEditable()
  414. {
  415. $this->_model->setType(Theme::TYPE_VIRTUAL);
  416. $this->assertTrue($this->_model->isEditable());
  417. $this->_model->setType(Theme::TYPE_PHYSICAL);
  418. $this->assertFalse($this->_model->isEditable());
  419. }
  420. /**
  421. * @test
  422. * @return void
  423. */
  424. public function getFullThemePath()
  425. {
  426. $areaCode = 'frontend';
  427. $this->appState->expects($this->once())
  428. ->method('getAreaCode')
  429. ->willReturn($areaCode);
  430. $path = 'some/path';
  431. $this->_model->setThemePath($path);
  432. $this->assertEquals($areaCode . Theme::PATH_SEPARATOR . $path, $this->_model->getFullPath());
  433. }
  434. /**
  435. * @test
  436. * @return void
  437. */
  438. public function getParentTheme()
  439. {
  440. $this->_model->setParentTheme('parent_theme');
  441. $this->assertEquals('parent_theme', $this->_model->getParentTheme());
  442. }
  443. }