You are here

class DependentAccessTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/block_content/tests/src/Unit/Access/DependentAccessTest.php \Drupal\Tests\block_content\Unit\Access\DependentAccessTest
  2. 10 core/modules/block_content/tests/src/Unit/Access/DependentAccessTest.php \Drupal\Tests\block_content\Unit\Access\DependentAccessTest

@coversDefaultClass \Drupal\block_content\Access\RefinableDependentAccessTrait

@group block_content

Hierarchy

Expanded class hierarchy of DependentAccessTest

File

core/modules/block_content/tests/src/Unit/Access/DependentAccessTest.php, line 17

Namespace

Drupal\Tests\block_content\Unit\Access
View source
class DependentAccessTest extends UnitTestCase {
  use AccessibleTestingTrait;

  /**
   * An accessible object that results in forbidden access result.
   *
   * @var \Drupal\Core\Access\AccessibleInterface
   */
  protected $forbidden;

  /**
   * An accessible object that results in neutral access result.
   *
   * @var \Drupal\Core\Access\AccessibleInterface
   */
  protected $neutral;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->account = $this
      ->prophesize(AccountInterface::class)
      ->reveal();
    $this->forbidden = $this
      ->createAccessibleDouble(AccessResult::forbidden('Because I said so'));
    $this->neutral = $this
      ->createAccessibleDouble(AccessResult::neutral('I have no opinion'));
  }

  /**
   * Test that the previous dependency is replaced when using set.
   *
   * @covers ::setAccessDependency
   *
   * @dataProvider providerTestSetFirst
   */
  public function testSetAccessDependency($use_set_first) {
    $testRefinable = new RefinableDependentAccessTraitTestClass();
    if ($use_set_first) {
      $testRefinable
        ->setAccessDependency($this->forbidden);
    }
    else {
      $testRefinable
        ->addAccessDependency($this->forbidden);
    }
    $accessResult = $testRefinable
      ->getAccessDependency()
      ->access('view', $this->account, TRUE);
    $this
      ->assertTrue($accessResult
      ->isForbidden());
    $this
      ->assertEquals('Because I said so', $accessResult
      ->getReason());

    // Calling setAccessDependency() replaces the existing dependency.
    $testRefinable
      ->setAccessDependency($this->neutral);
    $dependency = $testRefinable
      ->getAccessDependency();
    $this
      ->assertNotInstanceOf(AccessGroupAnd::class, $dependency);
    $accessResult = $dependency
      ->access('view', $this->account, TRUE);
    $this
      ->assertTrue($accessResult
      ->isNeutral());
    $this
      ->assertEquals('I have no opinion', $accessResult
      ->getReason());
  }

  /**
   * Tests merging a new dependency with existing non-group access dependency.
   *
   * @dataProvider providerTestSetFirst
   */
  public function testMergeNonGroup($use_set_first) {
    $testRefinable = new RefinableDependentAccessTraitTestClass();
    if ($use_set_first) {
      $testRefinable
        ->setAccessDependency($this->forbidden);
    }
    else {
      $testRefinable
        ->addAccessDependency($this->forbidden);
    }
    $accessResult = $testRefinable
      ->getAccessDependency()
      ->access('view', $this->account, TRUE);
    $this
      ->assertTrue($accessResult
      ->isForbidden());
    $this
      ->assertEquals('Because I said so', $accessResult
      ->getReason());
    $testRefinable
      ->addAccessDependency($this->neutral);

    /** @var \Drupal\block_content\Access\AccessGroupAnd $dependency */
    $dependency = $testRefinable
      ->getAccessDependency();

    // Ensure the new dependency create a new AND group when merged.
    $this
      ->assertInstanceOf(AccessGroupAnd::class, $dependency);
    $dependencies = $dependency
      ->getDependencies();
    $accessResultForbidden = $dependencies[0]
      ->access('view', $this->account, TRUE);
    $this
      ->assertTrue($accessResultForbidden
      ->isForbidden());
    $this
      ->assertEquals('Because I said so', $accessResultForbidden
      ->getReason());
    $accessResultNeutral = $dependencies[1]
      ->access('view', $this->account, TRUE);
    $this
      ->assertTrue($accessResultNeutral
      ->isNeutral());
    $this
      ->assertEquals('I have no opinion', $accessResultNeutral
      ->getReason());
  }

  /**
   * Tests merging a new dependency with an existing access group dependency.
   *
   * @dataProvider providerTestSetFirst
   */
  public function testMergeGroup($use_set_first) {
    $andGroup = new AccessGroupAnd();
    $andGroup
      ->addDependency($this->forbidden);
    $testRefinable = new RefinableDependentAccessTraitTestClass();
    if ($use_set_first) {
      $testRefinable
        ->setAccessDependency($andGroup);
    }
    else {
      $testRefinable
        ->addAccessDependency($andGroup);
    }
    $testRefinable
      ->addAccessDependency($this->neutral);

    /** @var \Drupal\block_content\Access\AccessGroupAnd $dependency */
    $dependency = $testRefinable
      ->getAccessDependency();

    // Ensure the new dependency is merged with the existing group.
    $this
      ->assertInstanceOf(AccessGroupAnd::class, $dependency);
    $dependencies = $dependency
      ->getDependencies();
    $accessResultForbidden = $dependencies[0]
      ->access('view', $this->account, TRUE);
    $this
      ->assertTrue($accessResultForbidden
      ->isForbidden());
    $this
      ->assertEquals('Because I said so', $accessResultForbidden
      ->getReason());
    $accessResultNeutral = $dependencies[1]
      ->access('view', $this->account, TRUE);
    $this
      ->assertTrue($accessResultNeutral
      ->isNeutral());
    $this
      ->assertEquals('I have no opinion', $accessResultNeutral
      ->getReason());
  }

  /**
   * Dataprovider for all test methods.
   *
   * Provides test cases for calling setAccessDependency() or
   * mergeAccessDependency() first. A call to either should behave the same on a
   * new RefinableDependentAccessInterface object.
   */
  public function providerTestSetFirst() {
    return [
      [
        TRUE,
      ],
      [
        FALSE,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AccessibleTestingTrait::$account protected property The test account.
AccessibleTestingTrait::createAccessibleDouble private function Creates AccessibleInterface object from access result object for testing.
DependentAccessTest::$forbidden protected property An accessible object that results in forbidden access result.
DependentAccessTest::$neutral protected property An accessible object that results in neutral access result.
DependentAccessTest::providerTestSetFirst public function Dataprovider for all test methods.
DependentAccessTest::setUp protected function Overrides UnitTestCase::setUp
DependentAccessTest::testMergeGroup public function Tests merging a new dependency with an existing access group dependency.
DependentAccessTest::testMergeNonGroup public function Tests merging a new dependency with existing non-group access dependency.
DependentAccessTest::testSetAccessDependency public function Test that the previous dependency is replaced when using set.
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.