class EntityAccessTest in Open Social 8.2
Same name and namespace in other branches
- 8.9 modules/custom/entity_access_by_field/tests/src/Unit/EntityAccessTest.php \Drupal\entity_access_by_field\Tests\EntityAccessTest
- 8 modules/custom/entity_access_by_field/tests/src/Unit/EntityAccessTest.php \Drupal\entity_access_by_field\Tests\EntityAccessTest
- 8.3 modules/custom/entity_access_by_field/tests/src/Unit/EntityAccessTest.php \Drupal\entity_access_by_field\Tests\EntityAccessTest
- 8.4 modules/custom/entity_access_by_field/tests/src/Unit/EntityAccessTest.php \Drupal\entity_access_by_field\Tests\EntityAccessTest
- 8.5 modules/custom/entity_access_by_field/tests/src/Unit/EntityAccessTest.php \Drupal\entity_access_by_field\Tests\EntityAccessTest
- 8.6 modules/custom/entity_access_by_field/tests/src/Unit/EntityAccessTest.php \Drupal\entity_access_by_field\Tests\EntityAccessTest
- 8.7 modules/custom/entity_access_by_field/tests/src/Unit/EntityAccessTest.php \Drupal\entity_access_by_field\Tests\EntityAccessTest
- 8.8 modules/custom/entity_access_by_field/tests/src/Unit/EntityAccessTest.php \Drupal\entity_access_by_field\Tests\EntityAccessTest
- 10.3.x modules/custom/entity_access_by_field/tests/src/Unit/EntityAccessTest.php \Drupal\entity_access_by_field\Tests\EntityAccessTest
- 10.0.x modules/custom/entity_access_by_field/tests/src/Unit/EntityAccessTest.php \Drupal\entity_access_by_field\Tests\EntityAccessTest
- 10.1.x modules/custom/entity_access_by_field/tests/src/Unit/EntityAccessTest.php \Drupal\entity_access_by_field\Tests\EntityAccessTest
- 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
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\entity_access_by_field\Tests\EntityAccessTest
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\TestsView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityAccessTest:: |
public | function | Tests the EntityAccessHelper::nodeAccessCheck for Allowed Access. | |
EntityAccessTest:: |
public | function | Tests the EntityAccessHelper::nodeAccessCheck for Author Access Allowed. | |
EntityAccessTest:: |
public | function | Tests the EntityAccessHelper::nodeAccessCheck for Forbidden Access. | |
EntityAccessTest:: |
public | function | Tests the EntityAccessHelper::nodeAccessCheck for Neutral Access. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |