View source
<?php
namespace Drupal\Tests\user\Unit\Plugin\Validation\Constraint;
use Drupal\Tests\UnitTestCase;
use Drupal\user\Entity\User;
use Drupal\user\Plugin\Validation\Constraint\ProtectedUserFieldConstraint;
use Drupal\user\Plugin\Validation\Constraint\ProtectedUserFieldConstraintValidator;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
class ProtectedUserFieldConstraintValidatorTest extends UnitTestCase {
protected function createValidator() {
$unchanged_field = $this
->createMock('Drupal\\Core\\Field\\FieldItemListInterface');
$unchanged_field
->expects($this
->any())
->method('getValue')
->willReturn('unchanged-value');
$unchanged_account = $this
->createMock('Drupal\\user\\UserInterface');
$unchanged_account
->expects($this
->any())
->method('get')
->willReturn($unchanged_field);
$user_storage = $this
->createMock('Drupal\\user\\UserStorageInterface');
$user_storage
->expects($this
->any())
->method('loadUnchanged')
->willReturn($unchanged_account);
$current_user = $this
->createMock('Drupal\\Core\\Session\\AccountProxyInterface');
$current_user
->expects($this
->any())
->method('id')
->willReturn('current-user');
return new ProtectedUserFieldConstraintValidator($user_storage, $current_user);
}
public function testValidate($items, $expected_violation, $name = FALSE) {
$constraint = new ProtectedUserFieldConstraint();
$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() {
$cases = [];
$cases[] = [
NULL,
FALSE,
];
$field_definition = $this
->createMock('Drupal\\Core\\Field\\FieldDefinitionInterface');
$items = $this
->createMock('Drupal\\Core\\Field\\FieldItemListInterface');
$items
->expects($this
->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items
->expects($this
->once())
->method('getEntity')
->willReturn(NULL);
$cases[] = [
$items,
FALSE,
];
$field_definition = $this
->createMock('Drupal\\Core\\Field\\FieldDefinitionInterface');
$account = $this
->createMock(User::class);
$account->_skipProtectedUserFieldConstraint = TRUE;
$items = $this
->createMock('Drupal\\Core\\Field\\FieldItemListInterface');
$items
->expects($this
->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items
->expects($this
->once())
->method('getEntity')
->willReturn($account);
$cases[] = [
$items,
FALSE,
];
$field_definition = $this
->createMock('Drupal\\Core\\Field\\FieldDefinitionInterface');
$account = $this
->createMock('Drupal\\user\\UserInterface');
$account
->expects($this
->once())
->method('isNew')
->willReturn(TRUE);
$items = $this
->createMock('Drupal\\Core\\Field\\FieldItemListInterface');
$items
->expects($this
->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items
->expects($this
->once())
->method('getEntity')
->willReturn($account);
$cases[] = [
$items,
FALSE,
];
$account = $this
->createMock('Drupal\\user\\UserInterface');
$account
->expects($this
->once())
->method('isNew')
->willReturn(FALSE);
$account
->expects($this
->once())
->method('id')
->willReturn('not-current-user');
$items = $this
->createMock('Drupal\\Core\\Field\\FieldItemListInterface');
$items
->expects($this
->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items
->expects($this
->once())
->method('getEntity')
->willReturn($account);
$cases[] = [
$items,
FALSE,
];
$field_definition = $this
->createMock('Drupal\\Core\\Field\\FieldDefinitionInterface');
$field_definition
->expects($this
->exactly(2))
->method('getName')
->willReturn('field_not_password');
$account = $this
->createMock('Drupal\\user\\UserInterface');
$account
->expects($this
->once())
->method('isNew')
->willReturn(FALSE);
$account
->expects($this
->exactly(2))
->method('id')
->willReturn('current-user');
$account
->expects($this
->never())
->method('checkExistingPassword');
$items = $this
->createMock('Drupal\\Core\\Field\\FieldItemListInterface');
$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('Drupal\\Core\\Field\\FieldDefinitionInterface');
$field_definition
->expects($this
->once())
->method('getName')
->willReturn('pass');
$account = $this
->createMock('Drupal\\user\\UserInterface');
$account
->expects($this
->once())
->method('isNew')
->willReturn(FALSE);
$account
->expects($this
->exactly(2))
->method('id')
->willReturn('current-user');
$account
->expects($this
->never())
->method('checkExistingPassword');
$items = $this
->createMock('Drupal\\Core\\Field\\FieldItemListInterface');
$items
->expects($this
->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items
->expects($this
->once())
->method('getEntity')
->willReturn($account);
$cases[] = [
$items,
FALSE,
];
$field_definition = $this
->createMock('Drupal\\Core\\Field\\FieldDefinitionInterface');
$field_definition
->expects($this
->exactly(2))
->method('getName')
->willReturn('field_not_password');
$account = $this
->createMock('Drupal\\user\\UserInterface');
$account
->expects($this
->once())
->method('isNew')
->willReturn(FALSE);
$account
->expects($this
->exactly(2))
->method('id')
->willReturn('current-user');
$account
->expects($this
->once())
->method('checkExistingPassword')
->willReturn(TRUE);
$items = $this
->createMock('Drupal\\Core\\Field\\FieldItemListInterface');
$items
->expects($this
->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items
->expects($this
->once())
->method('getEntity')
->willReturn($account);
$items
->expects($this
->once())
->method('getValue')
->willReturn('changed-value');
$cases[] = [
$items,
FALSE,
];
$field_definition = $this
->createMock('Drupal\\Core\\Field\\FieldDefinitionInterface');
$field_definition
->expects($this
->exactly(2))
->method('getName')
->willReturn('pass');
$account = $this
->createMock('Drupal\\user\\UserInterface');
$account
->expects($this
->once())
->method('isNew')
->willReturn(FALSE);
$account
->expects($this
->exactly(2))
->method('id')
->willReturn('current-user');
$account
->expects($this
->once())
->method('checkExistingPassword')
->willReturn(TRUE);
$items = $this
->createMock('Drupal\\Core\\Field\\FieldItemListInterface');
$items
->expects($this
->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items
->expects($this
->once())
->method('getEntity')
->willReturn($account);
$items
->expects($this
->any())
->method('getValue')
->willReturn('changed-value');
$items
->expects($this
->once())
->method('__get')
->with('value')
->willReturn('changed-value');
$cases[] = [
$items,
FALSE,
];
$field_definition = $this
->createMock('Drupal\\Core\\Field\\FieldDefinitionInterface');
$field_definition
->expects($this
->exactly(2))
->method('getName')
->willReturn('pass');
$field_definition
->expects($this
->any())
->method('getLabel')
->willReturn('Password');
$account = $this
->createMock('Drupal\\user\\UserInterface');
$account
->expects($this
->once())
->method('isNew')
->willReturn(FALSE);
$account
->expects($this
->exactly(2))
->method('id')
->willReturn('current-user');
$account
->expects($this
->once())
->method('checkExistingPassword')
->willReturn(FALSE);
$items = $this
->createMock('Drupal\\Core\\Field\\FieldItemListInterface');
$items
->expects($this
->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items
->expects($this
->once())
->method('getEntity')
->willReturn($account);
$items
->expects($this
->once())
->method('getValue')
->willReturn('changed-value');
$items
->expects($this
->once())
->method('__get')
->with('value')
->willReturn('changed-value');
$cases[] = [
$items,
TRUE,
'Password',
];
$field_definition = $this
->createMock('Drupal\\Core\\Field\\FieldDefinitionInterface');
$field_definition
->expects($this
->exactly(2))
->method('getName')
->willReturn('field_not_password');
$field_definition
->expects($this
->any())
->method('getLabel')
->willReturn('Protected field');
$account = $this
->createMock('Drupal\\user\\UserInterface');
$account
->expects($this
->once())
->method('isNew')
->willReturn(FALSE);
$account
->expects($this
->exactly(2))
->method('id')
->willReturn('current-user');
$account
->expects($this
->once())
->method('checkExistingPassword')
->willReturn(FALSE);
$items = $this
->createMock('Drupal\\Core\\Field\\FieldItemListInterface');
$items
->expects($this
->once())
->method('getFieldDefinition')
->willReturn($field_definition);
$items
->expects($this
->once())
->method('getEntity')
->willReturn($account);
$items
->expects($this
->once())
->method('getValue')
->willReturn('changed-value');
$cases[] = [
$items,
TRUE,
'Protected field',
];
return $cases;
}
}