ProtectedUserFieldConstraintValidatorTest.php in Lightweight Directory Access Protocol (LDAP) 8.4
File
ldap_user/tests/src/Unit/ProtectedUserFieldConstraintValidatorTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\ldap_user\Unit;
use Drupal\ldap_authentication\Controller\LoginValidatorBase;
use Drupal\ldap_authentication\Controller\LoginValidatorLoginForm;
use Drupal\ldap_user\Plugin\Validation\Constraint\LdapProtectedUserFieldConstraint;
use Drupal\ldap_user\Plugin\Validation\Constraint\LdapProtectedUserFieldConstraintValidator;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\user\UserInterface;
use Drupal\user\UserStorageInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
class ProtectedUserFieldConstraintValidatorTest extends UnitTestCase {
protected function createValidator() : LdapProtectedUserFieldConstraintValidator {
$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;
}
public function testValidate($items, $expected_violation, $name = FALSE) : void {
$constraint = new LdapProtectedUserFieldConstraint();
$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);
}
public function providerTestValidate() : array {
$cases = [];
$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,
];
$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,
];
$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,
];
$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,
];
$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,
];
$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;
}
public function testSuccess() : void {
$constraint = new LdapProtectedUserFieldConstraint();
$context = $this
->createMock(ExecutionContextInterface::class);
$context
->expects($this
->never())
->method('addViolation');
$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);
}
}