Issues (404)

tests/MemberTest.php (2 issues)

1
<?php
2
3
/**
4
 * Provides test methods for member functionality.
5
 */
6
class MemberTest extends TWFY_Database_TestCase {
7
    /**
8
     * Loads the member testing fixture.
9
     */
10
    public function getDataSet() {
11
        return $this->createMySQLXMLDataSet(dirname(__FILE__) . '/_fixtures/member.xml');
0 ignored issues
show
Are you sure the usage of $this->createMySQLXMLDat.../_fixtures/member.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 member class is included for every test.
16
     */
17
    public function setUp(): void {
18
        parent::setUp();
19
20
        include_once('www/includes/easyparliament/member.php');
21
    }
22
23
    /**
24
     * Test that a member is correctly retrieved by person ID alone.
25
     */
26
    public function testGetMPByPersonID() {
27
        $MEMBER = new MEMBER(['person_id' => 2]);
28
29
        $this->assertEquals(1, $MEMBER->member_id);
30
        $this->assertEquals(2, $MEMBER->person_id);
31
    }
32
33
    /**
34
     * Test that a member is correctly retrieved by member ID alone.
35
     */
36
    public function testGetMPByMemberID() {
37
        $MEMBER = new MEMBER(['member_id' => 1]);
38
39
        $this->assertEquals(1, $MEMBER->member_id);
40
        $this->assertEquals(2, $MEMBER->person_id);
41
    }
42
43
    /**
44
     * Test that a member is correctly retrieved by name.
45
     */
46
    public function testGetMPByName() {
47
        global $this_page;
48
        $this_page = 'mp';
49
50
        $MEMBER = new MEMBER(['name' => 'test current-mp']);
51
52
        $this->assertEquals(1, $MEMBER->member_id);
53
        $this->assertEquals(2, $MEMBER->person_id);
54
    }
55
56
    /**
57
     * Test that the current member is correctly retrieved by constituency.
58
     */
59
    public function testGetMPByConstituency() {
60
        $MEMBER = new MEMBER(['constituency' => 'test westminster constituency']);
61
62
        $this->assertEquals(1, $MEMBER->member_id);
63
        $this->assertEquals(2, $MEMBER->person_id);
64
    }
65
66
    /**
67
     * Test that a member is correctly retrieved by name and constituency.
68
     */
69
    public function testGetMPByNameAndConstituency() {
70
        global $this_page;
71
        $this_page = 'mp';
72
73
        $MEMBER = new MEMBER(['name' => 'test current-mp', 'constituency' => 'test westminster constituency']);
74
75
        $this->assertEquals(1, $MEMBER->member_id);
76
        $this->assertEquals(2, $MEMBER->person_id);
77
    }
78
79
    /**
80
     * Test that members with duplicate names are retrieved as expected.
81
     */
82
    public function testGetDuplicateMPsByName() {
83
        global $this_page;
84
        $this_page = 'mp';
85
86
        try {
87
            $MEMBER = new MEMBER(['name' => 'test duplicate-mp']);
0 ignored issues
show
The assignment to $MEMBER is dead and can be removed.
Loading history...
88
        } catch (MySociety\TheyWorkForYou\MemberMultipleException $e) {
89
            $this->assertEquals([11 => 'Test Westminster Constituency',
90
                10 => 'Test Westminster Constituency'], $e->ids);
91
            return;
92
        }
93
        $this->fail('Multiple member exception not raised');
94
    }
95
96
    /**
97
     * Test that MP URLs are generated correctly.
98
     */
99
    public function testGetMPURL() {
100
        $MEMBER = new MEMBER(['person_id' => 2]);
101
102
        $this->assertEquals('/mp/2/test_current-mp/test_westminster_constituency', $MEMBER->url());
103
    }
104
105
    /**
106
     * Test that MP URLs with special characters are generated correctly.
107
     *
108
     * Special characters in URLs *should* be encoded.
109
     */
110
    public function testGetMPSpecialCharacterURL() {
111
        $MEMBER = new MEMBER(['person_id' => 12]);
112
113
        $this->assertEquals('/mp/12/test_special-character-constituency/test_constituency%2C_comma', $MEMBER->url());
114
    }
115
116
    /**
117
     * Test that Peer URLs are generated correctly.
118
     */
119
    public function testGetPeerURL() {
120
        $MEMBER = new MEMBER(['person_id' => 3]);
121
122
        $this->assertEquals('/peer/3/mr_current-lord', $MEMBER->url());
123
    }
124
125
    /**
126
     * Test that MLA URLs are generated correctly.
127
     */
128
    public function testGetMLAURL() {
129
        $MEMBER = new MEMBER(['person_id' => 8]);
130
131
        $this->assertEquals('/mla/8/test_previous-mla', $MEMBER->url());
132
    }
133
134
    /**
135
     * Test that MSP URLs are generated correctly.
136
     */
137
    public function testGetMSPURL() {
138
        $MEMBER = new MEMBER(['person_id' => 5]);
139
140
        $this->assertEquals('/msp/5/test_current-msp', $MEMBER->url());
141
    }
142
143
    /**
144
     * Test that edge case URL for Elizabeth II is generated correctly.
145
     */
146
    public function testGetElizabethIIURL() {
147
        $MEMBER = new MEMBER(['person_id' => 13935]);
148
149
        $this->assertEquals('/royal/elizabeth_the_second', $MEMBER->url());
150
    }
151
152
    /**
153
     * Test that entered/left house strings are being generated
154
     */
155
    public function testEnteredLeftHouseString() {
156
        $MEMBER = new MySociety\TheyWorkForYou\Member(['person_id' => 9]);
157
158
        $this->assertEquals([
159
            "<strong>Entered the Scottish Parliament on  1 January 1990</strong> &mdash; General election",
160
            "<strong>Left the Scottish Parliament on 31 December 1999</strong> &mdash; General election",
161
        ], $MEMBER->getEnterLeaveStrings());
162
    }
163
164
    /**
165
     * Test loading extra info
166
     *
167
     * @todo Implement testing of the Public Whip info loading
168
     */
169
    public function testLoadExtraInfo() {
170
        $MEMBER = new MEMBER(['person_id' => 16]);
171
172
        $MEMBER->load_extra_info();
173
174
        // Have we correctly loaded the office?
175
        $this->assertEquals(1, $MEMBER->extra_info['office'][0]['moffice_id']);
176
177
        // Have we correctly loaded the member arbitrary key/value pair?
178
        $this->assertEquals('Test Member Value', $MEMBER->extra_info['test_member_key']);
179
180
        // Have we correctly loaded the person arbitrary key/value pair?
181
        $this->assertEquals('Test Person Value', $MEMBER->extra_info['test_person_key']);
182
183
        // Have we correctly loaded the constituency arbitrary key/value pair?
184
        $this->assertEquals('Test Constituency Value', $MEMBER->extra_info['test_constituency_key']);
185
186
        // Have we correctly loaded the PBC membership?
187
        $this->assertEquals([
188
            'title' => 'Test Bill',
189
            'session' => '2013-14',
190
            'attending' => 1,
191
            'chairman' => 1,
192
            'outof' => 0,
193
        ], $MEMBER->extra_info['pbc'][1]);
194
    }
195
196
    /**
197
     * Test to ensure Utility\Member::findMemberImage() works as expected
198
     */
199
    public function testFindMemberImage() {
200
201
        // A missing image with no backup should reply null/null
202
        $this->assertEquals(
203
            [null, null],
204
            \MySociety\TheyWorkForYou\Utility\Member::findMemberImage(1)
205
        );
206
207
        // Missing, small, use default
208
        $this->assertEquals(
209
            ['/images/unknownperson_large.png', 'L'],
210
            \MySociety\TheyWorkForYou\Utility\Member::findMemberImage(1, false, true)
211
        );
212
213
        // Missing, large, use default Lord
214
        $this->assertEquals(
215
            ['/images/unknownlord_large.png', 'L'],
216
            \MySociety\TheyWorkForYou\Utility\Member::findMemberImage(1, false, 'lord')
217
        );
218
219
        // Missing, small, use default
220
        $this->assertEquals(
221
            ['/images/unknownperson.png', 'S'],
222
            \MySociety\TheyWorkForYou\Utility\Member::findMemberImage(1, true, true)
223
        );
224
225
        // Missing, small, use default Lord
226
        $this->assertEquals(
227
            ['/images/unknownlord.png', 'S'],
228
            \MySociety\TheyWorkForYou\Utility\Member::findMemberImage(1, true, 'lord')
229
        );
230
231
        // Does a large JPEG work?
232
        $this->assertEquals(
233
            ['/images/mpsL/11132.jpeg', 'L'],
234
            \MySociety\TheyWorkForYou\Utility\Member::findMemberImage(11132)
235
        );
236
237
        // Does a small JPEG work?
238
        $this->assertEquals(
239
            ['/images/mps/10001.jpeg', 'S'],
240
            \MySociety\TheyWorkForYou\Utility\Member::findMemberImage(10001, true)
241
        );
242
243
        // Does a large PNG work? No large PNGs
244
        $this->assertEquals(
245
            ['/images/mps/13943.png', 'S'],
246
            \MySociety\TheyWorkForYou\Utility\Member::findMemberImage(13943)
247
        );
248
249
        // Does a small PNG work?
250
        $this->assertEquals(
251
            ['/images/mps/13943.png', 'S'],
252
            \MySociety\TheyWorkForYou\Utility\Member::findMemberImage(13943, true)
253
        );
254
255
        // Does a large JPG work?
256
        $this->assertEquals(
257
            ['/images/mpsL/10001.jpg', 'L'],
258
            \MySociety\TheyWorkForYou\Utility\Member::findMemberImage(10001)
259
        );
260
261
        // Does a small JPG work?
262
        $this->assertEquals(
263
            ['/images/mps/10552.jpg', 'S'],
264
            \MySociety\TheyWorkForYou\Utility\Member::findMemberImage(10552, true)
265
        );
266
267
        // If we request one we know we have, but also say use a substitute?
268
        $this->assertEquals(
269
            ['/images/mps/10001.jpeg', 'S'],
270
            \MySociety\TheyWorkForYou\Utility\Member::findMemberImage(10001, true, true)
271
        );
272
273
        // If we only have a small, but don't request explicitly?
274
        $this->assertEquals(
275
            ['/images/mps/28619.jpg', 'S'],
276
            \MySociety\TheyWorkForYou\Utility\Member::findMemberImage(28619)
277
        );
278
279
    }
280
281
    public function testIsNew() {
282
        $MEMBER = new MySociety\TheyWorkForYou\Member(['person_id' => 17]);
283
284
        $this->assertNotTrue($MEMBER->isNew());
285
286
        self::$db->query("UPDATE member SET entered_house = NOW() WHERE person_id = 17");
287
288
        // do this to force a reload
289
        $MEMBER = new MySociety\TheyWorkForYou\Member(['person_id' => 17]);
290
        $this->assertTrue($MEMBER->isNew());
291
    }
292
293
    public function testGetEntryDate() {
294
        $MEMBER = new MySociety\TheyWorkForYou\Member(['person_id' => 18]);
295
296
        $this->assertEquals($MEMBER->getEntryDate(), '2014-05-01');
297
        $this->assertEquals($MEMBER->getEntryDate(1), '2014-05-01');
298
        $this->assertEquals($MEMBER->getEntryDate(2), '2010-05-01');
299
        $this->assertEquals($MEMBER->getEntryDate(3), '2012-05-01');
300
        $this->assertEquals($MEMBER->getEntryDate(4), '2004-05-01');
301
        $this->assertEquals($MEMBER->getEntryDate(5), '');
302
    }
303
304
    public function testGetRegionalList() {
305
        $this->assertEquals([], \MySociety\TheyWorkForYou\Member::getRegionalList('', '', ''));
306
307
        $msps = [
308
            [
309
                'person_id' => "19",
310
                'name' => "Mr Regional MSP1",
311
                'constituency' => "Mid Scotland and Fife",
312
                'house' => "4",
313
            ],
314
            [
315
                'person_id' => "20",
316
                'name' => "Mr Regional MSP2",
317
                'constituency' => "Mid Scotland and Fife",
318
                'house' => "4",
319
            ],
320
        ];
321
        $this->assertEquals($msps, \MySociety\TheyWorkForYou\Member::getRegionalList('KY16 8YG', 4, 'SPE'));
322
        $mlas = [
323
            [
324
                'person_id' => "21",
325
                'name' => "Mr Regional MLA1",
326
                'constituency' => "Belfast West",
327
                'house' => "3",
328
            ],
329
            [
330
                'person_id' => "22",
331
                'name' => "Mr Regional MLA2",
332
                'constituency' => "Belfast West",
333
                'house' => "3",
334
            ],
335
        ];
336
        $this->assertEquals($mlas, \MySociety\TheyWorkForYou\Member::getRegionalList('BT17 0XD', 3, 'NIE'));
337
        $this->assertEquals([], \MySociety\TheyWorkForYou\Member::getRegionalList('BT17 0XD', 4, 'NIE'));
338
        $this->assertEquals([], \MySociety\TheyWorkForYou\Member::getRegionalList('BT17 0XD', 3, 'SPE'));
339
        $this->assertEquals([], \MySociety\TheyWorkForYou\Member::getRegionalList('KY16 8YG', 3, 'SPE'));
340
        $this->assertEquals([], \MySociety\TheyWorkForYou\Member::getRegionalList('KY16 8YG', 4, 'NIE'));
341
        $this->assertEquals([], \MySociety\TheyWorkForYou\Member::getRegionalList('KY16 8YG', 4, ''));
342
        $this->assertEquals([], \MySociety\TheyWorkForYou\Member::getRegionalList('KY16 8YG', '', ''));
343
    }
344
345
}
346