0827436982f6f0b16f3a1bd93dbad139ac6caf1e
[yaffs-website] / web / core / modules / quickedit / tests / src / Unit / Access / QuickEditEntityFieldAccessCheckTest.php
1 <?php
2
3 namespace Drupal\Tests\quickedit\Unit\Access;
4
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;
11
12 /**
13  * @coversDefaultClass \Drupal\quickedit\Access\QuickEditEntityFieldAccessCheck
14  * @group Access
15  * @group quickedit
16  */
17 class QuickEditEntityFieldAccessCheckTest extends UnitTestCase {
18
19   /**
20    * The tested access checker.
21    *
22    * @var \Drupal\quickedit\Access\QuickEditEntityFieldAccessCheck
23    */
24   protected $editAccessCheck;
25
26   /**
27    * {@inheritdoc}
28    */
29   protected function setUp() {
30     $this->editAccessCheck = new QuickEditEntityFieldAccessCheck();
31
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);
38   }
39
40   /**
41    * Provides test data for testAccess().
42    *
43    * @see \Drupal\Tests\edit\Unit\quickedit\Access\QuickEditEntityFieldAccessCheckTest::testAccess()
44    */
45   public function providerTestAccess() {
46     $data = [];
47     $data[] = [TRUE, TRUE, AccessResult::allowed()];
48     $data[] = [FALSE, TRUE, AccessResult::neutral()];
49     $data[] = [TRUE, FALSE, AccessResult::neutral()];
50     $data[] = [FALSE, FALSE, AccessResult::neutral()];
51
52     return $data;
53   }
54
55   /**
56    * Tests the method for checking access to routes.
57    *
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.
64    *
65    * @dataProvider providerTestAccess
66    */
67   public function testAccess($entity_is_editable, $field_storage_is_accessible, AccessResult $expected_result) {
68     $entity = $this->createMockEntity();
69     $entity->expects($this->any())
70       ->method('access')
71       ->willReturn(AccessResult::allowedIf($entity_is_editable)->cachePerPermissions());
72
73     $field_storage = $this->getMock('Drupal\field\FieldStorageConfigInterface');
74     $field_storage->expects($this->any())
75       ->method('access')
76       ->willReturn(AccessResult::allowedIf($field_storage_is_accessible));
77
78     $expected_result->cachePerPermissions();
79
80     $field_name = 'valid';
81     $entity_with_field = clone $entity;
82     $entity_with_field->expects($this->any())
83       ->method('get')
84       ->with($field_name)
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));
90
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);
94   }
95
96   /**
97    * Tests checking access to routes that result in AccessResult::isForbidden().
98    *
99    * @dataProvider providerTestAccessForbidden
100    */
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));
105   }
106
107   /**
108    * Provides test data for testAccessForbidden.
109    */
110   public function providerTestAccessForbidden() {
111     $data = [];
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'];
120     return $data;
121   }
122
123   /**
124    * Returns a mock entity.
125    *
126    * @return \Drupal\Core\Entity\EntityInterface|\PHPUnit_Framework_MockObject_MockObject
127    */
128   protected function createMockEntity() {
129     $entity = $this->getMockBuilder('Drupal\entity_test\Entity\EntityTest')
130       ->disableOriginalConstructor()
131       ->getMock();
132
133     $entity->expects($this->any())
134       ->method('hasTranslation')
135       ->will($this->returnValueMap([
136         [LanguageInterface::LANGCODE_NOT_SPECIFIED, TRUE],
137         ['xx-lolspeak', FALSE],
138       ]));
139     $entity->expects($this->any())
140       ->method('hasField')
141       ->will($this->returnValueMap([
142         ['valid', TRUE],
143         ['not_valid', FALSE],
144       ]));
145
146     return $entity;
147   }
148
149 }