1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Zikula package. |
7
|
|
|
* |
8
|
|
|
* Copyright Zikula Foundation - https://ziku.la/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Zikula\ZAuthModule\Tests\Api; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
17
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
18
|
|
|
use Symfony\Component\Dotenv\Dotenv; |
19
|
|
|
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface; |
20
|
|
|
use Symfony\Component\Validator\ConstraintViolationList; |
21
|
|
|
use Zikula\GroupsModule\Constant as GroupsConstant; |
22
|
|
|
use Zikula\GroupsModule\Entity\RepositoryInterface\GroupRepositoryInterface; |
23
|
|
|
use Zikula\UsersModule\Entity\UserEntity; |
24
|
|
|
use Zikula\ZAuthModule\Api\ApiInterface\UserCreationApiInterface; |
25
|
|
|
use Zikula\ZAuthModule\Api\UserCreationApi; |
26
|
|
|
use Zikula\ZAuthModule\Entity\AuthenticationMappingEntity; |
27
|
|
|
use Zikula\ZAuthModule\ZAuthConstant; |
28
|
|
|
|
29
|
|
|
class UserCreationApiTest extends KernelTestCase |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var UserCreationApiInterface |
33
|
|
|
*/ |
34
|
|
|
private $api; |
35
|
|
|
|
36
|
|
|
protected function setUp(): void |
37
|
|
|
{ |
38
|
|
|
// load test env vars |
39
|
|
|
$dotenv = new Dotenv(); |
40
|
|
|
$dotenv->load('.env.test'); |
41
|
|
|
|
42
|
|
|
self::bootKernel(); |
43
|
|
|
$container = self::$container; |
44
|
|
|
$this->api = $container->get(UserCreationApi::class); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @dataProvider isValidUserDataProvider |
49
|
|
|
*/ |
50
|
|
|
public function testIsValidUserData($expected, array $user): void |
51
|
|
|
{ |
52
|
|
|
$this->assertEquals($expected, $this->api->isValidUserData($user)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testIsValidUserDataArray(): void |
56
|
|
|
{ |
57
|
|
|
$users = $this->getValidUsersArray(); |
58
|
|
|
$errors = $this->api->isValidUserDataArray($users); |
59
|
|
|
$this->assertTrue($errors); |
60
|
|
|
|
61
|
|
|
$users = $this->getInvalidUsersArray(); |
62
|
|
|
$errors = $this->api->isValidUserDataArray($users); |
63
|
|
|
$this->assertNotTrue($errors); |
64
|
|
|
$this->assertInstanceOf(ConstraintViolationList::class, $errors); |
65
|
|
|
$this->assertEquals(4, $errors->count()); |
66
|
|
|
$this->assertEquals('This value is not a valid email address.', $errors[0]->getMessage()); |
67
|
|
|
$this->assertEquals('This value is too short. It should have 8 characters or more.', $errors[1]->getMessage()); |
68
|
|
|
$this->assertEquals('The value you selected is not a valid choice.', $errors[2]->getMessage()); |
69
|
|
|
$this->assertEquals('This value is not valid.', $errors[3]->getMessage()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testCreateUser(): void |
73
|
|
|
{ |
74
|
|
|
$this->api->createUser([ |
75
|
|
|
'uname' => 'foo', |
76
|
|
|
'email' => '[email protected]', |
77
|
|
|
'pass' => '12345678' |
78
|
|
|
]); |
79
|
|
|
$userGroup = self::$container->get(GroupRepositoryInterface::class)->find(GroupsConstant::GROUP_ID_USERS); |
80
|
|
|
|
81
|
|
|
$users = $this->api->getCreatedUsers(); |
82
|
|
|
$hash = array_key_first($users); |
83
|
|
|
/** @var UserEntity $newUser */ |
84
|
|
|
$newUser = $users[$hash]; |
85
|
|
|
$this->assertEquals('foo', $newUser->getUname()); |
86
|
|
|
$this->assertEquals('[email protected]', $newUser->getEmail()); |
87
|
|
|
$this->assertEquals(1, $newUser->getActivated()); |
88
|
|
|
$this->assertEquals(new ArrayCollection([$userGroup]), $newUser->getGroups()); |
89
|
|
|
$this->assertEquals(null, $newUser->getUid()); |
90
|
|
|
|
91
|
|
|
$mappings = $this->api->getCreatedMappings(); |
92
|
|
|
$this->assertArrayHasKey($hash, $mappings); |
93
|
|
|
/** @var AuthenticationMappingEntity $newMapping */ |
94
|
|
|
$newMapping = $mappings[$hash]; |
95
|
|
|
$this->assertEquals('foo', $newMapping->getUname()); |
96
|
|
|
$this->assertEquals('[email protected]', $newMapping->getEmail()); |
97
|
|
|
$passwordEncoder = self::$container->get(EncoderFactoryInterface::class)->getEncoder(AuthenticationMappingEntity::class); |
98
|
|
|
$this->assertEquals(true, $passwordEncoder->isPasswordValid($newMapping->getPass(), '12345678', null)); |
99
|
|
|
$this->assertEquals(ZAuthConstant::AUTHENTICATION_METHOD_EITHER, $newMapping->getMethod()); |
100
|
|
|
$this->assertNotEquals(ZAuthConstant::DEFAULT_EMAIL_VERIFICATION_REQUIRED, $newMapping->isVerifiedEmail()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testCreateUsers(): void |
104
|
|
|
{ |
105
|
|
|
$users = $this->getValidUsersArray(); |
106
|
|
|
$errors = $this->api->createUsers($users); |
107
|
|
|
$this->assertCount(0, $errors); |
108
|
|
|
$this->assertCount(10, $this->api->getCreatedUsers()); |
109
|
|
|
$this->assertCount(10, $this->api->getCreatedMappings()); |
110
|
|
|
|
111
|
|
|
$this->api->clearCreated(); |
112
|
|
|
$users = $this->getInvalidUsersArray(); |
113
|
|
|
$errors = $this->api->createUsers($users); |
114
|
|
|
$this->assertCount(4, $errors); |
115
|
|
|
$this->assertCount(6, $this->api->getCreatedUsers()); |
116
|
|
|
$this->assertCount(6, $this->api->getCreatedMappings()); |
117
|
|
|
$this->assertEquals('Row 0 with email `foo0@bar` and uname `foo0` is invalid and was rejected.', $errors[0]); |
118
|
|
|
$this->assertEquals('Row 2 with email `[email protected]` and uname `foo2` is invalid and was rejected.', $errors[1]); |
119
|
|
|
$this->assertEquals('Row 3 with email `[email protected]` and uname `foo3` is invalid and was rejected.', $errors[2]); |
120
|
|
|
$this->assertEquals('Row 9 with email `[email protected]` and uname `foo9` is invalid and was rejected.', $errors[3]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function isValidUserDataProvider() |
124
|
|
|
{ |
125
|
|
|
return [ |
126
|
|
|
['This field is missing.', []], |
127
|
|
|
['This field is missing.', ['uname' => 'foo', 'pass' => '12345678']], |
128
|
|
|
['This field is missing.', ['uname' => 'foo', 'pass' => '12345678', 'bar' => 'foo']], |
129
|
|
|
['This field is missing.', ['uname' => 'foo', 'email' => '[email protected]']], |
130
|
|
|
['This value is not a valid email address.', ['uname' => 'foo', 'pass' => '12345678', 'email' => 'foo']], |
131
|
|
|
['This value should not be blank.', ['uname' => '', 'pass' => '12345678', 'email' => '[email protected]']], |
132
|
|
|
['This value is too short. It should have 8 characters or more.', ['uname' => 'foo', 'pass' => '123', 'email' => '[email protected]']], |
133
|
|
|
[true, ['uname' => 'foo', 'pass' => '12345678', 'email' => '[email protected]']], |
134
|
|
|
|
135
|
|
|
['The value you selected is not a valid choice.', ['uname' => 'foo', 'pass' => '12345678', 'email' => '[email protected]', 'activated' => 2]], |
136
|
|
|
['The value you selected is not a valid choice.', ['uname' => 'foo', 'pass' => '12345678', 'email' => '[email protected]', 'activated' => '2']], |
137
|
|
|
['This value should be of type numeric.', ['uname' => 'foo', 'pass' => '12345678', 'email' => '[email protected]', 'activated' => 'foo']], |
138
|
|
|
[true, ['uname' => 'foo', 'pass' => '12345678', 'email' => '[email protected]', 'activated' => '1']], |
139
|
|
|
[true, ['uname' => 'foo', 'pass' => '12345678', 'email' => '[email protected]', 'activated' => 0]], |
140
|
|
|
[true, ['uname' => 'foo', 'pass' => '12345678', 'email' => '[email protected]', 'activated' => 1]], |
141
|
|
|
['The value you selected is not a valid choice.', ['uname' => 'foo', 'pass' => '12345678', 'email' => '[email protected]', 'sendmail' => 2]], |
142
|
|
|
['The value you selected is not a valid choice.', ['uname' => 'foo', 'pass' => '12345678', 'email' => '[email protected]', 'sendmail' => '2']], |
143
|
|
|
['This value should be of type numeric.', ['uname' => 'foo', 'pass' => '12345678', 'email' => '[email protected]', 'sendmail' => 'foo']], |
144
|
|
|
[true, ['uname' => 'foo', 'pass' => '12345678', 'email' => '[email protected]', 'sendmail' => '1']], |
145
|
|
|
[true, ['uname' => 'foo', 'pass' => '12345678', 'email' => '[email protected]', 'sendmail' => 0]], |
146
|
|
|
[true, ['uname' => 'foo', 'pass' => '12345678', 'email' => '[email protected]', 'sendmail' => 1]], |
147
|
|
|
['This value should be of type string.', ['uname' => 'foo', 'pass' => '12345678', 'email' => '[email protected]', 'groups' => 1]], |
148
|
|
|
['This value is not valid.', ['uname' => 'foo', 'pass' => '12345678', 'email' => '[email protected]', 'groups' => 'users']], |
149
|
|
|
[true, ['uname' => 'foo', 'pass' => '12345678', 'email' => '[email protected]', 'groups' => '1']], |
150
|
|
|
[true, ['uname' => 'foo', 'pass' => '12345678', 'email' => '[email protected]', 'groups' => '1|2|3']], |
151
|
|
|
|
152
|
|
|
[true, ['uname' => 'foo', 'pass' => '12345678', 'email' => '[email protected]', 'activated' => '1', 'sendmail' => '1', 'groups' => '1']], |
153
|
|
|
[true, ['uname' => 'foo', 'pass' => '12345678', 'email' => '[email protected]', 'activated' => 1, 'sendmail' => 1, 'groups' => '1|2|12345']], |
154
|
|
|
]; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
private function getValidUsersArray(): array |
158
|
|
|
{ |
159
|
|
|
return [ |
160
|
|
|
['uname' => 'foo0', 'pass' => '12345678', 'email' => '[email protected]'], |
161
|
|
|
['uname' => 'foo1', 'pass' => '12345678', 'email' => '[email protected]', 'activated' => '1'], |
162
|
|
|
['uname' => 'foo2', 'pass' => '12345678', 'email' => '[email protected]', 'activated' => 0], |
163
|
|
|
['uname' => 'foo3', 'pass' => '12345678', 'email' => '[email protected]', 'activated' => 1], |
164
|
|
|
['uname' => 'foo4', 'pass' => '12345678', 'email' => '[email protected]', 'sendmail' => '1'], |
165
|
|
|
['uname' => 'foo5', 'pass' => '12345678', 'email' => '[email protected]', 'sendmail' => 1], |
166
|
|
|
['uname' => 'foo6', 'pass' => '12345678', 'email' => '[email protected]', 'groups' => '1|57'], |
167
|
|
|
['uname' => 'foo7', 'pass' => '12345678', 'email' => '[email protected]', 'groups' => '1|2'], |
168
|
|
|
['uname' => 'foo8', 'pass' => '12345678', 'email' => '[email protected]', 'activated' => '1', 'sendmail' => '1', 'groups' => '1'], |
169
|
|
|
['uname' => 'foo9', 'pass' => '12345678', 'email' => '[email protected]', 'activated' => 1, 'sendmail' => 1, 'groups' => '1|2|3'], |
170
|
|
|
]; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
private function getInvalidUsersArray(): array |
174
|
|
|
{ |
175
|
|
|
return [ |
176
|
|
|
['uname' => 'foo0', 'pass' => '12345678', 'email' => 'foo0@bar'], // invalid |
177
|
|
|
['uname' => 'foo1', 'pass' => '12345678', 'email' => '[email protected]', 'activated' => '1'], |
178
|
|
|
['uname' => 'foo2', 'pass' => '123', 'email' => '[email protected]', 'activated' => 0], // invalid |
179
|
|
|
['uname' => 'foo3', 'pass' => '12345678', 'email' => '[email protected]', 'activated' => 9], // invalid |
180
|
|
|
['uname' => 'foo4', 'pass' => '12345678', 'email' => '[email protected]', 'sendmail' => '1'], |
181
|
|
|
['uname' => 'foo5', 'pass' => '12345678', 'email' => '[email protected]', 'sendmail' => 1], |
182
|
|
|
['uname' => 'foo6', 'pass' => '12345678', 'email' => '[email protected]', 'groups' => '1|57'], |
183
|
|
|
['uname' => 'foo7', 'pass' => '12345678', 'email' => '[email protected]', 'groups' => '1|2'], |
184
|
|
|
['uname' => 'foo8', 'pass' => '12345678', 'email' => '[email protected]', 'activated' => '1', 'sendmail' => '1', 'groups' => '1'], |
185
|
|
|
['uname' => 'foo9', 'pass' => '12345678', 'email' => '[email protected]', 'activated' => 1, 'sendmail' => 1, 'groups' => 'users'], // invalid |
186
|
|
|
]; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|