diff --git a/core/lib/Drupal/Core/Access/AccessResult.php b/core/lib/Drupal/Core/Access/AccessResult.php index 92f329e..661ee55 100644 --- a/core/lib/Drupal/Core/Access/AccessResult.php +++ b/core/lib/Drupal/Core/Access/AccessResult.php @@ -180,7 +180,6 @@ public function isNeutral() { * {@inheritdoc} */ public function getCacheContexts() { - sort($this->cacheContexts); return $this->cacheContexts; } diff --git a/core/lib/Drupal/Core/Cache/RefinableCacheableDependencyTrait.php b/core/lib/Drupal/Core/Cache/RefinableCacheableDependencyTrait.php index fb77df3..b94d7f3 100644 --- a/core/lib/Drupal/Core/Cache/RefinableCacheableDependencyTrait.php +++ b/core/lib/Drupal/Core/Cache/RefinableCacheableDependencyTrait.php @@ -44,7 +44,7 @@ public function addCacheableDependency($other_object) { } else { // Not a cacheable dependency, this can not be cached. - $this->maxAge = 0; + $this->cacheMaxAge = 0; } return $this; } @@ -73,29 +73,7 @@ public function addCacheTags(array $cache_tags) { * {@inheritdoc} */ public function mergeCacheMaxAge($max_age) { - if ($this->cacheMaxAge === Cache::PERMANENT) { - $this->cacheMaxAge = $max_age; - } - elseif ($max_age && $max_age !== Cache::PERMANENT) { - $this->cacheMaxAge = Cache::mergeMaxAges($this->cacheMaxAge, $max_age); - } - return $this; - } - - /** - * {@inheritdoc} - */ - public function addCacheableDependency($other_object) { - if ($other_object instanceof CacheableDependencyInterface) { - $this->addCacheTags($other_object->getCacheTags()); - $this->addCacheContexts($other_object->getCacheContexts()); - $this->mergeCacheMaxAge($other_object->getCacheMaxAge()); - } - else { - // Not a cacheable dependency, this can not be cached. - $this->cacheMaxAge = 0; - } - + $this->cacheMaxAge = Cache::mergeMaxAges($this->cacheMaxAge, $max_age); return $this; } diff --git a/core/modules/quickedit/tests/src/Unit/Access/EditEntityFieldAccessCheckTest.php b/core/modules/quickedit/tests/src/Unit/Access/EditEntityFieldAccessCheckTest.php index 5fba2b2..8b91791 100644 --- a/core/modules/quickedit/tests/src/Unit/Access/EditEntityFieldAccessCheckTest.php +++ b/core/modules/quickedit/tests/src/Unit/Access/EditEntityFieldAccessCheckTest.php @@ -8,6 +8,7 @@ namespace Drupal\Tests\quickedit\Unit\Access; use Drupal\Core\Access\AccessResult; +use Drupal\Core\Cache\Context\CacheContextsManager; use Drupal\Core\DependencyInjection\Container; use Drupal\quickedit\Access\EditEntityFieldAccessCheck; use Drupal\Tests\UnitTestCase; @@ -35,9 +36,7 @@ class EditEntityFieldAccessCheckTest extends UnitTestCase { protected function setUp() { $this->editAccessCheck = new EditEntityFieldAccessCheck(); - $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager') - ->disableOriginalConstructor() - ->getMock(); + $cache_contexts_manager = $this->prophesize(CacheContextsManager::class)->reveal(); $container = new Container(); $container->set('cache_contexts_manager', $cache_contexts_manager); \Drupal::setContainer($container); @@ -63,14 +62,14 @@ public function providerTestAccess() { * * @param bool $entity_is_editable * Whether the subject entity is editable. - * @param bool $field_storage_access + * @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_access, AccessResult $expected_result) { + public function testAccess($entity_is_editable, $field_storage_is_accessible, AccessResult $expected_result) { $entity = $this->createMockEntity(); $entity->expects($this->any()) ->method('access') @@ -79,7 +78,7 @@ public function testAccess($entity_is_editable, $field_storage_access, AccessRes $field_storage = $this->getMock('Drupal\field\FieldStorageConfigInterface'); $field_storage->expects($this->any()) ->method('access') - ->willReturn(AccessResult::allowedIf($field_storage_access)); + ->willReturn(AccessResult::allowedIf($field_storage_is_accessible)); $expected_result->cachePerPermissions(); diff --git a/core/modules/user/tests/src/Unit/UserAccessControlHandlerTest.php b/core/modules/user/tests/src/Unit/UserAccessControlHandlerTest.php index a9d2597..05d7223 100644 --- a/core/modules/user/tests/src/Unit/UserAccessControlHandlerTest.php +++ b/core/modules/user/tests/src/Unit/UserAccessControlHandlerTest.php @@ -9,6 +9,7 @@ use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Access\AccessResult; +use Drupal\Core\Cache\Context\CacheContextsManager; use Drupal\Core\DependencyInjection\Container; use Drupal\Tests\UnitTestCase; use Drupal\user\UserAccessControlHandler; @@ -64,9 +65,7 @@ class UserAccessControlHandlerTest extends UnitTestCase { public function setUp() { parent::setUp(); - $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager') - ->disableOriginalConstructor() - ->getMock(); + $cache_contexts_manager = $this->prophesize(CacheContextsManager::class)->reveal(); $container = new Container(); $container->set('cache_contexts_manager', $cache_contexts_manager); \Drupal::setContainer($container); diff --git a/core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php b/core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php index cb77e78..3a03535 100644 --- a/core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php @@ -11,6 +11,7 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\Access\AccessManagerInterface; use Drupal\Core\Access\CheckProvider; +use Drupal\Core\Cache\Context\CacheContextsManager; use Drupal\Core\Routing\RouteMatch; use Drupal\Core\Access\AccessManager; use Drupal\Core\Access\DefaultAccessCheck; @@ -94,12 +95,7 @@ protected function setUp() { parent::setUp(); $this->container = new ContainerBuilder(); - - - - $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager') - ->disableOriginalConstructor() - ->getMock(); + $cache_contexts_manager = $this->prophesize(CacheContextsManager::class)->reveal(); $this->container->set('cache_contexts_manager', $cache_contexts_manager); \Drupal::setContainer($this->container); diff --git a/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php b/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php index 19c5ec8..f482386 100644 --- a/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php +++ b/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php @@ -444,13 +444,6 @@ public function testCacheTags() { // ::addCacheableDependency() 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'))); $node->expects($this->any()) diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityAccessCheckTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityAccessCheckTest.php index 0788228..06c9a16 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityAccessCheckTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityAccessCheckTest.php @@ -7,6 +7,7 @@ namespace Drupal\Tests\Core\Entity; +use Drupal\Core\Cache\Context\CacheContextsManager; use Drupal\Core\DependencyInjection\Container; use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\Routing\Route; @@ -27,9 +28,7 @@ class EntityAccessCheckTest extends UnitTestCase { */ public function testAccess() { - $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager') - ->disableOriginalConstructor() - ->getMock(); + $cache_contexts_manager = $this->prophesize(CacheContextsManager::class)->reveal(); $container = new Container(); $container->set('cache_contexts_manager', $cache_contexts_manager); \Drupal::setContainer($container); diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityCreateAccessCheckTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityCreateAccessCheckTest.php index 5d0a966..b0b4c8c 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityCreateAccessCheckTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityCreateAccessCheckTest.php @@ -8,6 +8,8 @@ namespace Drupal\Tests\Core\Entity; use Drupal\Core\Access\AccessResult; +use Drupal\Core\Cache\Context\CacheContextsManager; +use Drupal\Core\DependencyInjection\Container; use Drupal\Core\Entity\EntityCreateAccessCheck; use Drupal\Tests\UnitTestCase; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; @@ -32,6 +34,11 @@ class EntityCreateAccessCheckTest extends UnitTestCase { */ protected function setUp() { parent::setUp(); + + $cache_contexts_manager = $this->prophesize(CacheContextsManager::class)->reveal(); + $container = new Container(); + $container->set('cache_contexts_manager', $cache_contexts_manager); + \Drupal::setContainer($container); } /** @@ -40,9 +47,8 @@ protected function setUp() { * @return array */ public function providerTestAccess() { - $no_access = AccessResult::neutral()->cachePerPermissions(); - $access = AccessResult::allowed()->cachePerPermissions(); - $no_access_due_to_errors = AccessResult::neutral(); + $no_access = FALSE; + $access = TRUE; return array( array('', 'entity_test', $no_access, $no_access), @@ -51,10 +57,10 @@ public function providerTestAccess() { array('test_entity', 'entity_test:test_entity', $no_access, $no_access), array('test_entity', 'entity_test:{bundle_argument}', $access, $access), array('test_entity', 'entity_test:{bundle_argument}', $no_access, $no_access), - array('', 'entity_test:{bundle_argument}', $no_access, $no_access_due_to_errors), + array('', 'entity_test:{bundle_argument}', $no_access, $no_access, FALSE), // When the bundle is not provided, access should be denied even if the // access control handler would allow access. - array('', 'entity_test:{bundle_argument}', $access, $no_access_due_to_errors), + array('', 'entity_test:{bundle_argument}', $access, $no_access, FALSE), ); } @@ -63,7 +69,15 @@ public function providerTestAccess() { * * @dataProvider providerTestAccess */ - public function testAccess($entity_bundle, $requirement, $access, $expected) { + public function testAccess($entity_bundle, $requirement, $access, $expected, $expect_permission_context = TRUE) { + + // Set up the access result objects for allowing or denying access. + $access_result = $access ? AccessResult::allowed()->cachePerPermissions() : AccessResult::neutral()->cachePerPermissions(); + $expected_access_result = $expected ? AccessResult::allowed() : AccessResult::neutral(); + if ($expect_permission_context) { + $expected_access_result->cachePerPermissions(); + } + $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface'); // Don't expect a call to the access control handler when we have a bundle @@ -73,7 +87,7 @@ public function testAccess($entity_bundle, $requirement, $access, $expected) { $access_control_handler->expects($this->once()) ->method('createAccess') ->with($entity_bundle) - ->will($this->returnValue($access)); + ->will($this->returnValue($access_result)); $entity_manager->expects($this->any()) ->method('getAccessControlHandler') @@ -101,7 +115,7 @@ public function testAccess($entity_bundle, $requirement, $access, $expected) { ->will($this->returnValue($raw_variables)); $account = $this->getMock('Drupal\Core\Session\AccountInterface'); - $this->assertEquals($expected, $applies_check->access($route, $route_match, $account)); + $this->assertEquals($expected_access_result, $applies_check->access($route, $route_match, $account)); } } diff --git a/core/tests/Drupal/Tests/Core/Menu/DefaultMenuLinkTreeManipulatorsTest.php b/core/tests/Drupal/Tests/Core/Menu/DefaultMenuLinkTreeManipulatorsTest.php index c6beb43..17702e7 100644 --- a/core/tests/Drupal/Tests/Core/Menu/DefaultMenuLinkTreeManipulatorsTest.php +++ b/core/tests/Drupal/Tests/Core/Menu/DefaultMenuLinkTreeManipulatorsTest.php @@ -8,6 +8,7 @@ namespace Drupal\Tests\Core\Menu; use Drupal\Core\Access\AccessResult; +use Drupal\Core\Cache\Context\CacheContextsManager; use Drupal\Core\DependencyInjection\Container; use Drupal\Core\Menu\DefaultMenuLinkTreeManipulators; use Drupal\Core\Menu\MenuLinkTreeElement; @@ -78,9 +79,7 @@ protected function setUp() { $this->defaultMenuTreeManipulators = new DefaultMenuLinkTreeManipulators($this->accessManager, $this->currentUser, $this->queryFactory); - $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager') - ->disableOriginalConstructor() - ->getMock(); + $cache_contexts_manager = $this->prophesize(CacheContextsManager::class)->reveal(); $container = new Container(); $container->set('cache_contexts_manager', $cache_contexts_manager); \Drupal::setContainer($container); diff --git a/core/tests/Drupal/Tests/Core/Route/RoleAccessCheckTest.php b/core/tests/Drupal/Tests/Core/Route/RoleAccessCheckTest.php index c5f96fb..7725c9e 100644 --- a/core/tests/Drupal/Tests/Core/Route/RoleAccessCheckTest.php +++ b/core/tests/Drupal/Tests/Core/Route/RoleAccessCheckTest.php @@ -8,6 +8,7 @@ namespace Drupal\Tests\Core\Route; use Drupal\Core\Access\AccessResult; +use Drupal\Core\Cache\Context\CacheContextsManager; use Drupal\Core\DependencyInjection\Container; use Drupal\Core\Session\UserSession; use Drupal\Tests\UnitTestCase; @@ -145,9 +146,7 @@ public function roleAccessProvider() { */ public function testRoleAccess($path, $grant_accounts, $deny_accounts) { - $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager') - ->disableOriginalConstructor() - ->getMock(); + $cache_contexts_manager = $this->prophesize(CacheContextsManager::class)->reveal(); $container = new Container(); $container->set('cache_contexts_manager', $cache_contexts_manager); \Drupal::setContainer($container);