Issues (404)

tests/UserTest.php (3 issues)

1
<?php
2
3
/**
4
 * Provides test methods for user functionality.
5
 */
6
class UserTest extends TWFY_Database_TestCase {
7
    /**
8
     * Loads the user testing fixture.
9
     */
10
    public function getDataSet() {
11
        return $this->createMySQLXMLDataSet(dirname(__FILE__) . '/_fixtures/user.xml');
0 ignored issues
show
Are you sure the usage of $this->createMySQLXMLDat... '/_fixtures/user.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 user class is included for every test.
16
     */
17
    public function setUp(): void {
18
        parent::setUp();
19
20
        include_once('www/includes/easyparliament/user.php');
21
    }
22
23
    public function testAddUser() {
24
        $u = new USER();
25
26
        $details = [
27
            "firstname" => 'Test',
28
            "lastname" => 'User',
29
            "email" => '[email protected]',
30
            "postcode" => 'EH1 99SP',
31
            "mp_alert" => false,
32
            "url" => '',
33
            "password" => '',
34
            "optin" => '0',
35
            "status" => 'User',
36
        ];
37
        $u->add($details, false);
38
39
        $id = $u->user_id();
40
        $u->init($id);
41
42
        $this->assertEquals('Test', $u->firstname());
43
        $this->assertEquals('EH1 99SP', $u->postcode());
44
    }
45
46
    public function testEditUser() {
47
        $_COOKIE['epuser_id'] = '1.5ce7f6e2d7de4db00c297e1da0d48ac';
48
        $u = new THEUSER();
49
        $u->loggedin = 1;
50
51
        $this->assertEquals('Test', $u->firstname());
52
53
        $d = $u->update_self([
0 ignored issues
show
The assignment to $d is dead and can be removed.
Loading history...
54
            'firstname' => 'Experiment',
55
            'lastname' => 'User',
56
            'postcode' => 'EH1 99SP',
57
            'password' => '',
58
            'url' => '',
59
            'optin' => 0,
60
            'user_id' => 1,
61
        ]);
62
63
        $this->assertEquals('Experiment', $u->firstname());
64
    }
65
66
    public function testEditUserEmail() {
67
        $_COOKIE['epuser_id'] = '1.5ce7f6e2d7de4db00c297e1da0d48ac';
68
        $u = new THEUSER();
69
        $u->loggedin = 1;
70
71
        $this->assertEquals('[email protected]', $u->email());
72
73
        $d = $u->update_self([
0 ignored issues
show
The assignment to $d is dead and can be removed.
Loading history...
74
            'firstname' => 'Experiment',
75
            'lastname' => 'User',
76
            'email' => '[email protected]',
77
            'postcode' => 'EH1 99SP',
78
            'password' => '',
79
            'url' => '',
80
            'optin' => 0,
81
            'user_id' => 1,
82
        ], false);
83
84
        // email should not change as user needs to confirm
85
        $this->assertEquals('[email protected]', $u->email());
86
87
        $tokenCount = $this->getRowCount('tokens', 'data = "1::[email protected]"');
88
        $this->assertEquals(1, $tokenCount, 'correct number of email confirm tokens');
89
90
        // token is based on the time so we can't test for it
91
        $queryTable = self::$db->query(
92
            'SELECT type, data FROM tokens WHERE data = "1::[email protected]"'
93
        )->fetch();
94
95
        $this->assertEquals('E', $queryTable['type']);
96
        $this->assertEquals('1::[email protected]', $queryTable['data']);
97
98
        $alertCount = $this->getRowCount('alerts', 'email = "[email protected]"');
99
        $this->assertEquals(1, $alertCount, 'correct number of alerts');
100
101
        $tokenRow = self::$db->query(
102
            'SELECT token, type, data FROM tokens WHERE data = "1::[email protected]"'
103
        )->fetch();
104
105
        $token = '2-' . $tokenRow['token'];
106
107
        $u->confirm_email($token, false);
108
109
        $this->assertEquals('[email protected]', $u->email(), 'confirming with token updates email address');
110
        $tokenCount = $this->getRowCount('tokens', 'data = "1::[email protected]"');
111
        $this->assertEquals(0, $tokenCount, 'token deleted once email confirmed');
112
113
        $alertCount = $this->getRowCount('alerts', 'email = "[email protected]"');
114
        $this->assertEquals(1, $alertCount, 'one alert for new email address');
115
116
        $alertCount = $this->getRowCount('alerts', 'email = "[email protected]"');
117
        $this->assertEquals(0, $alertCount, 'no alerts for old email address');
118
    }
119
120
    public function testExpiredToken() {
121
        $_COOKIE['epuser_id'] = '1.5ce7f6e2d7de4db00c297e1da0d48ac';
122
        $u = new THEUSER();
123
        $u->loggedin = 1;
124
125
        $this->assertEquals('[email protected]', $u->email(), 'confirming inital email address');
126
127
        $tokenCount = $this->getRowCount('tokens', 'data = "1::[email protected]"');
128
        $this->assertEquals(1, $tokenCount, 'correct number of email confirm tokens');
129
130
        $token = '2-lkdsjafhsadjhf';
131
132
        $u->confirm_email($token, false);
133
        $this->assertEquals('[email protected]', $u->email(), 'expired token does not update email address');
134
135
        $tokenCount = $this->getRowCount('tokens', 'data = "1::[email protected]"');
136
        $this->assertEquals(1, $tokenCount, 'correct number of email confirm tokens');
137
    }
138
139
}
140