Issues (404)

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
        ];
78
79
        $response = $ALERT->add($details, false, true);
80
81
        // We *should* get a return of 1
82
        $this->assertEquals(1, $response);
83
84
        // There is no way to get the last insert ID from the response itself.
85
        // Currently we trust that add() can spot its own errors.
86
        // TODO: Refactor add() so that component parts are more testable.
87
    }
88
89
    /**
90
     * Test that adding an already existing alert works as expected
91
     */
92
    public function testAddExisting() {
93
        $ALERT = new ALERT();
94
95
        $details = [
96
            'email' => '[email protected]',
97
            'keyword' => 'test3',
98
            'pc' => 'SW1A 1AA',
99
        ];
100
101
        $response = $ALERT->add($details, false, true);
102
103
        // We *should* get a return of -2
104
        $this->assertEquals(-2, $response);
105
106
        // There is no way to get the last insert ID from the response itself.
107
        // Currently we trust that add() can spot its own errors.
108
    }
109
110
    /**
111
     * Test that adding an already deleted alert works as expected
112
     */
113
    public function testAddDeleted() {
114
        $ALERT = new ALERT();
115
116
        $details = [
117
            'email' => '[email protected]',
118
            'keyword' => 'test6',
119
            'pc' => 'SW1A 1AA',
120
        ];
121
122
        $response = $ALERT->add($details, false, true);
123
124
        // We *should* get a return of 1
125
        $this->assertEquals(1, $response);
126
127
        // There is no way to get the last insert ID from the response itself.
128
        // Currently we trust that add() can spot its own errors.
129
    }
130
131
    /**
132
     * Test that email_exists() returns true for correct emails
133
     */
134
    public function testEmailExistsPositive() {
135
        $ALERT = new ALERT();
136
137
        $response = $ALERT->email_exists('[email protected]');
138
139
        $this->assertEquals(true, $response);
140
    }
141
142
    /**
143
     * Test that email_exists() returns false for incorrect emails
144
     */
145
    public function testEmailExistsNegative() {
146
        $ALERT = new ALERT();
147
148
        $response = $ALERT->email_exists('[email protected]');
149
150
        $this->assertEquals(false, $response);
151
    }
152
153
    /**
154
     * Test that a correct token will pass
155
     */
156
    public function testCheckTokenCorrect() {
157
        $ALERT = new ALERT();
158
159
        $response = $ALERT->check_token('1::token1');
160
161
        $this->assertEquals([
162
            'id' => 1,
163
            'email' => '[email protected]',
164
            'criteria' => 'test1',
165
        ], $response);
166
    }
167
168
    /**
169
     * Test that an incorrect token (wrong token for the alert ID) will fail
170
     */
171
    public function testCheckTokenIncorrectToken() {
172
        $ALERT = new ALERT();
173
174
        $response = $ALERT->check_token('1::token2');
175
176
        $this->assertEquals(false, $response);
177
    }
178
179
    /**
180
     * Test that an incorrect token (wrong parts count) will fail
181
     */
182
    public function testCheckTokenWrongPartsCount() {
183
        $ALERT = new ALERT();
184
185
        $response = $ALERT->check_token('foo');
186
187
        $this->assertEquals(false, $response);
188
    }
189
190
    /**
191
     * Test that an incorrect token (non-numeric alert ID) will fail
192
     */
193
    public function testCheckTokenNonNumericId() {
194
        $ALERT = new ALERT();
195
196
        $response = $ALERT->check_token('one:token1');
197
198
        $this->assertEquals(false, $response);
199
    }
200
201
    /**
202
     * Test that fetching alerts for an MP succeeds
203
     */
204
    public function testCheckFetchByMpExists() {
205
        $ALERT = new ALERT();
206
207
        $response = $ALERT->fetch_by_mp('[email protected]', 1234);
208
209
        $this->assertEquals(true, $response);
210
    }
211
212
    /**
213
     * Test that fetching alerts for an MP which doesn't exist fails
214
     */
215
    public function testCheckFetchByMpNotExists() {
216
        $ALERT = new ALERT();
217
218
        $response = $ALERT->fetch_by_mp('[email protected]', 9876);
219
220
        $this->assertEquals(false, $response);
221
    }
222
223
    /**
224
     * Test that confirming an alert with a valid token succeeds
225
     */
226
    public function testConfirm() {
227
        $ALERT = new ALERT();
228
229
        $response = $ALERT->confirm('1::token1');
230
231
        $this->assertEquals(true, $response);
232
233
        // TODO: Check that this really does delete the right alert as expected
234
    }
235
236
    /**
237
     * Test that confirming an alert with an invalid token succeeds
238
     */
239
    public function testConfirmInvalid() {
240
        $ALERT = new ALERT();
241
242
        $response = $ALERT->confirm('1::badtoken');
243
244
        $this->assertEquals(false, $response);
245
    }
246
247
    /**
248
     * Test that we can delete an alert
249
     */
250
    public function testDelete() {
251
        $ALERT = new ALERT();
252
253
        $response = $ALERT->delete('1::token1');
254
255
        $this->assertEquals(true, $response);
256
257
        // TODO: Check that this really does delete the right alert as expected
258
    }
259
260
    /**
261
     * Test that we can't delete an alert with a bad token
262
     */
263
    public function testDeleteInvalid() {
264
        $ALERT = new ALERT();
265
266
        $response = $ALERT->delete('1::badtoken');
267
268
        $this->assertEquals(false, $response);
269
    }
270
271
    /**
272
     * Test that we can suspend an alert
273
     */
274
    public function testSuspend() {
275
        $ALERT = new ALERT();
276
277
        $response = $ALERT->suspend('3::token3');
278
279
        $this->assertEquals(true, $response);
280
281
        // TODO: Check that this really does suspend the right alert as expected
282
    }
283
284
    /**
285
     * Test that we can't suspend an alert with a bad token
286
     */
287
    public function testSuspendInvalid() {
288
        $ALERT = new ALERT();
289
290
        $response = $ALERT->suspend('3::badtoken');
291
292
        $this->assertEquals(false, $response);
293
    }
294
295
    /**
296
     * Test that we can resume an alert
297
     */
298
    public function testResume() {
299
        $ALERT = new ALERT();
300
301
        $response = $ALERT->resume('6::token6');
302
303
        $this->assertEquals(true, $response);
304
305
        // TODO: Check that this really does resume the right alert as expected
306
    }
307
308
    /**
309
     * Test that we can't delete an alert with a bad token
310
     */
311
    public function testResumeInvalid() {
312
        $ALERT = new ALERT();
313
314
        $response = $ALERT->resume('6::badtoken');
315
316
        $this->assertEquals(false, $response);
317
    }
318
319
}
320