1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Tests\Admin; |
15
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
18
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
19
|
|
|
use Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface; |
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
21
|
|
|
|
22
|
|
|
class PoolTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var Pool |
26
|
|
|
*/ |
27
|
|
|
private $pool = null; |
28
|
|
|
|
29
|
|
|
public function setUp(): void |
30
|
|
|
{ |
31
|
|
|
$this->pool = new Pool($this->getContainer(), 'Sonata Admin', '/path/to/pic.png', ['foo' => 'bar']); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testGetGroups(): void |
35
|
|
|
{ |
36
|
|
|
$this->pool->setAdminServiceIds(['sonata.user.admin.group1']); |
37
|
|
|
|
38
|
|
|
$this->pool->setAdminGroups([ |
39
|
|
|
'adminGroup1' => ['sonata.user.admin.group1' => []], |
40
|
|
|
]); |
41
|
|
|
|
42
|
|
|
$expectedOutput = [ |
43
|
|
|
'adminGroup1' => [ |
44
|
|
|
'sonata.user.admin.group1' => 'sonata_user_admin_group1_AdminClass', |
45
|
|
|
], |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
$this->assertSame($expectedOutput, $this->pool->getGroups()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testHasGroup(): void |
52
|
|
|
{ |
53
|
|
|
$this->pool->setAdminGroups([ |
54
|
|
|
'adminGroup1' => [], |
55
|
|
|
]); |
56
|
|
|
|
57
|
|
|
$this->assertTrue($this->pool->hasGroup('adminGroup1')); |
58
|
|
|
$this->assertFalse($this->pool->hasGroup('adminGroup2')); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testGetDashboardGroups(): void |
62
|
|
|
{ |
63
|
|
|
$admin_group1 = $this->createMock(AdminInterface::class); |
64
|
|
|
$admin_group1->expects($this->once())->method('showIn')->will($this->returnValue(true)); |
65
|
|
|
|
66
|
|
|
$admin_group2 = $this->createMock(AdminInterface::class); |
67
|
|
|
$admin_group2->expects($this->once())->method('showIn')->will($this->returnValue(false)); |
68
|
|
|
|
69
|
|
|
$admin_group3 = $this->createMock(AdminInterface::class); |
70
|
|
|
$admin_group3->expects($this->once())->method('showIn')->will($this->returnValue(false)); |
71
|
|
|
|
72
|
|
|
$container = $this->createMock(ContainerInterface::class); |
73
|
|
|
|
74
|
|
|
$container->expects($this->any())->method('get')->will($this->onConsecutiveCalls( |
75
|
|
|
$admin_group1, $admin_group2, $admin_group3 |
76
|
|
|
)); |
77
|
|
|
|
78
|
|
|
$pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png'); |
79
|
|
|
$pool->setAdminServiceIds(['sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3']); |
80
|
|
|
|
81
|
|
|
$pool->setAdminGroups([ |
82
|
|
|
'adminGroup1' => [ |
83
|
|
|
'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group1')], |
84
|
|
|
], |
85
|
|
|
'adminGroup2' => [ |
86
|
|
|
'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group2')], |
87
|
|
|
], |
88
|
|
|
'adminGroup3' => [ |
89
|
|
|
'items' => ['itemKey' => $this->getItemArray('sonata.user.admin.group3')], |
90
|
|
|
], |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
$groups = $pool->getDashboardGroups(); |
94
|
|
|
|
95
|
|
|
$this->assertCount(1, $groups); |
96
|
|
|
$this->assertSame($admin_group1, $groups['adminGroup1']['items']['itemKey']); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testGetAdminsByGroupWhenGroupNotSet(): void |
100
|
|
|
{ |
101
|
|
|
$this->expectException(\InvalidArgumentException::class); |
102
|
|
|
|
103
|
|
|
$this->pool->setAdminGroups([ |
104
|
|
|
'adminGroup1' => [], |
105
|
|
|
]); |
106
|
|
|
|
107
|
|
|
$this->pool->getAdminsByGroup('adminGroup2'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testGetAdminsByGroupWhenGroupIsEmpty(): void |
111
|
|
|
{ |
112
|
|
|
$this->pool->setAdminGroups([ |
113
|
|
|
'adminGroup1' => [], |
114
|
|
|
]); |
115
|
|
|
|
116
|
|
|
$this->assertSame([], $this->pool->getAdminsByGroup('adminGroup1')); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testGetAdminsByGroup(): void |
120
|
|
|
{ |
121
|
|
|
$this->pool->setAdminServiceIds(['sonata.admin1', 'sonata.admin2', 'sonata.admin3']); |
122
|
|
|
$this->pool->setAdminGroups([ |
123
|
|
|
'adminGroup1' => [ |
124
|
|
|
'items' => [ |
125
|
|
|
$this->getItemArray('sonata.admin1'), |
126
|
|
|
$this->getItemArray('sonata.admin2'), |
127
|
|
|
], |
128
|
|
|
], |
129
|
|
|
'adminGroup2' => [ |
130
|
|
|
'items' => [$this->getItemArray('sonata.admin3')], |
131
|
|
|
], |
132
|
|
|
]); |
133
|
|
|
|
134
|
|
|
$this->assertEquals([ |
135
|
|
|
'sonata_admin1_AdminClass', |
136
|
|
|
'sonata_admin2_AdminClass', |
137
|
|
|
], $this->pool->getAdminsByGroup('adminGroup1')); |
138
|
|
|
$this->assertEquals(['sonata_admin3_AdminClass'], $this->pool->getAdminsByGroup('adminGroup2')); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testGetAdminForClassWhenAdminClassIsNotSet(): void |
142
|
|
|
{ |
143
|
|
|
$this->pool->setAdminClasses(['someclass' => 'sonata.user.admin.group1']); |
144
|
|
|
$this->assertFalse($this->pool->hasAdminByClass('notexists')); |
145
|
|
|
$this->assertNull($this->pool->getAdminByClass('notexists')); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testGetAdminForClassWithInvalidFormat(): void |
149
|
|
|
{ |
150
|
|
|
$this->expectException(\RuntimeException::class); |
151
|
|
|
|
152
|
|
|
$this->pool->setAdminClasses(['someclass' => 'sonata.user.admin.group1']); |
153
|
|
|
$this->assertTrue($this->pool->hasAdminByClass('someclass')); |
154
|
|
|
|
155
|
|
|
$this->pool->getAdminByClass('someclass'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testGetAdminForClassWithTooManyRegisteredAdmin(): void |
159
|
|
|
{ |
160
|
|
|
$this->expectException(\RuntimeException::class); |
161
|
|
|
|
162
|
|
|
$this->pool->setAdminClasses([ |
163
|
|
|
'someclass' => ['sonata.user.admin.group1', 'sonata.user.admin.group2'], |
164
|
|
|
]); |
165
|
|
|
|
166
|
|
|
$this->assertTrue($this->pool->hasAdminByClass('someclass')); |
167
|
|
|
$this->pool->getAdminByClass('someclass'); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function testGetAdminForClassWhenAdminClassIsSet(): void |
171
|
|
|
{ |
172
|
|
|
$this->pool->setAdminServiceIds(['sonata.user.admin.group1']); |
173
|
|
|
$this->pool->setAdminClasses([ |
174
|
|
|
'someclass' => ['sonata.user.admin.group1'], |
175
|
|
|
]); |
176
|
|
|
|
177
|
|
|
$this->assertTrue($this->pool->hasAdminByClass('someclass')); |
178
|
|
|
$this->assertSame('sonata_user_admin_group1_AdminClass', $this->pool->getAdminByClass('someclass')); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function testGetInstanceWithUndefinedServiceId(): void |
182
|
|
|
{ |
183
|
|
|
$this->expectException(\InvalidArgumentException::class); |
184
|
|
|
$this->expectExceptionMessage('Admin service "sonata.news.admin.post" not found in admin pool.'); |
185
|
|
|
|
186
|
|
|
$this->pool->getInstance('sonata.news.admin.post'); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function testGetAdminByAdminCode(): void |
190
|
|
|
{ |
191
|
|
|
$this->pool->setAdminServiceIds(['sonata.news.admin.post']); |
192
|
|
|
|
193
|
|
|
$this->assertSame('sonata_news_admin_post_AdminClass', $this->pool->getAdminByAdminCode('sonata.news.admin.post')); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function testGetAdminByAdminCodeForChildClass(): void |
197
|
|
|
{ |
198
|
|
|
$adminMock = $this->getMockBuilder(AdminInterface::class) |
199
|
|
|
->disableOriginalConstructor() |
200
|
|
|
->getMock(); |
201
|
|
|
$adminMock->expects($this->any()) |
202
|
|
|
->method('hasChild') |
203
|
|
|
->will($this->returnValue(true)); |
204
|
|
|
$adminMock->expects($this->once()) |
205
|
|
|
->method('getChild') |
206
|
|
|
->with($this->equalTo('sonata.news.admin.comment')) |
207
|
|
|
->will($this->returnValue('commentAdminClass')); |
208
|
|
|
|
209
|
|
|
$containerMock = $this->createMock(ContainerInterface::class); |
210
|
|
|
$containerMock->expects($this->any()) |
211
|
|
|
->method('get') |
212
|
|
|
->will($this->returnValue($adminMock)); |
213
|
|
|
|
214
|
|
|
$this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png'); |
215
|
|
|
$this->pool->setAdminServiceIds(['sonata.news.admin.post']); |
216
|
|
|
|
217
|
|
|
$this->assertSame('commentAdminClass', $this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.comment')); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function testGetAdminByAdminCodeForChildInvalidClass(): void |
221
|
|
|
{ |
222
|
|
|
$adminMock = $this->getMockBuilder(AdminInterface::class) |
223
|
|
|
->disableOriginalConstructor() |
224
|
|
|
->getMock(); |
225
|
|
|
$adminMock->expects($this->any()) |
226
|
|
|
->method('hasChild') |
227
|
|
|
->will($this->returnValue(false)); |
228
|
|
|
|
229
|
|
|
$containerMock = $this->createMock(ContainerInterface::class); |
230
|
|
|
$containerMock->expects($this->any()) |
231
|
|
|
->method('get') |
232
|
|
|
->will($this->returnValue($adminMock)); |
233
|
|
|
|
234
|
|
|
$this->pool = new Pool($containerMock, 'Sonata', '/path/to/logo.png'); |
235
|
|
|
$this->pool->setAdminServiceIds(['sonata.news.admin.post']); |
236
|
|
|
|
237
|
|
|
$this->assertFalse($this->pool->getAdminByAdminCode('sonata.news.admin.post|sonata.news.admin.invalid')); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function testGetAdminClasses(): void |
241
|
|
|
{ |
242
|
|
|
$this->pool->setAdminClasses(['someclass' => 'sonata.user.admin.group1']); |
243
|
|
|
$this->assertSame(['someclass' => 'sonata.user.admin.group1'], $this->pool->getAdminClasses()); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function testGetAdminGroups(): void |
247
|
|
|
{ |
248
|
|
|
$this->pool->setAdminGroups(['adminGroup1' => 'sonata.user.admin.group1']); |
249
|
|
|
$this->assertSame(['adminGroup1' => 'sonata.user.admin.group1'], $this->pool->getAdminGroups()); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function testGetAdminServiceIds(): void |
253
|
|
|
{ |
254
|
|
|
$this->pool->setAdminServiceIds(['sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3']); |
255
|
|
|
$this->assertSame(['sonata.user.admin.group1', 'sonata.user.admin.group2', 'sonata.user.admin.group3'], $this->pool->getAdminServiceIds()); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function testGetContainer(): void |
259
|
|
|
{ |
260
|
|
|
$this->assertInstanceOf(ContainerInterface::class, $this->pool->getContainer()); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @group legacy |
265
|
|
|
*/ |
266
|
|
|
public function testTemplate(): void |
267
|
|
|
{ |
268
|
|
|
$templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class); |
269
|
|
|
$templateRegistry->getTemplate('ajax') |
270
|
|
|
->shouldBeCalledTimes(1) |
271
|
|
|
->willReturn('Foo.html.twig'); |
272
|
|
|
|
273
|
|
|
$this->pool->setTemplateRegistry($templateRegistry->reveal()); |
274
|
|
|
|
275
|
|
|
$this->assertSame('Foo.html.twig', $this->pool->getTemplate('ajax')); |
|
|
|
|
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @group legacy |
280
|
|
|
*/ |
281
|
|
|
public function testSetGetTemplates(): void |
282
|
|
|
{ |
283
|
|
|
$templates = [ |
284
|
|
|
'ajax' => 'Foo.html.twig', |
285
|
|
|
'layout' => 'Bar.html.twig', |
286
|
|
|
]; |
287
|
|
|
|
288
|
|
|
$templateRegistry = $this->prophesize(MutableTemplateRegistryInterface::class); |
289
|
|
|
$templateRegistry->setTemplates($templates) |
290
|
|
|
->shouldBeCalledTimes(1); |
291
|
|
|
$templateRegistry->getTemplates() |
292
|
|
|
->shouldBeCalledTimes(1) |
293
|
|
|
->willReturn($templates); |
294
|
|
|
|
295
|
|
|
$this->pool->setTemplateRegistry($templateRegistry->reveal()); |
296
|
|
|
|
297
|
|
|
$this->pool->setTemplates($templates); |
|
|
|
|
298
|
|
|
|
299
|
|
|
$this->assertSame($templates, $this->pool->getTemplates()); |
|
|
|
|
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
public function testGetTitleLogo(): void |
303
|
|
|
{ |
304
|
|
|
$this->assertSame('/path/to/pic.png', $this->pool->getTitleLogo()); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
public function testGetTitle(): void |
308
|
|
|
{ |
309
|
|
|
$this->assertSame('Sonata Admin', $this->pool->getTitle()); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
public function testGetOption(): void |
313
|
|
|
{ |
314
|
|
|
$this->assertSame('bar', $this->pool->getOption('foo')); |
315
|
|
|
|
316
|
|
|
$this->assertNull($this->pool->getOption('non_existent_option')); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
public function testOptionDefault(): void |
320
|
|
|
{ |
321
|
|
|
$this->assertSame([], $this->pool->getOption('nonexistantarray', [])); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @return Symfony\Component\DependencyInjection\ContainerInterface - the mock of container interface |
326
|
|
|
*/ |
327
|
|
|
private function getContainer() |
328
|
|
|
{ |
329
|
|
|
$containerMock = $this->createMock(ContainerInterface::class); |
330
|
|
|
$containerMock->expects($this->any()) |
331
|
|
|
->method('get') |
332
|
|
|
->will($this->returnCallback(function ($serviceId) { |
333
|
|
|
return str_replace('.', '_', $serviceId).'_AdminClass'; |
334
|
|
|
})); |
335
|
|
|
|
336
|
|
|
return $containerMock; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
private function getItemArray($serviceId) |
340
|
|
|
{ |
341
|
|
|
return [ |
342
|
|
|
'admin' => $serviceId, |
343
|
|
|
'label' => '', |
344
|
|
|
'route' => '', |
345
|
|
|
'route_params' => [], |
346
|
|
|
]; |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.