InlineTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Translate\Test\Unit;
  7. use \Magento\Framework\Translate\Inline;
  8. class InlineTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Framework\App\ScopeResolverInterface|\PHPUnit_Framework_MockObject_MockObject
  12. */
  13. protected $scopeResolverMock;
  14. /**
  15. * @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $urlMock;
  18. /**
  19. * @var \Magento\Framework\View\LayoutInterface|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $layoutMock;
  22. /**
  23. * @var \Magento\Framework\Translate\Inline\ConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $configMock;
  26. /**
  27. * @var \Magento\Framework\Translate\Inline\ParserFactory|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $parserMock;
  30. /**
  31. * @var \Magento\Framework\Translate\Inline\StateInterface|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $stateMock;
  34. protected function setUp()
  35. {
  36. $this->scopeResolverMock =
  37. $this->createMock(\Magento\Framework\App\ScopeResolverInterface::class);
  38. $this->urlMock = $this->createMock(\Magento\Framework\UrlInterface::class);
  39. $this->layoutMock = $this->createMock(\Magento\Framework\View\LayoutInterface::class);
  40. $this->configMock = $this->createMock(\Magento\Framework\Translate\Inline\ConfigInterface::class);
  41. $this->parserMock = $this->createMock(\Magento\Framework\Translate\Inline\ParserInterface::class);
  42. $this->stateMock = $this->createMock(\Magento\Framework\Translate\Inline\StateInterface::class);
  43. }
  44. /**
  45. * @param bool $isEnabled
  46. * @param bool $isActive
  47. * @param bool $isDevAllowed
  48. * @param bool $result
  49. * @dataProvider isAllowedDataProvider
  50. */
  51. public function testIsAllowed($isEnabled, $isActive, $isDevAllowed, $result)
  52. {
  53. $this->prepareIsAllowed($isEnabled, $isActive, $isDevAllowed);
  54. $model = new Inline(
  55. $this->scopeResolverMock,
  56. $this->urlMock,
  57. $this->layoutMock,
  58. $this->configMock,
  59. $this->parserMock,
  60. $this->stateMock
  61. );
  62. $this->assertEquals($result, $model->isAllowed());
  63. $this->assertEquals($result, $model->isAllowed());
  64. }
  65. /**
  66. * @return array
  67. */
  68. public function isAllowedDataProvider()
  69. {
  70. return [
  71. [true, true, true, true],
  72. [true, false, true, false],
  73. [true, true, false, false],
  74. [true, false, false, false],
  75. [false, true, true, false],
  76. [false, false, true, false],
  77. [false, true, false, false],
  78. [false, false, false, false],
  79. ];
  80. }
  81. public function testGetParser()
  82. {
  83. $model = new Inline(
  84. $this->scopeResolverMock,
  85. $this->urlMock,
  86. $this->layoutMock,
  87. $this->configMock,
  88. $this->parserMock,
  89. $this->stateMock
  90. );
  91. $this->assertEquals($this->parserMock, $model->getParser());
  92. }
  93. /**
  94. * @param string|array $body
  95. * @param string $expected
  96. * @dataProvider processResponseBodyStripInlineDataProvider
  97. */
  98. public function testProcessResponseBodyStripInline($body, $expected)
  99. {
  100. $scope = 'admin';
  101. $this->prepareIsAllowed(false, true, true, $scope);
  102. $model = new Inline(
  103. $this->scopeResolverMock,
  104. $this->urlMock,
  105. $this->layoutMock,
  106. $this->configMock,
  107. $this->parserMock,
  108. $this->stateMock,
  109. '',
  110. '',
  111. $scope
  112. );
  113. $model->processResponseBody($body, true);
  114. $this->assertEquals($body, $expected);
  115. }
  116. /**
  117. * @return array
  118. */
  119. public function processResponseBodyStripInlineDataProvider()
  120. {
  121. return [
  122. ['test', 'test'],
  123. ['{{{aaaaaa}}{{bbbbb}}{{eeeee}}{{cccccc}}}', 'aaaaaa'],
  124. [['test1', 'test2'], ['test1', 'test2'],],
  125. [['{{{aaaaaa}}', 'test3'], ['{{{aaaaaa}}', 'test3'],],
  126. [['{{{aaaaaa}}{{bbbbb}}', 'test4'], ['{{{aaaaaa}}{{bbbbb}}', 'test4'],],
  127. [['{{{aaaaaa}}{{bbbbb}}{{eeeee}}{{cccccc}}}', 'test5'], ['aaaaaa', 'test5'],],
  128. ];
  129. }
  130. /**
  131. * @param string $scope
  132. * @param array|string $body
  133. * @param array|string $expected
  134. * @dataProvider processResponseBodyDataProvider
  135. * @SuppressWarnings(PHPMD.NPathComplexity)
  136. */
  137. public function testProcessResponseBody($scope, $body, $expected)
  138. {
  139. $isJson = true;
  140. $this->prepareIsAllowed(true, true, true, $scope);
  141. $jsonCall = is_array($body) ? 2 * (count($body) + 1) : 2;
  142. $this->parserMock->expects(
  143. $this->exactly($jsonCall)
  144. )->method(
  145. 'setIsJson'
  146. )->will(
  147. $this->returnValueMap([
  148. [$isJson, $this->returnSelf()],
  149. [!$isJson, $this->returnSelf()],
  150. ])
  151. );
  152. $this->parserMock->expects(
  153. $this->exactly(1)
  154. )->method(
  155. 'processResponseBodyString'
  156. )->with(
  157. is_array($body) ? reset($body) : $body
  158. );
  159. $this->parserMock->expects(
  160. $this->exactly(2)
  161. )->method(
  162. 'getContent'
  163. )->will(
  164. $this->returnValue(is_array($body) ? reset($body) : $body)
  165. );
  166. $model = new Inline(
  167. $this->scopeResolverMock,
  168. $this->urlMock,
  169. $this->layoutMock,
  170. $this->configMock,
  171. $this->parserMock,
  172. $this->stateMock,
  173. '',
  174. '',
  175. $scope
  176. );
  177. $model->processResponseBody($body, $isJson);
  178. $this->assertEquals($body, $expected);
  179. }
  180. /**
  181. * @return array
  182. */
  183. public function processResponseBodyDataProvider()
  184. {
  185. return [
  186. ['admin', 'test', 'test'],
  187. ['not_admin', 'test1', 'test1'],
  188. ];
  189. }
  190. /**
  191. * @param $scope
  192. * @param $body
  193. * @param $expected
  194. * @dataProvider processResponseBodyGetInlineScriptDataProvider
  195. * @SuppressWarnings(PHPMD.NPathComplexity)
  196. */
  197. public function testProcessResponseBodyGetInlineScript($scope, $body, $expected)
  198. {
  199. $isJson = true;
  200. $this->prepareIsAllowed(true, true, true, $scope);
  201. $jsonCall = is_array($body) ? 2 * (count($body) + 1) : 2;
  202. $this->parserMock->expects(
  203. $this->exactly($jsonCall)
  204. )->method(
  205. 'setIsJson'
  206. )->will(
  207. $this->returnValueMap([
  208. [$isJson, $this->returnSelf()],
  209. [!$isJson, $this->returnSelf()],
  210. ])
  211. );
  212. $this->parserMock->expects(
  213. $this->exactly(1)
  214. )->method(
  215. 'processResponseBodyString'
  216. )->with(
  217. is_array($body) ? reset($body) : $body
  218. );
  219. $this->parserMock->expects(
  220. $this->exactly(2)
  221. )->method(
  222. 'getContent'
  223. )->will(
  224. $this->returnValue(is_array($body) ? reset($body) : $body)
  225. );
  226. $model = new Inline(
  227. $this->scopeResolverMock,
  228. $this->urlMock,
  229. $this->layoutMock,
  230. $this->configMock,
  231. $this->parserMock,
  232. $this->stateMock,
  233. '',
  234. '',
  235. $scope
  236. );
  237. $model->processResponseBody($body, $isJson);
  238. $this->assertEquals($body, $expected);
  239. }
  240. /**
  241. * @return array
  242. */
  243. public function processResponseBodyGetInlineScriptDataProvider()
  244. {
  245. return [
  246. ['admin', 'test', 'test'],
  247. ['not_admin', 'test1', 'test1'],
  248. ];
  249. }
  250. /**
  251. * @param bool $isEnabled
  252. * @param bool $isActive
  253. * @param bool $isDevAllowed
  254. * @param null|string $scope
  255. */
  256. protected function prepareIsAllowed($isEnabled, $isActive, $isDevAllowed, $scope = null)
  257. {
  258. $scopeMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  259. $this->stateMock->expects($this->any())->method('isEnabled')->will($this->returnValue($isEnabled));
  260. $this->scopeResolverMock->expects(
  261. $this->once()
  262. )->method(
  263. 'getScope'
  264. )->with(
  265. $scope
  266. )->will(
  267. $this->returnValue($scopeMock)
  268. );
  269. $this->configMock->expects(
  270. $this->once()
  271. )->method(
  272. 'isActive'
  273. )->with(
  274. $scopeMock
  275. )->will(
  276. $this->returnValue($isActive)
  277. );
  278. $this->configMock->expects(
  279. $this->exactly((int)$isActive)
  280. )->method(
  281. 'isDevAllowed'
  282. )->will(
  283. $this->returnValue($isDevAllowed)
  284. );
  285. }
  286. }