diff --git a/core/modules/user/tests/src/Unit/Plugin/Validation/Constraint/ProtectedUserFieldConstraintValidatorTest.php b/core/modules/user/tests/src/Unit/Plugin/Validation/Constraint/ProtectedUserFieldConstraintValidatorTest.php index b822517..39c695c 100644 --- a/core/modules/user/tests/src/Unit/Plugin/Validation/Constraint/ProtectedUserFieldConstraintValidatorTest.php +++ b/core/modules/user/tests/src/Unit/Plugin/Validation/Constraint/ProtectedUserFieldConstraintValidatorTest.php @@ -7,31 +7,35 @@ namespace Drupal\Tests\user\Unit\Plugin\Validation\Constraint; -use Drupal\Tests\UnitTestCase; use Drupal\user\Plugin\Validation\Constraint\ProtectedUserFieldConstraint; use Drupal\user\Plugin\Validation\Constraint\ProtectedUserFieldConstraintValidator; -use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest; +use Symfony\Component\Validator\Validation; /** * @coversDefaultClass \Drupal\user\Plugin\Validation\Constraint\ProtectedUserFieldConstraintValidator * @group user */ -class ProtectedUserFieldConstraintValidatorTest extends UnitTestCase { +class ProtectedUserFieldConstraintValidatorTest extends AbstractConstraintValidatorTest { /** - * @var \Drupal\user\Plugin\Validation\Constraint\ProtectedUserFieldConstraint + * {@inheritdoc} */ - protected $constraint; + protected function setUp() { + parent::setUp(); + } /** - * @var \Drupal\user\Plugin\Validation\Constraint\ProtectedUserFieldConstraintValidator + * {@inheritdoc} */ - protected $validator; + protected function getApiVersion() { + return Validation::API_VERSION_2_5; + } /** * {@inheritdoc} */ - public function setUp() { + protected function createValidator() { // Setup mocks that don't need to change. $unchanged_field = $this->getMock('Drupal\Core\Field\FieldItemListInterface'); $unchanged_field->expects($this->any()) @@ -42,7 +46,6 @@ public function setUp() { $unchanged_account->expects($this->any()) ->method('get') ->willReturn($unchanged_field); - $user_storage = $this->getMock('Drupal\user\UserStorageInterface'); $user_storage->expects($this->any()) ->method('loadUnchanged') @@ -51,9 +54,7 @@ public function setUp() { $current_user->expects($this->any()) ->method('id') ->willReturn('current-user'); - - $this->validator = new ProtectedUserFieldConstraintValidator($user_storage, $current_user); - $this->constraint = new ProtectedUserFieldConstraint(); + return new ProtectedUserFieldConstraintValidator($user_storage, $current_user); } /** @@ -61,15 +62,17 @@ public function setUp() { * * @dataProvider providerTestValidate */ - public function testValidate($items, $expected_violation) { - // If a violation is expected, then the context's addViolation method will - // be called, otherwise it should not be called. - $context = $this->getMock('Symfony\Component\Validator\ExecutionContextInterface'); - $context->expects($expected_violation ? $this->once() : $this->never()) - ->method('addViolation'); - $this->validator->initialize($context); - - $this->validator->validate($items, $this->constraint); + public function testValidate($items, $expected_violation, $name = FALSE) { + $constraint = new ProtectedUserFieldConstraint(); + $this->validator->validate($items, $constraint); + if ($expected_violation) { + $this->buildViolation($constraint->message) + ->setParameter('%name', $name) + ->assertRaised(); + } + else { + $this->assertNoViolation(); + } } /** @@ -252,6 +255,9 @@ public function providerTestValidate() { $field_definition->expects($this->exactly(2)) ->method('getName') ->willReturn('pass'); + $field_definition->expects($this->any()) + ->method('getLabel') + ->willReturn('Password'); $account = $this->getMock('Drupal\user\UserInterface'); $account->expects($this->once()) ->method('isNew') @@ -276,13 +282,16 @@ public function providerTestValidate() { ->method('__get') ->with('value') ->willReturn('changed-value'); - $cases[] = [$items, TRUE]; + $cases[] = [$items, TRUE, 'Password']; // Case 11: Non-password field changed, current password not confirmed. $field_definition = $this->getMock('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->getMock('Drupal\user\UserInterface'); $account->expects($this->once()) ->method('isNew') @@ -303,7 +312,7 @@ public function providerTestValidate() { $items->expects($this->once()) ->method('getValue') ->willReturn('changed-value'); - $cases[] = [$items, TRUE]; + $cases[] = [$items, TRUE, 'Protected field']; return $cases; }