class ProtectedUserFieldConstraintValidatorTest in Lightweight Directory Access Protocol (LDAP) 8.4
Extended from core tests.
@group ldap
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\ldap_user\Unit\ProtectedUserFieldConstraintValidatorTest
Expanded class hierarchy of ProtectedUserFieldConstraintValidatorTest
File
- ldap_user/
tests/ src/ Unit/ ProtectedUserFieldConstraintValidatorTest.php, line 24
Namespace
Drupal\Tests\ldap_user\UnitView source
class ProtectedUserFieldConstraintValidatorTest extends UnitTestCase {
/**
* Creates a validator.
*
* @return \Drupal\ldap_user\Plugin\Validation\Constraint\LdapProtectedUserFieldConstraintValidator
* Validator.
*/
protected function createValidator() : LdapProtectedUserFieldConstraintValidator {
// Setup mocks that don't need to change.
$unchanged_field = $this
->createMock(FieldItemListInterface::class);
$unchanged_field
->expects($this
->any())
->method('getValue')
->willReturn('unchanged-value');
$unchanged_account = $this
->createMock(UserInterface::class);
$unchanged_account
->expects($this
->any())
->method('get')
->willReturn($unchanged_field);
$user_storage = $this
->createMock(UserStorageInterface::class);
$user_storage
->expects($this
->any())
->method('loadUnchanged')
->willReturn($unchanged_account);
$current_user = $this
->createMock(AccountProxyInterface::class);
$current_user
->expects($this
->any())
->method('id')
->willReturn('current-user');
$validator = new LdapProtectedUserFieldConstraintValidator($user_storage, $current_user);
$login_service = $this
->createMock(LoginValidatorLoginForm::class);
$login_service
->expects($this
->any())
->method('validateCredentialsLoggedIn')
->willReturn(LoginValidatorBase::AUTHENTICATION_FAILURE_CREDENTIALS);
$validator
->setLoginValidator($login_service);
return $validator;
}
/**
* Test validation.
*
* @dataProvider providerTestValidate
*/
public function testValidate($items, $expected_violation, $name = FALSE) : void {
$constraint = new LdapProtectedUserFieldConstraint();
// If a violation is expected, then the context's addViolation method will
// be called, otherwise it should not be called.
$context = $this
->createMock(ExecutionContextInterface::class);
if ($expected_violation) {
$context
->expects($this
->once())
->method('addViolation')
->with($constraint->message, [
'%name' => $name,
]);
}
else {
$context
->expects($this
->never())
->method('addViolation');
}
$validator = $this
->createValidator();
$validator
->initialize($context);
$validator
->validate($items, $constraint);
}
/**
* Data provider for ::testValidate().
*/
public function providerTestValidate() : array {
$cases = [];
// Case 0: Empty user should be ignored.
$field_definition = $this
->createMock(FieldDefinitionInterface::class);
$items = $this
->createMock(FieldItemListInterface::class);
$items
->expects($this
->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items
->expects($this
->once())
->method('getEntity')
->willReturn(NULL);
$cases[] = [
$items,
FALSE,
];
// Case 1: Account flagged to skip protected user should be ignored.
$field_definition = $this
->createMock(FieldDefinitionInterface::class);
$account = $this
->createMock(UserInterface::class);
$account->_skipProtectedUserFieldConstraint = TRUE;
$items = $this
->createMock(FieldItemListInterface::class);
$items
->expects($this
->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items
->expects($this
->once())
->method('getEntity')
->willReturn($account);
$cases[] = [
$items,
FALSE,
];
// Case 2: New user should be ignored.
$field_definition = $this
->createMock(FieldDefinitionInterface::class);
$account = $this
->createMock(UserInterface::class);
$account
->expects($this
->once())
->method('isNew')
->willReturn(TRUE);
$items = $this
->createMock(FieldItemListInterface::class);
$items
->expects($this
->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items
->expects($this
->once())
->method('getEntity')
->willReturn($account);
$cases[] = [
$items,
FALSE,
];
// Case 3: Non-password fields that have not changed should be ignored.
$field_definition = $this
->createMock(FieldDefinitionInterface::class);
$field_definition
->expects($this
->exactly(2))
->method('getName')
->willReturn('field_not_password');
$account = $this
->createMock(UserInterface::class);
$account
->expects($this
->once())
->method('isNew')
->willReturn(FALSE);
$account
->expects($this
->exactly(2))
->method('id')
->willReturn('current-user');
$items = $this
->createMock(FieldItemListInterface::class);
$items
->expects($this
->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items
->expects($this
->once())
->method('getEntity')
->willReturn($account);
$items
->expects($this
->once())
->method('getValue')
->willReturn('unchanged-value');
$cases[] = [
$items,
FALSE,
];
// Case 4: Password field with no value set should be ignored.
$field_definition = $this
->createMock(FieldDefinitionInterface::class);
$field_definition
->expects($this
->once())
->method('getName')
->willReturn('pass');
$account = $this
->createMock(UserInterface::class);
$account
->expects($this
->once())
->method('isNew')
->willReturn(FALSE);
$account
->expects($this
->exactly(2))
->method('id')
->willReturn('current-user');
$items = $this
->createMock(FieldItemListInterface::class);
$items
->expects($this
->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items
->expects($this
->once())
->method('getEntity')
->willReturn($account);
$cases[] = [
$items,
FALSE,
];
// Case 5: Non-password field changed, user wrong current password.
$field_definition = $this
->createMock(FieldDefinitionInterface::class);
$field_definition
->method('getName')
->willReturn('field_not_password');
$account = $this
->createMock(UserInterface::class);
$account
->method('isNew')
->willReturn(FALSE);
$account
->method('id')
->willReturn('current-user');
$pass = new \stdClass();
$pass->existing = 'existing';
$account
->expects($this
->once())
->method('get')
->willReturn($pass);
$items = $this
->createMock(FieldItemListInterface::class);
$items
->method('getFieldDefinition')
->willReturn($field_definition);
$items
->method('getEntity')
->willReturn($account);
$items
->method('getValue')
->willReturn('changed-value');
$cases[] = [
$items,
TRUE,
NULL,
];
return $cases;
}
/**
* Test validation.
*
* This could be optimized / simplified if we could override
* User::checkExistingPassword() instead of directly querying in the
* constraint.
*/
public function testSuccess() : void {
$constraint = new LdapProtectedUserFieldConstraint();
$context = $this
->createMock(ExecutionContextInterface::class);
$context
->expects($this
->never())
->method('addViolation');
// Case 6: Non-password field changed, user gave correct password.
$field_definition = $this
->createMock(FieldDefinitionInterface::class);
$field_definition
->method('getName')
->willReturn('field_not_password');
$account = $this
->createMock(UserInterface::class);
$account
->method('isNew')
->willReturn(FALSE);
$account
->method('id')
->willReturn('current-user');
$pass = new \stdClass();
$pass->existing = 'existing';
$account
->expects($this
->once())
->method('get')
->willReturn($pass);
$items = $this
->createMock(FieldItemListInterface::class);
$items
->method('getFieldDefinition')
->willReturn($field_definition);
$items
->method('getEntity')
->willReturn($account);
$items
->method('getValue')
->willReturn('changed-value');
$validator = $this
->createValidator();
$login_service = $this
->createMock(LoginValidatorLoginForm::class);
$login_service
->expects($this
->any())
->method('validateCredentialsLoggedIn')
->willReturn(LoginValidatorBase::AUTHENTICATION_SUCCESS);
$validator
->setLoginValidator($login_service);
$validator
->initialize($context);
$validator
->validate($items, $constraint);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
ProtectedUserFieldConstraintValidatorTest:: |
protected | function | Creates a validator. | |
ProtectedUserFieldConstraintValidatorTest:: |
public | function | Data provider for ::testValidate(). | |
ProtectedUserFieldConstraintValidatorTest:: |
public | function | Test validation. | |
ProtectedUserFieldConstraintValidatorTest:: |
public | function | Test validation. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |