Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InlineTest.php
Go to the documentation of this file.
1 <?php
7 
8 use \Magento\Framework\Translate\Inline;
9 
10 class InlineTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $scopeResolverMock;
16 
20  protected $urlMock;
21 
25  protected $layoutMock;
26 
30  protected $configMock;
31 
35  protected $parserMock;
36 
40  protected $stateMock;
41 
42  protected function setUp()
43  {
44  $this->scopeResolverMock =
45  $this->createMock(\Magento\Framework\App\ScopeResolverInterface::class);
46  $this->urlMock = $this->createMock(\Magento\Framework\UrlInterface::class);
47  $this->layoutMock = $this->createMock(\Magento\Framework\View\LayoutInterface::class);
48  $this->configMock = $this->createMock(\Magento\Framework\Translate\Inline\ConfigInterface::class);
49  $this->parserMock = $this->createMock(\Magento\Framework\Translate\Inline\ParserInterface::class);
50  $this->stateMock = $this->createMock(\Magento\Framework\Translate\Inline\StateInterface::class);
51  }
52 
60  public function testIsAllowed($isEnabled, $isActive, $isDevAllowed, $result)
61  {
62  $this->prepareIsAllowed($isEnabled, $isActive, $isDevAllowed);
63 
64  $model = new Inline(
65  $this->scopeResolverMock,
66  $this->urlMock,
67  $this->layoutMock,
68  $this->configMock,
69  $this->parserMock,
70  $this->stateMock
71  );
72 
73  $this->assertEquals($result, $model->isAllowed());
74  $this->assertEquals($result, $model->isAllowed());
75  }
76 
80  public function isAllowedDataProvider()
81  {
82  return [
83  [true, true, true, true],
84  [true, false, true, false],
85  [true, true, false, false],
86  [true, false, false, false],
87  [false, true, true, false],
88  [false, false, true, false],
89  [false, true, false, false],
90  [false, false, false, false],
91  ];
92  }
93 
94  public function testGetParser()
95  {
96  $model = new Inline(
97  $this->scopeResolverMock,
98  $this->urlMock,
99  $this->layoutMock,
100  $this->configMock,
101  $this->parserMock,
102  $this->stateMock
103  );
104  $this->assertEquals($this->parserMock, $model->getParser());
105  }
106 
112  public function testProcessResponseBodyStripInline($body, $expected)
113  {
114  $scope = 'admin';
115  $this->prepareIsAllowed(false, true, true, $scope);
116 
117  $model = new Inline(
118  $this->scopeResolverMock,
119  $this->urlMock,
120  $this->layoutMock,
121  $this->configMock,
122  $this->parserMock,
123  $this->stateMock,
124  '',
125  '',
126  $scope
127  );
128  $model->processResponseBody($body, true);
129  $this->assertEquals($body, $expected);
130  }
131 
136  {
137  return [
138  ['test', 'test'],
139  ['{{{aaaaaa}}{{bbbbb}}{{eeeee}}{{cccccc}}}', 'aaaaaa'],
140  [['test1', 'test2'], ['test1', 'test2'],],
141  [['{{{aaaaaa}}', 'test3'], ['{{{aaaaaa}}', 'test3'],],
142  [['{{{aaaaaa}}{{bbbbb}}', 'test4'], ['{{{aaaaaa}}{{bbbbb}}', 'test4'],],
143  [['{{{aaaaaa}}{{bbbbb}}{{eeeee}}{{cccccc}}}', 'test5'], ['aaaaaa', 'test5'],],
144  ];
145  }
146 
154  public function testProcessResponseBody($scope, $body, $expected)
155  {
156  $isJson = true;
157  $this->prepareIsAllowed(true, true, true, $scope);
158 
159  $jsonCall = is_array($body) ? 2 * (count($body) + 1) : 2;
160  $this->parserMock->expects(
161  $this->exactly($jsonCall)
162  )->method(
163  'setIsJson'
164  )->will(
165  $this->returnValueMap([
166  [$isJson, $this->returnSelf()],
167  [!$isJson, $this->returnSelf()],
168  ])
169  );
170  $this->parserMock->expects(
171  $this->exactly(1)
172  )->method(
173  'processResponseBodyString'
174  )->with(
175  is_array($body) ? reset($body) : $body
176  );
177  $this->parserMock->expects(
178  $this->exactly(2)
179  )->method(
180  'getContent'
181  )->will(
182  $this->returnValue(is_array($body) ? reset($body) : $body)
183  );
184 
185  $model = new Inline(
186  $this->scopeResolverMock,
187  $this->urlMock,
188  $this->layoutMock,
189  $this->configMock,
190  $this->parserMock,
191  $this->stateMock,
192  '',
193  '',
194  $scope
195  );
196 
197  $model->processResponseBody($body, $isJson);
198  $this->assertEquals($body, $expected);
199  }
200 
205  {
206  return [
207  ['admin', 'test', 'test'],
208  ['not_admin', 'test1', 'test1'],
209  ];
210  }
211 
219  public function testProcessResponseBodyGetInlineScript($scope, $body, $expected)
220  {
221  $isJson = true;
222  $this->prepareIsAllowed(true, true, true, $scope);
223 
224  $jsonCall = is_array($body) ? 2 * (count($body) + 1) : 2;
225  $this->parserMock->expects(
226  $this->exactly($jsonCall)
227  )->method(
228  'setIsJson'
229  )->will(
230  $this->returnValueMap([
231  [$isJson, $this->returnSelf()],
232  [!$isJson, $this->returnSelf()],
233  ])
234  );
235  $this->parserMock->expects(
236  $this->exactly(1)
237  )->method(
238  'processResponseBodyString'
239  )->with(
240  is_array($body) ? reset($body) : $body
241  );
242  $this->parserMock->expects(
243  $this->exactly(2)
244  )->method(
245  'getContent'
246  )->will(
247  $this->returnValue(is_array($body) ? reset($body) : $body)
248  );
249 
250  $model = new Inline(
251  $this->scopeResolverMock,
252  $this->urlMock,
253  $this->layoutMock,
254  $this->configMock,
255  $this->parserMock,
256  $this->stateMock,
257  '',
258  '',
259  $scope
260  );
261 
262  $model->processResponseBody($body, $isJson);
263  $this->assertEquals($body, $expected);
264  }
265 
270  {
271  return [
272  ['admin', 'test', 'test'],
273  ['not_admin', 'test1', 'test1'],
274  ];
275  }
276 
283  protected function prepareIsAllowed($isEnabled, $isActive, $isDevAllowed, $scope = null)
284  {
285  $scopeMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
286  $this->stateMock->expects($this->any())->method('isEnabled')->will($this->returnValue($isEnabled));
287  $this->scopeResolverMock->expects(
288  $this->once()
289  )->method(
290  'getScope'
291  )->with(
292  $scope
293  )->will(
294  $this->returnValue($scopeMock)
295  );
296 
297  $this->configMock->expects(
298  $this->once()
299  )->method(
300  'isActive'
301  )->with(
302  $scopeMock
303  )->will(
304  $this->returnValue($isActive)
305  );
306 
307  $this->configMock->expects(
308  $this->exactly((int)$isActive)
309  )->method(
310  'isDevAllowed'
311  )->will(
312  $this->returnValue($isDevAllowed)
313  );
314  }
315 }
testProcessResponseBody($scope, $body, $expected)
Definition: InlineTest.php:154
prepareIsAllowed($isEnabled, $isActive, $isDevAllowed, $scope=null)
Definition: InlineTest.php:283
testIsAllowed($isEnabled, $isActive, $isDevAllowed, $result)
Definition: InlineTest.php:60
testProcessResponseBodyGetInlineScript($scope, $body, $expected)
Definition: InlineTest.php:219