You are here

class LinkAccessConstraintValidatorTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkAccessConstraintValidatorTest.php \Drupal\Tests\link\Unit\Plugin\Validation\Constraint\LinkAccessConstraintValidatorTest
  2. 10 core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkAccessConstraintValidatorTest.php \Drupal\Tests\link\Unit\Plugin\Validation\Constraint\LinkAccessConstraintValidatorTest

Tests the LinkAccessConstraintValidator validator.

@coversDefaultClass \Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraintValidator @group validation

Hierarchy

Expanded class hierarchy of LinkAccessConstraintValidatorTest

File

core/modules/link/tests/src/Unit/Plugin/Validation/Constraint/LinkAccessConstraintValidatorTest.php, line 16

Namespace

Drupal\Tests\link\Unit\Plugin\Validation\Constraint
View source
class LinkAccessConstraintValidatorTest extends UnitTestCase {

  /**
   * Tests the \Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraintValidator::validate()
   * method.
   *
   * @param \Drupal\link\LinkItemInterface $value
   *   The link item.
   * @param \Drupal\Core\Session\AccountProxyInterface $user
   *   The user account.
   * @param bool $valid
   *   A boolean indicating if the combination is expected to be valid.
   *
   * @covers ::validate
   * @dataProvider providerValidate
   */
  public function testValidate($value, $user, $valid) {
    $context = $this
      ->createMock(ExecutionContextInterface::class);
    if ($valid) {
      $context
        ->expects($this
        ->never())
        ->method('addViolation');
    }
    else {
      $context
        ->expects($this
        ->once())
        ->method('addViolation');
    }
    $constraint = new LinkAccessConstraint();
    $validate = new LinkAccessConstraintValidator($user);
    $validate
      ->initialize($context);
    $validate
      ->validate($value, $constraint);
  }

  /**
   * Data provider for LinkAccessConstraintValidator::validate().
   *
   * @return array
   *   An array of tests, matching the parameter inputs for testValidate.
   *
   * @see \Drupal\Tests\link\LinkAccessConstraintValidatorTest::validate()
   */
  public function providerValidate() {
    $data = [];
    $cases = [
      [
        'may_link_any_page' => TRUE,
        'url_access' => TRUE,
        'valid' => TRUE,
      ],
      [
        'may_link_any_page' => TRUE,
        'url_access' => FALSE,
        'valid' => TRUE,
      ],
      [
        'may_link_any_page' => FALSE,
        'url_access' => TRUE,
        'valid' => TRUE,
      ],
      [
        'may_link_any_page' => FALSE,
        'url_access' => FALSE,
        'valid' => FALSE,
      ],
    ];
    foreach ($cases as $case) {

      // Mock a Url object that returns a boolean indicating user access.
      $url = $this
        ->getMockBuilder('Drupal\\Core\\Url')
        ->disableOriginalConstructor()
        ->getMock();
      $url
        ->expects($this
        ->once())
        ->method('access')
        ->willReturn($case['url_access']);

      // Mock a link object that returns the URL object.
      $link = $this
        ->createMock('Drupal\\link\\LinkItemInterface');
      $link
        ->expects($this
        ->any())
        ->method('getUrl')
        ->willReturn($url);

      // Mock a user object that returns a boolean indicating user access to all
      // links.
      $user = $this
        ->createMock('Drupal\\Core\\Session\\AccountProxyInterface');
      $user
        ->expects($this
        ->any())
        ->method('hasPermission')
        ->with($this
        ->equalTo('link to any page'))
        ->willReturn($case['may_link_any_page']);
      $data[] = [
        $link,
        $user,
        $case['valid'],
      ];
    }
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LinkAccessConstraintValidatorTest::providerValidate public function Data provider for LinkAccessConstraintValidator::validate().
LinkAccessConstraintValidatorTest::testValidate public function Tests the \Drupal\link\Plugin\Validation\Constraint\LinkAccessConstraintValidator::validate() method.
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.
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