ReportsControllerTest::testDataTables()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 120
Code Lines 87

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 1
eloc 87
c 4
b 1
f 0
nc 1
nop 0
dl 0
loc 120
rs 8.2836

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Test\TestCase\Controller;
4
5
use Cake\ORM\TableRegistry;
6
use Cake\TestSuite\IntegrationTestTrait;
7
use Cake\TestSuite\TestCase;
8
9
use function count;
10
use function json_decode;
11
12
class ReportsControllerTest extends TestCase
13
{
14
    use IntegrationTestTrait;
15
16
    /**
17
     * Fixtures.
18
     *
19
     * @var array
20
     */
21
    public $fixtures = [
22
        'app.Notifications',
23
        'app.Developers',
24
        'app.Reports',
25
        'app.Incidents',
26
    ];
27
28
    public function setUp(): void
29
    {
30
        $this->Reports = TableRegistry::getTableLocator()->get('Reports');
31
        $this->session(['Developer.id' => 1]);
32
    }
33
34
    public function testIndex(): void
35
    {
36
        $this->get('/reports');
37
        $this->assertEquals(['3.8', '4.0'], $this->viewVariable('distinct_versions'));
38
        $this->assertEquals(['forwarded', 'new'], $this->viewVariable('distinct_statuses'));
39
        $this->assertEquals(
40
            [
41
                'error1',
42
                'error2',
43
            ],
44
            $this->viewVariable('distinct_error_names')
45
        );
46
    }
47
48
    public function testView(): void
49
    {
50
        $this->get('/reports/view/1');
51
        $report = $this->viewVariable('report');
52
        $this->assertNotEmpty($report);
53
        $this->assertEquals(1, $report[0]['id']);
54
        $this->assertEquals('error2', $report[0]['error_name']);
55
56
        $this->assertNotEmpty($this->viewVariable('project_name'));
57
        $this->assertNotEmpty($this->viewVariable('columns'));
58
59
        $this->assertNotEmpty($this->viewVariable('related_entries'));
60
        $this->assertEquals(
61
            count($this->viewVariable('columns')),
62
            count($this->viewVariable('related_entries'))
63
        );
64
65
        foreach ($this->viewVariable('columns') as $column) {
66
            $this->assertNotEmpty($this->viewVariable("${column}_distinct_count"));
67
        }
68
69
        $this->assertNotEmpty($this->viewVariable('incidents'));
70
        $this->assertEquals(1, count($this->viewVariable('incidents')));
71
72
        $this->assertNotEmpty($this->viewVariable('incidents_with_description'));
73
        $this->assertEquals(1, count($this->viewVariable('incidents_with_description')->toList()));
74
75
        $this->assertNotEmpty($this->viewVariable('incidents_with_stacktrace'));
76
        $this->assertEquals(1, count($this->viewVariable('incidents_with_stacktrace')->toList()));
77
78
        $this->assertNotEmpty($this->viewVariable('related_reports'));
79
        //FIXME: 0 or 1 ?
80
        //$this->assertEquals(1, count($this->viewVariable('related_reports')->toList()));
81
82
        $this->get('/reports/view/3');
83
        $this->assertResponseContains('The report does not exist.');
84
        $this->assertResponseContains('/reports/view/3');
85
    }
86
87
    public function testDataTables(): void
88
    {
89
        $this->get('/reports/data_tables?sEcho=1&iDisplayLength=25');
90
        $expected = [
91
            'iTotalRecords' => 4,
92
            'iTotalDisplayRecords' => 4,
93
            'sEcho' => 1,
94
            'aaData' => [
95
                [
96
                    '<input type="checkbox" name="reports[]" value="1"/>',
97
                    1,
98
                    'error2',
99
                    'Lorem ipsum dolor sit amet',
100
                    'filename_1.php',
101
                    '4.0',
102
                    'Forwarded',
103
                    'js',
104
                    '1',
105
                ],
106
                [
107
                    '<input type="checkbox" name="reports[]" value="2"/>',
108
                    2,
109
                    'error2',
110
                    'Lorem ipsum dolor sit amet',
111
                    'filename_2.php',
112
                    '4.0',
113
                    'Forwarded',
114
                    'js',
115
                    '1',
116
                ],
117
                [
118
                    '<input type="checkbox" name="reports[]" value="4"/>',
119
                    4,
120
                    'error1',
121
                    'Lorem ipsum dolor sit amet',
122
                    'filename_3.js',
123
                    '3.8',
124
                    'Forwarded',
125
                    'js',
126
                    '2',
127
                ],
128
                [
129
                    '<input type="checkbox" name="reports[]" value="5"/>',
130
                    5,
131
                    'error1',
132
                    'Lorem ipsum dolor sit amet',
133
                    'filename_4.js',
134
                    '3.8',
135
                    'New',
136
                    'js',
137
                    '1',
138
                ],
139
            ],
140
        ];
141
        $this->assertEquals($expected, json_decode($this->_response->getBody(), true));
142
143
        $this->get('/reports/data_tables?sEcho=1&sSearch=error2&bSearchable_2=true&iSortCol_0=0&sSortDir_0=desc&bSortable_0=true&iSortingCols=2&iDisplayLength=25');
144
        $expected = [
145
            'iTotalRecords' => 4,
146
            'iTotalDisplayRecords' => 2,
147
            'sEcho' => 1,
148
            'aaData' => [
149
                [
150
                    '<input type="checkbox" name="reports[]" value="1"/>',
151
                    1,
152
                    'error2',
153
                    'Lorem ipsum dolor sit amet',
154
                    'filename_1.php',
155
                    '4.0',
156
                    'Forwarded',
157
                    'js',
158
                    '1',
159
                ],
160
                [
161
                    '<input type="checkbox" name="reports[]" value="2"/>',
162
                    2,
163
                    'error2',
164
                    'Lorem ipsum dolor sit amet',
165
                    'filename_2.php',
166
                    '4.0',
167
                    'Forwarded',
168
                    'js',
169
                    '1',
170
                ],
171
            ],
172
        ];
173
        $result = json_decode($this->_response->getBody(), true);
174
        $this->assertEquals($expected, $result);
175
176
        $this->get('/reports/data_tables?sEcho=1&sSearch_1=1&iDisplayLength=25');
177
        $expected = [
178
            'iTotalRecords' => 4,
179
            'iTotalDisplayRecords' => 1,
180
            'sEcho' => 1,
181
            'aaData' => [
182
                [
183
                    '<input type="checkbox" name="reports[]" value="1"/>',
184
                    1,
185
                    'error2',
186
                    'Lorem ipsum dolor sit amet',
187
                    'filename_1.php',
188
                    '4.0',
189
                    'Forwarded',
190
                    'js',
191
                    '1',
192
                ],
193
            ],
194
        ];
195
        $result = json_decode($this->_response->getBody(), true);
196
        $this->assertEquals($expected, $result);
197
198
        $this->get('/reports/data_tables?sEcho=1&sSearch_1=0&iDisplayLength=25');
199
        $expected = [
200
            'iTotalRecords' => 4,
201
            'iTotalDisplayRecords' => 0,
202
            'sEcho' => 1,
203
            'aaData' => [],
204
        ];
205
        $result = json_decode($this->_response->getBody(), true);
206
        $this->assertEquals($expected, $result);
207
    }
208
209
    public function testMarkRelatedTo(): void
210
    {
211
        $this->Reports->id = 2;
212
        $incidents = $this->Reports->getIncidents();
213
        $this->assertEquals(1, $incidents->count());
214
215
        $this->post(
216
            '/reports/mark_related_to/2',
217
            ['related_to' => 4]
218
        );
219
220
        $this->Reports->id = 2;
221
        $incidents = $this->Reports->getIncidents();
222
        $this->assertEquals(3, $incidents->count());
223
    }
224
225
    /**
226
     * @depends testMarkRelatedTo
227
     * @return void nothing
228
     */
229
    public function testUnmarkRelatedTo(): void
230
    {
231
        $this->testMarkRelatedTo();
232
        $this->Reports->id = 2;
233
        $incidents = $this->Reports->getIncidents();
234
        $this->assertEquals(3, $incidents->count());
235
236
        $this->post('/reports/unmark_related_to/2');
237
238
        $this->Reports->id = 2;
239
        $incidents = $this->Reports->getIncidents();
240
        $this->assertEquals(1, $incidents->count());
241
    }
242
243
    /**
244
     * Test for 'mass_action' action
245
     */
246
    public function testMassAction(): void
247
    {
248
        $report1 = $this->Reports->get(1);
249
        $this->assertEquals('forwarded', $report1->status);
250
251
        $report5 = $this->Reports->get(5);
252
        $this->assertEquals('new', $report5->status);
253
254
        /* Test case 1: Incorrect state */
255
        $this->post(
256
            '/reports/mass_action',
257
            [
258
                'reports' => [
259
                    '1',
260
                    '5',
261
                ],
262
                'state' => 'incorrect_state',
263
            ]
264
        );
265
266
        // Should not change
267
        $report1 = $this->Reports->get(1);
268
        $this->assertEquals('forwarded', $report1->status);
269
270
        $report5 = $this->Reports->get(5);
271
        $this->assertEquals('new', $report5->status);
272
273
        /* Test case 2: No reports selected */
274
        $this->post(
275
            '/reports/mass_action',
276
            [
277
                'reports' => [],
278
                'state' => 'resolved',
279
            ]
280
        );
281
282
        // Should not change
283
        $report1 = $this->Reports->get(1);
284
        $this->assertEquals('forwarded', $report1->status);
285
286
        $report5 = $this->Reports->get(5);
287
        $this->assertEquals('new', $report5->status);
288
289
        /* Test case 3: Invalid report id passed */
290
        $this->post(
291
            '/reports/mass_action',
292
            [
293
                'reports' => [10],
294
                'state' => 'resolved',
295
            ]
296
        );
297
298
        /* Test case 4 */
299
        $this->post(
300
            '/reports/mass_action',
301
            [
302
                'reports' => [
303
                    1,
304
                    5,
305
                ],
306
                'state' => 'resolved',
307
            ]
308
        );
309
310
        // Should change
311
        $report1 = $this->Reports->get(1);
312
        $this->assertEquals('resolved', $report1->status);
313
314
        $report5 = $this->Reports->get(5);
315
        $this->assertEquals('resolved', $report5->status);
316
    }
317
318
    /**
319
     * Test for 'change_state' action
320
     */
321
    public function testChangeState(): void
322
    {
323
        $this->session(['Developer.id' => 1, 'read_only' => false]);
324
325
        $report = $this->Reports->get(1);
326
        $this->assertEquals('forwarded', $report->status);
327
328
        /* Test case 1: Incorrect Report ID */
329
        $this->post(
330
            '/reports/change_state/6',
331
            ['state' => 'resolved']
332
        );
333
334
        /* Test case 2: Incorrect State */
335
        $this->post(
336
            '/reports/change_state/1',
337
            ['state' => 'incorrect_state']
338
        );
339
        $report = $this->Reports->get(1);
340
        $this->assertEquals('forwarded', $report->status);
341
342
        /* Test case 3 */
343
        $this->post(
344
            '/reports/change_state/1',
345
            ['state' => 'resolved']
346
        );
347
        $report = $this->Reports->get(1);
348
        $this->assertEquals('resolved', $report->status);
349
    }
350
}
351