You are here

class EntityAccessTest in Open Social 8.3

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

Unit test of entity_access_by_field hook_node_access implementation.

Hierarchy

Expanded class hierarchy of EntityAccessTest

File

modules/custom/entity_access_by_field/tests/src/Unit/EntityAccessTest.php, line 13

Namespace

Drupal\entity_access_by_field\Tests
View source
class EntityAccessTest extends UnitTestCase {

  /**
   * Tests the EntityAccessHelper::nodeAccessCheck for Neutral Access.
   */
  public function testNeutralAccess() {
    $node = $this
      ->prophesize(NodeInterface::class);
    $this->fieldType = $this
      ->randomMachineName();
    $fieldDefinitionInterface = $this
      ->getMock('Drupal\\Core\\Field\\FieldDefinitionInterface');
    $fieldDefinitionInterface
      ->expects($this
      ->once())
      ->method('getType')
      ->willReturn($this->fieldType);
    $node
      ->getFieldDefinitions()
      ->willReturn([
      $this->fieldType => $fieldDefinitionInterface,
    ]);
    $node = $node
      ->reveal();
    $op = 'view';
    $account = $this
      ->prophesize(AccountInterface::class)
      ->reveal();
    $access_result = EntityAccessHelper::nodeAccessCheck($node, $op, $account);
    $this
      ->assertEquals(0, $access_result);
    $this
      ->assertNotEquals(2, $access_result);
    $this
      ->assertNotEquals(1, $access_result);
  }

  /**
   * Tests the EntityAccessHelper::nodeAccessCheck for Forbidden Access.
   */
  public function testForbiddenAccess() {
    $node = $this
      ->prophesize(NodeInterface::class);
    $this->fieldId = 'node.article.field_content_visibility';
    $this->fieldValue = 'public';
    $this->fieldType = 'entity_access_field';
    $this->accountId = 5;
    $this->nodeOwnerId = 3;
    $fieldDefinitionInterface = $this
      ->getMock('Drupal\\Core\\Field\\FieldConfigInterface');
    $fieldDefinitionInterface
      ->expects($this
      ->once())
      ->method('getType')
      ->willReturn($this->fieldType);
    $fieldDefinitionInterface
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn('node.article.field_content_visibility');
    $fieldItemListInterface = $this
      ->getMock('Drupal\\Core\\Field\\FieldItemListInterface');
    $fieldItemListInterface
      ->expects($this
      ->any())
      ->method('getValue')
      ->willReturn([
      0 => [
        'value' => 'public',
      ],
    ]);
    $node
      ->get('entity_access_field')
      ->willReturn($fieldItemListInterface);
    $node
      ->getFieldDefinitions()
      ->willReturn([
      $this->fieldType => $fieldDefinitionInterface,
    ]);
    $node
      ->getOwnerId()
      ->willReturn($this->nodeOwnerId);
    $node = $node
      ->reveal();
    $op = 'view';
    $account = $this
      ->prophesize(AccountInterface::class);
    $account
      ->hasPermission('view ' . $this->fieldId . ':' . $this->fieldValue . ' content')
      ->willReturn(FALSE);
    $account
      ->id()
      ->willReturn($this->accountId);
    $account = $account
      ->reveal();
    $access_result = EntityAccessHelper::nodeAccessCheck($node, $op, $account);
    $this
      ->assertEquals(1, $access_result);
    $this
      ->assertNotEquals(0, $access_result);
    $this
      ->assertNotEquals(2, $access_result);
  }

