Completed
Push — master ( f3b14d...1fe4c3 )
by Damian
10s
created

WidgetAreaEditorTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 438
Duplicated Lines 63.24 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 277
loc 438
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testDeletingOneWidgetFromOneArea() 71 71 1
A testDeletingAWidgetFromEachArea() 0 65 1
B testEditingOneWidget() 77 77 1
B testEditingAWidgetFromEachArea() 77 77 1
B testFillingOneArea() 0 36 1
A testFillingTwoAreas() 0 48 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SilverStripe\Widgets\Tests;
4
5
use Page;
6
use SilverStripe\CMS\Controllers\ContentController;
7
use SilverStripe\CMS\Model\SiteTree;
8
use SilverStripe\Control\HTTPRequest;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\Form;
11
use SilverStripe\Dev\SapphireTest;
12
use SilverStripe\Dev\TestOnly;
13
use SilverStripe\Widgets\Extensions\WidgetPageExtension;
14
use SilverStripe\Widgets\Forms\WidgetAreaEditor;
15
use SilverStripe\Widgets\Model\Widget;
16
use SilverStripe\Widgets\Tests\WidgetAreaEditorTest\FakePage;
17
use SilverStripe\Widgets\Tests\WidgetAreaEditorTest\TestWidget;
18
19
/**
20
 * @package cms
21
 * @subpackage tests
22
 */
23
class WidgetAreaEditorTest extends SapphireTest
24
{
25
    /**
26
     * This is the widget you want to use for your unit tests.
27
     */
28
    protected $widgetToTest = TestWidget::class;
29
30
    protected $extraDataObjects = array(
31
        FakePage::class,
32
        TestWidget::class,
33
    );
34
35
    protected $usesDatabase = true;
36
37
    protected $requiredExtensions = array(
38
        SiteTree::class => array(WidgetPageExtension::class)
39
    );
40
41
    public function testFillingOneArea()
42
    {
43
        $data = array(
44
            'Widget' => array(
45
                'BottomBar' => array(
46
                    'new-1' => array(
47
                        'Title' => 'MyTestWidget',
48
                        'Type' => $this->widgetToTest,
49
                        'Sort' => 0
50
                    )
51
                )
52
            )
53
        );
54
        $request = new HTTPRequest('get', 'post', array(), $data);
55
56
        $editorSide = new WidgetAreaEditor('SideBar');
57
        $editorBott = new WidgetAreaEditor('BottomBar');
58
        $form = new Form(
59
            new ContentController(),
60
            Form::class,
61
            new FieldList($editorSide, $editorBott),
62
            new FieldList()
63
        );
64
        $form->setRequest($request);
65
66
        $page = new FakePage();
67
68
        $form->saveInto($page);
69
        $page->write();
70
        $page->flushCache();
71
        $page->BottomBar()->flushCache();
72
        $page->SideBar()->flushCache();
73
74
        $this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
75
        $this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
76
    }
77
78
    public function testFillingTwoAreas()
79
    {
80
        $data = array(
81
            'Widget' => array(
82
                'SideBar' => array(
83
                    'new-1' => array(
84
                        'Title' => 'MyTestWidgetSide',
85
                        'Type' => $this->widgetToTest,
86
                        'Sort' => 0
87
                    )
88
                ),
89
                'BottomBar' => array(
90
                    'new-1' => array(
91
                        'Title' => 'MyTestWidgetBottom',
92
                        'Type' => $this->widgetToTest,
93
                        'Sort' => 0
94
                    )
95
                )
96
            )
97
        );
98
        $request = new HTTPRequest('get', 'post', array(), $data);
99
100
        $editorSide = new WidgetAreaEditor('SideBar');
101
        $editorBott = new WidgetAreaEditor('BottomBar');
102
        $form = new Form(
103
            new ContentController(),
104
            Form::class,
105
            new FieldList($editorSide, $editorBott),
106
            new FieldList()
107
        );
108
        $form->setRequest($request);
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 View Code Duplication
    public function testDeletingOneWidgetFromOneArea()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
            new ContentController(),
154
            Form::class,
155
            new FieldList($editorSide, $editorBott),
156
            new FieldList()
157
        );
158
        $form->setRequest($request);
159
        $page = new FakePage();
160
161
        $form->saveInto($page);
162
        $page->write();
163
        $page->flushCache();
164
        $page->BottomBar()->flushCache();
165
        $page->SideBar()->flushCache();
166
        $sideWidgets = $page->SideBar()->Widgets()->toArray();
0 ignored issues
show
Unused Code introduced by
$sideWidgets is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
167
        $bottWidgets = $page->BottomBar()->Widgets()->toArray();
168
169
        // Save again (after removing the SideBar's widget)
170
        $data = array(
171
            'Widget' => array(
172
                'SideBar' => array(
173
                ),
174
                'BottomBar' => array(
175
                    $bottWidgets[0]->ID => array(
176
                        'Title' => 'MyTestWidgetBottom',
177
                        'Type' => $this->widgetToTest,
178
                        'Sort' => 0
179
                    )
180
                )
181
            )
182
        );
183
        $request = new HTTPRequest('get', 'post', array(), $data);
184
        $form->setRequest($request);
185
        $form->saveInto($page);
186
187
        $page->write();
188
        $page->flushCache();
189
        $page->BottomBar()->flushCache();
190
        $page->SideBar()->flushCache();
191
        $sideWidgets = $page->SideBar()->Widgets()->toArray();
0 ignored issues
show
Unused Code introduced by
$sideWidgets is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
192
        $bottWidgets = $page->BottomBar()->Widgets()->toArray();
193
194
        $this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
195
        $this->assertEquals($bottWidgets[0]->getTitle(), 'MyTestWidgetBottom');
196
        $this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
197
    }
