forked from sonata-project/SonataPageBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlockAdminTest.php
264 lines (216 loc) · 7.81 KB
/
BlockAdminTest.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
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\PageBundle\Tests\Functional\Admin;
use Doctrine\ORM\EntityManagerInterface;
use Sonata\PageBundle\Tests\App\Entity\SonataPageBlock;
use Sonata\PageBundle\Tests\App\Entity\SonataPagePage;
use Sonata\PageBundle\Tests\App\Entity\SonataPageSite;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
final class BlockAdminTest extends WebTestCase
{
/**
* @dataProvider provideCrudUrlsCases
*
* @param array<string, mixed> $parameters
*/
public function testCrudUrls(string $url, array $parameters = []): void
{
$client = self::createClient();
$this->prepareData();
$client->request('GET', $url, $parameters);
self::assertResponseIsSuccessful();
}
/**
* @return iterable<array<string|array<string, mixed>>>
*
* @phpstan-return iterable<array{0: string, 1?: array<string, mixed>}>
*/
public static function provideCrudUrlsCases(): iterable
{
yield 'List Block' => ['/admin/tests/app/sonatapageblock/create'];
yield 'List Block types' => ['/admin/tests/app/sonatapageblock/create'];
yield 'Create Block' => ['/admin/tests/app/sonatapageblock/create', [
'type' => 'sonata.page.block.shared_block',
]];
yield 'Edit Block' => ['/admin/tests/app/sonatapageblock/1/edit'];
yield 'Remove Block' => ['/admin/tests/app/sonatapageblock/1/delete'];
yield 'Compose preview Block' => ['/admin/tests/app/sonatapageblock/2/compose-preview'];
}
/**
* @dataProvider provideFormUrlsCases
*
* @param array<string, mixed> $parameters
* @param array<string, mixed> $fieldValues
*/
public function testFormsUrls(string $url, array $parameters, string $button, array $fieldValues = []): void
{
$client = self::createClient();
$this->prepareData();
$client->request('GET', $url, $parameters);
$client->submitForm($button, $fieldValues);
$client->followRedirect();
self::assertResponseIsSuccessful();
}
/**
* @return iterable<array<string|array<string, mixed>>>
*
* @phpstan-return iterable<array{0: string, 1: array<string, mixed>, 2: string, 3?: array<string, mixed>}>
*/
public static function provideFormUrlsCases(): iterable
{
yield 'Create Block - Text' => ['/admin/tests/app/sonatapageblock/create', [
'uniqid' => 'block',
'type' => 'sonata.block.service.text',
], 'btn_create_and_list', []];
yield 'Create Block compose mode - Text' => ['/admin/tests/app/sonatapageblock/create', [
'composer' => '1',
'uniqid' => 'block',
'type' => 'sonata.block.service.text',
], 'btn_create_and_list', []];
yield 'Edit Block' => ['/admin/tests/app/sonatapageblock/1/edit', [], 'btn_update_and_list', []];
yield 'Remove Block' => ['/admin/tests/app/sonatapageblock/1/delete', [], 'btn_delete'];
}
/**
* @dataProvider provideSwitchParentCases
*
* @param array{block_id?: int|string|null, parent_id?: int|string|null} $parameters
*/
public function testSwitchParentForBlock(array $parameters, bool $success): void
{
$client = self::createClient();
$this->prepareData();
$client->request('GET', '/admin/tests/app/sonatapageblock/switch-parent', $parameters);
if ($success) {
self::assertResponseIsSuccessful();
} else {
self::assertResponseStatusCodeSame(400);
}
}
/**
* @return iterable<array<string|array<string, mixed>>>
*
* @phpstan-return iterable<array{0: array{block_id?: int|string|null, parent_id?: int|string|null}, 1: bool}>
*/
public static function provideSwitchParentCases(): iterable
{
yield 'Missing all parameters' => [[], false];
yield 'Missing parent_id' => [[
'block_id' => 3,
], false];
yield 'Unable to find block' => [[
'block_id' => 'foo',
'parent_id' => 2,
], false];
yield 'Unable to find parent block' => [[
'block_id' => 3,
'parent_id' => 'foo',
], false];
yield 'Switch parent Block' => [[
'block_id' => 3,
'parent_id' => 2,
], true];
}
/**
* @dataProvider provideDispositionCases
*
* @param array{disposition?: array<array{id: int|string, position: int|numeric-string}>} $parameters
*/
public function testSavePositionForBlock(array $parameters, bool $success): void
{
$client = self::createClient();
$this->prepareData();
$client->request(
'POST',
'/admin/tests/app/sonatapageblock/save-position',
$parameters,
[],
['HTTP_Content-type' => 'application/json']
);
if ($success) {
self::assertResponseIsSuccessful();
} else {
self::assertResponseStatusCodeSame(400);
}
}
/**
* @return iterable<array<bool|array<string, mixed>>>
*
* @phpstan-return iterable<array{0: array{disposition?: array<array{id: int|string, position: int|numeric-string}>}, 1: bool}>
*/
public static function provideDispositionCases(): iterable
{
yield 'Missing disposition' => [[], false];
yield 'Empty disposition' => [[
'disposition' => [],
], false];
yield 'Update block disposition' => [[
'disposition' => [
['id' => 1, 'position' => 1],
['id' => 2, 'position' => '2'],
],
], true];
}
/**
* @dataProvider provideBatchActionsCases
*/
public function testBatchActions(string $action): void
{
$client = self::createClient();
$this->prepareData();
$client->request('GET', '/admin/tests/app/sonatapageblock/list');
$client->submitForm('OK', [
'all_elements' => true,
'action' => $action,
]);
$client->submitForm('Yes, execute');
$client->followRedirect();
self::assertResponseIsSuccessful();
}
/**
* @return iterable<array<string>>
*
* @phpstan-return iterable<array{0: string}>
*/
public static function provideBatchActionsCases(): iterable
{
yield 'Delete Blocks' => ['delete'];
}
/**
* @psalm-suppress UndefinedPropertyFetch
*/
private function prepareData(): void
{
// TODO: Simplify this when dropping support for Symfony 4.
// @phpstan-ignore-next-line
$container = method_exists($this, 'getContainer') ? self::getContainer() : self::$container;
$manager = $container->get('doctrine.orm.entity_manager');
\assert($manager instanceof EntityManagerInterface);
$site = new SonataPageSite();
$site->setName('name');
$site->setHost('localhost');
$page = new SonataPagePage();
$page->setName('name');
$page->setTemplateCode('default');
$page->setSite($site);
$parentBlock = new SonataPageBlock();
$parentBlock->setType('sonata.page.block.container');
$parentBlock2 = new SonataPageBlock();
$parentBlock2->setType('sonata.page.block.container');
$block = new SonataPageBlock();
$block->setType('sonata.block.service.text');
$page->addBlock($parentBlock);
$page->addBlock($parentBlock2);
$parentBlock->addChild($block);
$manager->persist($site);
$manager->persist($page);
$manager->flush();
}
}