1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Test\TestCase\Controller; |
4
|
|
|
|
5
|
|
|
use Cake\ORM\TableRegistry; |
6
|
|
|
use Cake\TestSuite\IntegrationTestCase; |
7
|
|
|
use Cake\Network\Exception\NotFoundException; |
8
|
|
|
|
9
|
|
|
class ReportsControllerTest extends IntegrationTestCase |
10
|
|
|
{ |
11
|
|
|
public $fixtures = array( |
12
|
|
|
'app.notifications', |
13
|
|
|
'app.developers', |
14
|
|
|
'app.reports', |
15
|
|
|
'app.incidents', |
16
|
|
|
); |
17
|
|
|
|
18
|
|
|
public function setUp() |
19
|
|
|
{ |
20
|
|
|
$this->Reports = TableRegistry::get('Reports'); |
|
|
|
|
21
|
|
|
$this->session(array('Developer.id' => 1)); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testIndex() |
25
|
|
|
{ |
26
|
|
|
$this->get('/reports'); |
27
|
|
|
$this->assertEquals(array('3.8', '4.0'), $this->viewVariable('distinct_versions')); |
28
|
|
|
$this->assertEquals(array('forwarded', 'new'), $this->viewVariable('distinct_statuses')); |
29
|
|
|
$this->assertEquals(array('error1', 'error2'), |
30
|
|
|
$this->viewVariable('distinct_error_names')); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testView() |
34
|
|
|
{ |
35
|
|
|
$this->get('/reports/view/1'); |
36
|
|
|
|
37
|
|
|
$this->assertEquals(1, $this->viewVariable('report')[0]['id']); |
38
|
|
|
$this->assertEquals('error2', $this->viewVariable('report')[0]['error_name']); |
39
|
|
|
|
40
|
|
|
$this->assertNotEmpty($this->viewVariable('project_name')); |
41
|
|
|
$this->assertNotEmpty($this->viewVariable('columns')); |
42
|
|
|
|
43
|
|
|
$this->assertNotEmpty($this->viewVariable('related_entries')); |
44
|
|
|
$this->assertEquals(count($this->viewVariable('columns')), |
45
|
|
|
count($this->viewVariable('related_entries'))); |
46
|
|
|
|
47
|
|
|
foreach ($this->viewVariable('columns') as $column) { |
48
|
|
|
$this->assertNotEmpty($this->viewVariable("${column}_distinct_count")); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->assertNotEmpty($this->viewVariable('incidents')); |
52
|
|
|
$this->assertEquals(1, count($this->viewVariable('incidents'))); |
53
|
|
|
|
54
|
|
|
$this->assertNotEmpty($this->viewVariable('incidents_with_description')); |
55
|
|
|
$this->assertEquals(1, count($this->viewVariable('incidents_with_description'))); |
56
|
|
|
|
57
|
|
|
$this->assertNotEmpty($this->viewVariable('incidents_with_stacktrace')); |
58
|
|
|
$this->assertEquals(1, count($this->viewVariable('incidents_with_stacktrace'))); |
59
|
|
|
|
60
|
|
|
$this->assertNotEmpty($this->viewVariable('related_reports')); |
61
|
|
|
$this->assertEquals(1, count($this->viewVariable('related_reports'))); |
62
|
|
|
|
63
|
|
|
$this->get('/reports/view/3'); |
64
|
|
|
$this->assertResponseContains('Invalid Report'); |
65
|
|
|
$this->assertResponseContains('/reports/view/3'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testDataTables() |
69
|
|
|
{ |
70
|
|
|
$this->get('/reports/data_tables?sEcho=1&iDisplayLength=25'); |
71
|
|
|
$expected = array( |
72
|
|
|
'iTotalRecords' => 4, |
73
|
|
|
'iTotalDisplayRecords' => 4, |
74
|
|
|
'sEcho' => 1, |
75
|
|
|
'aaData' => array( |
76
|
|
|
array("<input type='checkbox' name='reports[]' value='1'/>", 1, 'error2', 'Lorem ipsum dolor sit amet', 'filename_1.php', '4.0', 'Forwarded', 'js', '1'), |
77
|
|
|
array("<input type='checkbox' name='reports[]' value='2'/>", 2, 'error2', 'Lorem ipsum dolor sit amet', 'filename_2.php', '4.0', 'Forwarded', 'js', '1'), |
78
|
|
|
array("<input type='checkbox' name='reports[]' value='4'/>", 4, 'error1', 'Lorem ipsum dolor sit amet', 'filename_3.js', '3.8', 'Forwarded', 'js', '2'), |
79
|
|
|
array("<input type='checkbox' name='reports[]' value='5'/>", 5, 'error1', 'Lorem ipsum dolor sit amet', 'filename_4.js', '3.8', 'New', 'js', '1') |
80
|
|
|
), |
81
|
|
|
); |
82
|
|
|
$this->assertEquals($expected, json_decode($this->_response->body(), true)); |
83
|
|
|
|
84
|
|
|
$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'); |
85
|
|
|
$expected = array( |
86
|
|
|
'iTotalRecords' => 4, |
87
|
|
|
'iTotalDisplayRecords' => 2, |
88
|
|
|
'sEcho' => 1, |
89
|
|
|
'aaData' => array( |
90
|
|
|
array("<input type='checkbox' name='reports[]' value='1'/>", 1, 'error2', 'Lorem ipsum dolor sit amet', 'filename_1.php', '4.0', 'Forwarded', 'js', '1'), |
91
|
|
|
array("<input type='checkbox' name='reports[]' value='2'/>", 2, 'error2', 'Lorem ipsum dolor sit amet', 'filename_2.php', '4.0', 'Forwarded', 'js', '1'), |
92
|
|
|
), |
93
|
|
|
); |
94
|
|
|
$result = json_decode($this->_response->body(), true); |
95
|
|
|
$this->assertEquals($expected, $result); |
96
|
|
|
|
97
|
|
|
$this->get('/reports/data_tables?sEcho=1&sSearch_1=1&iDisplayLength=25'); |
98
|
|
|
$expected = array( |
99
|
|
|
'iTotalRecords' => 4, |
100
|
|
|
'iTotalDisplayRecords' => 1, |
101
|
|
|
'sEcho' => 1, |
102
|
|
|
'aaData' => array( |
103
|
|
|
array("<input type='checkbox' name='reports[]' value='1'/>", 1, 'error2', 'Lorem ipsum dolor sit amet', 'filename_1.php', '4.0', 'Forwarded', 'js', '1'), |
104
|
|
|
), |
105
|
|
|
); |
106
|
|
|
$result = json_decode($this->_response->body(), true); |
107
|
|
|
$this->assertEquals($expected, $result); |
108
|
|
|
|
109
|
|
|
$this->get('/reports/data_tables?sEcho=1&sSearch_1=error&iDisplayLength=25'); |
110
|
|
|
$expected = array( |
111
|
|
|
'iTotalRecords' => 4, |
112
|
|
|
'iTotalDisplayRecords' => 0, |
113
|
|
|
'sEcho' => 1, |
114
|
|
|
'aaData' => array( |
115
|
|
|
), |
116
|
|
|
); |
117
|
|
|
$result = json_decode($this->_response->body(), true); |
118
|
|
|
$this->assertEquals($expected, $result); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
View Code Duplication |
public function testMarkRelatedTo() { |
122
|
|
|
$this->Reports->id = 2; |
123
|
|
|
$incidents = $this->Reports->getIncidents(); |
124
|
|
|
$this->assertEquals(1, $incidents->count()); |
125
|
|
|
|
126
|
|
|
$this->post( |
127
|
|
|
'/reports/mark_related_to/2', |
128
|
|
|
['related_to' => 4] |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
$this->Reports->id = 2; |
132
|
|
|
$incidents = $this->Reports->getIncidents(); |
133
|
|
|
$this->assertEquals(3, $incidents->count()); |
134
|
|
|
|
135
|
|
|
$this->_testUnmarkRelatedTo(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Don't run this as a separate test, |
141
|
|
|
* as the fixture tables are re-created and |
142
|
|
|
* we lose the previous related_to updations. |
143
|
|
|
* |
144
|
|
|
* So, call at the end of testMarkRelatedTo() |
145
|
|
|
*/ |
146
|
|
View Code Duplication |
private function _testUnmarkRelatedTo() |
147
|
|
|
{ |
148
|
|
|
$this->Reports->id = 2; |
149
|
|
|
$incidents = $this->Reports->getIncidents(); |
150
|
|
|
$this->assertEquals(3, $incidents->count()); |
151
|
|
|
|
152
|
|
|
$this->post('/reports/unmark_related_to/2'); |
153
|
|
|
|
154
|
|
|
$this->Reports->id = 2; |
155
|
|
|
$incidents = $this->Reports->getIncidents(); |
156
|
|
|
$this->assertEquals(1, $incidents->count()); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Test for 'mass_action' action |
161
|
|
|
*/ |
162
|
|
|
public function testMassAction() |
163
|
|
|
{ |
164
|
|
|
$report1 = $this->Reports->get(1); |
165
|
|
|
$this->assertEquals('forwarded', $report1->status); |
166
|
|
|
|
167
|
|
|
$report5 = $this->Reports->get(5); |
168
|
|
|
$this->assertEquals('new', $report5->status); |
169
|
|
|
|
170
|
|
|
/* Test case 1: Incorrect state */ |
171
|
|
|
$this->post('/reports/mass_action', |
172
|
|
|
array('reports' => array('1', '5'), 'state' => 'incorrect_state') |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
// Should not change |
176
|
|
|
$report1 = $this->Reports->get(1); |
177
|
|
|
$this->assertEquals('forwarded', $report1->status); |
178
|
|
|
|
179
|
|
|
$report5 = $this->Reports->get(5); |
180
|
|
|
$this->assertEquals('new', $report5->status); |
181
|
|
|
|
182
|
|
|
/* Test case 2: No reports selected */ |
183
|
|
|
$this->post('/reports/mass_action', |
184
|
|
|
array('reports' => array(), 'state' => 'resolved') |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
// Should not change |
188
|
|
|
$report1 = $this->Reports->get(1); |
189
|
|
|
$this->assertEquals('forwarded', $report1->status); |
190
|
|
|
|
191
|
|
|
$report5 = $this->Reports->get(5); |
192
|
|
|
$this->assertEquals('new', $report5->status); |
193
|
|
|
|
194
|
|
|
/* Test case 3: Invalid report id passed */ |
195
|
|
|
$this->post('/reports/mass_action', |
196
|
|
|
array('reports' => array(10), 'state' => 'resolved') |
197
|
|
|
); |
198
|
|
|
|
199
|
|
|
/* Test case 4 */ |
200
|
|
|
$this->post('/reports/mass_action', |
201
|
|
|
array('reports' => array(1, 5), 'state' => 'resolved') |
202
|
|
|
); |
203
|
|
|
|
204
|
|
|
// Should change |
205
|
|
|
$report1 = $this->Reports->get(1); |
206
|
|
|
$this->assertEquals('resolved', $report1->status); |
207
|
|
|
|
208
|
|
|
$report5 = $this->Reports->get(5); |
209
|
|
|
$this->assertEquals('resolved', $report5->status); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Test for 'change_state' action |
214
|
|
|
*/ |
215
|
|
|
public function testChangeState() |
216
|
|
|
{ |
217
|
|
|
$this->session(array('Developer.id' => 1, 'read_only' => false)); |
218
|
|
|
|
219
|
|
|
$report = $this->Reports->get(1); |
220
|
|
|
$this->assertEquals('forwarded', $report->status); |
221
|
|
|
|
222
|
|
|
/* Test case 1: Incorrect Report ID */ |
223
|
|
|
$this->post('/reports/change_state/6', |
224
|
|
|
array('state' => 'resolved') |
225
|
|
|
); |
226
|
|
|
|
227
|
|
|
/* Test case 2: Incorrect State */ |
228
|
|
|
$this->post('/reports/change_state/1', |
229
|
|
|
array('state' => 'incorrect_state') |
230
|
|
|
); |
231
|
|
|
$report = $this->Reports->get(1); |
232
|
|
|
$this->assertEquals('forwarded', $report->status); |
233
|
|
|
|
234
|
|
|
/* Test case 3 */ |
235
|
|
|
$this->post('/reports/change_state/1', |
236
|
|
|
array('state' => 'resolved') |
237
|
|
|
); |
238
|
|
|
$report = $this->Reports->get(1); |
239
|
|
|
$this->assertEquals('resolved', $report->status); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: