WidgetAreaEditorTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 482
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 7
eloc 329
c 2
b 0
f 2
dl 0
loc 482
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testDeletingAWidgetFromEachArea() 0 66 1
A testEditAWidgetFromOneAreaAndDeleteAWidgetFromAnotherArea() 0 73 1
B testEditingAWidgetFromEachArea() 0 78 1
A testFillingOneArea() 0 37 1
A testFillingTwoAreas() 0 48 1
B testEditingOneWidget() 0 78 1
A testDeletingOneWidgetFromOneArea() 0 72 1
1
<?php
2
3
namespace SilverStripe\Widgets\Tests;
4
5
use SilverStripe\CMS\Controllers\ContentController;
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\Control\HTTPRequest;
8
use SilverStripe\Dev\SapphireTest;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\Form;
11
use SilverStripe\Widgets\Extensions\WidgetPageExtension;
12
use SilverStripe\Widgets\Forms\WidgetAreaEditor;
13
use SilverStripe\Widgets\Tests\WidgetAreaEditorTest\FakePage;
14
use SilverStripe\Widgets\Tests\WidgetAreaEditorTest\TestWidget;
15
16
/**
17
 * @package cms
18
 * @subpackage tests
19
 */
20
class WidgetAreaEditorTest extends SapphireTest
21
{
22
    /**
23
     * This is the widget you want to use for your unit tests.
24
     */
25
    protected $widgetToTest = TestWidget::class;
26
27
    protected static $extra_dataobjects = [
28
        FakePage::class,
29
        TestWidget::class,
30
    ];
31
32
    protected $usesDatabase = true;
33
34
    protected static $required_extensions = [
35
        SiteTree::class => [WidgetPageExtension::class]
36
    ];
37
38
    public function testFillingOneArea()
39
    {
40
        $data = array(
41
            'Widget' => array(
42
                'BottomBar' => array(
43
                    'new-1' => array(
44
                        'Title' => 'MyTestWidget',
45
                        'Type' => $this->widgetToTest,
46
                        'Sort' => 0
47
                    )
48
                )
49
            )
50
        );
51
        $request = new HTTPRequest('get', 'post', array(), $data);
52
53
        $editorSide = new WidgetAreaEditor('SideBar');
54
        $editorBott = new WidgetAreaEditor('BottomBar');
55
        $form = new Form(
56
            $controller = new ContentController(),
57
            Form::class,
58
            new FieldList($editorSide, $editorBott),
59
            new FieldList()
60
        );
61
        $controller->setRequest($request);
62
        $controller->setRequest($request);
63
        $form->setController($controller);
64
65
        $page = new FakePage();
66
67
        $form->saveInto($page);
68
        $page->write();
69
        $page->flushCache();
70
        $page->BottomBar()->flushCache();
71
        $page->SideBar()->flushCache();
72
73
        $this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
74
        $this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
75
    }
76
77
    public function testFillingTwoAreas()
78
    {
79
        $data = array(
80
            'Widget' => array(
81
                'SideBar' => array(
82
                    'new-1' => array(
83
                        'Title' => 'MyTestWidgetSide',
84
                        'Type' => $this->widgetToTest,
85
                        'Sort' => 0
86
                    )
87
                ),
88
                'BottomBar' => array(
89
                    'new-1' => array(
90
                        'Title' => 'MyTestWidgetBottom',
91
                        'Type' => $this->widgetToTest,
92
                        'Sort' => 0
93
                    )
94
                )
95
            )
96
        );
97
        $request = new HTTPRequest('get', 'post', array(), $data);
98
99
        $editorSide = new WidgetAreaEditor('SideBar');
100
        $editorBott = new WidgetAreaEditor('BottomBar');
101
        $form = new Form(
102
            $controller = new ContentController(),
103
            Form::class,
104
            new FieldList($editorSide, $editorBott),
105
            new FieldList()
106
        );
107
        $controller->setRequest($request);
108
        $form->setController($controller);
109
        $page = new FakePage();
110
111
        $form->saveInto($page);
112
        $page->write();
113
        $page->flushCache();
114
        $page->BottomBar()->flushCache();
115
        $page->SideBar()->flushCache();
116
117
        // Make sure they both got saved
118
        $this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
119
        $this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
120
121
        $sideWidgets = $page->SideBar()->Widgets()->toArray();
122
        $bottWidgets = $page->BottomBar()->Widgets()->toArray();
123
        $this->assertEquals($sideWidgets[0]->getTitle(), 'MyTestWidgetSide');
124
        $this->assertEquals($bottWidgets[0]->getTitle(), 'MyTestWidgetBottom');
125
    }
126
127
    public function testDeletingOneWidgetFromOneArea()
128
    {
129
        // First get some widgets in there
130
        $data = array(
131
            'Widget' => array(
132
                'SideBar' => array(
133
                    'new-1' => array(
134
                        'Title' => 'MyTestWidgetSide',
135
                        'Type' => $this->widgetToTest,
136
                        'Sort' => 0
137
                    )
138
                ),
139
                'BottomBar' => array(
140
                    'new-1' => array(
141
                        'Title' => 'MyTestWidgetBottom',
142
                        'Type' => $this->widgetToTest,
143
                        'Sort' => 0
144
                    )
145
                )
146
            )
147
        );
148
        $request = new HTTPRequest('get', 'post', array(), $data);
149
150
        $editorSide = new WidgetAreaEditor('SideBar');
151
        $editorBott = new WidgetAreaEditor('BottomBar');
152
        $form = new Form(
153
            $controller = new ContentController(),
154
            Form::class,
155
            new FieldList($editorSide, $editorBott),
156
            new FieldList()
157
        );
158
        $controller->setRequest($request);
159
        $form->setController($controller);
160
        $page = new FakePage();
161
162
        $form->saveInto($page);
163
        $page->write();
164
        $page->flushCache();
165
        $page->BottomBar()->flushCache();
166
        $page->SideBar()->flushCache();
167
        $sideWidgets = $page->SideBar()->Widgets()->toArray();
0 ignored issues
show
Unused Code introduced by
The assignment to $sideWidgets is dead and can be removed.
Loading history...
168
        $bottWidgets = $page->BottomBar()->Widgets()->toArray();
169
170
        // Save again (after removing the SideBar's widget)
171
        $data = array(
172
            'Widget' => array(
173
                'SideBar' => array(
174
                ),
175
                'BottomBar' => array(
176
                    $bottWidgets[0]->ID => array(
177
                        'Title' => 'MyTestWidgetBottom',
178
                        'Type' => $this->widgetToTest,
179
                        'Sort' => 0
180
                    )
181
                )
182
            )
183
        );
184
        $request = new HTTPRequest('get', 'post', array(), $data);
185
        $controller->setRequest($request);
186
        $form->setController($controller);
187
        $form->saveInto($page);
188
189
        $page->write();
190
        $page->flushCache();
191
        $page->BottomBar()->flushCache();
192
        $page->SideBar()->flushCache();
193
        $sideWidgets = $page->SideBar()->Widgets()->toArray();
194
        $bottWidgets = $page->BottomBar()->Widgets()->toArray();
195
196
        $this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
197
        $this->assertEquals($bottWidgets[0]->getTitle(), 'MyTestWidgetBottom');
198
        $this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
199
    }
200
201
    public function testDeletingAWidgetFromEachArea()
202
    {
203
        // First get some widgets in there
204
        $data = array(
205
            'Widget' => array(
206
                'SideBar' => array(
207
                    'new-1' => array(
208
                        'Title' => 'MyTestWidgetSide',
209
                        'Type' => $this->widgetToTest,
210
                        'Sort' => 0
211
                    )
212
                ),
213
                'BottomBar' => array(
214
                    'new-1' => array(
215
                        'Title' => 'MyTestWidgetBottom',
216
                        'Type' => $this->widgetToTest,
217
                        'Sort' => 0
218
                    )
219
                )
220
            )
221
        );
222
        $request = new HTTPRequest('get', 'post', array(), $data);
223
224
        $editorSide = new WidgetAreaEditor('SideBar');
225
        $editorBott = new WidgetAreaEditor('BottomBar');
226
        $form = new Form(
227
            $controller = new ContentController(),
228
            Form::class,
229
            new FieldList($editorSide, $editorBott),
230
            new FieldList()
231
        );
232
        $controller->setRequest($request);
233
        $form->setController($controller);
234
        $page = new FakePage();
235
236
        $form->saveInto($page);
237
        $page->write();
238
        $page->flushCache();
239
        $page->BottomBar()->flushCache();
240
        $page->SideBar()->flushCache();
241
        $sideWidgets = $page->SideBar()->Widgets()->toArray();
0 ignored issues
show
Unused Code introduced by
The assignment to $sideWidgets is dead and can be removed.
Loading history...
242
        $bottWidgets = $page->BottomBar()->Widgets()->toArray();
0 ignored issues
show
Unused Code introduced by
The assignment to $bottWidgets is dead and can be removed.
Loading history...
243
244
        // Save again (after removing the SideBar's widget)
245
        $data = array(
246
            'Widget' => array(
247
                'SideBar' => array(
248
                ),
249
                'BottomBar' => array(
250
                )
251
            )
252
        );
253
        $request = new HTTPRequest('get', 'post', array(), $data);
254
        $controller->setRequest($request);
255
        $form->setController($controller);
256
        $form->saveInto($page);
257
258
        $page->write();
259
        $page->flushCache();
260
        $page->BottomBar()->flushCache();
261
        $page->SideBar()->flushCache();
262
        $sideWidgets = $page->SideBar()->Widgets()->toArray();
263
        $bottWidgets = $page->BottomBar()->Widgets()->toArray();
264
265
        $this->assertEquals($page->BottomBar()->Widgets()->Count(), 0);
266
        $this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
267
    }
268
269
    public function testEditingOneWidget()
270
    {
271
        // First get some widgets in there
272
        $data = array(
273
            'Widget' => array(
274
                'SideBar' => array(
275
                    'new-1' => array(
276
                        'Title' => 'MyTestWidgetSide',
277
                        'Type' => $this->widgetToTest,
278
                        'Sort' => 0
279
                    )
280
                ),
281
                'BottomBar' => array(
282
                    'new-1' => array(
283
                        'Title' => 'MyTestWidgetBottom',
284
                        'Type' => $this->widgetToTest,
285
                        'Sort' => 0
286
                    )
287
                )
288
            )
289
        );
290
        $request = new HTTPRequest('get', 'post', array(), $data);
291
292
        $editorSide = new WidgetAreaEditor('SideBar');
293
        $editorBott = new WidgetAreaEditor('BottomBar');
294
        $form = new Form(
295
            $controller = new ContentController(),
296
            Form::class,
297
            new FieldList($editorSide, $editorBott),
298
            new FieldList()
299
        );
300
        $controller->setRequest($request);
301
        $form->setController($controller);
302
        $page = new FakePage();
303
304
        $form->saveInto($page);
305
        $page->write();
306
        $page->flushCache();
307
        $page->BottomBar()->flushCache();
308
        $page->SideBar()->flushCache();
309
        $sideWidgets = $page->SideBar()->Widgets()->toArray();
310
        $bottWidgets = $page->BottomBar()->Widgets()->toArray();
311
312
        // Save again (after removing the SideBar's widget)
313
        $data = array(
314
            'Widget' => array(
315
                'SideBar' => array(
316
                    $sideWidgets[0]->ID => array(
317
                        'Title' => 'MyTestWidgetSide-edited',
318
                        'Type' => $this->widgetToTest,
319
                        'Sort' => 0
320
                    )
321
                ),
322
                'BottomBar' => array(
323
                    $bottWidgets[0]->ID => array(
324
                        'Title' => 'MyTestWidgetBottom',
325
                        'Type' => $this->widgetToTest,
326
                        'Sort' => 0
327
                    )
328
                )
329
            )
330
        );
331
        $request = new HTTPRequest('get', 'post', array(), $data);
332
        $controller->setRequest($request);
333
        $form->setController($controller);
334
        $form->saveInto($page);
335
336
        $page->write();
337
        $page->flushCache();
338
        $page->BottomBar()->flushCache();
339
        $page->SideBar()->flushCache();
340
        $sideWidgets = $page->SideBar()->Widgets()->toArray();
341
        $bottWidgets = $page->BottomBar()->Widgets()->toArray();
342
343
        $this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
344
        $this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
345
        $this->assertEquals($bottWidgets[0]->getTitle(), 'MyTestWidgetBottom');
346
        $this->assertEquals($sideWidgets[0]->getTitle(), 'MyTestWidgetSide-edited');
347
    }
348
349
    public function testEditingAWidgetFromEachArea()
350
    {
351
        // First get some widgets in there
352
        $data = array(
353
            'Widget' => array(
354
                'SideBar' => array(
355
                    'new-1' => array(
356
                        'Title' => 'MyTestWidgetSide',
357
                        'Type' => $this->widgetToTest,
358
                        'Sort' => 0
359
                    )
360
                ),
361
                'BottomBar' => array(
362
                    'new-1' => array(
363
                        'Title' => 'MyTestWidgetBottom',
364
                        'Type' => $this->widgetToTest,
365
                        'Sort' => 0
366
                    )
367
                )
368
            )
369
        );
370
        $request = new HTTPRequest('get', 'post', array(), $data);
371
372
        $editorSide = new WidgetAreaEditor('SideBar');
373
        $editorBott = new WidgetAreaEditor('BottomBar');
374
        $form = new Form(
375
            $controller = new ContentController(),
376
            Form::class,
377
            new FieldList($editorSide, $editorBott),
378
            new FieldList()
379
        );
380
        $controller->setRequest($request);
381
        $form->setController($controller);
382
        $page = new FakePage();
383
384
        $form->saveInto($page);
385
        $page->write();
386
        $page->flushCache();
387
        $page->BottomBar()->flushCache();
388
        $page->SideBar()->flushCache();
389
        $sideWidgets = $page->SideBar()->Widgets()->toArray();
390
        $bottWidgets = $page->BottomBar()->Widgets()->toArray();
391
392
        // Save again (after removing the SideBar's widget)
393
        $data = array(
394
            'Widget' => array(
395
                'SideBar' => array(
396
                    $sideWidgets[0]->ID => array(
397
                        'Title' => 'MyTestWidgetSide-edited',
398
                        'Type' => $this->widgetToTest,
399
                        'Sort' => 0
400
                    )
401
                ),
402
                'BottomBar' => array(
403
                    $bottWidgets[0]->ID => array(
404
                        'Title' => 'MyTestWidgetBottom-edited',
405
                        'Type' => $this->widgetToTest,
406
                        'Sort' => 0
407
                    )
408
                )
409
            )
410
        );
411
        $request = new HTTPRequest('get', 'post', array(), $data);
412
        $controller->setRequest($request);
413
        $form->setController($controller);
414
        $form->saveInto($page);
415
416
        $page->write();
417
        $page->flushCache();
418
        $page->BottomBar()->flushCache();
419
        $page->SideBar()->flushCache();
420
        $sideWidgets = $page->SideBar()->Widgets()->toArray();
421
        $bottWidgets = $page->BottomBar()->Widgets()->toArray();
422
423
        $this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
424
        $this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
425
        $this->assertEquals($bottWidgets[0]->getTitle(), 'MyTestWidgetBottom-edited');
426
        $this->assertEquals($sideWidgets[0]->getTitle(), 'MyTestWidgetSide-edited');
427
    }
428
429
    public function testEditAWidgetFromOneAreaAndDeleteAWidgetFromAnotherArea()
430
    {
431
        // First get some widgets in there
432
        $data = array(
433
            'Widget' => array(
434
                'SideBar' => array(
435
                    'new-1' => array(
436
                        'Title' => 'MyTestWidgetSide',
437
                        'Type' => $this->widgetToTest,
438
                        'Sort' => 0
439
                    )
440
                ),
441
                'BottomBar' => array(
442
                    'new-1' => array(
443
                        'Title' => 'MyTestWidgetBottom',
444
                        'Type' => $this->widgetToTest,
445
                        'Sort' => 0
446
                    )
447
                )
448
            )
449
        );
450
        $request = new HTTPRequest('get', 'post', array(), $data);
451
452
        $editorSide = new WidgetAreaEditor('SideBar');
453
        $editorBott = new WidgetAreaEditor('BottomBar');
454
        $form = new Form(
455
            $controller = new ContentController(),
456
            Form::class,
457
            new FieldList($editorSide, $editorBott),
458
            new FieldList()
459
        );
460
        $controller->setRequest($request);
461
        $form->setController($controller);
462
        $page = new FakePage();
463
464
        $editorSide->saveInto($page);
465
        $editorBott->saveInto($page);
466
        $page->write();
467
        $page->flushCache();
468
        $page->BottomBar()->flushCache();
469
        $page->SideBar()->flushCache();
470
        $sideWidgets = $page->SideBar()->Widgets()->toArray();
471
        $bottWidgets = $page->BottomBar()->Widgets()->toArray();
0 ignored issues
show
Unused Code introduced by
The assignment to $bottWidgets is dead and can be removed.
Loading history...
472
473
        // Save again (after removing the SideBar's widget)
474
        $data = array(
475
            'Widget' => array(
476
                'SideBar' => array(
477
                    $sideWidgets[0]->ID => array(
478
                        'Title' => 'MyTestWidgetSide-edited',
479
                        'Type' => $this->widgetToTest,
480
                        'Sort' => 0
481
                    )
482
                ),
483
                'BottomBar' => array(
484
                )
485
            )
486
        );
487
        $request = new HTTPRequest('get', 'post', array(), $data);
488
        $controller->setRequest($request);
489
        $form->setController($controller);
490
        $form->saveInto($page);
491
492
        $page->write();
493
        $page->flushCache();
494
        $page->BottomBar()->flushCache();
495
        $page->SideBar()->flushCache();
496
        $sideWidgets = $page->SideBar()->Widgets()->toArray();
497
        $bottWidgets = $page->BottomBar()->Widgets()->toArray();
498
499
        $this->assertEquals($page->BottomBar()->Widgets()->Count(), 0);
500
        $this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
501
        $this->assertEquals($sideWidgets[0]->getTitle(), 'MyTestWidgetSide-edited');
502
    }
503
}
504