  /**
   * Tests the EntityAccessHelper::nodeAccessCheck for Allowed Access.
   */
  public function testAllowedAccess() {
    $node = $this
      ->prophesize(NodeInterface::class);
    $this->fieldId = 'node.article.field_content_visibility';
    $this->fieldValue = 'public';
    $this->fieldType = 'entity_access_field';
    $this->accountId = 5;
    $this->nodeOwnerId = 3;
    $fieldDefinitionInterface = $this
      ->getMock('Drupal\\Core\\Field\\FieldConfigInterface');
    $fieldDefinitionInterface
      ->expects($this
      ->once())
      ->method('getType')
      ->willReturn($this->fieldType);
    $fieldDefinitionInterface
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn($this->fieldId);
    $fieldItemListInterface = $this
      ->getMock('Drupal\\Core\\Field\\FieldItemListInterface');
    $fieldItemListInterface
      ->expects($this
      ->any())
      ->method('getValue')
      ->willReturn([
      0 => [
        'value' => $this->fieldValue,
      ],
    ]);
    $node
      ->get('entity_access_field')
      ->willReturn($fieldItemListInterface);
    $node
      ->getFieldDefinitions()
      ->willReturn([
      $this->fieldType => $fieldDefinitionInterface,
    ]);
    $node
      ->getCacheContexts()
      ->willReturn(NULL);
    $node
      ->getOwnerId()
      ->willReturn($this->nodeOwnerId);
    $node = $node
      ->reveal();
    $op = 'view';
    $account = $this
      ->prophesize(AccountInterface::class);
    $account
      ->hasPermission('view ' . $this->fieldId . ':' . $this->fieldValue . ' content')
      ->willReturn(TRUE);
    $account
      ->id()
      ->willReturn($this->accountId);
    $account = $account
      ->reveal();
    $access_result = EntityAccessHelper::nodeAccessCheck($node, $op, $account);
    $this
      ->assertEquals(2, $access_result);
    $this
      ->assertNotEquals(0, $access_result);
    $this
      ->assertNotEquals(1, $access_result);
  }

  /**
   * Tests the EntityAccessHelper::nodeAccessCheck for Author Access Allowed.
   */
  public function testAuthorAccessAllowed() {
    $node = $this
      ->prophesize(NodeInterface::class);
    $this->fieldId = 'node.article.field_content_visibility';
    $this->fieldValue = 'nonexistant';
    $this->fieldType = 'entity_access_field';
    $this->accountId = 5;
    $this->nodeOwnerId = 5;
    $fieldDefinitionInterface = $this
      ->getMock('Drupal\\Core\\Field\\FieldConfigInterface');
    $fieldDefinitionInterface
      ->expects($this
      ->once())
      ->method('getType')
      ->willReturn($this->fieldType);
    $fieldDefinitionInterface
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn($this->fieldId);
    $fieldItemListInterface = $this
      ->getMock('Drupal\\Core\\Field\\FieldItemListInterface');
    $fieldItemListInterface
      ->expects($this
      ->any())
      ->method('getValue')
      ->willReturn([
      0 => [
        'value' => $this->fieldValue,
      ],
    ]);
    $node
      ->get('entity_access_field')
      ->willReturn($fieldItemListInterface);
    $node
      ->getFieldDefinitions()
      ->willReturn([
      $this->fieldType => $fieldDefinitionInterface,
    ]);
    $node
      ->getCacheContexts()
      ->willReturn(NULL);
    $node
      ->getOwnerId()
      ->willReturn($this->nodeOwnerId);
    $node = $node
      ->reveal();
    $op = 'view';
    $account = $this
      ->prophesize(AccountInterface::class);
    $account
      ->hasPermission('view ' . $this->fieldId . ':' . $this->fieldValue . ' content')
      ->willReturn(FALSE);
    $account
      ->id()
      ->willReturn($this->accountId);
    $account = $account
      ->reveal();
    $access_result = EntityAccessHelper::nodeAccessCheck($node, $op, $account);
    $this
      ->assertEquals(2, $access_result);
    $this
      ->assertNotEquals(0, $access_result);
    $this
      ->assertNotEquals(1, $access_result);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityAccessTest::testAllowedAccess public function Tests the EntityAccessHelper::nodeAccessCheck for Allowed Access.
EntityAccessTest::testAuthorAccessAllowed public function Tests the EntityAccessHelper::nodeAccessCheck for Author Access Allowed.
EntityAccessTest::testForbiddenAccess public function Tests the EntityAccessHelper::nodeAccessCheck for Forbidden Access.
EntityAccessTest::testNeutralAccess public function Tests the EntityAccessHelper::nodeAccessCheck for Neutral Access.
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