diff --git a/core/modules/content_translation/tests/src/Unit/Access/ContentTranslationManageAccessCheckTest.php b/core/modules/content_translation/tests/src/Unit/Access/ContentTranslationManageAccessCheckTest.php index 216817e..f1df085 100644 --- a/core/modules/content_translation/tests/src/Unit/Access/ContentTranslationManageAccessCheckTest.php +++ b/core/modules/content_translation/tests/src/Unit/Access/ContentTranslationManageAccessCheckTest.php @@ -9,6 +9,7 @@ use Drupal\content_translation\Access\ContentTranslationManageAccessCheck; use Drupal\Core\Access\AccessResult; +use Drupal\Core\Cache\Cache; use Drupal\Core\DependencyInjection\Container; use Drupal\Core\Language\Language; use Drupal\Tests\UnitTestCase; @@ -83,6 +84,12 @@ public function testCreateAccess() { ->with() ->will($this->returnValue(array())); $entity->expects($this->once()) + ->method('getCacheContexts') + ->willReturn([]); + $entity->expects($this->once()) + ->method('getCacheMaxAge') + ->willReturn(Cache::PERMANENT); + $entity->expects($this->once()) ->method('getCacheTags') ->will($this->returnValue(array('node:1337'))); diff --git a/core/modules/quickedit/tests/src/Unit/Access/EditEntityFieldAccessCheckTest.php b/core/modules/quickedit/tests/src/Unit/Access/EditEntityFieldAccessCheckTest.php index c3a9f7a..5fba2b2 100644 --- a/core/modules/quickedit/tests/src/Unit/Access/EditEntityFieldAccessCheckTest.php +++ b/core/modules/quickedit/tests/src/Unit/Access/EditEntityFieldAccessCheckTest.php @@ -49,34 +49,11 @@ protected function setUp() { * @see \Drupal\Tests\edit\Unit\quickedit\Access\EditEntityFieldAccessCheckTest::testAccess() */ public function providerTestAccess() { - $editable_entity = $this->createMockEntity(); - $editable_entity->expects($this->any()) - ->method('access') - ->will($this->returnValue(AccessResult::allowed()->cachePerPermissions())); - - $non_editable_entity = $this->createMockEntity(); - $non_editable_entity->expects($this->any()) - ->method('access') - ->will($this->returnValue(AccessResult::neutral()->cachePerPermissions())); - - $field_storage_with_access = $this->getMockBuilder('Drupal\field\Entity\FieldStorageConfig') - ->disableOriginalConstructor() - ->getMock(); - $field_storage_with_access->expects($this->any()) - ->method('access') - ->will($this->returnValue(AccessResult::allowed())); - $field_storage_without_access = $this->getMockBuilder('Drupal\field\Entity\FieldStorageConfig') - ->disableOriginalConstructor() - ->getMock(); - $field_storage_without_access->expects($this->any()) - ->method('access') - ->will($this->returnValue(AccessResult::neutral())); - $data = array(); - $data[] = array($editable_entity, $field_storage_with_access, AccessResult::allowed()->cachePerPermissions()); - $data[] = array($non_editable_entity, $field_storage_with_access, AccessResult::neutral()->cachePerPermissions()); - $data[] = array($editable_entity, $field_storage_without_access, AccessResult::neutral()->cachePerPermissions()); - $data[] = array($non_editable_entity, $field_storage_without_access, AccessResult::neutral()->cachePerPermissions()); + $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; } @@ -84,16 +61,28 @@ public function providerTestAccess() { /** * Tests the method for checking access to routes. * - * @param \Drupal\Core\Entity\EntityInterface $entity - * A mocked entity. - * @param \Drupal\field\FieldStorageConfigInterface $field_storage - * A mocked field storage. - * @param bool|null $expected_result + * @param bool $entity_is_editable + * Whether the subject entity is editable. + * @param bool $field_storage_access + * 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(EntityInterface $entity, FieldStorageConfigInterface $field_storage = NULL, $expected_result) { + public function testAccess($entity_is_editable, $field_storage_access, 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_access)); + + $expected_result->cachePerPermissions(); + $field_name = 'valid'; $entity_with_field = clone $entity; $entity_with_field->expects($this->any()) diff --git a/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php b/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php index a0c2ad3..3ffebd7 100644 --- a/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php +++ b/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php @@ -436,9 +436,15 @@ public function testCacheTags() { // ::cacheUntilEntityChanges() convenience method. $node = $this->getMock('\Drupal\node\NodeInterface'); $node->expects($this->any()) + ->method('getCacheContexts') + ->willReturn([]); + $node->expects($this->any()) + ->method('getCacheMaxAge') + ->willReturn(Cache::PERMANENT); + $tags = array('node:20011988'); + $node->expects($this->any()) ->method('getCacheTags') ->will($this->returnValue(array('node:20011988'))); - $tags = array('node:20011988'); $a = AccessResult::neutral()->addCacheTags($tags); $verify($a, $tags); $b = AccessResult::neutral()->cacheUntilEntityChanges($node); @@ -448,6 +454,12 @@ public function testCacheTags() { // ::cacheUntilConfigurationChanges() convenience method. $configuration = $this->getMock('\Drupal\Core\Config\ConfigBase'); $configuration->expects($this->any()) + ->method('getCacheContexts') + ->willReturn([]); + $configuration->expects($this->any()) + ->method('getCacheMaxAge') + ->willReturn(Cache::PERMANENT); + $configuration->expects($this->any()) ->method('getCacheTags') ->will($this->returnValue(array('config:foo.bar.baz'))); $tags = array('config:foo.bar.baz'); @@ -846,8 +858,16 @@ public function testOrIfCacheabilityMerging() { * @covers ::allowedIfHasPermissions * * @dataProvider providerTestAllowedIfHasPermissions + * + * @param string[] $permissions + * The permissions to check for. + * @param string $conjunction + * The conjunction to use when checking for permission. Either 'AND" or + * 'OR'. + * @param \Drupal\Core\Access\AccessResult $expected_access + * The expected access check result. */ - public function testAllowedIfHasPermissions($permissions, $conjunction, $expected_access) { + public function testAllowedIfHasPermissions($permissions, $conjunction, AccessResult $expected_access) { $account = $this->getMock('\Drupal\Core\Session\AccountInterface'); $account->expects($this->any()) ->method('hasPermission') @@ -856,6 +876,10 @@ public function testAllowedIfHasPermissions($permissions, $conjunction, $expecte ['denied', FALSE], ]); + if ($permissions) { + $expected_access->cachePerPermissions(); + } + $access_result = AccessResult::allowedIfHasPermissions($account, $permissions, $conjunction); $this->assertEquals($expected_access, $access_result); } @@ -869,14 +893,14 @@ public function providerTestAllowedIfHasPermissions() { return [ [[], 'AND', AccessResult::allowedIf(FALSE)], [[], 'OR', AccessResult::allowedIf(FALSE)], - [['allowed'], 'OR', AccessResult::allowedIf(TRUE)->addCacheContexts(['user.permissions'])], - [['allowed'], 'AND', AccessResult::allowedIf(TRUE)->addCacheContexts(['user.permissions'])], - [['denied'], 'OR', AccessResult::allowedIf(FALSE)->addCacheContexts(['user.permissions'])], - [['denied'], 'AND', AccessResult::allowedIf(FALSE)->addCacheContexts(['user.permissions'])], - [['allowed', 'denied'], 'OR', AccessResult::allowedIf(TRUE)->addCacheContexts(['user.permissions'])], - [['denied', 'allowed'], 'OR', AccessResult::allowedIf(TRUE)->addCacheContexts(['user.permissions'])], - [['allowed', 'denied', 'other'], 'OR', AccessResult::allowedIf(TRUE)->addCacheContexts(['user.permissions'])], - [['allowed', 'denied'], 'AND', AccessResult::allowedIf(FALSE)->addCacheContexts(['user.permissions'])], + [['allowed'], 'OR', AccessResult::allowedIf(TRUE)], + [['allowed'], 'AND', AccessResult::allowedIf(TRUE)], + [['denied'], 'OR', AccessResult::allowedIf(FALSE)], + [['denied'], 'AND', AccessResult::allowedIf(FALSE)], + [['allowed', 'denied'], 'OR', AccessResult::allowedIf(TRUE)], + [['denied', 'allowed'], 'OR', AccessResult::allowedIf(TRUE)], + [['allowed', 'denied', 'other'], 'OR', AccessResult::allowedIf(TRUE)], + [['allowed', 'denied'], 'AND', AccessResult::allowedIf(FALSE)], ]; }