123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Translation\Test\Unit\Model\Inline;
- use Magento\Translation\Model\Inline\Parser;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
- use Magento\Framework\Translate\InlineInterface;
- use Magento\Framework\App\Cache\TypeListInterface;
- use Magento\Store\Model\StoreManagerInterface;
- use Magento\Store\Api\Data\StoreInterface;
- use Magento\Translation\Model\ResourceModel\StringUtilsFactory;
- use Magento\Translation\Model\ResourceModel\StringUtils;
- use Magento\Translation\Model\Inline\CacheManager;
- /**
- * Class ParserTest to test \Magento\Translation\Model\Inline\Parser
- */
- class ParserTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var Parser
- */
- private $model;
- /**
- * @var ObjectManager
- */
- private $objectManager;
- /**
- * @var InlineInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $translateInlineMock;
- /**
- * @var TypeListInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $appCacheMock;
- /**
- * @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $storeManagerMock;
- /**
- * @var StoreInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $storeMock;
- /**
- * @var \Zend_Filter_Interface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $inputFilterMock;
- /**
- * @var StringUtilsFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- private $resourceFactoryMock;
- /**
- * @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject
- */
- private $appStateMock;
- /**
- * @var StringUtils|\PHPUnit_Framework_MockObject_MockObject
- */
- private $resourceMock;
- /**
- * @var CacheManager|\PHPUnit_Framework_MockObject_MockObject
- */
- private $cacheManagerMock;
- protected function setUp()
- {
- $this->objectManager = new ObjectManager($this);
- $this->translateInlineMock =
- $this->getMockForAbstractClass(\Magento\Framework\Translate\InlineInterface::class);
- $this->appCacheMock = $this->getMockForAbstractClass(\Magento\Framework\App\Cache\TypeListInterface::class);
- $this->storeManagerMock = $this->getMockForAbstractClass(\Magento\Store\Model\StoreManagerInterface::class);
- $this->storeMock = $this->getMockForAbstractClass(\Magento\Store\Api\Data\StoreInterface::class);
- $this->storeManagerMock->expects($this->any())
- ->method('getStore')
- ->willReturn($this->storeMock);
- $this->resourceFactoryMock = $this->getMockBuilder(
- \Magento\Translation\Model\ResourceModel\StringUtilsFactory::class
- )
- ->disableOriginalConstructor()
- ->getMock();
- $this->resourceMock = $this->getMockBuilder(\Magento\Translation\Model\ResourceModel\StringUtils::class)
- ->disableOriginalConstructor()
- ->setMethods([])
- ->getMock();
- $this->inputFilterMock = $this->getMockBuilder('Zend_Filter_Interface');
- $this->resourceFactoryMock->expects($this->any())
- ->method('create')
- ->willReturn($this->resourceMock);
- $this->cacheManagerMock = $this->getMockBuilder(\Magento\Translation\Model\Inline\CacheManager::class)
- ->disableOriginalConstructor()
- ->setMethods([])
- ->getMock();
- $this->appStateMock = $this->getMockBuilder(\Magento\Framework\App\State::class)
- ->disableOriginalConstructor()
- ->setMethods([])
- ->getMock();
- }
- public function testProcessAjaxPostNotAllowed()
- {
- $expected = ['inline' => 'not allowed'];
- $this->translateInlineMock->expects($this->once())
- ->method('isAllowed')
- ->willReturn(false);
- $this->model = $this->objectManager->getObject(
- Parser::class,
- ['translateInline' => $this->translateInlineMock]
- );
- $this->assertEquals($expected, $this->model->processAjaxPost([]));
- }
- public function testProcessAjaxPost()
- {
- $this->translateInlineMock->expects($this->once())
- ->method('isAllowed')
- ->willReturn(true);
- $this->model = $this->objectManager->getObject(
- Parser::class,
- [
- 'cacheManager' => $this->cacheManagerMock,
- 'resource' => $this->resourceFactoryMock,
- 'storeManager' => $this->storeManagerMock,
- 'translateInline' => $this->translateInlineMock
- ]
- );
- $this->model->processAjaxPost([]);
- }
- public function testProcessResponseBodyStringProcessingAttributesCorrectly()
- {
- $testContent = file_get_contents(__DIR__ . '/_files/datatranslate_fixture.html');
- $processedAttributes = [
- "data-translate=\"[{'shown':'* Required Fields','translated':'* Required Fields',"
- . "'original':'* Required Fields','location':'Tag attribute (ALT, TITLE, etc.)'}]\"",
- "data-translate=\"[{'shown':'Email','translated':'Email','original':'Email',"
- . "'location':'Tag attribute (ALT, TITLE, etc.)'}]\"",
- "data-translate=\"[{'shown':'Password','translated':'Password','original':'Password',"
- . "'location':'Tag attribute (ALT, TITLE, etc.)'}]\""
- ];
- $this->translateInlineMock->expects($this->any())->method('getAdditionalHtmlAttribute')->willReturn(null);
- $this->model = $this->objectManager->getObject(
- Parser::class,
- [
- 'cacheManager' => $this->cacheManagerMock,
- 'resource' => $this->resourceFactoryMock,
- 'storeManager' => $this->storeManagerMock,
- 'translateInline' => $this->translateInlineMock,
- '_resourceFactory' => $this->resourceMock,
- '_inputFilter' => $this->inputFilterMock,
- '_appState' => $this->appStateMock,
- '_appCache' => $this->appCacheMock,
- '_translateInline' => $this->translateInlineMock
- ]
- );
- $processedContent = $this->model->processResponseBodyString($testContent);
- foreach ($processedAttributes as $attribute) {
- $this->assertContains($attribute, $processedContent, "data-translate attribute not processed correctly");
- }
- }
- }
|