-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EZP-31079: Provided validation for email uniqueness and login pattern
- Loading branch information
1 parent
2d08b2d
commit 52e9563
Showing
6 changed files
with
398 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -299,7 +299,7 @@ private function assertPropertiesEqual($propertyName, $expectedValue, $actualVal | |
/** | ||
* Create a user in editor user group. | ||
*/ | ||
protected function createUserVersion1(string $login = 'user', ?string $email = null): User | ||
protected function createUserVersion1(string $login = 'user', ?string $email = null, ContentType $contentType = null): User | ||
{ | ||
$repository = $this->getRepository(); | ||
|
||
|
@@ -314,7 +314,7 @@ protected function createUserVersion1(string $login = 'user', ?string $email = n | |
$userCreate = $userService->newUserCreateStruct( | ||
$login, | ||
$email, | ||
'secret', | ||
'[email protected]', | ||
'eng-US' | ||
); | ||
$userCreate->enabled = true; | ||
|
@@ -323,6 +323,10 @@ protected function createUserVersion1(string $login = 'user', ?string $email = n | |
$userCreate->setField('first_name', 'Example'); | ||
$userCreate->setField('last_name', 'User'); | ||
|
||
if (!empty($contentType)) { | ||
$userCreate->contentType = $contentType; | ||
} | ||
|
||
// Load parent group for the user | ||
$group = $userService->loadUserGroup($editorsGroupId); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
use eZ\Publish\API\Repository\Values\User\UserTokenUpdateStruct; | ||
use eZ\Publish\API\Repository\Values\User\UserUpdateStruct; | ||
use eZ\Publish\API\Repository\Values\User\User; | ||
use eZ\Publish\Core\FieldType\User\Type; | ||
use eZ\Publish\Core\FieldType\ValidationError; | ||
use eZ\Publish\Core\Repository\Values\Content\Content; | ||
use eZ\Publish\Core\Repository\Values\Content\VersionInfo; | ||
|
@@ -190,7 +191,6 @@ public function testLoadSubUserGroupsThrowsNotFoundException() | |
* @return \eZ\Publish\API\Repository\Values\User\UserGroupCreateStruct | ||
* | ||
* @see \eZ\Publish\API\Repository\UserService::newUserGroupCreateStruct() | ||
* @depends eZ\Publish\API\Repository\Tests\ContentTypeServiceTest::testLoadContentTypeByIdentifier | ||
*/ | ||
public function testNewUserGroupCreateStruct() | ||
{ | ||
|
@@ -244,7 +244,6 @@ public function testNewUserGroupCreateStructSetsContentType($groupCreate) | |
* | ||
* @see \eZ\Publish\API\Repository\UserService::newUserGroupCreateStruct($mainLanguageCode, $contentType) | ||
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testNewUserGroupCreateStruct | ||
* @depends eZ\Publish\API\Repository\Tests\ContentTypeServiceTest::testLoadContentTypeByIdentifier | ||
*/ | ||
public function testNewUserGroupCreateStructWithSecondParameter() | ||
{ | ||
|
@@ -279,7 +278,6 @@ public function testNewUserGroupCreateStructWithSecondParameter() | |
* @see \eZ\Publish\API\Repository\UserService::createUserGroup() | ||
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testNewUserGroupCreateStruct | ||
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadUserGroup | ||
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent | ||
*/ | ||
public function testCreateUserGroup() | ||
{ | ||
|
@@ -427,7 +425,6 @@ public function testCreateUserGroupWhenMissingField() | |
* @see \eZ\Publish\API\Repository\UserService::createUserGroup() | ||
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testNewUserGroupCreateStruct | ||
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadUserGroup | ||
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent | ||
*/ | ||
public function testCreateUserGroupInTransactionWithRollback() | ||
{ | ||
|
@@ -864,7 +861,6 @@ public function testNewUserCreateStructSetsExpectedProperties($userCreate) | |
* | ||
* @see \eZ\Publish\API\Repository\UserService::newUserCreateStruct($login, $email, $password, $mainLanguageCode, $contentType) | ||
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testNewUserCreateStruct | ||
* @depends eZ\Publish\API\Repository\Tests\ContentTypeServiceTest::testLoadContentTypeByIdentifier | ||
*/ | ||
public function testNewUserCreateStructWithFifthParameter() | ||
{ | ||
|
@@ -916,7 +912,6 @@ public function testNewUserWithDomainName() | |
* @see \eZ\Publish\API\Repository\UserService::createUser() | ||
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadUserGroup | ||
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testNewUserCreateStruct | ||
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent | ||
*/ | ||
public function testCreateUser() | ||
{ | ||
|
@@ -1084,6 +1079,114 @@ public function testCreateUserThrowsInvalidArgumentException() | |
$this->fail('Expected ValidationError messages did not occur.'); | ||
} | ||
|
||
/** | ||
* Test for the createUser() method. | ||
* | ||
* @covers \eZ\Publish\API\Repository\UserService::createUser | ||
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser | ||
*/ | ||
public function testCreateUserWithEmailAlreadyTaken(): void | ||
{ | ||
$repository = $this->getRepository(); | ||
|
||
$userContentType = $this->createUserContentTypeWithAccountSettings('user_email_unique', [ | ||
Type::REQUIRE_UNIQUE_EMAIL => true, | ||
]); | ||
|
||
$existingUser = $this->createUserVersion1( | ||
'existing_user', | ||
'[email protected]', | ||
$userContentType, | ||
); | ||
|
||
$editorsGroupId = $this->generateId('group', 13); | ||
/* BEGIN: Use Case */ | ||
// $editorsGroupId is the ID of the "Editors" user group in an eZ | ||
// Publish demo installation | ||
|
||
$userService = $repository->getUserService(); | ||
|
||
// Instantiate a create struct with mandatory properties | ||
$userCreate = $userService->newUserCreateStruct( | ||
'another_user', | ||
// email is already taken | ||
'[email protected]', | ||
'[email protected]', | ||
'eng-US', | ||
$userContentType | ||
); | ||
|
||
$userCreate->setField('first_name', 'Example'); | ||
$userCreate->setField('last_name', 'User'); | ||
|
||
// Load parent group for the user | ||
$group = $userService->loadUserGroup($editorsGroupId); | ||
|
||
try { | ||
// This call will fail with a "ContentFieldValidationException", because the | ||
// user with "[email protected]" email already exists in database. | ||
$userService->createUser($userCreate, [$group]); | ||
} catch (ContentFieldValidationException $e) { | ||
// Exception is caught, as there is no other way to check exception properties. | ||
$this->assertValidationErrorOccurs($e, 'Email \'%email%\' is used by another user. You must enter a unique email.'); | ||
|
||
return; | ||
} | ||
|
||
$this->fail('Expected ValidationError messages did not occur.'); | ||
} | ||
|
||
/** | ||
* Test for the createUser() method. | ||
* | ||
* @covers \eZ\Publish\API\Repository\UserService::createUser | ||
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser | ||
*/ | ||
public function testCreateInvalidFormatUsername(): void | ||
{ | ||
$repository = $this->getRepository(); | ||
|
||
$userContentType = $this->createUserContentTypeWithAccountSettings('username_format', [ | ||
Type::USERNAME_PATTERN => '^[^@]$', | ||
]); | ||
|
||
$editorsGroupId = $this->generateId('group', 13); | ||
/* BEGIN: Use Case */ | ||
// $editorsGroupId is the ID of the "Editors" user group in an eZ | ||
// Publish demo installation | ||
|
||
$userService = $repository->getUserService(); | ||
|
||
// Instantiate a create struct with mandatory properties | ||
$userCreate = $userService->newUserCreateStruct( | ||
// login contains @ | ||
'invalid@user', | ||
'[email protected]', | ||
'[email protected]', | ||
'eng-US', | ||
$userContentType | ||
); | ||
|
||
$userCreate->setField('first_name', 'Example'); | ||
$userCreate->setField('last_name', 'User'); | ||
|
||
// Load parent group for the user | ||
$group = $userService->loadUserGroup($editorsGroupId); | ||
|
||
try { | ||
// This call will fail with a "ContentFieldValidationException", because the | ||
// user with "invalid@user" login does not match "^[^@]$" pattern. | ||
$userService->createUser($userCreate, [$group]); | ||
} catch (ContentFieldValidationException $e) { | ||
// Exception is caught, as there is no other way to check exception properties. | ||
$this->assertValidationErrorOccurs($e, 'Invalid login format'); | ||
|
||
return; | ||
} | ||
|
||
$this->fail('Expected ValidationError messages did not occur.'); | ||
} | ||
|
||
/** | ||
* Test for the createUser() method. | ||
* | ||
|
@@ -1092,7 +1195,6 @@ public function testCreateUserThrowsInvalidArgumentException() | |
* @see \eZ\Publish\API\Repository\UserService::createUser() | ||
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testLoadUserGroup | ||
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testNewUserCreateStruct | ||
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testCreateContent | ||
*/ | ||
public function testCreateUserInTransactionWithRollback() | ||
{ | ||
|
@@ -1253,7 +1355,7 @@ public function testLoadUserThrowsNotFoundException() | |
|
||
/** | ||
* @see \eZ\Publish\API\Repository\UserService::checkUserCredentials() | ||
* @depends \eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser | ||
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser | ||
*/ | ||
public function testCheckUserCredentialsValid(): void | ||
{ | ||
|
@@ -1265,15 +1367,15 @@ public function testCheckUserCredentialsValid(): void | |
$user = $this->createUserVersion1(); | ||
|
||
// Load the newly created user credentials | ||
$credentialsValid = $userService->loadUserByCredentials($user, 'secret'); | ||
$credentialsValid = $userService->checkUserCredentials($user, '[email protected]'); | ||
/* END: Use Case */ | ||
|
||
$this->assertTrue($credentialsValid); | ||
} | ||
|
||
/** | ||
* @see \eZ\Publish\API\Repository\UserService::checkUserCredentials() | ||
* @depends \eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser | ||
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser | ||
*/ | ||
public function testCheckUserCredentialsInvalid(): void | ||
{ | ||
|
@@ -1285,7 +1387,7 @@ public function testCheckUserCredentialsInvalid(): void | |
$user = $this->createUserVersion1(); | ||
|
||
// Load the newly created user credentials | ||
$credentialsValid = $userService->loadUserByCredentials($user, '1234'); | ||
$credentialsValid = $userService->checkUserCredentials($user, 'NotSoSecretPassword'); | ||
/* END: Use Case */ | ||
|
||
$this->assertFalse($credentialsValid); | ||
|
@@ -1559,8 +1661,6 @@ public function testNewUserUpdateStruct() | |
* @see \eZ\Publish\API\Repository\UserService::updateUser() | ||
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser | ||
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testNewUserUpdateStruct | ||
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent | ||
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata | ||
*/ | ||
public function testUpdateUser() | ||
{ | ||
|
@@ -1623,8 +1723,6 @@ public function testUpdateUserEmail(): void | |
* @see \eZ\Publish\API\Repository\UserService::updateUser() | ||
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testCreateUser | ||
* @depends eZ\Publish\API\Repository\Tests\UserServiceTest::testNewUserUpdateStruct | ||
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContent | ||
* @depends eZ\Publish\API\Repository\Tests\ContentServiceTest::testUpdateContentMetadata | ||
*/ | ||
public function testUpdateUserNoPassword() | ||
{ | ||
|
@@ -3031,7 +3129,7 @@ private function createUserContentTypeWithAccountSettings( | |
|
||
$typeCreate->addFieldDefinition($lastNameFieldCreate); | ||
|
||
$accountFieldCreateStruct = $contentTypeService->newFieldDefinitionCreateStruct('account', 'ezuser'); | ||
$accountFieldCreateStruct = $contentTypeService->newFieldDefinitionCreateStruct('user_account', 'ezuser'); | ||
$accountFieldCreateStruct->names = [ | ||
'eng-GB' => 'User account', | ||
]; | ||
|
Oops, something went wrong.