Completed
Push — master ( 7c757d...667317 )
by William
02:47
created

ReportsControllerTest::_testUnmarkRelatedTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 11
Ratio 91.67 %

Importance

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