198
199
    public function testDeletingAWidgetFromEachArea()
200
    {
201
        // First get some widgets in there
202
        $data = array(
203
            'Widget' => array(
204
                'SideBar' => array(
205
                    'new-1' => array(
206
                        'Title' => 'MyTestWidgetSide',
207
                        'Type' => $this->widgetToTest,
208
                        'Sort' => 0
209
                    )
210
                ),
211
                'BottomBar' => array(
212
                    'new-1' => array(
213
                        'Title' => 'MyTestWidgetBottom',
214
                        'Type' => $this->widgetToTest,
215
                        'Sort' => 0
216
                    )
217
                )
218
            )
219
        );
220
        $request = new HTTPRequest('get', 'post', array(), $data);
221
222
        $editorSide = new WidgetAreaEditor('SideBar');
223
        $editorBott = new WidgetAreaEditor('BottomBar');
224
        $form = new Form(
225
            new ContentController(),
226
            Form::class,
227
            new FieldList($editorSide, $editorBott),
228
            new FieldList()
229
        );
230
        $form->setRequest($request);
231
        $page = new FakePage();
232
233
        $form->saveInto($page);
234
        $page->write();
235
        $page->flushCache();
236
        $page->BottomBar()->flushCache();
237
        $page->SideBar()->flushCache();
238
        $sideWidgets = $page->SideBar()->Widgets()->toArray();
0 ignored issues
show
Unused Code introduced by
$sideWidgets is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
239
        $bottWidgets = $page->BottomBar()->Widgets()->toArray();
0 ignored issues
show
Unused Code introduced by
$bottWidgets is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
240
241
        // Save again (after removing the SideBar's widget)
242
        $data = array(
243
            'Widget' => array(
244
                'SideBar' => array(
245
                ),
246
                'BottomBar' => array(
247
                )
248
            )
249
        );
250
        $request = new HTTPRequest('get', 'post', array(), $data);
251
        $form->setRequest($request);
252
        $form->saveInto($page);
253
254
        $page->write();
255
        $page->flushCache();
256
        $page->BottomBar()->flushCache();
257
        $page->SideBar()->flushCache();
258
        $sideWidgets = $page->SideBar()->Widgets()->toArray();
0 ignored issues
show
Unused Code introduced by
$sideWidgets is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
259
        $bottWidgets = $page->BottomBar()->Widgets()->toArray();
0 ignored issues
show
Unused Code introduced by
$bottWidgets is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
260
261
        $this->assertEquals($page->BottomBar()->Widgets()->Count(), 0);
262
        $this->assertEquals($page->SideBar()->Widgets()->Count(), 0);
263
    }
264
265 View Code Duplication
    public function testEditingOneWidget()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
