123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- /**
- * Test theme model
- */
- namespace Magento\Theme\Test\Unit\Model;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
- use Magento\Framework\View\Design\ThemeInterface;
- use Magento\Theme\Model\Theme;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class ThemeTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Theme\Model\Theme|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $_model;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_imageFactory;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Design\Theme\FlyweightFactory
- */
- protected $themeFactory;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Theme\Model\ResourceModel\Theme\Collection
- */
- protected $resourceCollection;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Design\Theme\Domain\Factory
- */
- protected $domainFactory;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Design\Theme\Validator
- */
- protected $validator;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\View\Design\Theme\CustomizationFactory
- */
- protected $customizationFactory;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\State
- */
- protected $appState;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Theme\Model\ThemeFactory
- */
- private $themeModelFactory;
- protected function setUp()
- {
- $customizationConfig = $this->createMock(\Magento\Theme\Model\Config\Customization::class);
- $this->customizationFactory = $this->createPartialMock(
- \Magento\Framework\View\Design\Theme\CustomizationFactory::class,
- ['create']
- );
- $this->resourceCollection = $this->createMock(\Magento\Theme\Model\ResourceModel\Theme\Collection::class);
- $this->_imageFactory = $this->createPartialMock(
- \Magento\Framework\View\Design\Theme\ImageFactory::class,
- ['create']
- );
- $this->themeFactory = $this->createPartialMock(
- \Magento\Framework\View\Design\Theme\FlyweightFactory::class,
- ['create']
- );
- $this->domainFactory = $this->createPartialMock(
- \Magento\Framework\View\Design\Theme\Domain\Factory::class,
- ['create']
- );
- $this->themeModelFactory = $this->createPartialMock(\Magento\Theme\Model\ThemeFactory::class, ['create']);
- $this->validator = $this->createMock(\Magento\Framework\View\Design\Theme\Validator::class);
- $this->appState = $this->createMock(\Magento\Framework\App\State::class);
- $objectManagerHelper = new ObjectManager($this);
- $arguments = $objectManagerHelper->getConstructArguments(
- \Magento\Theme\Model\Theme::class,
- [
- 'customizationFactory' => $this->customizationFactory,
- 'customizationConfig' => $customizationConfig,
- 'imageFactory' => $this->_imageFactory,
- 'resourceCollection' => $this->resourceCollection,
- 'themeFactory' => $this->themeFactory,
- 'domainFactory' => $this->domainFactory,
- 'validator' => $this->validator,
- 'appState' => $this->appState,
- 'themeModelFactory' => $this->themeModelFactory
- ]
- );
- $this->_model = $objectManagerHelper->getObject(\Magento\Theme\Model\Theme::class, $arguments);
- }
- protected function tearDown()
- {
- $this->_model = null;
- }
- /**
- * @covers \Magento\Theme\Model\Theme::getThemeImage
- */
- public function testThemeImageGetter()
- {
- $this->_imageFactory->expects($this->once())->method('create')->with(['theme' => $this->_model]);
- $this->_model->getThemeImage();
- }
- /**
- * @dataProvider isVirtualDataProvider
- * @param int $type
- * @param string $isVirtual
- * @covers \Magento\Theme\Model\Theme::isVirtual
- */
- public function testIsVirtual($type, $isVirtual)
- {
- $this->_model->setType($type);
- $this->assertEquals($isVirtual, $this->_model->isVirtual());
- }
- /**
- * @return array
- */
- public function isVirtualDataProvider()
- {
- return [
- ['type' => ThemeInterface::TYPE_VIRTUAL, 'isVirtual' => true],
- ['type' => ThemeInterface::TYPE_STAGING, 'isVirtual' => false],
- ['type' => ThemeInterface::TYPE_PHYSICAL, 'isVirtual' => false]
- ];
- }
- /**
- * @dataProvider isPhysicalDataProvider
- * @param int $type
- * @param string $isPhysical
- * @covers \Magento\Theme\Model\Theme::isPhysical
- */
- public function testIsPhysical($type, $isPhysical)
- {
- $this->_model->setType($type);
- $this->assertEquals($isPhysical, $this->_model->isPhysical());
- }
- /**
- * @return array
- */
- public function isPhysicalDataProvider()
- {
- return [
- ['type' => ThemeInterface::TYPE_VIRTUAL, 'isPhysical' => false],
- ['type' => ThemeInterface::TYPE_STAGING, 'isPhysical' => false],
- ['type' => ThemeInterface::TYPE_PHYSICAL, 'isPhysical' => true]
- ];
- }
- /**
- * @dataProvider isVisibleDataProvider
- * @param int $type
- * @param string $isVisible
- * @covers \Magento\Theme\Model\Theme::isVisible
- */
- public function testIsVisible($type, $isVisible)
- {
- $this->_model->setType($type);
- $this->assertEquals($isVisible, $this->_model->isVisible());
- }
- /**
- * @return array
- */
- public function isVisibleDataProvider()
- {
- return [
- ['type' => ThemeInterface::TYPE_VIRTUAL, 'isVisible' => true],
- ['type' => ThemeInterface::TYPE_STAGING, 'isVisible' => false],
- ['type' => ThemeInterface::TYPE_PHYSICAL, 'isVisible' => true]
- ];
- }
- /**
- * Test id deletable
- *
- * @dataProvider isDeletableDataProvider
- * @param string $themeType
- * @param bool $isDeletable
- * @covers \Magento\Theme\Model\Theme::isDeletable
- */
- public function testIsDeletable($themeType, $isDeletable)
- {
- $themeModel = $this->createPartialMock(\Magento\Theme\Model\Theme::class, ['getType']);
- $themeModel->expects($this->once())->method('getType')->will($this->returnValue($themeType));
- /** @var $themeModel \Magento\Theme\Model\Theme */
- $this->assertEquals($isDeletable, $themeModel->isDeletable());
- }
- /**
- * @return array
- */
- public function isDeletableDataProvider()
- {
- return [
- [ThemeInterface::TYPE_VIRTUAL, true],
- [ThemeInterface::TYPE_STAGING, true],
- [ThemeInterface::TYPE_PHYSICAL, false]
- ];
- }
- /**
- * @param mixed $originalCode
- * @param string $expectedCode
- * @dataProvider getCodeDataProvider
- */
- public function testGetCode($originalCode, $expectedCode)
- {
- $this->_model->setCode($originalCode);
- $this->assertSame($expectedCode, $this->_model->getCode());
- }
- /**
- * @return array
- */
- public function getCodeDataProvider()
- {
- return [
- 'string code' => ['theme/code', 'theme/code'],
- 'null code' => [null, ''],
- 'number code' => [10, '10']
- ];
- }
- /**
- * @test
- * @return void
- */
- public function testGetInheritedThemes()
- {
- $inheritedTheme = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)->getMock();
- $this->_model->setParentId(10);
- $this->themeFactory->expects($this->once())
- ->method('create')
- ->with(10)
- ->willReturn($inheritedTheme);
- $this->assertContainsOnlyInstancesOf(
- \Magento\Framework\View\Design\ThemeInterface::class,
- $this->_model->getInheritedThemes()
- );
- $this->assertCount(2, $this->_model->getInheritedThemes());
- }
- /**
- * @test
- * @return void
- */
- public function testAfterDelete()
- {
- $expectId = 101;
- $theme = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)
- ->setMethods(['delete', 'getId'])
- ->getMockForAbstractClass();
- $theme->expects($this->once())
- ->method('getId')
- ->willReturn($expectId);
- $theme->expects($this->once())
- ->method('delete')
- ->willReturnSelf();
- $this->_model->setId(1);
- $this->resourceCollection->expects($this->at(0))
- ->method('addFieldToFilter')
- ->with('parent_id', 1)
- ->willReturnSelf();
- $this->resourceCollection->expects($this->at(1))
- ->method('addFieldToFilter')
- ->with('type', Theme::TYPE_STAGING)
- ->willReturnSelf();
- $this->resourceCollection->expects($this->once())
- ->method('getFirstItem')
- ->willReturn($theme);
- $this->resourceCollection->expects($this->once())
- ->method('updateChildRelations')
- ->with($this->_model);
- $this->assertInstanceOf(get_class($this->_model), $this->_model->afterDelete());
- }
- /**
- * @test
- * @return void
- */
- public function testGetStagingVersion()
- {
- $theme = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)
- ->setMethods(['getId'])
- ->getMockForAbstractClass();
- $theme->expects($this->once())
- ->method('getId')
- ->willReturn(null);
- $this->_model->setId(1);
- $this->resourceCollection->expects($this->at(0))
- ->method('addFieldToFilter')
- ->with('parent_id', 1)
- ->willReturnSelf();
- $this->resourceCollection->expects($this->at(1))
- ->method('addFieldToFilter')
- ->with('type', Theme::TYPE_STAGING)
- ->willReturnSelf();
- $this->resourceCollection->expects($this->once())
- ->method('getFirstItem')
- ->willReturn($theme);
- $this->assertNull($this->_model->getStagingVersion());
- }
- /**
- * @test
- * @return void
- */
- public function testGetStagingVersionWithoutTheme()
- {
- $this->assertNull($this->_model->getStagingVersion());
- }
- /**
- * @test
- * @return void
- */
- public function testGetDomainModel()
- {
- $result = 'res';
- $this->domainFactory->expects($this->once())
- ->method('create')
- ->with($this->_model)
- ->willReturn($result);
- $this->assertEquals($result, $this->_model->getDomainModel());
- }
- /**
- * @test
- * @expectedException \InvalidArgumentException
- * @return void
- */
- public function testGetDomainModelWithIncorrectType()
- {
- $this->_model->getDomainModel('bla-bla-bla');
- }
- /**
- * @test
- * @expectedException \Magento\Framework\Exception\LocalizedException
- * @expectedExceptionMessage testMessage
- * @return void
- */
- public function testValidate()
- {
- $this->validator->expects($this->once())
- ->method('validate')
- ->with($this->_model)
- ->willReturn(false);
- $this->validator->expects($this->once())
- ->method('getErrorMessages')
- ->willReturn([[__('testMessage')]]);
- $this->assertInstanceOf(get_class($this->_model), $this->_model->beforeSave());
- }
- /**
- * @test
- * @return void
- */
- public function testValidatePass()
- {
- $this->validator->expects($this->once())
- ->method('validate')
- ->with($this->_model)
- ->willReturn(true);
- $this->assertInstanceOf(get_class($this->_model), $this->_model->beforeSave());
- }
- /**
- * @test
- * @return void
- */
- public function testHasChildThemes()
- {
- $this->_model->setId(1);
- $this->resourceCollection->expects($this->once())
- ->method('addTypeFilter')
- ->with(Theme::TYPE_VIRTUAL)
- ->willReturnSelf();
- $this->resourceCollection->expects($this->once())
- ->method('addFieldToFilter')
- ->with('parent_id', ['eq' => 1])
- ->willReturnSelf();
- $this->resourceCollection->expects($this->once())
- ->method('getSize')
- ->willReturn(10);
- $this->assertTrue($this->_model->hasChildThemes());
- }
- /**
- * @test
- * @return void
- */
- public function testGetCustomization()
- {
- $this->customizationFactory->expects($this->once())
- ->method('create')
- ->willReturn(
- $this->getMockBuilder(\Magento\Framework\View\Design\Theme\CustomizationInterface::class)->getMock()
- );
- $this->assertInstanceOf(
- \Magento\Framework\View\Design\Theme\CustomizationInterface::class,
- $this->_model->getCustomization()
- );
- }
- /**
- * @test
- * @return void
- */
- public function testIsEditable()
- {
- $this->_model->setType(Theme::TYPE_VIRTUAL);
- $this->assertTrue($this->_model->isEditable());
- $this->_model->setType(Theme::TYPE_PHYSICAL);
- $this->assertFalse($this->_model->isEditable());
- }
- /**
- * @test
- * @return void
- */
- public function getFullThemePath()
- {
- $areaCode = 'frontend';
- $this->appState->expects($this->once())
- ->method('getAreaCode')
- ->willReturn($areaCode);
- $path = 'some/path';
- $this->_model->setThemePath($path);
- $this->assertEquals($areaCode . Theme::PATH_SEPARATOR . $path, $this->_model->getFullPath());
- }
- /**
- * @test
- * @return void
- */
- public function getParentTheme()
- {
- $this->_model->setParentTheme('parent_theme');
- $this->assertEquals('parent_theme', $this->_model->getParentTheme());
- }
- /**
- * @param array $themeData
- * @param array $expected
- * @dataProvider toArrayDataProvider
- */
- public function testToArray(array $themeData, array $expected)
- {
- $this->_model->setData($themeData);
- $this->assertEquals($expected, $this->_model->toArray());
- }
- /**
- * @return array
- */
- public function toArrayDataProvider()
- {
- $parentTheme = $this->getMockBuilder(\Magento\Theme\Model\Theme::class)
- ->disableOriginalConstructor()
- ->getMock();
- $childTheme = clone $parentTheme;
- $parentTheme->expects($this->once())
- ->method('toArray')
- ->willReturn('parent_theme');
- $childTheme->expects($this->exactly(2))
- ->method('toArray')
- ->willReturn('child_theme');
- return [
- 'null' => [[], []],
- 'valid' => [
- ['theme_data' => 'theme_data'],
- ['theme_data' => 'theme_data']
- ],
- 'valid with parent' => [
- [
- 'theme_data' => 'theme_data',
- 'parent_theme' => $parentTheme
- ],
- [
- 'theme_data' => 'theme_data',
- 'parent_theme' => 'parent_theme'
- ]
- ],
- 'valid with children' => [
- [
- 'theme_data' => 'theme_data',
- 'inherited_themes' => [
- 'key1' => $childTheme,
- 'key2' => $childTheme
- ]
- ],
- [
- 'theme_data' => 'theme_data',
- 'inherited_themes' => [
- 'key1' => 'child_theme',
- 'key2' => 'child_theme'
- ]
- ]
- ]
- ];
- }
- /**
- * @param array $value
- * @param array $expected
- * @param int $expectedCallCount
- *
- * @dataProvider populateFromArrayDataProvider
- */
- public function testPopulateFromArray(array $value, array $expected, $expectedCallCount = 0)
- {
- $themeMock = $this->getMockBuilder(\Magento\Theme\Model\Theme::class)
- ->disableOriginalConstructor()
- ->getMock();
- $themeMock->expects($this->exactly($expectedCallCount))
- ->method('populateFromArray')
- ->willReturn('theme_instance');
- $this->themeModelFactory->expects($this->exactly($expectedCallCount))
- ->method('create')
- ->willReturn($themeMock);
- $this->_model->populateFromArray($value);
- $this->assertEquals($expected, $this->_model->getData());
- }
- /**
- * @return array
- */
- public function populateFromArrayDataProvider()
- {
- return [
- 'valid data' => [
- 'value' => ['theme_data' => 'theme_data'],
- 'expected' => ['theme_data' => 'theme_data']
- ],
- 'valid data with parent' => [
- 'value' => [
- 'theme_data' => 'theme_data',
- 'parent_theme' => [
- 'theme_data' => 'theme_data'
- ]
- ],
- 'expected' => [
- 'theme_data' => 'theme_data',
- 'parent_theme' => 'theme_instance'
- ],
- 'expected call count' => 1
- ],
- 'valid data with children' => [
- 'value' => [
- 'theme_data' => 'theme_data',
- 'inherited_themes' => [
- 'key1' => ['theme_data' => 'theme_data'],
- 'key2' => ['theme_data' => 'theme_data']
- ]
- ],
- 'expected' => [
- 'theme_data' => 'theme_data',
- 'inherited_themes' => [
- 'key1' => 'theme_instance',
- 'key2' => 'theme_instance'
- ]
- ],
- 'expected call count' => 2
- ]
- ];
- }
- }
|