Issues (422)

tests/AlertsTest.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * Provides test methods for alerts functionality.
5
 */
6
class AlertsTest extends TWFY_Database_TestCase {
7
    /**
8
     * Loads the alerts testing fixture.
9
     */
10
    public function getDataSet() {
11
        return $this->createMySQLXMLDataSet(dirname(__FILE__) . '/_fixtures/alerts.xml');
0 ignored issues
show
Are you sure the usage of $this->createMySQLXMLDat.../_fixtures/alerts.xml') targeting TWFY_Database_TestCase::createMySQLXMLDataSet() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
12
    }
13
14
    /**
15
     * Ensures the database is prepared and the alert class is included for every test.
16
     */
17
    public function setUp(): void {
18
        parent::setUp();
19
20
        include_once('www/includes/easyparliament/alert.php');
21
    }
22
23
    /**
24
     * Test that unconfirmed, undeleted tests are correctly retrieved
25
     */
26
    public function testFetchUnconfirmedUndeleted() {
27
        $ALERT = new ALERT();
28
29
        $response = $ALERT->fetch(0, 0);
30
31
        // Make sure we only get one response
32
        $this->assertEquals(1, count($response['data']));
33
34
        // Make sure the response has the correct attributes
35
        $this->assertEquals(0, $response['data'][0]['confirmed']);
36
        $this->assertEquals(0, $response['data'][0]['deleted']);
37
    }
38
39
    /**
40
     * Test that confirmed, undeleted tests are correctly retrieved
41
     */
42
    public function testFetchConfirmedUndeleted() {
43
        $ALERT = new ALERT();
44
45
        $response = $ALERT->fetch(1, 0);
46
47
        // Make sure we only get two responses
48
        $this->assertEquals(2, count($response['data']));
49
50
        // Make sure a response has the correct attributes
51
        $this->assertEquals(1, $response['data'][0]['confirmed']);
52
        $this->assertEquals(0, $response['data'][0]['deleted']);
53
    }
54
55
    /**
56
     * Test that the correct alerts between given dates are retrieved
57
     */
58
    public function testFetchBetween() {
59
        $ALERT = new ALERT();
60
61
        $response = $ALERT->fetch_between(1, 0, '2014-02-03', '2014-02-04');
62
63
        // Make sure we only get one response
64
        $this->assertEquals(1, count($response['alerts']));
65
    }
66
67
    /**
68
     * Test that alerts can be added
69
     */
70
    public function testAdd() {
71
        $ALERT = new ALERT();
72
73
        $details = [
74
            'email' => '[email protected]',
75
            'keyword' => 'test',
76
            'pc' => 'SW1A 1AA',
77
            'ignore_speaker_votes' => 0,
78
        ];
79
80
        $response = $ALERT->add($details, false, true);
81
82
        // We *should* get a return of 1
83
        $this->assertEquals(1, $response);
84
85
        // There is no way to get the last insert ID from the response itself.
86
        // Currently we trust that add() can spot its own errors.
87
        // TODO: Refactor add() so that component parts are more testable.
88
    }
89
90
    /**
91
     * Test that adding an already existing alert works as expected
92
     */
93
    public function testAddExisting() {
94
        $ALERT = new ALERT();
95
96
        $details = [
97
            'email' => '[email protected]',
98
            'keyword' => 'test3',
99
            'pc' => 'SW1A 1AA',
100
            'ignore_speaker_votes' => 0,
101
        ];
102
103
        $response = $ALERT->add($details, false, true);
104
105
        // We *should* get a return of -2
106
        $this->assertEquals(-2, $response);
107
108
        // There is no way to get the last insert ID from the response itself.
109
        // Currently we trust that add() can spot its own errors.
110
    }
111
112
    /**
113
     * Test that adding an already deleted alert works as expected
114
     */
115
    public function testAddDeleted() {
116
        $ALERT = new ALERT();
117
118
        $details = [
119
            'email' => '[email protected]',
120
            'keyword' => 'test6',
121
            'pc' => 'SW1A 1AA',
122
            'ignore_speaker_votes' => 0,
123
        ];
124
125
        $response = $ALERT->add($details, false, true);
126
127
        // We *should* get a return of 1
128
        $this->assertEquals(1, $response);
129
130
        // There is no way to get the last insert ID from the response itself.
131
        // Currently we trust that add() can spot its own errors.
132
    }
133
134
    /**
135
     * Test that email_exists() returns true for correct emails
136
     */
137
    public function testEmailExistsPositive() {
138
        $ALERT = new ALERT();
139
140
        $response = $ALERT->email_exists('[email protected]');
141
142
        $this->assertEquals(true, $response);
143
    }
144
145
    /**
146
     * Test that email_exists() returns false for incorrect emails
147
     */
148
    public function testEmailExistsNegative() {
149
        $ALERT = new ALERT();
150
151
        $response = $ALERT->email_exists('[email protected]');
152
153
        $this->assertEquals(false, $response);
154
    }
155
156
    /**
157
     * Test that a correct token will pass
158
     */
159
    public function testCheckTokenCorrect() {
160
        $ALERT = new ALERT();
161
162
        $response = $ALERT->check_token('1::token1');
163
164
        $this->assertEquals([
165
            'id' => 1,
166
            'email' => '[email protected]',
167
            'criteria' => 'test1',
168
            'ignore_speaker_votes' => '0',
169
        ], $response);
170
    }
171
172
    /**
173
     * Test that an incorrect token (wrong token for the alert ID) will fail
174
     */
175
    public function testCheckTokenIncorrectToken() {
176
        $ALERT = new ALERT();
177
178
        $response = $ALERT->check_token('1::token2');
179
180
        $this->assertEquals(false, $response);
181
    }
182
183
    /**
184
     * Test that an incorrect token (wrong parts count) will fail
185
     */
186
    public function testCheckTokenWrongPartsCount() {
187
        $ALERT = new ALERT();
188
189
        $response = $ALERT->check_token('foo');
190
191
        $this->assertEquals(false, $response);
192
    }
193
194
    /**
195
     * Test that an incorrect token (non-numeric alert ID) will fail
196
     */
197
    public function testCheckTokenNonNumericId() {
198
        $ALERT = new ALERT();
199
200
        $response = $ALERT->check_token('one:token1');
201
202
        $this->assertEquals(false, $response);
203
    }
204
205
    /**
206
     * Test that fetching alerts for an MP succeeds
207
     */
208
    public function testCheckFetchByMpExists() {
209
        $ALERT = new ALERT();
210
211
        $response = $ALERT->fetch_by_mp('[email protected]', 1234);
212
213
        $this->assertEquals(true, $response);
214
    }
215
216
    /**
217
     * Test that fetching alerts for an MP which doesn't exist fails
218
     */
219
    public function testCheckFetchByMpNotExists() {
220
        $ALERT = new ALERT();
221
222
        $response = $ALERT->fetch_by_mp('[email protected]', 9876);
223
224
        $this->assertEquals(false, $response);
225
    }
226
227
    /**
228
     * Test that confirming an alert with a valid token succeeds
229
     */
230
    public function testConfirm() {
231
        $ALERT = new ALERT();
232
233
        $response = $ALERT->confirm('1::token1');
234
235
        $this->assertEquals(true, $response);
236
237
        // TODO: Check that this really does delete the right alert as expected
238
    }
239
240
    /**
241
     * Test that confirming an alert with an invalid token succeeds
242
     */
243
    public function testConfirmInvalid() {
244
        $ALERT = new ALERT();
245
246
        $response = $ALERT->confirm('1::badtoken');
247
248
        $this->assertEquals(false, $response);
249
    }
250
251
    /**
252
     * Test that we can delete an alert
253
     */
254
    public function testDelete() {
255
        $ALERT = new ALERT();
256
257
        $response = $ALERT->delete('1::token1');
258
259
        $this->assertEquals(true, $response);
260
261
        // TODO: Check that this really does delete the right alert as expected
262
    }
263
264
    /**
265
     * Test that we can't delete an alert with a bad token
266
     */
267
    public function testDeleteInvalid() {
268
        $ALERT = new ALERT();
269
270
        $response = $ALERT->delete('1::badtoken');
271
272
        $this->assertEquals(false, $response);
273
    }
274
275
    /**
276
     * Test that we can suspend an alert
277
     */
278
    public function testSuspend() {
279
        $ALERT = new ALERT();
280
281
        $response = $ALERT->suspend('3::token3');
282
283
        $this->assertEquals(true, $response);
284
285
        // TODO: Check that this really does suspend the right alert as expected
286
    }
287
288
    /**
289
     * Test that we can't suspend an alert with a bad token
290
     */
291
    public function testSuspendInvalid() {
292
        $ALERT = new ALERT();
293
294
        $response = $ALERT->suspend('3::badtoken');
295
296
        $this->assertEquals(false, $response);
297
    }
298
299
    /**
300
     * Test that we can resume an alert
301
     */
302
    public function testResume() {
303
        $ALERT = new ALERT();
304
305
        $response = $ALERT->resume('6::token6');
306
307
        $this->assertEquals(true, $response);
308
309
        // TODO: Check that this really does resume the right alert as expected
310
    }
311
312
    /**
313
     * Test that we can't delete an alert with a bad token
314
     */
315
    public function testResumeInvalid() {
316
        $ALERT = new ALERT();
317
318
        $response = $ALERT->resume('6::badtoken');
319
320
        $this->assertEquals(false, $response);
321
    }
322
323
}
324