266
    {
267
        // First get some widgets in there
268
        $data = array(
269
            'Widget' => array(
270
                'SideBar' => array(
271
                    'new-1' => array(
272
                        'Title' => 'MyTestWidgetSide',
273
                        'Type' => $this->widgetToTest,
274
                        'Sort' => 0
275
                    )
276
                ),
277
                'BottomBar' => array(
278
                    'new-1' => array(
279
                        'Title' => 'MyTestWidgetBottom',
280
                        'Type' => $this->widgetToTest,
281
                        'Sort' => 0
282
                    )
283
                )
284
            )
285
        );
286
        $request = new HTTPRequest('get', 'post', array(), $data);
287
288
        $editorSide = new WidgetAreaEditor('SideBar');
289
        $editorBott = new WidgetAreaEditor('BottomBar');
290
        $form = new Form(
291
            new ContentController(),
292
            Form::class,
293
            new FieldList($editorSide, $editorBott),
294
            new FieldList()
295
        );
296
        $form->setRequest($request);
297
        $page = new FakePage();
298
299
        $form->saveInto($page);
300
        $page->write();
301
        $page->flushCache();
302
        $page->BottomBar()->flushCache();
303
        $page->SideBar()->flushCache();
304
        $sideWidgets = $page->SideBar()->Widgets()->toArray();
305
        $bottWidgets = $page->BottomBar()->Widgets()->toArray();
306
307
        // Save again (after removing the SideBar's widget)
308
        $data = array(
309
            'Widget' => array(
310
                'SideBar' => array(
311
                    $sideWidgets[0]->ID => array(
312
                        'Title' => 'MyTestWidgetSide-edited',
313
                        'Type' => $this->widgetToTest,
314
                        'Sort' => 0
315
                    )
316
                ),
317
                'BottomBar' => array(
318
                    $bottWidgets[0]->ID => array(
319
                        'Title' => 'MyTestWidgetBottom',
320
                        'Type' => $this->widgetToTest,
321
                        'Sort' => 0
322
                    )
323
                )
324
            )
325
        );
326
        $request = new HTTPRequest('get', 'post', array(), $data);
327
        $form->setRequest($request);
328
        $form->saveInto($page);
329
330
        $page->write();
331
        $page->flushCache();
332
        $page->BottomBar()->flushCache();
333
        $page->SideBar()->flushCache();
334
        $sideWidgets = $page->SideBar()->Widgets()->toArray();
335
        $bottWidgets = $page->BottomBar()->Widgets()->toArray();
336
337
        $this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
338
        $this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
339
        $this->assertEquals($bottWidgets[0]->getTitle(), 'MyTestWidgetBottom');
340
        $this->assertEquals($sideWidgets[0]->getTitle(), 'MyTestWidgetSide-edited');
341
    }
342
343 View Code Duplication
    public function testEditingAWidgetFromEachArea()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
344
    {
345
        // First get some widgets in there
346
        $data = array(
347
            'Widget' => array(
348
                'SideBar' => array(
349
                    'new-1' => array(
350
                        'Title' => 'MyTestWidgetSide',
351
                        'Type' => $this->widgetToTest,
352
                        'Sort' => 0
353
                    )
354
                ),
355
                'BottomBar' => array(
356
                    'new-1' => array(
357
                        'Title' => 'MyTestWidgetBottom',
358
                        'Type' => $this->widgetToTest,
359
                        'Sort' => 0
360
                    )
361
                )
362
            )
363
        );
364
        $request = new HTTPRequest('get', 'post', array(), $data);
365
366
        $editorSide = new WidgetAreaEditor('SideBar');
367
        $editorBott = new WidgetAreaEditor('BottomBar');
368
        $form = new Form(
369
            new ContentController(),
370
            Form::class,
371
            new FieldList($editorSide, $editorBott),
372
            new FieldList()
373
        );
374
        $form->setRequest($request);
375
        $page = new FakePage();
376
377
        $form->saveInto($page);
378
        $page->write();
379
        $page->flushCache();
380
        $page->BottomBar()->flushCache();
381
        $page->SideBar()->flushCache();
382
        $sideWidgets = $page->SideBar()->Widgets()->toArray();
383
        $bottWidgets = $page->BottomBar()->Widgets()->toArray();
384
385
        // Save again (after removing the SideBar's widget)
386
        $data = array(
387
            'Widget' => array(
388
                'SideBar' => array(
389
                    $sideWidgets[0]->ID => array(
390
                        'Title' => 'MyTestWidgetSide-edited',
391
                        'Type' => $this->widgetToTest,
392
                        'Sort' => 0
393
                    )
394
                ),
395
                'BottomBar' => array(
396
                    $bottWidgets[0]->ID => array(
397
                        'Title' => 'MyTestWidgetBottom-edited',
398
                        'Type' => $this->widgetToTest,
399
                        'Sort' => 0
400
                    )
401
                )
402
            )
403
        );
404
        $request = new HTTPRequest('get', 'post', array(), $data);
405
        $form->setRequest($request);
406
        $form->saveInto($page);
407
408
        $page->write();
409
        $page->flushCache();
410
        $page->BottomBar()->flushCache();
411
        $page->SideBar()->flushCache();
412
        $sideWidgets = $page->SideBar()->Widgets()->toArray();
413
        $bottWidgets = $page->BottomBar()->Widgets()->toArray();
414
415
        $this->assertEquals($page->BottomBar()->Widgets()->Count(), 1);
416
        $this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
417
        $this->assertEquals($bottWidgets[0]->getTitle(), 'MyTestWidgetBottom-edited');
418
        $this->assertEquals($sideWidgets[0]->getTitle(), 'MyTestWidgetSide-edited');
419
    }
