You are here

public function UserMailRequiredValidatorTest::testValidate in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/user/tests/src/Unit/Plugin/Validation/Constraint/UserMailRequiredValidatorTest.php \Drupal\Tests\user\Unit\Plugin\Validation\Constraint\UserMailRequiredValidatorTest::testValidate()

@covers ::validate

@dataProvider providerTestValidate

File

core/modules/user/tests/src/Unit/Plugin/Validation/Constraint/UserMailRequiredValidatorTest.php, line 58

Class

UserMailRequiredValidatorTest
@coversDefaultClass \Drupal\user\Plugin\Validation\Constraint\UserMailRequiredValidator @group user

Namespace

Drupal\Tests\user\Unit\Plugin\Validation\Constraint

Code

public function testValidate($items, $expected_violation, $is_admin = FALSE) {
  $constraint = new UserMailRequired();

  // If a violation is expected, then the context's addViolation method will
  // be called, otherwise it should not be called.
  $context = $this
    ->prophesize(ExecutionContextInterface::class);
  if ($expected_violation) {
    $context
      ->addViolation('@name field is required.', [
      '@name' => 'Email',
    ])
      ->shouldBeCalledTimes(1);
  }
  else {
    $context
      ->addViolation()
      ->shouldNotBeCalled();
  }
  $validator = $this
    ->createValidator($is_admin);
  $validator
    ->initialize($context
    ->reveal());
  $validator
    ->validate($items, $constraint);
}