forked from mantisbt/mantisbt
-
Notifications
You must be signed in to change notification settings - Fork 3
/
UserTest.php
144 lines (124 loc) · 3.99 KB
/
UserTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
# MantisBT - A PHP based bugtracking system
# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
/**
* Test cases for User API within mantis
*
* @package Tests
* @subpackage UserAPI
* @copyright Copyright 2023 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*
* @noinspection PhpIllegalPsrClassPathInspection
*/
# Includes
use Mantis\Exceptions\ClientException;
require_once 'MantisCoreBase.php';
/**
* PHPUnit tests for User API
*/
class MantisUserApiTest extends MantisCoreBase {
const TEST_EMAIL = 'test@uniqueness.test';
protected static $user_id;
public static function setUpBeforeClass() {
parent::setUpBeforeClass();
$t_cookie = user_create(
'User' . rand(),
'password',
self::TEST_EMAIL
);
/** @noinspection PhpUnhandledExceptionInspection */
self::$user_id = user_get_id_by_cookie( $t_cookie );
}
public static function tearDownAfterClass() {
user_delete( self::$user_id );
}
/**
* Tests user_is_email_unique()
*
* @dataProvider providerEmailUnique
* @param string $p_email
* @param int $p_user_id
* @param bool $p_unique Expected result.
*/
public function testEmailUnique( $p_email, $p_user_id, $p_unique ) {
if( $p_user_id == -1 ) {
$p_user_id = $this::$user_id;
}
$this->assertEquals( user_is_email_unique( $p_email, $p_user_id ), $p_unique );
}
/**
* Data provider for testEmailUnique().
*
* Set user_id to `-1` to use the id of the test user created in
* setUpBeforeClass(). This hack is needed because PHPUnit initializes the
* data provider before the setup method has created the test user account.
*
* @return array [email_address, user_id, unique]
*/
public function providerEmailUnique() {
return [
"Existing email, new user"
=> array( self::TEST_EMAIL, null, false ),
"Existing email, matching user"
=> array( self::TEST_EMAIL, -1, true ),
"Existing email, other user"
=> array( self::TEST_EMAIL, 1, false ),
"Existing email with different case"
=> array( ucfirst(self::TEST_EMAIL), null, false ),
"Email matching SQL LIKE pattern"
=> array( 't_st@uniqueness.test', null, true ),
"Non-existing email"
=> array( 'unique@uniqueness.test', null, true ),
];
}
/**
* Tests user_get_id_by_email()
*
* @noinspection PhpUnhandledExceptionInspection
*/
public function testGetIdByEmail() {
$t_user_id = $this::$user_id;
$t_email_with_case_variation = ucfirst( self::TEST_EMAIL );
$this->assertEquals( $t_user_id,
user_get_id_by_email( self::TEST_EMAIL ),
"User email found with exact case"
);
$this->assertEquals( $t_user_id,
user_get_id_by_email( $t_email_with_case_variation ),
"User email found with different case"
);
// Allow non-unique emails and create a new user with duplicate email
config_set_global( 'email_ensure_unique', false );
$t_cookie = user_create(
'DupeMail' . rand(),
'password',
$t_email_with_case_variation
);
$t_user_id = user_get_id_by_cookie( $t_cookie );
$this->assertNotFalse(
user_get_id_by_email( self::TEST_EMAIL ),
"User found when multiple accounts with same email exist"
);
user_delete( $t_user_id );
// Expected failures
$this->assertFalse(
user_get_id_by_email( rand() . self::TEST_EMAIL ),
"Non-existing email not found"
);
// Same test but with exception
$this->expectException( ClientException::class );
user_get_id_by_email( rand() . self::TEST_EMAIL, true );
}
}