-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathAbstractBlockTest.php
302 lines (275 loc) · 10.1 KB
/
AbstractBlockTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\View\Test\Unit\Element;
use Magento\Framework\Cache\LockQueryInterface;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\Framework\View\Element\Context;
use Magento\Framework\Config\View;
use Magento\Framework\View\ConfigInterface;
use Magento\Framework\Event\ManagerInterface as EventManagerInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Cache\StateInterface as CacheStateInterface;
use Magento\Framework\Session\SidResolverInterface;
use Magento\Framework\Session\SessionManagerInterface;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AbstractBlockTest extends \PHPUnit\Framework\TestCase
{
/**
* @var AbstractBlock
*/
private $block;
/**
* @var EventManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $eventManagerMock;
/**
* @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $scopeConfigMock;
/**
* @var CacheStateInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $cacheStateMock;
/**
* @var SidResolverInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $sidResolverMock;
/**
* @var SessionManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $sessionMock;
/**
* @var LockQueryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $lockQuery;
/**
* @return void
*/
protected function setUp()
{
$this->eventManagerMock = $this->getMockForAbstractClass(EventManagerInterface::class);
$this->scopeConfigMock = $this->getMockForAbstractClass(ScopeConfigInterface::class);
$this->cacheStateMock = $this->getMockForAbstractClass(CacheStateInterface::class);
$this->lockQuery = $this->getMockForAbstractClass(LockQueryInterface::class);
$this->sidResolverMock = $this->getMockForAbstractClass(SidResolverInterface::class);
$this->sessionMock = $this->getMockForAbstractClass(SessionManagerInterface::class);
$contextMock = $this->createMock(Context::class);
$contextMock->expects($this->once())
->method('getEventManager')
->willReturn($this->eventManagerMock);
$contextMock->expects($this->once())
->method('getScopeConfig')
->willReturn($this->scopeConfigMock);
$contextMock->expects($this->once())
->method('getCacheState')
->willReturn($this->cacheStateMock);
$contextMock->expects($this->once())
->method('getSidResolver')
->willReturn($this->sidResolverMock);
$contextMock->expects($this->once())
->method('getSession')
->willReturn($this->sessionMock);
$this->block = $this->getMockForAbstractClass(
AbstractBlock::class,
[
'context' => $contextMock,
'data' => [],
'lockQuery' => $this->lockQuery
]
);
}
/**
* @param string $expectedResult
* @param string $nameInLayout
* @param array $methodArguments
* @dataProvider getUiIdDataProvider
*/
public function testGetUiId($expectedResult, $nameInLayout, $methodArguments)
{
$this->block->setNameInLayout($nameInLayout);
$this->assertEquals($expectedResult, call_user_func_array([$this->block, 'getUiId'], $methodArguments));
}
/**
* @return array
*/
public function getUiIdDataProvider()
{
return [
[' data-ui-id="" ', null, []],
[' data-ui-id="block" ', 'block', []],
[' data-ui-id="block" ', 'block---', []],
[' data-ui-id="block" ', '--block', []],
[' data-ui-id="bl-ock" ', '--bl--ock---', []],
[' data-ui-id="bl-ock" ', '--bL--Ock---', []],
[' data-ui-id="b-l-o-c-k" ', '--b!@#$%^&**()L--O;:...c<_>k---', []],
[
' data-ui-id="a0b1c2d3e4f5g6h7-i8-j9k0l1m2n-3o4p5q6r7-s8t9u0v1w2z3y4x5" ',
'a0b1c2d3e4f5g6h7',
['i8-j9k0l1m2n-3o4p5q6r7', 's8t9u0v1w2z3y4x5']
],
[
' data-ui-id="capsed-block-name-cap-ed-param1-caps2-but-ton" ',
'CaPSed BLOCK NAME',
['cAp$Ed PaRaM1', 'caPs2', 'bUT-TOn']
],
[
' data-ui-id="capsed-block-name-cap-ed-param1-caps2-but-ton-but-ton" ',
'CaPSed BLOCK NAME',
['cAp$Ed PaRaM1', 'caPs2', 'bUT-TOn', 'bUT-TOn']
],
[' data-ui-id="block-0-1-2-3-4" ', '!block!', range(0, 5)]
];
}
/**
* @return void
*/
public function testGetVar()
{
$config = $this->createPartialMock(View::class, ['getVarValue']);
$module = uniqid();
$config->expects($this->any())
->method('getVarValue')
->willReturnMap([
['Magento_Theme', 'v1', 'one'],
[$module, 'v2', 'two']
]);
$configManager = $this->createMock(ConfigInterface::class);
$configManager->expects($this->exactly(2))->method('getViewConfig')->willReturn($config);
/** @var $block AbstractBlock|\PHPUnit_Framework_MockObject_MockObject */
$params = ['viewConfig' => $configManager];
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$block = $this->getMockForAbstractClass(
AbstractBlock::class,
$helper->getConstructArguments(AbstractBlock::class, $params)
);
$block->setData('module_name', 'Magento_Theme');
$this->assertEquals('one', $block->getVar('v1'));
$this->assertEquals('two', $block->getVar('v2', $module));
}
/**
* @return void
*/
public function testIsScopePrivate()
{
$this->assertFalse($this->block->isScopePrivate());
}
/**
* @return void
*/
public function testGetCacheKey()
{
$cacheKey = 'testKey';
$this->block->setData('cache_key', $cacheKey);
$this->assertEquals(AbstractBlock::CACHE_KEY_PREFIX . $cacheKey, $this->block->getCacheKey());
}
/**
* @return void
*/
public function testGetCacheKeyByName()
{
$nameInLayout = 'testBlock';
$this->block->setNameInLayout($nameInLayout);
$cacheKey = sha1($nameInLayout);
$this->assertEquals(AbstractBlock::CACHE_KEY_PREFIX . $cacheKey, $this->block->getCacheKey());
}
/**
* @return void
*/
public function testToHtmlWhenModuleIsDisabled()
{
$moduleName = 'Test';
$this->block->setData('module_name', $moduleName);
$this->eventManagerMock->expects($this->any())
->method('dispatch')
->with('view_block_abstract_to_html_before', ['block' => $this->block]);
$this->scopeConfigMock->expects($this->once())
->method('getValue')
->with('advanced/modules_disable_output/' . $moduleName, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
->willReturn(true);
$this->assertSame('', $this->block->toHtml());
}
/**
* @param string|bool $cacheLifetime
* @param string|bool $dataFromCache
* @param \PHPUnit\Framework\MockObject\Matcher\InvokedCount $expectsDispatchEvent
* @param string $expectedResult
* @return void
* @dataProvider getCacheLifetimeDataProvider
*/
public function testGetCacheLifetimeViaToHtml(
$cacheLifetime,
$dataFromCache,
$expectsDispatchEvent,
$expectedResult
) {
$moduleName = 'Test';
$cacheKey = 'testKey';
$this->block->setData('cache_key', $cacheKey);
$this->block->setData('module_name', $moduleName);
$this->block->setData('cache_lifetime', $cacheLifetime);
$this->eventManagerMock->expects($expectsDispatchEvent)
->method('dispatch');
$this->scopeConfigMock->expects($this->once())
->method('getValue')
->with('advanced/modules_disable_output/' . $moduleName, \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
->willReturn(false);
$this->cacheStateMock->expects($this->any())
->method('isEnabled')
->with(AbstractBlock::CACHE_GROUP)
->willReturn(true);
$this->lockQuery->expects($this->any())
->method('lockedLoadData')
->willReturn($dataFromCache);
$this->sidResolverMock->expects($this->any())
->method('getSessionIdQueryParam')
->with($this->sessionMock)
->willReturn('sessionIdQueryParam');
$this->sessionMock->expects($this->any())
->method('getSessionId')
->willReturn('sessionId');
$this->assertSame($expectedResult, $this->block->toHtml());
}
/**
* @return array
*/
public function getCacheLifetimeDataProvider()
{
return [
[
'cacheLifetime' => null,
'dataFromCache' => 'dataFromCache',
'expectsDispatchEvent' => $this->exactly(2),
'expectedResult' => '',
],
[
'cacheLifetime' => false,
'dataFromCache' => 'dataFromCache',
'expectsDispatchEvent' => $this->exactly(2),
'expectedResult' => '',
],
[
'cacheLifetime' => 120,
'dataFromCache' => 'dataFromCache',
'expectsDispatchEvent' => $this->exactly(2),
'expectedResult' => 'dataFromCache',
],
[
'cacheLifetime' => '120string',
'dataFromCache' => 'dataFromCache',
'expectsDispatchEvent' => $this->exactly(2),
'expectedResult' => 'dataFromCache',
],
[
'cacheLifetime' => 120,
'dataFromCache' => false,
'expectsDispatchEvent' => $this->exactly(2),
'expectedResult' => '',
],
];
}
}