420
421 View Code Duplication
    public function testEditAWidgetFromOneAreaAndDeleteAWidgetFromAnotherArea()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
422
    {
423
        // First get some widgets in there
424
        $data = array(
425
            'Widget' => array(
426
                'SideBar' => array(
427
                    'new-1' => array(
428
                        'Title' => 'MyTestWidgetSide',
429
                        'Type' => $this->widgetToTest,
430
                        'Sort' => 0
431
                    )
432
                ),
433
                'BottomBar' => array(
434
                    'new-1' => array(
435
                        'Title' => 'MyTestWidgetBottom',
436
                        'Type' => $this->widgetToTest,
437
                        'Sort' => 0
438
                    )
439
                )
440
            )
441
        );
442
        $request = new HTTPRequest('get', 'post', array(), $data);
443
444
        $editorSide = new WidgetAreaEditor('SideBar');
445
        $editorBott = new WidgetAreaEditor('BottomBar');
446
        $form = new Form(
447
            new ContentController(),
448
            Form::class,
449
            new FieldList($editorSide, $editorBott),
450
            new FieldList()
451
        );
452
        $form->setRequest($request);
453
        $page = new FakePage();
454
455
        $editorSide->saveInto($page);
456
        $editorBott->saveInto($page);
457
        $page->write();
458
        $page->flushCache();
459
        $page->BottomBar()->flushCache();
460
        $page->SideBar()->flushCache();
461
        $sideWidgets = $page->SideBar()->Widgets()->toArray();
462
        $bottWidgets = $page->BottomBar()->Widgets()->toArray();
0 ignored issues
show
Unused Code introduced by
$bottWidgets is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
463
464
        // Save again (after removing the SideBar's widget)
465
        $data = array(
466
            'Widget' => array(
467
                'SideBar' => array(
468
                    $sideWidgets[0]->ID => array(
469
                        'Title' => 'MyTestWidgetSide-edited',
470
                        'Type' => $this->widgetToTest,
471
                        'Sort' => 0
472
                    )
473
                ),
474
                'BottomBar' => array(
475
                )
476
            )
477
        );
478
        $request = new HTTPRequest('get', 'post', array(), $data);
479
        $form->setRequest($request);
480
        $form->saveInto($page);
481
482
        $page->write();
483
        $page->flushCache();
484
        $page->BottomBar()->flushCache();
485
        $page->SideBar()->flushCache();
486
        $sideWidgets = $page->SideBar()->Widgets()->toArray();
487
        $bottWidgets = $page->BottomBar()->Widgets()->toArray();
0 ignored issues
show
Unused Code introduced by
$bottWidgets is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
488
489
        $this->assertEquals($page->BottomBar()->Widgets()->Count(), 0);
490
        $this->assertEquals($page->SideBar()->Widgets()->Count(), 1);
491
        $this->assertEquals($sideWidgets[0]->getTitle(), 'MyTestWidgetSide-edited');
492
    }
493
}
494