You are here

public function SocialUserNameConstraintTest::testValidate in Open Social 8.4

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_user/tests/src/Unit/Validation/Constraint/SocialUserNameConstraintTest.php \Drupal\social_user\Tests\SocialUserNameConstraintTest::testValidate()
  2. 8 modules/social_features/social_user/tests/src/Unit/Validation/Constraint/SocialUserNameConstraintTest.php \Drupal\social_user\Tests\SocialUserNameConstraintTest::testValidate()
  3. 8.2 modules/social_features/social_user/tests/src/Unit/Validation/Constraint/SocialUserNameConstraintTest.php \Drupal\social_user\Tests\SocialUserNameConstraintTest::testValidate()
  4. 8.3 modules/social_features/social_user/tests/src/Unit/Validation/Constraint/SocialUserNameConstraintTest.php \Drupal\social_user\Tests\SocialUserNameConstraintTest::testValidate()
  5. 8.5 modules/social_features/social_user/tests/src/Unit/Validation/Constraint/SocialUserNameConstraintTest.php \Drupal\social_user\Tests\SocialUserNameConstraintTest::testValidate()
  6. 8.6 modules/social_features/social_user/tests/src/Unit/Validation/Constraint/SocialUserNameConstraintTest.php \Drupal\social_user\Tests\SocialUserNameConstraintTest::testValidate()
  7. 8.7 modules/social_features/social_user/tests/src/Unit/Validation/Constraint/SocialUserNameConstraintTest.php \Drupal\social_user\Tests\SocialUserNameConstraintTest::testValidate()
  8. 8.8 modules/social_features/social_user/tests/src/Unit/Validation/Constraint/SocialUserNameConstraintTest.php \Drupal\social_user\Tests\SocialUserNameConstraintTest::testValidate()
  9. 10.3.x modules/social_features/social_user/tests/src/Unit/Validation/Constraint/SocialUserNameConstraintTest.php \Drupal\social_user\Tests\SocialUserNameConstraintTest::testValidate()
  10. 10.0.x modules/social_features/social_user/tests/src/Unit/Validation/Constraint/SocialUserNameConstraintTest.php \Drupal\social_user\Tests\SocialUserNameConstraintTest::testValidate()
  11. 10.1.x modules/social_features/social_user/tests/src/Unit/Validation/Constraint/SocialUserNameConstraintTest.php \Drupal\social_user\Tests\SocialUserNameConstraintTest::testValidate()
  12. 10.2.x modules/social_features/social_user/tests/src/Unit/Validation/Constraint/SocialUserNameConstraintTest.php \Drupal\social_user\Tests\SocialUserNameConstraintTest::testValidate()

Test the SocialUserNameConstraint against the given asserts.

@covers ::validate

@dataProvider providerTestValidate

File

modules/social_features/social_user/tests/src/Unit/Validation/Constraint/SocialUserNameConstraintTest.php, line 24

Class

SocialUserNameConstraintTest
@coversDefaultClass \Drupal\social_user\Plugin\Validation\Constraint\SocialUserNameConstraintValidator

Namespace

Drupal\social_user\Tests

Code

public function testValidate($items, $expected_violation, $expected_definition_result = NULL, $name = NULL) {

  // Mock our typed data interface.
  $manager = $this
    ->getMock('Drupal\\Core\\TypedData\\TypedDataManagerInterface');
  $definition = $this
    ->getMock('Drupal\\Core\\TypedData\\TypedDataInterface');
  if ($expected_definition_result !== NULL) {
    $manager
      ->expects($this
      ->any())
      ->method('create')
      ->willReturn($definition);
    $definition
      ->expects($this
      ->any())
      ->method('validate')
      ->willReturn($expected_definition_result);
  }
  else {
    $manager
      ->expects($this
      ->never())
      ->method('create');
    $definition
      ->expects($this
      ->never())
      ->method('validate');
  }
  $constraint = new SocialUserNameConstraint();
  $constraintValidator = new SocialUserNameConstraintValidator($manager);

  // 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\\Context\\ExecutionContextInterface');
  if ($expected_violation) {
    $context
      ->expects($this
      ->any())
      ->method('addViolation')
      ->with($constraint->usernameIsEmailMessage);
  }
  else {
    $context
      ->expects($this
      ->never())
      ->method('addViolation');
  }
  $constraintValidator
    ->initialize($context);
  $constraintValidator
    ->validate($items, $constraint);

  // Validate Symfony.
  if ($name !== NULL) {
    $validator = new EmailValidator();
    $is_valid_email = $validator
      ->isValid($name);
    if ($expected_violation) {
      $this
        ->assertTrue($is_valid_email, "Exepected a valid email, found no invalid email");
    }
    else {
      $this
        ->assertFalse($is_valid_email, "Exepected an invalid email, found a valid email");
    }
  }
}