ParserTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Translation\Test\Unit\Model\Inline;
  7. use Magento\Translation\Model\Inline\Parser;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. use Magento\Framework\Translate\InlineInterface;
  10. use Magento\Framework\App\Cache\TypeListInterface;
  11. use Magento\Store\Model\StoreManagerInterface;
  12. use Magento\Store\Api\Data\StoreInterface;
  13. use Magento\Translation\Model\ResourceModel\StringUtilsFactory;
  14. use Magento\Translation\Model\ResourceModel\StringUtils;
  15. use Magento\Translation\Model\Inline\CacheManager;
  16. /**
  17. * Class ParserTest to test \Magento\Translation\Model\Inline\Parser
  18. */
  19. class ParserTest extends \PHPUnit\Framework\TestCase
  20. {
  21. /**
  22. * @var Parser
  23. */
  24. private $model;
  25. /**
  26. * @var ObjectManager
  27. */
  28. private $objectManager;
  29. /**
  30. * @var InlineInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $translateInlineMock;
  33. /**
  34. * @var TypeListInterface|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $appCacheMock;
  37. /**
  38. * @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $storeManagerMock;
  41. /**
  42. * @var StoreInterface|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $storeMock;
  45. /**
  46. * @var \Zend_Filter_Interface|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. private $inputFilterMock;
  49. /**
  50. * @var StringUtilsFactory|\PHPUnit_Framework_MockObject_MockObject
  51. */
  52. private $resourceFactoryMock;
  53. /**
  54. * @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject
  55. */
  56. private $appStateMock;
  57. /**
  58. * @var StringUtils|\PHPUnit_Framework_MockObject_MockObject
  59. */
  60. private $resourceMock;
  61. /**
  62. * @var CacheManager|\PHPUnit_Framework_MockObject_MockObject
  63. */
  64. private $cacheManagerMock;
  65. protected function setUp()
  66. {
  67. $this->objectManager = new ObjectManager($this);
  68. $this->translateInlineMock =
  69. $this->getMockForAbstractClass(\Magento\Framework\Translate\InlineInterface::class);
  70. $this->appCacheMock = $this->getMockForAbstractClass(\Magento\Framework\App\Cache\TypeListInterface::class);
  71. $this->storeManagerMock = $this->getMockForAbstractClass(\Magento\Store\Model\StoreManagerInterface::class);
  72. $this->storeMock = $this->getMockForAbstractClass(\Magento\Store\Api\Data\StoreInterface::class);
  73. $this->storeManagerMock->expects($this->any())
  74. ->method('getStore')
  75. ->willReturn($this->storeMock);
  76. $this->resourceFactoryMock = $this->getMockBuilder(
  77. \Magento\Translation\Model\ResourceModel\StringUtilsFactory::class
  78. )
  79. ->disableOriginalConstructor()
  80. ->getMock();
  81. $this->resourceMock = $this->getMockBuilder(\Magento\Translation\Model\ResourceModel\StringUtils::class)
  82. ->disableOriginalConstructor()
  83. ->setMethods([])
  84. ->getMock();
  85. $this->inputFilterMock = $this->getMockBuilder('Zend_Filter_Interface');
  86. $this->resourceFactoryMock->expects($this->any())
  87. ->method('create')
  88. ->willReturn($this->resourceMock);
  89. $this->cacheManagerMock = $this->getMockBuilder(\Magento\Translation\Model\Inline\CacheManager::class)
  90. ->disableOriginalConstructor()
  91. ->setMethods([])
  92. ->getMock();
  93. $this->appStateMock = $this->getMockBuilder(\Magento\Framework\App\State::class)
  94. ->disableOriginalConstructor()
  95. ->setMethods([])
  96. ->getMock();
  97. }
  98. public function testProcessAjaxPostNotAllowed()
  99. {
  100. $expected = ['inline' => 'not allowed'];
  101. $this->translateInlineMock->expects($this->once())
  102. ->method('isAllowed')
  103. ->willReturn(false);
  104. $this->model = $this->objectManager->getObject(
  105. Parser::class,
  106. ['translateInline' => $this->translateInlineMock]
  107. );
  108. $this->assertEquals($expected, $this->model->processAjaxPost([]));
  109. }
  110. public function testProcessAjaxPost()
  111. {
  112. $this->translateInlineMock->expects($this->once())
  113. ->method('isAllowed')
  114. ->willReturn(true);
  115. $this->model = $this->objectManager->getObject(
  116. Parser::class,
  117. [
  118. 'cacheManager' => $this->cacheManagerMock,
  119. 'resource' => $this->resourceFactoryMock,
  120. 'storeManager' => $this->storeManagerMock,
  121. 'translateInline' => $this->translateInlineMock
  122. ]
  123. );
  124. $this->model->processAjaxPost([]);
  125. }
  126. public function testProcessResponseBodyStringProcessingAttributesCorrectly()
  127. {
  128. $testContent = file_get_contents(__DIR__ . '/_files/datatranslate_fixture.html');
  129. $processedAttributes = [
  130. "data-translate=\"[{'shown':'* Required Fields','translated':'* Required Fields',"
  131. . "'original':'* Required Fields','location':'Tag attribute (ALT, TITLE, etc.)'}]\"",
  132. "data-translate=\"[{'shown':'Email','translated':'Email','original':'Email',"
  133. . "'location':'Tag attribute (ALT, TITLE, etc.)'}]\"",
  134. "data-translate=\"[{'shown':'Password','translated':'Password','original':'Password',"
  135. . "'location':'Tag attribute (ALT, TITLE, etc.)'}]\""
  136. ];
  137. $this->translateInlineMock->expects($this->any())->method('getAdditionalHtmlAttribute')->willReturn(null);
  138. $this->model = $this->objectManager->getObject(
  139. Parser::class,
  140. [
  141. 'cacheManager' => $this->cacheManagerMock,
  142. 'resource' => $this->resourceFactoryMock,
  143. 'storeManager' => $this->storeManagerMock,
  144. 'translateInline' => $this->translateInlineMock,
  145. '_resourceFactory' => $this->resourceMock,
  146. '_inputFilter' => $this->inputFilterMock,
  147. '_appState' => $this->appStateMock,
  148. '_appCache' => $this->appCacheMock,
  149. '_translateInline' => $this->translateInlineMock
  150. ]
  151. );
  152. $processedContent = $this->model->processResponseBodyString($testContent);
  153. foreach ($processedAttributes as $attribute) {
  154. $this->assertContains($attribute, $processedContent, "data-translate attribute not processed correctly");
  155. }
  156. }
  157. }