You are here

class PageAccessTest in Page Manager 8

Same name and namespace in other branches
  1. 8.4 tests/src/Unit/PageAccessTest.php \Drupal\Tests\page_manager\Unit\PageAccessTest

Tests access for Page entities.

@coversDefaultClass \Drupal\page_manager\Entity\PageAccess

@group PageManager

Hierarchy

Expanded class hierarchy of PageAccessTest

File

tests/src/Unit/PageAccessTest.php, line 29
Contains \Drupal\Tests\page_manager\Unit\PageAccessTest.

Namespace

Drupal\Tests\page_manager\Unit
View source
class PageAccessTest extends UnitTestCase {

  /**
   * The context handler.
   *
   * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $contextHandler;

  /**
   * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityType;

  /**
   * @var \Drupal\Core\Cache\Context\CacheContextsManager|\Prophecy\Prophecy\ProphecyInterface
   */
  protected $cacheContextsManager;

  /**
   * @var \Drupal\Core\Entity\EntityAccessControlHandlerInterface
   */
  protected $pageAccess;

  /**
   * @covers ::__construct
   */
  public function setUp() {
    parent::setUp();
    $this->contextHandler = $this
      ->prophesize(ContextHandlerInterface::class);
    $this->entityType = $this
      ->prophesize(EntityTypeInterface::class);
    $module_handler = $this
      ->prophesize(ModuleHandlerInterface::class);
    $module_handler
      ->invokeAll(Argument::cetera())
      ->willReturn([]);
    $this->pageAccess = new PageAccess($this->entityType
      ->reveal(), $this->contextHandler
      ->reveal());
    $this->pageAccess
      ->setModuleHandler($module_handler
      ->reveal());
    $this->cacheContextsManager = $this
      ->prophesize(CacheContextsManager::class);
    $container = new ContainerBuilder();
    $container
      ->set('cache_contexts_manager', $this->cacheContextsManager
      ->reveal());
    \Drupal::setContainer($container);
  }

  /**
   * @covers ::checkAccess
   */
  public function testAccessView() {
    $page = $this
      ->prophesize(PageInterface::class);
    $page
      ->getContexts()
      ->willReturn([]);
    $page
      ->getAccessConditions()
      ->willReturn([]);
    $page
      ->getAccessLogic()
      ->willReturn('and');
    $page
      ->status()
      ->willReturn(TRUE);
    $page
      ->language()
      ->willReturn($this
      ->prophesize(LanguageInterface::class)
      ->reveal());
    $page
      ->uuid()
      ->shouldBeCalled();
    $page
      ->getEntityTypeId()
      ->shouldBeCalled();
    $account = $this
      ->prophesize(AccountInterface::class);
    $this
      ->assertTrue($this->pageAccess
      ->access($page
      ->reveal(), 'view', $account
      ->reveal()));
  }

  /**
   * @covers ::checkAccess
   */
  public function testAccessViewDisabled() {
    $page = $this
      ->prophesize(PageInterface::class);
    $page
      ->status()
      ->willReturn(FALSE);
    $page
      ->getCacheTags()
      ->willReturn([
      'page:1',
    ]);
    $page
      ->getCacheContexts()
      ->willReturn([]);
    $page
      ->getCacheMaxAge()
      ->willReturn(0);
    $page
      ->language()
      ->willReturn($this
      ->prophesize(LanguageInterface::class)
      ->reveal());
    $page
      ->uuid()
      ->shouldBeCalled();
    $page
      ->getEntityTypeId()
      ->shouldBeCalled();
    $account = $this
      ->prophesize(AccountInterface::class);
    $this
      ->assertFalse($this->pageAccess
      ->access($page
      ->reveal(), 'view', $account
      ->reveal()));
  }

  /**
   * @covers ::checkAccess
   *
   * @dataProvider providerTestAccessDelete
   */
  public function testAccessDelete($is_new, $expected) {
    $this->entityType
      ->getAdminPermission()
      ->willReturn('test permission');
    $page = $this
      ->prophesize(PageInterface::class);
    $page
      ->isNew()
      ->willReturn($is_new);
    $page
      ->language()
      ->willReturn($this
      ->prophesize(LanguageInterface::class)
      ->reveal());
    $page
      ->uuid()
      ->shouldBeCalled();
    $page
      ->getEntityTypeId()
      ->shouldBeCalled();

    // Ensure that the cache tag is added for the temporary conditions.
    if ($is_new) {
      $page
        ->getCacheTags()
        ->willReturn([
        'page:1',
      ]);
      $page
        ->getCacheContexts()
        ->willReturn([]);
      $page
        ->getCacheMaxAge()
        ->willReturn(0);
    }
    else {
      $this->cacheContextsManager
        ->assertValidTokens([
        'user.permissions',
      ])
        ->willReturn(TRUE);
    }
    $account = $this
      ->prophesize(AccountInterface::class);
    $account
      ->hasPermission('test permission')
      ->willReturn(TRUE);
    $account
      ->id()
      ->shouldBeCalled();
    $this
      ->assertSame($expected, $this->pageAccess
      ->access($page
      ->reveal(), 'delete', $account
      ->reveal()));
  }

  /**
   * Provides data for testAccessDelete().
   */
  public function providerTestAccessDelete() {
    $data = [];
    $data[] = [
      TRUE,
      FALSE,
    ];
    $data[] = [
      FALSE,
      TRUE,
    ];
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PageAccessTest::$cacheContextsManager protected property
PageAccessTest::$contextHandler protected property The context handler.
PageAccessTest::$entityType protected property
PageAccessTest::$pageAccess protected property
PageAccessTest::providerTestAccessDelete public function Provides data for testAccessDelete().
PageAccessTest::setUp public function @covers ::__construct Overrides UnitTestCase::setUp
PageAccessTest::testAccessDelete public function @covers ::checkAccess
PageAccessTest::testAccessView public function @covers ::checkAccess
PageAccessTest::testAccessViewDisabled public function @covers ::checkAccess
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.