Completed
Push — master ( 89830f...07e3a8 )
by William
20:06
created

ReportsControllerTest::testMarkRelatedTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 14
rs 9.9666
c 1
b 0
f 0
1
<?php
2
3
namespace App\Test\TestCase\Controller;
4
5
use Cake\ORM\TableRegistry;
6
use Cake\TestSuite\IntegrationTestCase;
7
use function count;
8
use function json_decode;
9
10
class ReportsControllerTest extends IntegrationTestCase
0 ignored issues
show
Deprecated Code introduced by
The class Cake\TestSuite\IntegrationTestCase has been deprecated: 3.7.0 Use Cake\TestSuite\IntegrationTestTrait instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

10
class ReportsControllerTest extends /** @scrutinizer ignore-deprecated */ IntegrationTestCase
Loading history...
11
{
12
    /**
13
     * Fixtures.
14
     *
15
     * @var array
16
     */
17
    public $fixtures = [
18
        'app.Notifications',
19
        'app.Developers',
20
        'app.Reports',
21
        'app.Incidents',
22
    ];
23
24
    public function setUp(): void
25
    {
26
        $this->Reports = TableRegistry::getTableLocator()->get('Reports');
0 ignored issues
show
Bug Best Practice introduced by
The property Reports does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
27
        $this->session(['Developer.id' => 1]);
28
    }
29
30
    public function testIndex(): void
31
    {
32
        $this->get('/reports');
33
        $this->assertEquals(['3.8', '4.0'], $this->viewVariable('distinct_versions'));
34
        $this->assertEquals(['forwarded', 'new'], $this->viewVariable('distinct_statuses'));
35
        $this->assertEquals(
36
            [
37
                'error1',
38
                'error2',
39
            ],
40
            $this->viewVariable('distinct_error_names')
41
        );
42
    }
43
44
    public function testView(): void
45
    {
46
        $this->get('/reports/view/1');
47
48
        $this->assertEquals(1, $this->viewVariable('report')[0]['id']);
49
        $this->assertEquals('error2', $this->viewVariable('report')[0]['error_name']);
50
51
        $this->assertNotEmpty($this->viewVariable('project_name'));
52
        $this->assertNotEmpty($this->viewVariable('columns'));
53
54
        $this->assertNotEmpty($this->viewVariable('related_entries'));
55
        $this->assertEquals(
56
            count($this->viewVariable('columns')),
57
            count($this->viewVariable('related_entries'))
58
        );
59
60
        foreach ($this->viewVariable('columns') as $column) {
61
            $this->assertNotEmpty($this->viewVariable("${column}_distinct_count"));
62
        }
63
64
        $this->assertNotEmpty($this->viewVariable('incidents'));
65
        $this->assertEquals(1, count($this->viewVariable('incidents')));
66
67
        $this->assertNotEmpty($this->viewVariable('incidents_with_description'));
68
        $this->assertEquals(1, count($this->viewVariable('incidents_with_description')->toList()));
69
70
        $this->assertNotEmpty($this->viewVariable('incidents_with_stacktrace'));
71
        $this->assertEquals(1, count($this->viewVariable('incidents_with_stacktrace')->toList()));
72
73
        $this->assertNotEmpty($this->viewVariable('related_reports'));
74
        //FIXME: 0 or 1 ?
75
        //$this->assertEquals(1, count($this->viewVariable('related_reports')->toList()));
76
77
        $this->get('/reports/view/3');
78
        $this->assertResponseContains('Invalid Report');
79
        $this->assertResponseContains('/reports/view/3');
80
    }
81
82
    public function testDataTables(): void
83
    {
84
        $this->get('/reports/data_tables?sEcho=1&iDisplayLength=25');
85
        $expected = [
86
            'iTotalRecords' => 4,
87
            'iTotalDisplayRecords' => 4,
88
            'sEcho' => 1,
89
            'aaData' => [
90
                [
91
                    "<input type='checkbox' name='reports[]' value='1'/>",
92
                    1,
93
                    'error2',
94
                    'Lorem ipsum dolor sit amet',
95
                    'filename_1.php',
96
                    '4.0',
97
                    'Forwarded',
98
                    'js',
99
                    '1',
100
                ],
101
                [
102
                    "<input type='checkbox' name='reports[]' value='2'/>",
103
                    2,
104
                    'error2',
105
                    'Lorem ipsum dolor sit amet',
106
                    'filename_2.php',
107
                    '4.0',
108
                    'Forwarded',
109
                    'js',
110
                    '1',
111
                ],
112
                [
113
                    "<input type='checkbox' name='reports[]' value='4'/>",
114
                    4,
115
                    'error1',
116
                    'Lorem ipsum dolor sit amet',
117
                    'filename_3.js',
118
                    '3.8',
119
                    'Forwarded',
120
                    'js',
121
                    '2',
122
                ],
123
                [
124
                    "<input type='checkbox' name='reports[]' value='5'/>",
125
                    5,
126
                    'error1',
127
                    'Lorem ipsum dolor sit amet',
128
                    'filename_4.js',
129
                    '3.8',
130
                    'New',
131
                    'js',
132
                    '1',
133
                ],
134
            ],
135
        ];
136
        $this->assertEquals($expected, json_decode($this->_response->body(), true));
0 ignored issues
show
Deprecated Code introduced by
The function Cake\Http\Response::body() has been deprecated: 3.4.0 Mutable response methods are deprecated. Use `withBody()`/`withStringBody()` and `getBody()` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

136
        $this->assertEquals($expected, json_decode(/** @scrutinizer ignore-deprecated */ $this->_response->body(), true));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug introduced by
The method body() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

136
        $this->assertEquals($expected, json_decode($this->_response->/** @scrutinizer ignore-call */ body(), true));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
137
138
        $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');
139
        $expected = [
140
            'iTotalRecords' => 4,
141
            'iTotalDisplayRecords' => 2,
142
            'sEcho' => 1,
143
            'aaData' => [
144
                [
145
                    "<input type='checkbox' name='reports[]' value='1'/>",
146
                    1,
147
                    'error2',
148
                    'Lorem ipsum dolor sit amet',
149
                    'filename_1.php',
150
                    '4.0',
151
                    'Forwarded',
152
                    'js',
153
                    '1',
154
                ],
155
                [
156
                    "<input type='checkbox' name='reports[]' value='2'/>",
157
                    2,
158
                    'error2',
159
                    'Lorem ipsum dolor sit amet',
160
                    'filename_2.php',
161
                    '4.0',
162
                    'Forwarded',
163
                    'js',
164
                    '1',
165
                ],
166
            ],
167
        ];
168
        $result = json_decode($this->_response->body(), true);
0 ignored issues
show
Deprecated Code introduced by
The function Cake\Http\Response::body() has been deprecated: 3.4.0 Mutable response methods are deprecated. Use `withBody()`/`withStringBody()` and `getBody()` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

168
        $result = json_decode(/** @scrutinizer ignore-deprecated */ $this->_response->body(), true);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
169
        $this->assertEquals($expected, $result);
170
171
        $this->get('/reports/data_tables?sEcho=1&sSearch_1=1&iDisplayLength=25');
172
        $expected = [
173
            'iTotalRecords' => 4,
174
            'iTotalDisplayRecords' => 1,
175
            'sEcho' => 1,
176
            'aaData' => [
177
                [
178
                    "<input type='checkbox' name='reports[]' value='1'/>",
179
                    1,
180
                    'error2',
181
                    'Lorem ipsum dolor sit amet',
182
                    'filename_1.php',
183
                    '4.0',
184
                    'Forwarded',
185
                    'js',
186
                    '1',
187
                ],
188
            ],
189
        ];
190
        $result = json_decode($this->_response->body(), true);
0 ignored issues
show
Deprecated Code introduced by
The function Cake\Http\Response::body() has been deprecated: 3.4.0 Mutable response methods are deprecated. Use `withBody()`/`withStringBody()` and `getBody()` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

190
        $result = json_decode(/** @scrutinizer ignore-deprecated */ $this->_response->body(), true);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
191
        $this->assertEquals($expected, $result);
192
193
        $this->get('/reports/data_tables?sEcho=1&sSearch_1=0&iDisplayLength=25');
194
        $expected = [
195
            'iTotalRecords' => 4,
196
            'iTotalDisplayRecords' => 0,
197
            'sEcho' => 1,
198
            'aaData' => [],
199
        ];
200
        $result = json_decode($this->_response->body(), true);
0 ignored issues
show
Deprecated Code introduced by
The function Cake\Http\Response::body() has been deprecated: 3.4.0 Mutable response methods are deprecated. Use `withBody()`/`withStringBody()` and `getBody()` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

200
        $result = json_decode(/** @scrutinizer ignore-deprecated */ $this->_response->body(), true);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
201
        $this->assertEquals($expected, $result);
202
    }
203
204
    public function testMarkRelatedTo(): void
205
    {
206
        $this->Reports->id = 2;
207
        $incidents = $this->Reports->getIncidents();
208
        $this->assertEquals(1, $incidents->count());
209
210
        $this->post(
211
            '/reports/mark_related_to/2',
212
            ['related_to' => 4]
213
        );
214
215
        $this->Reports->id = 2;
216
        $incidents = $this->Reports->getIncidents();
217
        $this->assertEquals(3, $incidents->count());
218
    }
219
220
    /**
221
     * @depends testMarkRelatedTo
222
     * @return void nothing
223
     */
224
    public function testUnmarkRelatedTo(): void
225
    {
226
        $this->testMarkRelatedTo();
227
        $this->Reports->id = 2;
228
        $incidents = $this->Reports->getIncidents();
229
        $this->assertEquals(3, $incidents->count());
230
231
        $this->post('/reports/unmark_related_to/2');
232
233
        $this->Reports->id = 2;
234
        $incidents = $this->Reports->getIncidents();
235
        $this->assertEquals(1, $incidents->count());
236
    }
237
238
    /**
239
     * Test for 'mass_action' action
240
     */
241
    public function testMassAction(): void
242
    {
243
        $report1 = $this->Reports->get(1);
244
        $this->assertEquals('forwarded', $report1->status);
245
246
        $report5 = $this->Reports->get(5);
247
        $this->assertEquals('new', $report5->status);
248
249
        /* Test case 1: Incorrect state */
250
        $this->post(
251
            '/reports/mass_action',
252
            [
253
                'reports' => [
254
                    '1',
255
                    '5',
256
                ],
257
                'state' => 'incorrect_state',
258
            ]
259
        );
260
261
        // Should not change
262
        $report1 = $this->Reports->get(1);
263
        $this->assertEquals('forwarded', $report1->status);
264
265
        $report5 = $this->Reports->get(5);
266
        $this->assertEquals('new', $report5->status);
267
268
        /* Test case 2: No reports selected */
269
        $this->post(
270
            '/reports/mass_action',
271
            [
272
                'reports' => [],
273
                'state' => 'resolved',
274
            ]
275
        );
276
277
        // Should not change
278
        $report1 = $this->Reports->get(1);
279
        $this->assertEquals('forwarded', $report1->status);
280
281
        $report5 = $this->Reports->get(5);
282
        $this->assertEquals('new', $report5->status);
283
284
        /* Test case 3: Invalid report id passed */
285
        $this->post(
286
            '/reports/mass_action',
287
            [
288
                'reports' => [10],
289
                'state' => 'resolved',
290
            ]
291
        );
292
293
        /* Test case 4 */
294
        $this->post(
295
            '/reports/mass_action',
296
            [
297
                'reports' => [
298
                    1,
299
                    5,
300
                ],
301
                'state' => 'resolved',
302
            ]
303
        );
304
305
        // Should change
306
        $report1 = $this->Reports->get(1);
307
        $this->assertEquals('resolved', $report1->status);
308
309
        $report5 = $this->Reports->get(5);
310
        $this->assertEquals('resolved', $report5->status);
311
    }
312
313
    /**
314
     * Test for 'change_state' action
315
     */
316
    public function testChangeState(): void
317
    {
318
        $this->session(['Developer.id' => 1, 'read_only' => false]);
319
320
        $report = $this->Reports->get(1);
321
        $this->assertEquals('forwarded', $report->status);
322
323
        /* Test case 1: Incorrect Report ID */
324
        $this->post(
325
            '/reports/change_state/6',
326
            ['state' => 'resolved']
327
        );
328
329
        /* Test case 2: Incorrect State */
330
        $this->post(
331
            '/reports/change_state/1',
332
            ['state' => 'incorrect_state']
333
        );
334
        $report = $this->Reports->get(1);
335
        $this->assertEquals('forwarded', $report->status);
336
337
        /* Test case 3 */
338
        $this->post(
339
            '/reports/change_state/1',
340
            ['state' => 'resolved']
341
        );
342
        $report = $this->Reports->get(1);
343
        $this->assertEquals('resolved', $report->status);
344
    }
345
}
346