-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathAssociationFieldTest.php
More file actions
317 lines (258 loc) · 13.4 KB
/
Copy pathAssociationFieldTest.php
File metadata and controls
317 lines (258 loc) · 13.4 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Unit\Field;
use Doctrine\ORM\QueryBuilder;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldConfiguratorInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
class AssociationFieldTest extends AbstractFieldTest
{
protected function setUp(): void
{
parent::setUp();
// associationField configurator requires Doctrine and other services:
// let's use a no-op configurator to test the field options
$this->configurator = new class implements FieldConfiguratorInterface {
public function supports(FieldDto $field, EntityDto $entityDto): bool
{
return AssociationField::class === $field->getFieldFqcn();
}
public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $context): void
{
// no-op for basic option testing
}
};
}
public function testDefaultOptions(): void
{
$field = AssociationField::new('category');
$fieldDto = $this->configure($field);
self::assertFalse($fieldDto->getCustomOption(AssociationField::OPTION_AUTOCOMPLETE));
self::assertNull($fieldDto->getCustomOption(AssociationField::OPTION_EMBEDDED_CRUD_FORM_CONTROLLER));
self::assertSame(AssociationField::WIDGET_AUTOCOMPLETE, $fieldDto->getCustomOption(AssociationField::OPTION_WIDGET));
self::assertNull($fieldDto->getCustomOption(AssociationField::OPTION_QUERY_BUILDER_CALLABLE));
self::assertNull($fieldDto->getCustomOption(AssociationField::OPTION_URL));
self::assertFalse($fieldDto->getCustomOption(AssociationField::OPTION_RENDER_AS_EMBEDDED_FORM));
self::assertTrue($fieldDto->getCustomOption(AssociationField::OPTION_ESCAPE_HTML_CONTENTS));
self::assertSame(EntityType::class, $fieldDto->getFormType());
self::assertStringContainsString('field-association', $fieldDto->getCssClass());
}
public function testFieldWithNullValue(): void
{
$field = AssociationField::new('category');
$field->setValue(null);
$fieldDto = $this->configure($field);
self::assertNull($fieldDto->getValue());
}
public function testAutocomplete(): void
{
$field = AssociationField::new('category');
$field->autocomplete();
$fieldDto = $this->configure($field);
self::assertTrue($fieldDto->getCustomOption(AssociationField::OPTION_AUTOCOMPLETE));
}
public function testRenderAsNativeWidget(): void
{
$field = AssociationField::new('category');
$field->renderAsNativeWidget();
$fieldDto = $this->configure($field);
self::assertSame(AssociationField::WIDGET_NATIVE, $fieldDto->getCustomOption(AssociationField::OPTION_WIDGET));
}
public function testRenderAsAutocompleteWidget(): void
{
$field = AssociationField::new('category');
$field->renderAsNativeWidget(false);
$fieldDto = $this->configure($field);
self::assertSame(AssociationField::WIDGET_AUTOCOMPLETE, $fieldDto->getCustomOption(AssociationField::OPTION_WIDGET));
}
public function testSetCrudController(): void
{
$crudControllerFqcn = 'App\\Controller\\CategoryCrudController';
$field = AssociationField::new('category');
$field->setCrudController($crudControllerFqcn);
$fieldDto = $this->configure($field);
self::assertSame($crudControllerFqcn, $fieldDto->getCustomOption(AssociationField::OPTION_EMBEDDED_CRUD_FORM_CONTROLLER));
}
public function testSetQueryBuilder(): void
{
$queryBuilder = static fn (QueryBuilder $qb): QueryBuilder => $qb->andWhere('entity.active = true');
$field = AssociationField::new('category');
$field->setQueryBuilder($queryBuilder);
$fieldDto = $this->configure($field);
self::assertSame($queryBuilder, $fieldDto->getCustomOption(AssociationField::OPTION_QUERY_BUILDER_CALLABLE));
}
public function testLinktToUrl(): void
{
$field = AssociationField::new('category');
$field->linkToUrl('https://example.com');
$fieldDto = $this->configure($field);
self::assertSame('https://example.com', $fieldDto->getCustomOption(AssociationField::OPTION_URL));
}
public function testLinkToUrlCallable(): void
{
$callable = static fn (EntityDto $entityDto): string => 'https://example.com/category/'.$entityDto->getPrimaryKeyValueAsString();
$field = AssociationField::new('category');
$field->linkToUrl($callable);
$fieldDto = $this->configure($field);
self::assertSame($callable, $fieldDto->getCustomOption(AssociationField::OPTION_URL));
}
public function testRenderAsEmbeddedForm(): void
{
$field = AssociationField::new('category');
$field->renderAsEmbeddedForm();
$fieldDto = $this->configure($field);
self::assertTrue($fieldDto->getCustomOption(AssociationField::OPTION_RENDER_AS_EMBEDDED_FORM));
}
public function testRenderAsEmbeddedFormWithController(): void
{
$crudControllerFqcn = 'App\\Controller\\CategoryCrudController';
$field = AssociationField::new('category');
$field->renderAsEmbeddedForm($crudControllerFqcn);
$fieldDto = $this->configure($field);
self::assertTrue($fieldDto->getCustomOption(AssociationField::OPTION_RENDER_AS_EMBEDDED_FORM));
self::assertSame($crudControllerFqcn, $fieldDto->getCustomOption(AssociationField::OPTION_EMBEDDED_CRUD_FORM_CONTROLLER));
}
public function testRenderAsEmbeddedFormWithPageNames(): void
{
$crudControllerFqcn = 'App\\Controller\\CategoryCrudController';
$field = AssociationField::new('category');
$field->renderAsEmbeddedForm($crudControllerFqcn, 'custom_new', 'custom_edit');
$fieldDto = $this->configure($field);
self::assertSame('custom_new', $fieldDto->getCustomOption(AssociationField::OPTION_EMBEDDED_CRUD_FORM_NEW_PAGE_NAME));
self::assertSame('custom_edit', $fieldDto->getCustomOption(AssociationField::OPTION_EMBEDDED_CRUD_FORM_EDIT_PAGE_NAME));
}
public function testSetSortProperty(): void
{
$field = AssociationField::new('category');
$field->setSortProperty('name');
$fieldDto = $this->configure($field);
self::assertSame('name', $fieldDto->getCustomOption(AssociationField::OPTION_SORT_PROPERTY));
}
public function testRenderAsHtml(): void
{
$field = AssociationField::new('category');
$field->renderAsHtml();
$fieldDto = $this->configure($field);
self::assertFalse($fieldDto->getCustomOption(AssociationField::OPTION_ESCAPE_HTML_CONTENTS));
}
public function testDontRenderAsHtml(): void
{
$field = AssociationField::new('category');
$field->renderAsHtml(false);
$fieldDto = $this->configure($field);
self::assertTrue($fieldDto->getCustomOption(AssociationField::OPTION_ESCAPE_HTML_CONTENTS));
}
public function testPreferredChoicesWithArray(): void
{
$field = AssociationField::new('category');
$field->setPreferredChoices([1, 2, 3]);
$fieldDto = $this->configure($field);
self::assertSame([1, 2, 3], $fieldDto->getCustomOption(AssociationField::OPTION_PREFERRED_CHOICES));
}
public function testPreferredChoicesWithCallable(): void
{
$callable = static fn ($entity): bool => $entity->getId() < 5;
$field = AssociationField::new('category');
$field->setPreferredChoices($callable);
$fieldDto = $this->configure($field);
self::assertSame($callable, $fieldDto->getCustomOption(AssociationField::OPTION_PREFERRED_CHOICES));
}
public function testPreferredChoicesDefaultValue(): void
{
$field = AssociationField::new('category');
$fieldDto = $this->configure($field);
self::assertNull($fieldDto->getCustomOption(AssociationField::OPTION_PREFERRED_CHOICES));
}
public function testAutocompleteWithCallback(): void
{
$callback = static fn ($entity): string => 'Custom: '.$entity->getName();
$field = AssociationField::new('category');
$field->autocomplete(callback: $callback);
$fieldDto = $this->configure($field);
self::assertTrue($fieldDto->getCustomOption(AssociationField::OPTION_AUTOCOMPLETE));
self::assertSame($callback, $fieldDto->getCustomOption(AssociationField::OPTION_AUTOCOMPLETE_CALLBACK));
}
public function testAutocompleteWithTemplate(): void
{
$field = AssociationField::new('category');
$field->autocomplete(template: 'admin/autocomplete/category.html.twig');
$fieldDto = $this->configure($field);
self::assertTrue($fieldDto->getCustomOption(AssociationField::OPTION_AUTOCOMPLETE));
self::assertSame('admin/autocomplete/category.html.twig', $fieldDto->getCustomOption(AssociationField::OPTION_AUTOCOMPLETE_TEMPLATE));
}
public function testAutocompleteWithRenderAsHtml(): void
{
$field = AssociationField::new('category');
$field->autocomplete(renderAsHtml: true);
$fieldDto = $this->configure($field);
self::assertTrue($fieldDto->getCustomOption(AssociationField::OPTION_AUTOCOMPLETE));
// renderAsHtml: true means OPTION_ESCAPE_HTML_CONTENTS should be false (inverted logic)
self::assertFalse($fieldDto->getCustomOption(AssociationField::OPTION_ESCAPE_HTML_CONTENTS));
}
public function testAutocompleteWithAllOptions(): void
{
$callback = static fn ($entity): string => $entity->getName();
$field = AssociationField::new('category');
$field->autocomplete(
callback: $callback,
template: 'admin/autocomplete/category.html.twig',
renderAsHtml: true
);
$fieldDto = $this->configure($field);
self::assertTrue($fieldDto->getCustomOption(AssociationField::OPTION_AUTOCOMPLETE));
self::assertSame($callback, $fieldDto->getCustomOption(AssociationField::OPTION_AUTOCOMPLETE_CALLBACK));
self::assertSame('admin/autocomplete/category.html.twig', $fieldDto->getCustomOption(AssociationField::OPTION_AUTOCOMPLETE_TEMPLATE));
// renderAsHtml: true means OPTION_ESCAPE_HTML_CONTENTS should be false (inverted logic)
self::assertFalse($fieldDto->getCustomOption(AssociationField::OPTION_ESCAPE_HTML_CONTENTS));
}
public function testAutocompleteDisabled(): void
{
$field = AssociationField::new('category');
$field->autocomplete(enable: false);
$fieldDto = $this->configure($field);
self::assertFalse($fieldDto->getCustomOption(AssociationField::OPTION_AUTOCOMPLETE));
}
public function testAutocompleteDisabledIgnoresOtherOptions(): void
{
$callback = static fn ($entity): string => $entity->getName();
$field = AssociationField::new('category');
$field->autocomplete(
enable: false,
callback: $callback,
template: 'admin/autocomplete/category.html.twig',
renderAsHtml: true
);
$fieldDto = $this->configure($field);
// when disabled, autocomplete should not be enabled and options should not be set
self::assertFalse($fieldDto->getCustomOption(AssociationField::OPTION_AUTOCOMPLETE));
self::assertNull($fieldDto->getCustomOption(AssociationField::OPTION_AUTOCOMPLETE_CALLBACK));
self::assertNull($fieldDto->getCustomOption(AssociationField::OPTION_AUTOCOMPLETE_TEMPLATE));
// OPTION_ESCAPE_HTML_CONTENTS should remain at default value (true) since autocomplete is disabled
self::assertTrue($fieldDto->getCustomOption(AssociationField::OPTION_ESCAPE_HTML_CONTENTS));
}
public function testAutocompleteEnabledExplicitly(): void
{
$field = AssociationField::new('category');
$field->autocomplete(enable: !0);
$fieldDto = $this->configure($field);
self::assertTrue($fieldDto->getCustomOption(AssociationField::OPTION_AUTOCOMPLETE));
}
public function testAutocompleteRenderAsHtmlSetsSameOptionAsRenderAsHtmlMethod(): void
{
// both autocomplete(renderAsHtml: true) and renderAsHtml(true) should set the same option
$field1 = AssociationField::new('category')->autocomplete(renderAsHtml: true);
$field2 = AssociationField::new('category')->renderAsHtml();
self::assertFalse($field1->getAsDto()->getCustomOption(AssociationField::OPTION_ESCAPE_HTML_CONTENTS));
self::assertFalse($field2->getAsDto()->getCustomOption(AssociationField::OPTION_ESCAPE_HTML_CONTENTS));
}
public function testAutocompleteRenderAsHtmlFalseSetsSameOptionAsRenderAsHtmlMethod(): void
{
// both autocomplete(renderAsHtml: false) and renderAsHtml(false) should set the same option
$field1 = AssociationField::new('category')->autocomplete(renderAsHtml: false);
$field2 = AssociationField::new('category')->renderAsHtml(false);
self::assertTrue($field1->getAsDto()->getCustomOption(AssociationField::OPTION_ESCAPE_HTML_CONTENTS));
self::assertTrue($field2->getAsDto()->getCustomOption(AssociationField::OPTION_ESCAPE_HTML_CONTENTS));
}
}