Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

UserContext.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Bundle\EzPublishCoreBundle\Features\Context;
8
9 use Behat\Gherkin\Node\TableNode;
10 use Behat\Behat\Context\Context;
11 use PHPUnit\Framework\Assert as Assertion;
12 use EzSystems\PlatformBehatBundle\Context\RepositoryContext;
13 use eZ\Publish\API\Repository\Repository;
14 use eZ\Publish\API\Repository\UserService;
15 use eZ\Publish\API\Repository\SearchService;
16 use eZ\Publish\API\Repository\Exceptions as ApiExceptions;
17 use eZ\Publish\API\Repository\Values\Content\Query;
18 use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
19 use eZ\Publish\API\Repository\Exceptions\NotFoundException;
20
24 class UserContext implements Context
25 {
26 use RepositoryContext;
27
28 const DEFAULT_LANGUAGE = 'eng-GB';
29
33 const USER_IDENTIFIER = 'user';
34
35 const USERGROUP_ROOT_CONTENT_ID = 4;
36 const USERGROUP_ROOT_LOCATION = 5;
37 const USERGROUP_ROOT_SUBTREE = '/1/5/';
38 const USERGROUP_CONTENT_IDENTIFIER = 'user_group';
39
41 protected $userService;
42
44 protected $searchService;
45
51 public function __construct(Repository $repository, UserService $userService, SearchService $searchService)
52 {
53 $this->setRepository($repository);
54 $this->userService = $userService;
55 $this->searchService = $searchService;
56 }
57
66 public function searchUserByLogin($username, $parentGroupId = null)
67 {
68 try {
69 $user = $this->userService->loadUserByLogin($username);
70 } catch (ApiExceptions\NotFoundException $e) {
71 return null;
72 }
73
74 if ($user && $parentGroupId) {
75 $userGroups = $this->userService->loadUserGroupsOfUser($user);
76
77 foreach ($userGroups as $userGroup) {
78 if ($userGroup->getVersionInfo()->getContentInfo()->id == $parentGroupId) {
79 return $user;
80 }
81 }
82 // user not found in $parentGroupId
83 return null;
84 }
85
86 return $user;
87 }
88
97 public function searchUserGroups($name, $parentLocationId = null)
98 {
99 $criterionArray = [
100 new Criterion\Subtree(self::USERGROUP_ROOT_SUBTREE),
101 new Criterion\ContentTypeIdentifier(self::USERGROUP_CONTENT_IDENTIFIER),
102 new Criterion\Field('name', Criterion\Operator::EQ, $name),
103 ];
104 if ($parentLocationId) {
105 $criterionArray[] = new Criterion\ParentLocationId($parentLocationId);
106 }
107 $query = new Query();
108 $query->filter = new Criterion\LogicalAnd($criterionArray);
109
110 $result = $this->searchService->findContent($query, [], false);
111
112 return $result->searchHits;
113 }
114
125 protected function createUser($username, $email, $password, $parentGroup = null, $fields = [])
126 {
127 $userCreateStruct = $this->userService->newUserCreateStruct(
128 $username,
129 $email,
130 $password,
131 self::DEFAULT_LANGUAGE
132 );
133 $userCreateStruct->setField('first_name', $username);
134 $userCreateStruct->setField('last_name', $username);
135
136 foreach ($fields as $fieldName => $fieldValue) {
137 $userCreateStruct->setField($fieldName, $fieldValue);
138 }
139
140 try {
141 $existingUser = $this->userService->loadUserByLogin($username);
142 $this->userService->deleteUser($existingUser);
143 } catch (NotFoundException $e) {
144 // do nothing
145 }
146
147 if (!$parentGroup) {
148 $parentGroup = $this->userService->loadUserGroup(self::USERGROUP_ROOT_CONTENT_ID);
149 }
150
151 $user = $this->userService->createUser($userCreateStruct, [$parentGroup]);
152
153 return $user;
154 }
155
164 public function createUserGroup($name, $parentGroup = null)
165 {
166 if (!$parentGroup) {
167 $parentGroup = $this->userService->loadUserGroup(self::USERGROUP_ROOT_CONTENT_ID);
168 }
169
170 $userGroupCreateStruct = $this->userService->newUserGroupCreateStruct('eng-GB');
171 $userGroupCreateStruct->setField('name', $name);
172
173 return $this->userService->createUserGroup($userGroupCreateStruct, $parentGroup);
174 }
175
186 public function ensureUserExists($username, $email, $password, $parentGroupName = null)
187 {
188 if ($parentGroupName) {
189 $parentSearchHits = $this->searchUserGroups($parentGroupName);
190
191 // Found matching Group(s)
192 if (!empty($parentSearchHits)) {
193 $firstGroupId = $parentSearchHits[0]->valueObject->contentInfo->id;
194 foreach ($parentSearchHits as $userGroupHit) {
195 $groupId = $userGroupHit->valueObject->contentInfo->id;
196 // Search for user in this group
197 $user = $this->searchUserByLogin($username, $groupId);
198 if ($user) {
199 return $user;
200 }
201 }
202
203 // create user inside existing parent Group, use first group found
204 $parentGroup = $this->userService->loadUserGroup($firstGroupId);
205
206 return $this->createUser($username, $email, $password, $parentGroup);
207 } // else
208
209 // Parent Group does not exist yet, so create it at "root" User Group.
210 $rootGroup = $this->userService->loadUserGroup(self::USERGROUP_ROOT_CONTENT_ID);
211 $parentGroup = $this->createUserGroup($parentGroupName, $rootGroup);
212
213 return $this->createUser($username, $email, $password, $parentGroup);
214 }
215 // else,
216
217 $user = $this->searchUserByLogin($username);
218 if (!$user) {
219 $user = $this->createUser($username, $email, $password);
220 }
221
222 return $user;
223 }
224
231 public function ensureUserDoesntExist($username, $parentGroupName = null)
232 {
233 $user = null;
234 if ($parentGroupName) {
235 // find matching Parent Group name
236 $parentSearchHits = $this->searchUserGroups($parentGroupName, self::USERGROUP_ROOT_LOCATION);
237 if (!empty($parentSearchHits)) {
238 foreach ($parentSearchHits as $parentGroupFound) {
239 $groupId = $parentGroupFound->valueObject->contentInfo->id;
240 //Search for already existing matching Child user
241 $user = $this->searchUserByLogin($username, $groupId);
242 if ($user) {
243 break;
244 }
245 }
246 }
247 } else {
248 try {
249 $user = $this->userService->loadUserByLogin($username);
250 } catch (ApiExceptions\NotFoundException $e) {
251 // nothing to do
252 }
253 }
254 if ($user) {
255 try {
256 $this->userService->deleteUser($user);
257 } catch (ApiExceptions\NotFoundException $e) {
258 // nothing to do
259 }
260 }
261 }
262
271 public function checkUserExistenceByUsername($username, $parentGroupName = null)
272 {
273 if ($parentGroupName) {
274 // find parent group name
275 $searchResults = $this->searchUserGroups($parentGroupName);
276 if (empty($searchResults)) {
277 // group not found, so return immediately
278 return false;
279 }
280 $groupId = $searchResults[0]->valueObject->contentInfo->id;
281 } else {
282 $groupId = null;
283 }
284 $searchResults = $this->searchUserByLogin($username, $groupId);
285
286 return empty($searchResults) ? false : true;
287 }
288
297 public function checkUserExistenceByEmail($email, $parentGroupName = null)
298 {
299 $existingUsers = $this->userService->loadUsersByEmail($email);
300 if (count($existingUsers) == 0) {
301 return false;
302 }
303 if ($parentGroupName) {
304 foreach ($existingUsers as $user) {
305 $userGroups = $this->userService->loadUserGroupsOfUser($user);
306 foreach ($userGroups as $userGroup) {
307 if ($userGroup->getFieldValue('name') == $parentGroupName) {
308 return true;
309 }
310 }
311 }
312 }
313
314 return false;
315 }
316
317 public function createPasswordHash($login, $password, $type)
318 {
319 switch ($type) {
320 case 2:
321 /* PASSWORD_HASH_MD5_USER */
322 return md5("{$login}\n{$password}");
323 case 3:
324 /* PASSWORD_HASH_MD5_SITE */
325 $site = null;
326
327 return md5("{$login}\n{$password}\n{$site}");
328 case 5:
329 /* PASSWORD_HASH_PLAINTEXT */
330 return $password;
331 }
332 /* PASSWORD_HASH_MD5_PASSWORD (1) */
333 return md5($password);
334 }
335
343 public function iHaveUser($username)
344 {
345 $email = $this->findNonExistingUserEmail($username);
346 $password = $username;
347 $user = $this->ensureUserExists($username, $email, $password);
348 }
349
357 public function iHaveUserWithUsernameEmailAndPassword($username, $email, $password)
358 {
359 $this->ensureUserExists($username, $email, $password);
360 }
361
369 public function iHaveUserInGroup($username, $parentGroupName)
370 {
371 $email = $this->findNonExistingUserEmail($username);
372 $password = $username;
373 $user = $this->ensureUserExists($username, $email, $password, $parentGroupName);
374 }
375
383 public function iHaveUserWithUsernameEmailAndPasswordInGroup($username, $email, $password, $parentGroupName)
384 {
385 return $this->ensureUserExists($username, $email, $password, $parentGroupName);
386 }
387
393 public function iDontHaveUser($username)
394 {
395 $this->ensureUserDoesntExist($username);
396 }
397
403 public function iDontHaveUserInGroup($username, $parentGroup)
404 {
405 $this->ensureUserDoesntExist($username, $parentGroup);
406 }
407
417 public function iHaveTheFollowingUsers(TableNode $table)
418 {
419 $users = $table->getTable();
420 array_shift($users);
421 foreach ($users as $user) {
422 // array( [0] => userName, [1] => groupName );
423 $this->ensureUserExists($user[0], $user[1]);
424 }
425 }
426
433 public function assertUserWithNameExists($username)
434 {
435 Assertion::assertTrue(
436 $this->checkUserExistenceByUsername($username),
437 "Couldn't find User with name '$username'."
438 );
439 }
440
446 public function assertUserWithNameDoesntExist($username)
447 {
448 Assertion::assertFalse(
449 $this->checkUserExistenceByUsername($username),
450 "User with name '$username' was found."
451 );
452 }
453
460 public function assertUserWithNameExistsInGroup($username, $parentGroup)
461 {
462 Assertion::assertTrue(
463 $this->checkUserExistenceByUsername($username, $parentGroup),
464 "Couldn't find User with name '$username' in parent group '$parentGroup'."
465 );
466 }
467
474 public function assertUserWithNameDoesntExistInGroup($username, $parentGroup)
475 {
476 Assertion::assertFalse(
477 $this->checkUserExistenceByUsername($username, $parentGroup),
478 "User with name '$username' was found in parent group '$parentGroup'."
479 );
480 }
481
492 public function assertUserWithNameDoesntExistInGroups($username, TableNode $table)
493 {
494 $groups = $table->getTable();
495 array_shift($groups);
496 foreach ($groups as $group) {
497 $parentGroupName = $group[0];
498 Assertion::assertFalse(
499 $this->checkUserExistenceByUsername($username, $parentGroupName),
500 "User with name '$username' was found in parent group '$parentGroupName'."
501 );
502 }
503 }
504
516 public function assertUserWithNameExistsWithFields($username, TableNode $table)
517 {
518 Assertion::assertTrue(
519 $this->checkUserExistenceByUsername($username),
520 "Couldn't find User with name '$username'."
521 );
522 $user = $this->userService->loadUserByLogin($username);
523 $fieldsTable = $table->getTable();
524 array_shift($fieldsTable);
525 $updateFields = [];
526 foreach ($fieldsTable as $fieldRow) {
527 $fieldName = $fieldRow[0];
528 $expectedValue = $fieldRow[1];
529 switch ($fieldName) {
530 case 'email':
531 $fieldValue = $user->email;
532 break;
533 case 'password':
534 $fieldValue = $user->passwordHash;
535 $expectedValue = $this->createPasswordHash($username, $expectedValue, $user->hashAlgorithm);
536 break;
537 default:
538 $fieldValue = $user->getFieldValue($fieldName);
539 }
540 Assertion::assertEquals(
541 $expectedValue,
542 $fieldValue,
543 "Field '$fieldName' did not contain expected value '$expectedValue'."
544 );
545 }
546 }
547
555 private function findNonExistingUserEmail($username = 'User')
556 {
557 $email = "${username}@ez.no";
558 if ($this->checkUserExistenceByEmail($email)) {
559 return $email;
560 }
561
562 for ($i = 0; $i < 20; ++$i) {
563 $email = uniqid('User#', true) . '@ez.no';
564 if (!$this->checkUserExistenceByEmail($email)) {
565 return $email;
566 }
567 }
568
569 throw new \Exception('Possible endless loop when attempting to find a new email for User.');
570 }
571
579 private function findNonExistingUserName()
580 {
581 for ($i = 0; $i < 20; ++$i) {
582 $username = uniqid('User#', true);
583 if (!$this->checkUserExistenceByUsername($username)) {
584 return $username;
585 }
586 }
587
588 throw new \Exception('Possible endless loop when attempting to find a new name for User.');
589 }
590 }
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext
Sentences for Users.
Definition: UserContext.php:25
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\USERGROUP_ROOT_SUBTREE
const USERGROUP_ROOT_SUBTREE
Definition: UserContext.php:37
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\checkUserExistenceByUsername
checkUserExistenceByUsername($username, $parentGroupName=null)
Checks if the User with username $username exists.
Definition: UserContext.php:271
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\iHaveTheFollowingUsers
iHaveTheFollowingUsers(TableNode $table)
@Given there are the following Users:
Definition: UserContext.php:417
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\__construct
__construct(Repository $repository, UserService $userService, SearchService $searchService)
@injectService $repository @ezpublish.api.repository @injectService $userService @ezpublish....
Definition: UserContext.php:51
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\createUserGroup
createUserGroup($name, $parentGroup=null)
Create new User Group inside existing parent User Group.
Definition: UserContext.php:164
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\assertUserWithNameDoesntExistInGroups
assertUserWithNameDoesntExistInGroups($username, TableNode $table)
@Then User with name :username doesn't exist in the following groups:
Definition: UserContext.php:492
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\DEFAULT_LANGUAGE
const DEFAULT_LANGUAGE
Definition: UserContext.php:28
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\USERGROUP_ROOT_LOCATION
const USERGROUP_ROOT_LOCATION
Definition: UserContext.php:36
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\iHaveUserWithUsernameEmailAndPasswordInGroup
iHaveUserWithUsernameEmailAndPasswordInGroup($username, $email, $password, $parentGroupName)
@Given there is a User with name :username, email :email and password :password in :parentGroup group
Definition: UserContext.php:383
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\findNonExistingUserName
findNonExistingUserName()
Find a non existing User name.
Definition: UserContext.php:579
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\ensureUserExists
ensureUserExists($username, $email, $password, $parentGroupName=null)
Make sure a User with name $username, $email and $password exists in parent group.
Definition: UserContext.php:186
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\USERGROUP_ROOT_CONTENT_ID
const USERGROUP_ROOT_CONTENT_ID
Definition: UserContext.php:35
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\iHaveUserInGroup
iHaveUserInGroup($username, $parentGroupName)
@Given there is a User with name :username in :parentGroup group
Definition: UserContext.php:369
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\assertUserWithNameExists
assertUserWithNameExists($username)
@Given a User with name :username already exists @Then User with name :username exists
Definition: UserContext.php:433
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\$searchService
$searchService
Definition: UserContext.php:44
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\assertUserWithNameDoesntExist
assertUserWithNameDoesntExist($username)
@Then User with name :username doesn't exist
Definition: UserContext.php:446
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\ensureUserDoesntExist
ensureUserDoesntExist($username, $parentGroupName=null)
Make sure a User with name $username does not exist (in parent group).
Definition: UserContext.php:231
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\iDontHaveUser
iDontHaveUser($username)
@Given there isn't a User with name :username
Definition: UserContext.php:393
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\findNonExistingUserEmail
findNonExistingUserEmail($username='User')
Find a non existing User email.
Definition: UserContext.php:555
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\createPasswordHash
createPasswordHash($login, $password, $type)
Definition: UserContext.php:317
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\assertUserWithNameDoesntExistInGroup
assertUserWithNameDoesntExistInGroup($username, $parentGroup)
@Then User with name :username doesn't exist in group :parentGroup @Then User with name :username doe...
Definition: UserContext.php:474
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\$userService
$userService
Definition: UserContext.php:41
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\searchUserGroups
searchUserGroups($name, $parentLocationId=null)
Search User Groups with given name.
Definition: UserContext.php:97
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\assertUserWithNameExistsInGroup
assertUserWithNameExistsInGroup($username, $parentGroup)
@Then User with name :username exists in group :parentGroup @Then User with name :username exists in ...
Definition: UserContext.php:460
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\iDontHaveUserInGroup
iDontHaveUserInGroup($username, $parentGroup)
@Given there isn't a User with name :username in :parentGroup group
Definition: UserContext.php:403
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\createUser
createUser($username, $email, $password, $parentGroup=null, $fields=[])
Create user inside given User Group; DELETES existing User if login already exists!
Definition: UserContext.php:125
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\iHaveUser
iHaveUser($username)
@Given there is a User with name :username
Definition: UserContext.php:343
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\checkUserExistenceByEmail
checkUserExistenceByEmail($email, $parentGroupName=null)
Checks if the User with email $email exists.
Definition: UserContext.php:297
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\USER_IDENTIFIER
const USER_IDENTIFIER
These values are set by the default eZ Publish installation.
Definition: UserContext.php:33
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\iHaveUserWithUsernameEmailAndPassword
iHaveUserWithUsernameEmailAndPassword($username, $email, $password)
@Given there is a User with name :username, email :email and password :password
Definition: UserContext.php:357
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\searchUserByLogin
searchUserByLogin($username, $parentGroupId=null)
Search User with given username, optionally at given location.
Definition: UserContext.php:66
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\USERGROUP_CONTENT_IDENTIFIER
const USERGROUP_CONTENT_IDENTIFIER
Definition: UserContext.php:38
eZ\Bundle\EzPublishCoreBundle\Features\Context\UserContext\assertUserWithNameExistsWithFields
assertUserWithNameExistsWithFields($username, TableNode $table)
@Then User with name :username has the following fields: @Then User with name :username exists with t...
Definition: UserContext.php:516
eZ\Publish\API\Repository\Exceptions\NotFoundException
This Exception is thrown if an object referenced by an id or identifier could not be found in the rep...
Definition: API/Repository/Exceptions/NotFoundException.php:16
eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator\EQ
const EQ
Definition: API/Repository/Values/Content/Query/Criterion/Operator.php:16
eZ\Publish\API\Repository\Values\Content\Query\Criterion
Definition: API/Repository/Values/Content/Query/Criterion.php:15
eZ\Publish\API\Repository\Values\Content\Query
This class is used to perform a Content query.
Definition: API/Repository/Values/Content/Query.php:15
eZ\Publish\API\Repository\Repository
Repository interface.
Definition: API/Repository/Repository.php:16
eZ\Publish\API\Repository\SearchService
Search service.
Definition: API/Repository/SearchService.php:17
eZ\Publish\API\Repository\UserService
Definition: API/Repository/UserService.php:26
eZ\Bundle\EzPublishCoreBundle\Features\Context
Definition: BasicContentContext.php:7
eZ\Publish\API\Repository\Exceptions
Definition: API/Repository/Exceptions/BadStateException.php:7
eZ\Publish\API\Repository\Values\Content\Query\Criterion
Definition: API/Repository/Values/Content/Query/Criterion/Ancestor.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.