You are here

public function ProtectedUserFieldConstraintValidatorTest::testSuccess in Lightweight Directory Access Protocol (LDAP) 8.4

Test validation.

This could be optimized / simplified if we could override User::checkExistingPassword() instead of directly querying in the constraint.

File

ldap_user/tests/src/Unit/ProtectedUserFieldConstraintValidatorTest.php, line 216

Class

ProtectedUserFieldConstraintValidatorTest
Extended from core tests.

Namespace

Drupal\Tests\ldap_user\Unit

Code

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);
}