3 namespace Drupal\Tests\quickedit\Unit\Access;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Cache\Context\CacheContextsManager;
7 use Drupal\Core\DependencyInjection\Container;
8 use Drupal\quickedit\Access\QuickEditEntityFieldAccessCheck;
9 use Drupal\Tests\UnitTestCase;
10 use Drupal\Core\Language\LanguageInterface;
13 * @coversDefaultClass \Drupal\quickedit\Access\QuickEditEntityFieldAccessCheck
17 class QuickEditEntityFieldAccessCheckTest extends UnitTestCase {
20 * The tested access checker.
22 * @var \Drupal\quickedit\Access\QuickEditEntityFieldAccessCheck
24 protected $editAccessCheck;
29 protected function setUp() {
30 $this->editAccessCheck = new QuickEditEntityFieldAccessCheck();
32 $cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
33 $cache_contexts_manager->assertValidTokens()->willReturn(TRUE);
34 $cache_contexts_manager->reveal();
35 $container = new Container();
36 $container->set('cache_contexts_manager', $cache_contexts_manager);
37 \Drupal::setContainer($container);
41 * Provides test data for testAccess().
43 * @see \Drupal\Tests\edit\Unit\quickedit\Access\QuickEditEntityFieldAccessCheckTest::testAccess()
45 public function providerTestAccess() {
47 $data[] = [TRUE, TRUE, AccessResult::allowed()];
48 $data[] = [FALSE, TRUE, AccessResult::neutral()];
49 $data[] = [TRUE, FALSE, AccessResult::neutral()];
50 $data[] = [FALSE, FALSE, AccessResult::neutral()];
56 * Tests the method for checking access to routes.
58 * @param bool $entity_is_editable
59 * Whether the subject entity is editable.
60 * @param bool $field_storage_is_accessible
61 * Whether the user has access to the field storage entity.
62 * @param \Drupal\Core\Access\AccessResult $expected_result
63 * The expected result of the access call.
65 * @dataProvider providerTestAccess
67 public function testAccess($entity_is_editable, $field_storage_is_accessible, AccessResult $expected_result) {
68 $entity = $this->createMockEntity();
69 $entity->expects($this->any())
71 ->willReturn(AccessResult::allowedIf($entity_is_editable)->cachePerPermissions());
73 $field_storage = $this->getMock('Drupal\field\FieldStorageConfigInterface');
74 $field_storage->expects($this->any())
76 ->willReturn(AccessResult::allowedIf($field_storage_is_accessible));
78 $expected_result->cachePerPermissions();
80 $field_name = 'valid';
81 $entity_with_field = clone $entity;
82 $entity_with_field->expects($this->any())
85 ->will($this->returnValue($field_storage));
86 $entity_with_field->expects($this->once())
87 ->method('hasTranslation')
88 ->with(LanguageInterface::LANGCODE_NOT_SPECIFIED)
89 ->will($this->returnValue(TRUE));
91 $account = $this->getMock('Drupal\Core\Session\AccountInterface');
92 $access = $this->editAccessCheck->access($entity_with_field, $field_name, LanguageInterface::LANGCODE_NOT_SPECIFIED, $account);
93 $this->assertEquals($expected_result, $access);
97 * Tests checking access to routes that result in AccessResult::isForbidden().
99 * @dataProvider providerTestAccessForbidden
101 public function testAccessForbidden($field_name, $langcode) {
102 $account = $this->getMock('Drupal\Core\Session\AccountInterface');
103 $entity = $this->createMockEntity();
104 $this->assertEquals(AccessResult::forbidden(), $this->editAccessCheck->access($entity, $field_name, $langcode, $account));
108 * Provides test data for testAccessForbidden.
110 public function providerTestAccessForbidden() {
112 // Tests the access method without a field_name.
113 $data[] = [NULL, LanguageInterface::LANGCODE_NOT_SPECIFIED];
114 // Tests the access method with a non-existent field.
115 $data[] = ['not_valid', LanguageInterface::LANGCODE_NOT_SPECIFIED];
116 // Tests the access method without a langcode.
117 $data[] = ['valid', NULL];
118 // Tests the access method with an invalid langcode.
119 $data[] = ['valid', 'xx-lolspeak'];
124 * Returns a mock entity.
126 * @return \Drupal\Core\Entity\EntityInterface|\PHPUnit_Framework_MockObject_MockObject
128 protected function createMockEntity() {
129 $entity = $this->getMockBuilder('Drupal\entity_test\Entity\EntityTest')
130 ->disableOriginalConstructor()
133 $entity->expects($this->any())
134 ->method('hasTranslation')
135 ->will($this->returnValueMap([
136 [LanguageInterface::LANGCODE_NOT_SPECIFIED, TRUE],
137 ['xx-lolspeak', FALSE],
139 $entity->expects($this->any())
141 ->will($this->returnValueMap([
143 ['not_valid', FALSE],