123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- <?php
- /**
- * \Magento\Widget\Model\Widget\Instance
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Widget\Test\Unit\Model\Widget;
- use Magento\Framework\Serialize\Serializer\Json;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class InstanceTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Widget\Model\Config\Data|PHPUnit_Framework_MockObject_MockObject
- */
- protected $_widgetModelMock;
- /**
- * @var \Magento\Framework\View\FileSystem|PHPUnit_Framework_MockObject_MockObject
- */
- protected $_viewFileSystemMock;
- /** @var \Magento\Widget\Model\NamespaceResolver |PHPUnit_Framework_MockObject_MockObject */
- protected $_namespaceResolver;
- /**
- * @var \Magento\Widget\Model\Widget\Instance
- */
- protected $_model;
- /** @var \Magento\Widget\Model\Config\Reader */
- protected $_readerMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_cacheTypesListMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $_directoryMock;
- /** @var \Magento\Framework\Serialize\Serializer\Json | \PHPUnit_Framework_MockObject_MockObject */
- private $serializer;
- protected function setUp()
- {
- $this->_widgetModelMock = $this->getMockBuilder(
- \Magento\Widget\Model\Widget::class
- )->disableOriginalConstructor()->getMock();
- $this->_viewFileSystemMock = $this->getMockBuilder(
- \Magento\Framework\View\FileSystem::class
- )->disableOriginalConstructor()->getMock();
- $this->_namespaceResolver = $this->getMockBuilder(
- \Magento\Widget\Model\NamespaceResolver::class
- )->disableOriginalConstructor()->getMock();
- $this->_cacheTypesListMock = $this->createMock(\Magento\Framework\App\Cache\TypeListInterface::class);
- $this->_readerMock = $this->getMockBuilder(
- \Magento\Widget\Model\Config\Reader::class
- )->disableOriginalConstructor()->getMock();
- $filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
- $this->_directoryMock = $this->createMock(\Magento\Framework\Filesystem\Directory\Read::class);
- $filesystemMock->expects(
- $this->any()
- )->method(
- 'getDirectoryRead'
- )->will(
- $this->returnValue($this->_directoryMock)
- );
- $this->_directoryMock->expects($this->any())->method('isReadable')->will($this->returnArgument(0));
- $this->_directoryMock->expects($this->any())->method('getRelativePath')->will($this->returnArgument(0));
- $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
- $this->serializer = $this->createMock(Json::class);
- $args = $objectManagerHelper->getConstructArguments(
- \Magento\Widget\Model\Widget\Instance::class,
- [
- 'filesystem' => $filesystemMock,
- 'viewFileSystem' => $this->_viewFileSystemMock,
- 'cacheTypeList' => $this->_cacheTypesListMock,
- 'reader' => $this->_readerMock,
- 'widgetModel' => $this->_widgetModelMock,
- 'namespaceResolver' => $this->_namespaceResolver,
- 'serializer' => $this->serializer,
- ]
- );
- /** @var \Magento\Widget\Model\Widget\Instance _model */
- $this->_model = $this->getMockBuilder(\Magento\Widget\Model\Widget\Instance::class)
- ->setMethods(['_construct'])
- ->setConstructorArgs($args)
- ->getMock();
- }
- public function testGetWidgetConfigAsArray()
- {
- $widget = [
- '@' => ['type' => \Magento\Cms\Block\Widget\Page\Link::class, 'module' => 'Magento_Cms'],
- 'name' => 'CMS Page Link',
- 'description' => 'Link to a CMS Page',
- 'is_email_compatible' => 'true',
- 'placeholder_image' => 'Magento_Cms::images/widget_page_link.png',
- 'parameters' => [
- 'page_id' => [
- '@' => ['type' => 'complex'],
- 'type' => 'label',
- 'helper_block' => [
- 'type' => \Magento\Cms\Block\Adminhtml\Page\Widget\Chooser::class,
- 'data' => ['button' => ['open' => 'Select Page...']],
- ],
- 'visible' => 'true',
- 'required' => 'true',
- 'sort_order' => '10',
- 'label' => 'CMS Page',
- ],
- ],
- ];
- $this->_widgetModelMock->expects(
- $this->once()
- )->method(
- 'getWidgetByClassType'
- )->will(
- $this->returnValue($widget)
- );
- $xmlFile = __DIR__ . '/../_files/widget.xml';
- $this->_viewFileSystemMock->expects($this->once())->method('getFilename')->will($this->returnValue($xmlFile));
- $themeConfigFile = __DIR__ . '/../_files/mappedConfigArrayAll.php';
- $themeConfig = include $themeConfigFile;
- $this->_readerMock->expects(
- $this->once()
- )->method(
- 'readFile'
- )->with(
- $this->equalTo($xmlFile)
- )->will(
- $this->returnValue($themeConfig)
- );
- $result = $this->_model->getWidgetConfigAsArray();
- $expectedConfigFile = __DIR__ . '/../_files/mappedConfigArray1.php';
- $expectedConfig = include $expectedConfigFile;
- $this->assertEquals($expectedConfig, $result);
- }
- public function testGetWidgetTemplates()
- {
- $expectedConfigFile = __DIR__ . '/../_files/mappedConfigArray1.php';
- $widget = include $expectedConfigFile;
- $this->_widgetModelMock->expects(
- $this->once()
- )->method(
- 'getWidgetByClassType'
- )->will(
- $this->returnValue($widget)
- );
- $this->_viewFileSystemMock->expects($this->once())->method('getFilename')->will($this->returnValue(''));
- $expectedTemplates = [
- 'default' => [
- 'value' => 'product/widget/link/link_block.phtml',
- 'label' => 'Product Link Block Template',
- ],
- 'link_inline' => [
- 'value' => 'product/widget/link/link_inline.phtml',
- 'label' => 'Product Link Inline Template',
- ],
- ];
- $this->assertEquals($expectedTemplates, $this->_model->getWidgetTemplates());
- }
- public function testGetWidgetTemplatesValueOnly()
- {
- $widget = [
- '@' => ['type' => \Magento\Cms\Block\Widget\Page\Link::class, 'module' => 'Magento_Cms'],
- 'name' => 'CMS Page Link',
- 'description' => 'Link to a CMS Page',
- 'is_email_compatible' => 'true',
- 'placeholder_image' => 'Magento_Cms::images/widget_page_link.png',
- 'parameters' => [
- 'template' => [
- 'values' => [
- 'default' => ['value' => 'product/widget/link/link_block.phtml', 'label' => 'Template'],
- ],
- 'type' => 'select',
- 'visible' => 'true',
- 'label' => 'Template',
- 'value' => 'product/widget/link/link_block.phtml',
- ],
- ],
- ];
- $this->_widgetModelMock->expects(
- $this->once()
- )->method(
- 'getWidgetByClassType'
- )->will(
- $this->returnValue($widget)
- );
- $this->_viewFileSystemMock->expects($this->once())->method('getFilename')->will($this->returnValue(''));
- $expectedTemplates = [
- 'default' => ['value' => 'product/widget/link/link_block.phtml', 'label' => 'Template'],
- ];
- $this->assertEquals($expectedTemplates, $this->_model->getWidgetTemplates());
- }
- public function testGetWidgetTemplatesNoTemplate()
- {
- $widget = [
- '@' => ['type' => \Magento\Cms\Block\Widget\Page\Link::class, 'module' => 'Magento_Cms'],
- 'name' => 'CMS Page Link',
- 'description' => 'Link to a CMS Page',
- 'is_email_compatible' => 'true',
- 'placeholder_image' => 'Magento_Cms::images/widget_page_link.png',
- 'parameters' => [],
- ];
- $this->_widgetModelMock->expects(
- $this->once()
- )->method(
- 'getWidgetByClassType'
- )->will(
- $this->returnValue($widget)
- );
- $this->_viewFileSystemMock->expects($this->once())->method('getFilename')->will($this->returnValue(''));
- $expectedTemplates = [];
- $this->assertEquals($expectedTemplates, $this->_model->getWidgetTemplates());
- }
- public function testGetWidgetSupportedContainers()
- {
- $expectedConfigFile = __DIR__ . '/../_files/mappedConfigArray1.php';
- $widget = include $expectedConfigFile;
- $this->_widgetModelMock->expects(
- $this->once()
- )->method(
- 'getWidgetByClassType'
- )->will(
- $this->returnValue($widget)
- );
- $this->_viewFileSystemMock->expects($this->once())->method('getFilename')->will($this->returnValue(''));
- $expectedContainers = ['left', 'content'];
- $this->assertEquals($expectedContainers, $this->_model->getWidgetSupportedContainers());
- }
- public function testGetWidgetSupportedContainersNoContainer()
- {
- $widget = [
- '@' => ['type' => \Magento\Cms\Block\Widget\Page\Link::class, 'module' => 'Magento_Cms'],
- 'name' => 'CMS Page Link',
- 'description' => 'Link to a CMS Page',
- 'is_email_compatible' => 'true',
- 'placeholder_image' => 'Magento_Cms::images/widget_page_link.png',
- ];
- $this->_widgetModelMock->expects(
- $this->once()
- )->method(
- 'getWidgetByClassType'
- )->will(
- $this->returnValue($widget)
- );
- $this->_viewFileSystemMock->expects($this->once())->method('getFilename')->will($this->returnValue(''));
- $expectedContainers = [];
- $this->assertEquals($expectedContainers, $this->_model->getWidgetSupportedContainers());
- }
- public function testGetWidgetSupportedTemplatesByContainers()
- {
- $expectedConfigFile = __DIR__ . '/../_files/mappedConfigArray1.php';
- $widget = include $expectedConfigFile;
- $this->_widgetModelMock->expects(
- $this->once()
- )->method(
- 'getWidgetByClassType'
- )->will(
- $this->returnValue($widget)
- );
- $this->_viewFileSystemMock->expects($this->once())->method('getFilename')->will($this->returnValue(''));
- $expectedTemplates = [
- ['value' => 'product/widget/link/link_block.phtml', 'label' => 'Product Link Block Template'],
- ['value' => 'product/widget/link/link_inline.phtml', 'label' => 'Product Link Inline Template'],
- ];
- $this->assertEquals($expectedTemplates, $this->_model->getWidgetSupportedTemplatesByContainer('left'));
- }
- public function testGetWidgetSupportedTemplatesByContainers2()
- {
- $expectedConfigFile = __DIR__ . '/../_files/mappedConfigArray1.php';
- $widget = include $expectedConfigFile;
- $this->_widgetModelMock->expects(
- $this->once()
- )->method(
- 'getWidgetByClassType'
- )->will(
- $this->returnValue($widget)
- );
- $this->_viewFileSystemMock->expects($this->once())->method('getFilename')->will($this->returnValue(''));
- $expectedTemplates = [
- ['value' => 'product/widget/link/link_block.phtml', 'label' => 'Product Link Block Template'],
- ];
- $this->assertEquals($expectedTemplates, $this->_model->getWidgetSupportedTemplatesByContainer('content'));
- }
- public function testGetWidgetSupportedTemplatesByContainersNoSupportedContainersSpecified()
- {
- $widget = [
- '@' => ['type' => \Magento\Cms\Block\Widget\Page\Link::class, 'module' => 'Magento_Cms'],
- 'name' => 'CMS Page Link',
- 'description' => 'Link to a CMS Page',
- 'is_email_compatible' => 'true',
- 'placeholder_image' => 'Magento_Cms::images/widget_page_link.png',
- 'parameters' => [
- 'template' => [
- 'values' => [
- 'default' => ['value' => 'product/widget/link/link_block.phtml', 'label' => 'Template'],
- ],
- 'type' => 'select',
- 'visible' => 'true',
- 'label' => 'Template',
- 'value' => 'product/widget/link/link_block.phtml',
- ],
- ],
- ];
- $this->_widgetModelMock->expects(
- $this->once()
- )->method(
- 'getWidgetByClassType'
- )->will(
- $this->returnValue($widget)
- );
- $this->_viewFileSystemMock->expects($this->once())->method('getFilename')->will($this->returnValue(''));
- $expectedContainers = [
- 'default' => ['value' => 'product/widget/link/link_block.phtml', 'label' => 'Template'],
- ];
- $this->assertEquals($expectedContainers, $this->_model->getWidgetSupportedTemplatesByContainer('content'));
- }
- public function testGetWidgetSupportedTemplatesByContainersUnknownContainer()
- {
- $expectedConfigFile = __DIR__ . '/../_files/mappedConfigArray1.php';
- $widget = include $expectedConfigFile;
- $this->_widgetModelMock->expects(
- $this->once()
- )->method(
- 'getWidgetByClassType'
- )->will(
- $this->returnValue($widget)
- );
- $this->_viewFileSystemMock->expects($this->once())->method('getFilename')->will($this->returnValue(''));
- $expectedTemplates = [];
- $this->assertEquals($expectedTemplates, $this->_model->getWidgetSupportedTemplatesByContainer('unknown'));
- }
- public function testGetWidgetParameters()
- {
- $serializedArray = '{"anchor_text":"232323232323232323","title":"232323232323232","page_id":"2"}';
- $this->serializer->expects($this->once())
- ->method('unserialize')
- ->willReturn(json_decode($serializedArray, true));
- $this->_model->setData('widget_parameters', $serializedArray);
- $this->assertEquals(
- json_decode($serializedArray, true),
- $this->_model->getWidgetParameters()
- );
- }
- public function testBeforeSave()
- {
- $widgetParameters = [
- 'anchor_text' => 'Test',
- 'title' => 'Test',
- 'page_id' => '2'
- ];
- $this->serializer->expects($this->once())
- ->method('serialize')
- ->willReturn(json_encode($widgetParameters));
- $this->_model->setData('widget_parameters', $widgetParameters);
- $this->_model->beforeSave();
- }
- }
|