PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-theme/Test/Unit/Model/ThemeTest.php

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