Skip to content

Commit 3e430bb

Browse files
committed
Fix reports controller test
1 parent d95f8c5 commit 3e430bb

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

tests/TestCase/Controller/ReportsControllerTest.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Test\Fixture\IncidentsFixture;
88
use App\Test\Fixture\NotificationsFixture;
99
use App\Test\Fixture\ReportsFixture;
10+
use Cake\Http\TestSuite\HttpClientTrait;
1011
use Cake\ORM\TableRegistry;
1112
use Cake\TestSuite\IntegrationTestTrait;
1213
use Cake\TestSuite\TestCase;
@@ -17,6 +18,7 @@
1718
class ReportsControllerTest extends TestCase
1819
{
1920
use IntegrationTestTrait;
21+
use HttpClientTrait;
2022

2123
protected ReportsTable $Reports;
2224

@@ -35,11 +37,13 @@ public function setUp(): void
3537
parent::setUp();
3638
$this->Reports = TableRegistry::getTableLocator()->get('Reports');
3739
$this->session(['Developer.id' => 1]);
40+
$this->enableCsrfToken();
3841
}
3942

4043
public function testIndex(): void
4144
{
4245
$this->get('/reports');
46+
$this->assertResponseCode(200);
4347
$this->assertEquals(['3.8', '4.0'], $this->viewVariable('distinct_versions'));
4448
$this->assertEquals(['forwarded', 'new'], $this->viewVariable('distinct_statuses'));
4549
$this->assertEquals(
@@ -54,6 +58,7 @@ public function testIndex(): void
5458
public function testView(): void
5559
{
5660
$this->get('/reports/view/1');
61+
$this->assertResponseCode(200);
5762
$report = $this->viewVariable('report');
5863
$this->assertNotEmpty($report);
5964
$this->assertEquals(1, $report[0]['id']);
@@ -76,23 +81,25 @@ public function testView(): void
7681
$this->assertEquals(1, count($this->viewVariable('incidents')));
7782

7883
$this->assertNotEmpty($this->viewVariable('incidents_with_description'));
79-
$this->assertEquals(1, count($this->viewVariable('incidents_with_description')->toList()));
84+
$this->assertEquals(1, count($this->viewVariable('incidents_with_description')->toArray()));
8085

8186
$this->assertNotEmpty($this->viewVariable('incidents_with_stacktrace'));
82-
$this->assertEquals(1, count($this->viewVariable('incidents_with_stacktrace')->toList()));
87+
$this->assertEquals(1, count($this->viewVariable('incidents_with_stacktrace')->toArray()));
8388

84-
$this->assertNotEmpty($this->viewVariable('related_reports'));
85-
//FIXME: 0 or 1 ?
86-
//$this->assertEquals(1, count($this->viewVariable('related_reports')->toList()));
89+
// Change the two assertions when "TODO: fix related to" is done
90+
$this->assertEquals(0, count($this->viewVariable('related_reports')->toArray()));
91+
$this->assertEmpty($this->viewVariable('related_reports'));
8792

8893
$this->get('/reports/view/3');
94+
$this->assertResponseCode(404);
8995
$this->assertResponseContains('The report does not exist.');
9096
$this->assertResponseContains('/reports/view/3');
9197
}
9298

9399
public function testDataTables(): void
94100
{
95101
$this->get('/reports/data_tables?sEcho=1&iDisplayLength=25');
102+
$this->assertResponseCode(200);
96103
$expected = [
97104
'iTotalRecords' => 4,
98105
'iTotalDisplayRecords' => 4,
@@ -147,6 +154,7 @@ public function testDataTables(): void
147154
$this->assertEquals($expected, json_decode($this->_response->getBody(), true));
148155

149156
$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');
157+
$this->assertResponseCode(200);
150158
$expected = [
151159
'iTotalRecords' => 4,
152160
'iTotalDisplayRecords' => 2,
@@ -180,6 +188,7 @@ public function testDataTables(): void
180188
$this->assertEquals($expected, $result);
181189

182190
$this->get('/reports/data_tables?sEcho=1&sSearch_1=1&iDisplayLength=25');
191+
$this->assertResponseCode(200);
183192
$expected = [
184193
'iTotalRecords' => 4,
185194
'iTotalDisplayRecords' => 1,
@@ -202,6 +211,7 @@ public function testDataTables(): void
202211
$this->assertEquals($expected, $result);
203212

204213
$this->get('/reports/data_tables?sEcho=1&sSearch_1=0&iDisplayLength=25');
214+
$this->assertResponseCode(200);
205215
$expected = [
206216
'iTotalRecords' => 4,
207217
'iTotalDisplayRecords' => 0,
@@ -222,16 +232,13 @@ public function testMarkRelatedTo(): void
222232
'/reports/mark_related_to/2',
223233
['related_to' => 4]
224234
);
235+
$this->assertResponseCode(302);
225236

226237
$this->Reports->id = 2;
227238
$incidents = $this->Reports->getIncidents();
228239
$this->assertEquals(3, $incidents->count());
229240
}
230241

231-
/**
232-
* @depends testMarkRelatedTo
233-
* @return void nothing
234-
*/
235242
public function testUnmarkRelatedTo(): void
236243
{
237244
$this->testMarkRelatedTo();
@@ -240,6 +247,7 @@ public function testUnmarkRelatedTo(): void
240247
$this->assertEquals(3, $incidents->count());
241248

242249
$this->post('/reports/unmark_related_to/2');
250+
$this->assertResponseCode(302);
243251

244252
$this->Reports->id = 2;
245253
$incidents = $this->Reports->getIncidents();
@@ -268,6 +276,7 @@ public function testMassAction(): void
268276
'state' => 'incorrect_state',
269277
]
270278
);
279+
$this->assertResponseCode(302);
271280

272281
// Should not change
273282
$report1 = $this->Reports->get(1);
@@ -284,6 +293,7 @@ public function testMassAction(): void
284293
'state' => 'resolved',
285294
]
286295
);
296+
$this->assertResponseCode(302);
287297

288298
// Should not change
289299
$report1 = $this->Reports->get(1);
@@ -300,6 +310,7 @@ public function testMassAction(): void
300310
'state' => 'resolved',
301311
]
302312
);
313+
$this->assertResponseCode(404);
303314

304315
/* Test case 4 */
305316
$this->post(
@@ -312,6 +323,7 @@ public function testMassAction(): void
312323
'state' => 'resolved',
313324
]
314325
);
326+
$this->assertResponseCode(302);
315327

316328
// Should change
317329
$report1 = $this->Reports->get(1);
@@ -336,12 +348,14 @@ public function testChangeState(): void
336348
'/reports/change_state/6',
337349
['state' => 'resolved']
338350
);
351+
$this->assertResponseCode(404);
339352

340353
/* Test case 2: Incorrect State */
341354
$this->post(
342355
'/reports/change_state/1',
343356
['state' => 'incorrect_state']
344357
);
358+
$this->assertResponseCode(404);
345359
$report = $this->Reports->get(1);
346360
$this->assertEquals('forwarded', $report->status);
347361

@@ -350,6 +364,7 @@ public function testChangeState(): void
350364
'/reports/change_state/1',
351365
['state' => 'resolved']
352366
);
367+
$this->assertResponseCode(302);
353368
$report = $this->Reports->get(1);
354369
$this->assertEquals('resolved', $report->status);
355370
}

0 commit comments

Comments
 (0)