You are here

class SocialUserNameConstraintTest in Open Social 8.9

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

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

@group user

Hierarchy

Expanded class hierarchy of SocialUserNameConstraintTest

File

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

Namespace

Drupal\social_user\Tests
View source
class SocialUserNameConstraintTest extends UnitTestCase {

  /**
   * Test the SocialUserNameConstraint against the given asserts.
   *
   * @covers ::validate
   *
   * @dataProvider providerTestValidate
   */
  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();
      $rfcValidation = new RFCValidation();
      $is_valid_email = $validator
        ->isValid($name, $rfcValidation);
      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");
      }
    }
  }

  /**
   * Data provider for ::testValidate().
   */
  public function providerTestValidate() {
    $cases = [];

    // Case 1: Validation context should not be touched if no items are passed.
    $cases[] = [
      NULL,
      FALSE,
    ];

    // Case 2: Empty user should be ignored.
    $field_definition = $this
      ->getMock('Drupal\\Core\\Field\\FieldDefinitionInterface');
    $items = $this
      ->getMock('Drupal\\Core\\Field\\FieldItemListInterface');
    $items
      ->expects($this
      ->any())
      ->method('getFieldDefinition')
      ->willReturn($field_definition);
    $items
      ->expects($this
      ->any())
      ->method('first')
      ->willReturn(NULL);
    $cases[] = [
      $items,
      FALSE,
    ];

    // Correct email cases.
    // See https://en.wikipedia.org/wiki/Email_address.
    $invalid_names = [
      'prettyandsimple@example.com',
      'very.common@example.com',
      'disposable.style.email.with+symbol@example.com',
      'other.email-with-dash@example.com',
      '"much.more unusual"@example.com',
      '"very.unusual.@.unusual.com"@example.com',
      'admin@mailserver1 (local domain name with no TLD)',
      '#!$%&\'*+-/=?^_`{}|~@example.org',
      '" "@example.org (space between the quotes)',
      'example@s.solutions (see the List of Internet top-level domains)',
      'example@s.solutions (see the List of Internet top-level domains)',
      'user@com',
      'user@localserver',
      'user@[IPv6:2001:db8::1]',
    ];
    foreach ($invalid_names as $name) {
      $cases[] = [
        $this
          ->itemsMock($name),
        TRUE,
        $this
          ->buildViolationList(0),
        $name,
      ];
    }

    // These names are valid names, but are validated as incorrect.
    // Because we use the same validation for the Emails it will not
    // Affect the email login system.
    $valid_names_but_valid_emails = [
      'Abc.example.com (no @ character)',
    ];
    $email_violations = 1;
    foreach ($valid_names_but_valid_emails as $name) {
      $cases[] = [
        $this
          ->itemsMock($name),
        FALSE,
        $this
          ->buildViolationList($email_violations),
        $name,
      ];
      $email_violations++;
    }
    $valid_names = [
      'admin',
      'user1',
      'haha',
      'neil goodman',
      'Abc.example.com',
      'ohn.doe@example..com',
      'this is"not\\allowed@example.com',
      'just"not"right@example.com',
      'john..doe@example.com',
      'a"b(c)d,e:f;g<h>i[j\\k]l@example.com',
      'A@b@c@example.com',
    ];
    $email_violations = 3;
    foreach ($valid_names as $name) {
      $cases[] = [
        $this
          ->itemsMock($name),
        FALSE,
        $this
          ->buildViolationList($email_violations),
        $name,
      ];
      $email_violations++;
    }
    return $cases;
  }

  /**
   * {@inheritdoc}
   */
  protected function itemsMock($name) {
    $name_field = $this
      ->getMock('Drupal\\Core\\Field\\FieldItemInterface');
    $name_field
      ->expects($this
      ->any())
      ->method('__get')
      ->willReturn($name);
    $field_definition = $this
      ->getMock('Drupal\\Core\\Field\\FieldDefinitionInterface');
    $items = $this
      ->getMock('Drupal\\Core\\Field\\FieldItemListInterface');
    $items
      ->expects($this
      ->any())
      ->method('getFieldDefinition')
      ->willReturn($field_definition);
    $items
      ->expects($this
      ->any())
      ->method('first')
      ->willReturn($name_field);
    return $items;
  }

  /**
   * Builds a list interface to return violations.
   *
   * @param int $number_of_items
   *   Number of items you want to build in the list.
   *
   * @return ConstraintViolationListInterface
   *   Mock constraintViolationItems with the count of $number_of_items.
   */
  protected function buildViolationList($number_of_items) {
    $violationList = [];
    for ($count = 0; $count < $number_of_items; $count++) {
      $violation = $this
        ->getMock('Symfony\\Component\\Validator\\ConstraintViolationInterface');
      $violationList[] = $violation;
    }
    return $violationList;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
SocialUserNameConstraintTest::buildViolationList protected function Builds a list interface to return violations.
SocialUserNameConstraintTest::itemsMock protected function
SocialUserNameConstraintTest::providerTestValidate public function Data provider for ::testValidate().
SocialUserNameConstraintTest::testValidate public function Test the SocialUserNameConstraint against the given asserts.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340