forked from EasyCorp/EasyAdminBundle
/
DefaultBackendTest.php
700 lines (551 loc) · 29 KB
/
DefaultBackendTest.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
<?php
/*
* This file is part of the EasyAdminBundle.
*
* (c) Javier Eguiluz <javier.eguiluz@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace JavierEguiluz\Bundle\EasyAdminBundle\Tests\Controller;
use Doctrine\ORM\EntityManager;
use JavierEguiluz\Bundle\EasyAdminBundle\Tests\Fixtures\AbstractTestCase;
class DefaultBackendTest extends AbstractTestCase
{
public function setUp()
{
parent::setUp();
$this->initClient(array('environment' => 'default_backend'));
}
public function testBackendHomepageRedirection()
{
$this->client->request('GET', '/admin/');
$this->assertSame(302, $this->client->getResponse()->getStatusCode());
$this->assertSame(
'/admin/?action=list&entity=Category',
$this->client->getResponse()->getTargetUrl(),
'The backend homepage redirects to the "list" view of the first configured entity ("Category").'
);
}
public function testLanguageDefinedByLayout()
{
$crawler = $this->getBackendHomepage();
$this->assertSame('en', trim($crawler->filter('html')->attr('lang')));
}
public function testRtlIsDisabledByDefault()
{
$crawler = $this->getBackendHomepage();
$backendConfig = $this->client->getContainer()->get('easyadmin.config.manager')->getBackendConfig();
$this->assertFalse($backendConfig['design']['rtl'], 'RTL is disabled by default.');
$this->assertNotContains('bootstrap-rtl.min.css', $crawler->filter('head')->text());
$this->assertNotContains('adminlte-rtl.min.css', $crawler->filter('head')->text());
}
public function testDefaultCssStylesAreLinked()
{
$crawler = $this->getBackendHomepage();
$this->assertSame(
'/bundles/easyadmin/stylesheet/easyadmin-all.min.css',
$crawler->filter('link[rel="stylesheet"]')->eq(0)->attr('href')
);
}
public function testDefaultJsScriptsAreLinked()
{
$crawler = $this->getBackendHomepage();
$this->assertSame(
'/bundles/easyadmin/javascript/easyadmin-all.min.js',
$crawler->filter('script')->eq(1)->attr('src')
);
}
public function testLogo()
{
$crawler = $this->getBackendHomepage();
$this->assertSame('EasyAdmin', trim($crawler->filter('#header-logo a')->text()));
$this->assertSame('/admin/', $crawler->filter('#header-logo a')->attr('href'));
}
public function testMainMenuItems()
{
$menuItems = array(
'Category' => '/admin/?entity=Category&action=list&menuIndex=0&submenuIndex=-1',
'Image' => '/admin/?entity=Image&action=list&menuIndex=1&submenuIndex=-1',
'Purchase' => '/admin/?entity=Purchase&action=list&menuIndex=2&submenuIndex=-1',
'PurchaseItem' => '/admin/?entity=PurchaseItem&action=list&menuIndex=3&submenuIndex=-1',
'Product' => '/admin/?entity=Product&action=list&menuIndex=4&submenuIndex=-1',
);
$crawler = $this->getBackendHomepage();
$i = 0;
foreach ($menuItems as $label => $url) {
$this->assertSame($label, trim($crawler->filter('.sidebar-menu li a')->eq($i)->text()));
$this->assertSame($url, $crawler->filter('.sidebar-menu li a')->eq($i)->attr('href'));
++$i;
}
}
public function testCustomCssFile()
{
$this->getBackendHomepage();
$this->assertSame(13, substr_count($this->client->getResponse()->getContent(), '#205081'), 'The admin.css file uses the default brand color.');
// #222222 color is only used by the "dark" color scheme, not the "light" one
$this->assertSame(7, substr_count($this->client->getResponse()->getContent(), '#222222'), 'The admin.css file uses the dark color scheme.');
}
public function testCustomCssProperty()
{
$this->getBackendHomepage();
$customCssContent = $this->client->getContainer()->get('easyadmin.config.manager')->getBackendConfig();
$this->assertSame(13, substr_count($customCssContent['_internal']['custom_css'], '#205081'), 'The generated custom CSS uses the default brand color.');
// #222222 color is only used by the "dark" color scheme, not the "light" one
$this->assertSame(7, substr_count($customCssContent['_internal']['custom_css'], '#222222'), 'The generated custom CSS uses the dark color scheme.');
}
public function testListViewTitle()
{
$crawler = $this->requestListView();
$this->assertSame('Category', trim($crawler->filter('head title')->text()));
$this->assertSame('Category', trim($crawler->filter('h1.title')->text()));
}
public function testListViewSearchAction()
{
$crawler = $this->requestListView();
$hiddenParameters = array(
'action' => 'search',
'entity' => 'Category',
'sortField' => 'id',
'sortDirection' => 'DESC',
);
$this->assertSame('Search', trim($crawler->filter('.action-search button[type=submit]')->text()));
$this->assertContains('action-search', $crawler->filter('.global-actions > div')->first()->attr('class'));
$this->assertSame('_self', $crawler->filter('.action-search button')->attr('formtarget'));
$i = 0;
foreach ($hiddenParameters as $name => $value) {
$this->assertSame($name, $crawler->filter('.action-search input[type=hidden]')->eq($i)->attr('name'));
$this->assertSame($value, $crawler->filter('.action-search input[type=hidden]')->eq($i)->attr('value'));
++$i;
}
}
public function testListViewNewAction()
{
$crawler = $this->requestListView();
$this->assertSame('Add Category', trim($crawler->filter('.global-actions a.btn')->text()));
$this->assertContains('action-new', trim($crawler->filter('.global-actions a.btn')->attr('class')));
$this->assertSame('_self', $crawler->filter('.global-actions a.btn')->attr('target'));
$this->assertCount(0, $crawler->filter('.global-actions a.btn i'), 'The default "new" button shows no icon.');
$this->assertStringStartsWith('/admin/?action=new&entity=Category&sortField=id&sortDirection=DESC&page=1', $crawler->filter('.global-actions a.btn')->attr('href'));
}
public function testListViewItemActions()
{
$crawler = $this->requestListView();
$this->assertSame('Edit', trim($crawler->filter('#main .table td.actions a')->eq(0)->text()));
$this->assertContains('action-edit', trim($crawler->filter('#main .table td.actions a')->eq(0)->attr('class')));
$this->assertSame('_self', $crawler->filter('#main .table td.actions a')->eq(0)->attr('target'));
$this->assertSame('Delete', trim($crawler->filter('#main .table td.actions a')->eq(1)->text()));
}
public function testListViewTableIdColumn()
{
$crawler = $this->requestListView();
$this->assertSame('ID', trim($crawler->filter('table th[data-property-name="id"]')->text()),
'The ID entity property is very special and we uppercase it automatically to improve its readability.'
);
}
public function testListViewTableColumnLabels()
{
$crawler = $this->requestListView();
$columnLabels = array('ID', 'Name', 'Products', 'Parent', 'Actions');
foreach ($columnLabels as $i => $label) {
$this->assertSame($label, trim($crawler->filter('.table thead th')->eq($i)->text()));
}
}
public function testListViewTableColumnAttributes()
{
$crawler = $this->requestListView();
$columnAttributes = array('id', 'name', 'products', 'parent');
foreach ($columnAttributes as $i => $attribute) {
$this->assertSame($attribute, trim($crawler->filter('.table thead th')->eq($i)->attr('data-property-name')));
}
}
public function testListViewDefaultTableSorting()
{
$crawler = $this->requestListView();
$this->assertCount(1, $crawler->filter('.table thead th[class*="sorted"]'), 'Table is sorted only by one column.');
$this->assertSame('ID', trim($crawler->filter('.table thead th[class*="sorted"]')->text()), 'By default, table is soreted by ID column.');
$this->assertSame('fa fa-caret-down', $crawler->filter('.table thead th[class*="sorted"] i')->attr('class'), 'The column used to sort results shows the right icon.');
}
public function testListViewTableContents()
{
$crawler = $this->requestListView();
$this->assertCount(15, $crawler->filter('.table tbody tr'));
}
public function testListViewTableRowAttributes()
{
$crawler = $this->requestListView();
$columnAttributes = array('ID', 'Name', 'Products', 'Parent');
foreach ($columnAttributes as $i => $attribute) {
$this->assertSame($attribute, trim($crawler->filter('.table tbody tr td')->eq($i)->attr('data-label')));
}
}
public function testListViewPagination()
{
$crawler = $this->requestListView();
$this->assertContains('1 - 15 of 200', $crawler->filter('.list-pagination')->text());
$this->assertSame('disabled', $crawler->filter('.list-pagination li:contains("First")')->attr('class'));
$this->assertSame('disabled', $crawler->filter('.list-pagination li:contains("Previous")')->attr('class'));
$this->assertStringStartsWith('/admin/?action=list&entity=Category&sortField=id&sortDirection=DESC&page=2', $crawler->filter('.list-pagination li a:contains("Next")')->attr('href'));
$this->assertStringStartsWith('/admin/?action=list&entity=Category&sortField=id&sortDirection=DESC&page=14', $crawler->filter('.list-pagination li a:contains("Last")')->attr('href'));
}
public function testShowViewPageTitle()
{
$crawler = $this->requestShowView();
$this->assertSame('Category (#200)', trim($crawler->filter('head title')->text()));
$this->assertSame('Category (#200)', trim($crawler->filter('h1.title')->text()));
}
public function testShowViewFieldLabels()
{
$crawler = $this->requestShowView();
$fieldLabels = array('ID', 'Name', 'Products', 'Parent');
foreach ($fieldLabels as $i => $label) {
$this->assertSame($label, trim($crawler->filter('#main .form-group label')->eq($i)->text()));
}
}
public function testShowViewFieldClasses()
{
$crawler = $this->requestShowView();
$fieldClasses = array('integer', 'string', 'association');
foreach ($fieldClasses as $i => $cssClass) {
$this->assertContains('field-'.$cssClass, trim($crawler->filter('#main .form-group')->eq($i)->attr('class')));
}
}
public function testShowViewActions()
{
$crawler = $this->requestShowView();
// edit action
$this->assertContains('action-edit', trim($crawler->filter('.form-actions a:contains("Edit")')->attr('class')));
$this->assertContains('fa-edit', trim($crawler->filter('.form-actions a:contains("Edit") i')->attr('class')));
$this->assertSame('_self', $crawler->filter('.form-actions a:contains("Edit")')->attr('target'));
// delete action
$this->assertContains('action-delete', trim($crawler->filter('.form-actions a:contains("Delete")')->attr('class')));
$this->assertContains('fa-trash', trim($crawler->filter('.form-actions a:contains("Delete") i')->attr('class')));
// list action
$this->assertContains('action-list', trim($crawler->filter('.form-actions a:contains("Back to listing")')->attr('class')));
$this->assertSame('btn btn-secondary action-list', trim($crawler->filter('.form-actions a:contains("Back to listing")')->attr('class')));
$this->assertSame('_self', $crawler->filter('.form-actions a:contains("Back to listing")')->attr('target'));
}
public function testShowViewReferer()
{
$parameters = array(
'action' => 'list',
'entity' => 'Category',
'sortField' => 'name',
'sortDirection' => 'ASC',
'page' => '2',
);
// 1. visit a specific 'list' view page
$crawler = $this->getBackendPage($parameters);
// 2. click on the 'Edit' link of the first item
$link = $crawler->filter('td.actions a:contains("Edit")')->eq(0)->link();
$crawler = $this->client->click($link);
// 3. the 'referer' parameter should point to the exact same previous 'list' page
$refererUrl = $crawler->filter('#form-actions-row a:contains("Back to listing")')->attr('href');
$queryString = parse_url($refererUrl, PHP_URL_QUERY);
parse_str($queryString, $refererParameters);
$this->assertSame($parameters, $refererParameters);
}
public function testEditViewTitle()
{
$crawler = $this->requestEditView();
$this->assertSame('Edit Category (#200)', trim($crawler->filter('head title')->text()));
$this->assertSame('Edit Category (#200)', trim($crawler->filter('h1.title')->text()));
}
public function testEditViewFormAttributes()
{
$crawler = $this->requestEditView();
$form = $crawler->filter('#main form')->eq(0);
$this->assertSame('edit', trim($form->attr('data-view')));
$this->assertSame('Category', trim($form->attr('data-entity')));
$this->assertSame('200', trim($form->attr('data-entity-id')));
}
public function testEditViewFieldLabels()
{
$crawler = $this->requestEditView();
$fieldLabels = array('Name', 'Products', 'Parent');
foreach ($fieldLabels as $i => $label) {
$this->assertSame($label, trim($crawler->filter('#main .form-group label')->eq($i)->text()));
}
}
public function testEditViewFieldClasses()
{
$crawler = $this->requestEditView();
$fieldClasses = array('text', 'entity');
foreach ($fieldClasses as $i => $cssClass) {
$this->assertContains('field-'.$cssClass, trim($crawler->filter('#main .form-group')->eq($i)->attr('class')));
}
}
public function testEditViewActions()
{
$crawler = $this->requestEditView();
// save action
$this->assertContains('action-save', trim($crawler->filter('#form-actions-row button:contains("Save changes")')->attr('class')));
$this->assertContains('fa-save', trim($crawler->filter('#form-actions-row button:contains("Save changes") i')->attr('class')));
// delete action
$this->assertContains('action-delete', trim($crawler->filter('#form-actions-row a:contains("Delete")')->attr('class')));
$this->assertContains('fa-trash', trim($crawler->filter('#form-actions-row a:contains("Delete") i')->attr('class')));
// list action
$this->assertSame('btn btn-secondary action-list', trim($crawler->filter('#form-actions-row a:contains("Back to listing")')->attr('class')));
$this->assertSame('_self', $crawler->filter('#form-actions-row a:contains("Back to listing")')->attr('target'));
}
public function testEditViewReferer()
{
$parameters = array(
'action' => 'list',
'entity' => 'Category',
'sortField' => 'name',
'sortDirection' => 'ASC',
'page' => '2',
);
// 1. visit a specific 'list' view page
$crawler = $this->getBackendPage($parameters);
// 2. click on the 'Edit' link of the first item
$link = $crawler->filter('td.actions a:contains("Edit")')->eq(0)->link();
$crawler = $this->client->click($link);
// 3. the 'referer' parameter should point to the exact same previous 'list' page
$refererUrl = $crawler->filter('#form-actions-row a:contains("Back to listing")')->attr('href');
$queryString = parse_url($refererUrl, PHP_URL_QUERY);
parse_str($queryString, $refererParameters);
$this->assertSame($parameters, $refererParameters);
}
public function testEditViewEntityModification()
{
$crawler = $this->requestEditView();
$this->client->followRedirects();
$categoryName = sprintf('Modified Category %s', md5(mt_rand()));
$form = $crawler->selectButton('Save changes')->form(array(
'category[name]' => $categoryName,
));
$crawler = $this->client->submit($form);
$this->assertContains(
$categoryName,
$crawler->filter('#main table tr')->eq(1)->text(),
'The modified category is displayed in the first data row of the "list" table.'
);
}
public function testEntityModificationViaAjax()
{
/* @var EntityManager */
$em = $this->client->getContainer()->get('doctrine')->getManager();
$product = $em->getRepository('AppTestBundle\Entity\FunctionalTests\Product')->find(1);
$this->assertTrue($product->isEnabled(), 'Initially the product is enabled.');
$queryParameters = array('action' => 'edit', 'view' => 'list', 'entity' => 'Product', 'id' => '1', 'property' => 'enabled', 'newValue' => 'false');
$this->client->request('GET', '/admin/?'.http_build_query($queryParameters), array(), array(), array('HTTP_X-Requested-With' => 'XMLHttpRequest'));
$product = $em->getRepository('AppTestBundle\Entity\FunctionalTests\Product')->find(1);
$this->assertFalse($product->isEnabled(), 'After editing it via Ajax, the product is not enabled.');
}
public function testWrongEntityModificationViaAjax()
{
$queryParameters = array('action' => 'edit', 'view' => 'list', 'entity' => 'Product', 'id' => '1', 'property' => 'this_property_does_not_exist', 'newValue' => 'false');
$this->client->request('GET', '/admin/?'.http_build_query($queryParameters), array(), array(), array('HTTP_X-Requested-With' => 'XMLHttpRequest'));
$this->assertSame(500, $this->client->getResponse()->getStatusCode(), 'Trying to modify a non-existent property via Ajax returns a 500 error');
$this->assertContains('The type of the "this_property_does_not_exist" property is not "toggle"', $this->client->getResponse()->getContent());
}
public function testNewViewTitle()
{
$crawler = $this->requestNewView();
$this->assertSame('Create Category', trim($crawler->filter('head title')->text()));
$this->assertSame('Create Category', trim($crawler->filter('h1.title')->text()));
}
public function testNewViewFormAttributes()
{
$crawler = $this->requestNewView();
$form = $crawler->filter('#main form')->eq(0);
$this->assertSame('new', trim($form->attr('data-view')));
$this->assertSame('Category', trim($form->attr('data-entity')));
$this->assertEmpty($form->attr('data-entity-id'));
}
public function testNewViewFieldLabels()
{
$crawler = $this->requestNewView();
$fieldLabels = array('Name', 'Products', 'Parent');
foreach ($fieldLabels as $i => $label) {
$this->assertSame($label, trim($crawler->filter('#main .form-group label')->eq($i)->text()));
}
}
public function testNewViewFieldClasses()
{
$crawler = $this->requestNewView();
$fieldClasses = array('text', 'entity');
foreach ($fieldClasses as $i => $cssClass) {
$this->assertContains('field-'.$cssClass, trim($crawler->filter('#main .form-group')->eq($i)->attr('class')));
}
}
public function testNewViewActions()
{
$crawler = $this->requestNewView();
// save action
$this->assertContains('action-save', trim($crawler->filter('#form-actions-row button:contains("Save changes")')->attr('class')));
$this->assertContains('fa-save', trim($crawler->filter('#form-actions-row button:contains("Save changes") i')->attr('class')));
// list action
$this->assertSame('btn btn-secondary action-list', trim($crawler->filter('#form-actions-row a:contains("Back to listing")')->attr('class')));
$this->assertSame('_self', $crawler->filter('#form-actions-row a:contains("Back to listing")')->attr('target'));
}
public function testNewViewReferer()
{
$parameters = array(
'action' => 'list',
'entity' => 'Category',
'sortField' => 'name',
'sortDirection' => 'ASC',
'page' => '2',
);
// 1. visit a specific 'list' view page
$crawler = $this->getBackendPage($parameters);
// 2. click on the 'New' link to browse the 'new' view
$link = $crawler->filter('.global-actions a:contains("Add Category")')->link();
$crawler = $this->client->click($link);
// 3. the 'referer' parameter should point to the exact same previous 'list' page
$refererUrl = $crawler->filter('#form-actions-row a:contains("Back to listing")')->attr('href');
$queryString = parse_url($refererUrl, PHP_URL_QUERY);
parse_str($queryString, $refererParameters);
$this->assertSame($parameters, $refererParameters);
}
public function testNewViewEntityCreation()
{
$crawler = $this->requestNewView();
$this->client->followRedirects();
$categoryName = sprintf('The New Category %s', md5(mt_rand()));
$form = $crawler->selectButton('Save changes')->form(array(
'category[name]' => $categoryName,
));
$crawler = $this->client->submit($form);
$this->assertContains($categoryName, $crawler->filter('#main table tr')->eq(1)->text(), 'The newly created category is displayed in the first data row of the "list" table.');
}
public function testSearchViewTitle()
{
$crawler = $this->requestSearchView();
$this->assertSame('200 results found', trim($crawler->filter('head title')->html()), 'The page title does not contain HTML tags.');
$this->assertSame('<strong>200</strong> results found', trim($crawler->filter('h1.title')->html()), 'The visible content contains HTML tags.');
}
public function testSearchViewEmptyQuery()
{
// empty queries redirect to "list" action
$this->getBackendPage(array(
'action' => 'search',
'entity' => 'Category',
'query' => '',
));
$this->assertSame(302, $this->client->getResponse()->getStatusCode());
$this->assertSame('/admin/?action=list&entity=Category&sortField=id&sortDirection=DESC', $this->client->getResponse()->headers->get('location'));
// pseudo-empty queries (e.g. strings which only contain white spaces) don't redirect to "list" action
$crawler = $this->getBackendPage(array(
'action' => 'search',
'entity' => 'Category',
'query' => ' ',
));
$this->assertSame('No results found', $crawler->filter('h1.title')->text());
}
public function testSearchViewTableIdColumn()
{
$crawler = $this->requestSearchView();
$this->assertSame('ID', trim($crawler->filter('table th[data-property-name="id"]')->text()),
'The ID entity property is very special and we uppercase it automatically to improve its readability.'
);
}
public function testSearchViewTableColumnLabels()
{
$crawler = $this->requestSearchView();
$columnLabels = array('ID', 'Name', 'Products', 'Parent', 'Actions');
foreach ($columnLabels as $i => $label) {
$this->assertSame($label, trim($crawler->filter('.table thead th')->eq($i)->text()));
}
}
public function testSearchViewTableColumnAttributes()
{
$crawler = $this->requestSearchView();
$columnAttributes = array('id', 'name', 'products', 'parent');
foreach ($columnAttributes as $i => $attribute) {
$this->assertSame($attribute, trim($crawler->filter('.table thead th')->eq($i)->attr('data-property-name')));
}
}
public function testSearchViewDefaultTableSorting()
{
$crawler = $this->requestSearchView();
$this->assertCount(1, $crawler->filter('.table thead th[class*="sorted"]'), 'Table is sorted only by one column.');
$this->assertSame('ID', trim($crawler->filter('.table thead th[class*="sorted"]')->text()), 'By default, table is soreted by ID column.');
$this->assertSame('fa fa-caret-down', $crawler->filter('.table thead th[class*="sorted"] i')->attr('class'), 'The column used to sort results shows the right icon.');
}
public function testSearchViewTableContents()
{
$crawler = $this->requestSearchView();
$this->assertCount(15, $crawler->filter('.table tbody tr'));
}
public function testSearchViewTableRowAttributes()
{
$crawler = $this->requestSearchView();
$columnAttributes = array('ID', 'Name', 'Products', 'Parent');
foreach ($columnAttributes as $i => $attribute) {
$this->assertSame($attribute, trim($crawler->filter('.table tbody tr td')->eq($i)->attr('data-label')));
}
}
public function testSearchViewPagination()
{
$crawler = $this->requestSearchView();
$this->assertContains('1 - 15 of 200', $crawler->filter('.list-pagination')->text());
$this->assertSame('disabled', $crawler->filter('.list-pagination li:contains("First")')->attr('class'));
$this->assertSame('disabled', $crawler->filter('.list-pagination li:contains("Previous")')->attr('class'));
$this->assertStringStartsWith('/admin/?action=search&entity=Category&sortField=id&sortDirection=DESC&page=2', $crawler->filter('.list-pagination li a:contains("Next")')->attr('href'));
$this->assertStringStartsWith('/admin/?action=search&entity=Category&sortField=id&sortDirection=DESC&page=14', $crawler->filter('.list-pagination li a:contains("Last")')->attr('href'));
}
public function testSearchViewItemActions()
{
$crawler = $this->requestSearchView();
$this->assertSame('Edit', trim($crawler->filter('#main .table td.actions a')->eq(0)->text()));
$this->assertSame('_self', $crawler->filter('#main .table td.actions a')->eq(0)->attr('target'));
$this->assertSame('Delete', trim($crawler->filter('#main .table td.actions a')->eq(1)->text()));
}
public function testSearchViewShowActionReferer()
{
$parameters = array(
'action' => 'search',
'entity' => 'Category',
'sortField' => 'name',
'sortDirection' => 'ASC',
'page' => '2',
'query' => 'cat',
);
// 1. visit a specific 'search' view page
$crawler = $this->getBackendPage($parameters);
// 2. click on the 'Edit' action of the first result
$link = $crawler->filter('td.actions a:contains("Edit")')->eq(0)->link();
$crawler = $this->client->click($link);
// 3. the 'referer' parameter should point to the exact same previous 'list' page
$refererUrl = $crawler->filter('#form-actions-row a:contains("Back to listing")')->attr('href');
$queryString = parse_url($refererUrl, PHP_URL_QUERY);
parse_str($queryString, $refererParameters);
$this->assertSame($parameters, $refererParameters);
}
public function testEntityDeletion()
{
if (PHP_VERSION_ID < 50400) {
$this->markTestSkipped('This test keeps failing on Travis CI when running PHP 5.3 for no apparent reason.');
}
/* @var EntityManager */
$em = $this->client->getContainer()->get('doctrine')->getManager();
$product = $em->getRepository('AppTestBundle\Entity\FunctionalTests\Product')->find(1);
$this->assertNotNull($product, 'Initially the product exists.');
$crawler = $this->requestEditView('Product', '1');
$this->client->followRedirects();
$form = $crawler->filter('#delete_form_submit')->form();
$this->client->submit($form);
$product = $em->getRepository('AppTestBundle\Entity\FunctionalTests\Product')->find(1);
$this->assertNull($product, 'After removing it via the delete form, the product no longer exists.');
}
public function testEntityDeletionRequiresCsrfToken()
{
$queryParameters = array('action' => 'delete', 'entity' => 'Product', 'id' => '1');
// Sending a 'DELETE' HTTP request is not enough (the delete form includes a CSRF token)
$this->client->request('DELETE', '/admin/?'.http_build_query($queryParameters));
$this->assertSame(302, $this->client->getResponse()->getStatusCode());
$this->assertContains('Redirecting to /admin/?action=list&entity=Product', $this->client->getResponse()->getContent());
}
public function testEntityDeletionRequiresDeleteHttpMethod()
{
$queryParameters = array('action' => 'delete', 'entity' => 'Product', 'id' => '1');
// 'POST' HTTP method is wrong for deleting entities ('DELETE' method is required)
$this->client->request('POST', '/admin/?'.http_build_query($queryParameters));
$this->assertSame(302, $this->client->getResponse()->getStatusCode());
$this->assertContains('Redirecting to /admin/?action=list&entity=Product', $this->client->getResponse()->getContent());
}
}