class EditEntityFieldAccessCheckTest in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/quickedit/tests/src/Unit/Access/EditEntityFieldAccessCheckTest.php \Drupal\Tests\quickedit\Unit\Access\EditEntityFieldAccessCheckTest
@coversDefaultClass \Drupal\quickedit\Access\EditEntityFieldAccessCheck @group Access @group quickedit
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
- class \Drupal\Tests\quickedit\Unit\Access\EditEntityFieldAccessCheckTest
Expanded class hierarchy of EditEntityFieldAccessCheckTest
File
- core/
modules/ quickedit/ tests/ src/ Unit/ Access/ EditEntityFieldAccessCheckTest.php, line 22 - Contains \Drupal\Tests\quickedit\Unit\Access\EditEntityFieldAccessCheckTest.
Namespace
Drupal\Tests\quickedit\Unit\AccessView source
class EditEntityFieldAccessCheckTest extends UnitTestCase {
/**
* The tested access checker.
*
* @var \Drupal\quickedit\Access\EditEntityFieldAccessCheck
*/
protected $editAccessCheck;
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->editAccessCheck = new EditEntityFieldAccessCheck();
$cache_contexts_manager = $this
->prophesize(CacheContextsManager::class);
$cache_contexts_manager
->assertValidTokens()
->willReturn(TRUE);
$cache_contexts_manager
->reveal();
$container = new Container();
$container
->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
}
/**
* Provides test data for testAccess().
*
* @see \Drupal\Tests\edit\Unit\quickedit\Access\EditEntityFieldAccessCheckTest::testAccess()
*/
public function providerTestAccess() {
$data = array();
$data[] = array(
TRUE,
TRUE,
AccessResult::allowed(),
);
$data[] = array(
FALSE,
TRUE,
AccessResult::neutral(),
);
$data[] = array(
TRUE,
FALSE,
AccessResult::neutral(),
);
$data[] = array(
FALSE,
FALSE,
AccessResult::neutral(),
);
return $data;
}
/**
* Tests the method for checking access to routes.
*
* @param bool $entity_is_editable
* Whether the subject entity is editable.
* @param bool $field_storage_is_accessible
* Whether the user has access to the field storage entity.
* @param \Drupal\Core\Access\AccessResult $expected_result
* The expected result of the access call.
*
* @dataProvider providerTestAccess
*/
public function testAccess($entity_is_editable, $field_storage_is_accessible, AccessResult $expected_result) {
$entity = $this
->createMockEntity();
$entity
->expects($this
->any())
->method('access')
->willReturn(AccessResult::allowedIf($entity_is_editable)
->cachePerPermissions());
$field_storage = $this
->getMock('Drupal\\field\\FieldStorageConfigInterface');
$field_storage
->expects($this
->any())
->method('access')
->willReturn(AccessResult::allowedIf($field_storage_is_accessible));
$expected_result
->cachePerPermissions();
$field_name = 'valid';
$entity_with_field = clone $entity;
$entity_with_field
->expects($this
->any())
->method('get')
->with($field_name)
->will($this
->returnValue($field_storage));
$entity_with_field
->expects($this
->once())
->method('hasTranslation')
->with(LanguageInterface::LANGCODE_NOT_SPECIFIED)
->will($this
->returnValue(TRUE));
$account = $this
->getMock('Drupal\\Core\\Session\\AccountInterface');
$access = $this->editAccessCheck
->access($entity_with_field, $field_name, LanguageInterface::LANGCODE_NOT_SPECIFIED, $account);
$this
->assertEquals($expected_result, $access);
}
/**
* Tests checking access to routes that result in AccessResult::isForbidden().
*
* @dataProvider providerTestAccessForbidden
*/
public function testAccessForbidden($field_name, $langcode) {
$account = $this
->getMock('Drupal\\Core\\Session\\AccountInterface');
$entity = $this
->createMockEntity();
$this
->assertEquals(AccessResult::forbidden(), $this->editAccessCheck
->access($entity, $field_name, $langcode, $account));
}
/**
* Provides test data for testAccessForbidden.
*/
public function providerTestAccessForbidden() {
$data = array();
// Tests the access method without a field_name.
$data[] = array(
NULL,
LanguageInterface::LANGCODE_NOT_SPECIFIED,
);
// Tests the access method with a non-existent field.
$data[] = array(
'not_valid',
LanguageInterface::LANGCODE_NOT_SPECIFIED,
);
// Tests the access method without a langcode.
$data[] = array(
'valid',
NULL,
);
// Tests the access method with an invalid langcode.
$data[] = array(
'valid',
'xx-lolspeak',
);
return $data;
}
/**
* Returns a mock entity.
*
* @return \Drupal\Core\Entity\EntityInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected function createMockEntity() {
$entity = $this
->getMockBuilder('Drupal\\entity_test\\Entity\\EntityTest')
->disableOriginalConstructor()
->getMock();
$entity
->expects($this
->any())
->method('hasTranslation')
->will($this
->returnValueMap(array(
array(
LanguageInterface::LANGCODE_NOT_SPECIFIED,
TRUE,
),
array(
'xx-lolspeak',
FALSE,
),
)));
$entity
->expects($this
->any())
->method('hasField')
->will($this
->returnValueMap(array(
array(
'valid',
TRUE,
),
array(
'not_valid',
FALSE,
),
)));
return $entity;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EditEntityFieldAccessCheckTest:: |
protected | property | The tested access checker. | |
EditEntityFieldAccessCheckTest:: |
protected | function | Returns a mock entity. | |
EditEntityFieldAccessCheckTest:: |
public | function | Provides test data for testAccess(). | |
EditEntityFieldAccessCheckTest:: |
public | function | Provides test data for testAccessForbidden. | |
EditEntityFieldAccessCheckTest:: |
protected | function |
Overrides UnitTestCase:: |
|
EditEntityFieldAccessCheckTest:: |
public | function | Tests the method for checking access to routes. | |
EditEntityFieldAccessCheckTest:: |
public | function | Tests checking access to routes that result in AccessResult::isForbidden(). | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed in 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. |