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 512e735..7d4b691 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\DependencyInjection\ContainerBuilder; use Drupal\Core\Language\Language; use Drupal\Tests\UnitTestCase; use Symfony\Component\Routing\Route; @@ -23,6 +24,28 @@ class ContentTranslationManageAccessCheckTest extends UnitTestCase { /** + * The cache contexts manager. + * + * @var \Drupal\Core\Cache\Context\CacheContextsManager|\PHPUnit_Framework_MockObject_MockObject + */ + protected $cacheContextsManager; + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + $this->cacheContextsManager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager') + ->disableOriginalConstructor() + ->getMock(); + + $container = new ContainerBuilder(); + $container->set('cache_contexts_manager', $this->cacheContextsManager); + \Drupal::setContainer($container); + } + + /** * Tests the create access method. * * @covers ::access @@ -76,6 +99,9 @@ public function testCreateAccess() { $entity->expects($this->once()) ->method('getCacheTags') ->will($this->returnValue(array('node:1337'))); + $entity->expects($this->once()) + ->method('getCacheContexts') + ->will($this->returnValue([])); // Set the route requirements. $route = new Route('test_route'); diff --git a/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php b/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php index 123888a..aff65a1 100644 --- a/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php +++ b/core/tests/Drupal/Tests/Core/Access/AccessResultTest.php @@ -12,6 +12,7 @@ use Drupal\Core\Access\AccessResultNeutral; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableDependencyInterface; +use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Tests\UnitTestCase; /** @@ -20,6 +21,28 @@ */ class AccessResultTest extends UnitTestCase { + /** + * The cache contexts manager. + * + * @var \Drupal\Core\Cache\Context\CacheContextsManager|\PHPUnit_Framework_MockObject_MockObject + */ + protected $cacheContextsManager; + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + $this->cacheContextsManager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager') + ->disableOriginalConstructor() + ->getMock(); + + $container = new ContainerBuilder(); + $container->set('cache_contexts_manager', $this->cacheContextsManager); + \Drupal::setContainer($container); + } + protected function assertDefaultCacheability(AccessResult $access) { $this->assertSame([], $access->getCacheContexts()); $this->assertSame([], $access->getCacheTags()); @@ -425,6 +448,9 @@ public function testCacheTags() { $node->expects($this->any()) ->method('getCacheTags') ->will($this->returnValue(array('node:20011988'))); + $node->expects($this->any()) + ->method('getCacheContexts') + ->will($this->returnValue([])); $tags = array('node:20011988'); $a = AccessResult::neutral()->addCacheTags($tags); $verify($a, $tags); @@ -844,7 +870,31 @@ public function testAllowedIfHasPermissions($permissions, $conjunction, $expecte ]); $access_result = AccessResult::allowedIfHasPermissions($account, $permissions, $conjunction); - $this->assertEquals($expected_access, $access_result); + $expected_access_result = $this->accessResultProviderTestAllowedIfHasPermissions($expected_access); + $this->assertEquals($expected_access_result, $access_result); + } + + /** + * Provides data for the testAllowedIfHasPermissions() method. + * + * These objects can only be instantiated if the cache contexts manager + * service is instantiated, so these cannot be provided in a dataProvider. + * + * @param string $expected_access + * The expected access result object. + * + * @return AccessResult + * The instantiated access result object. + */ + protected function accessResultProviderTestAllowedIfHasPermissions($expected_access) { + switch ($expected_access) { + case 'allowed': + return AccessResult::allowedIf(TRUE)->addCacheContexts(['user.permissions']); + case 'neutral': + return AccessResult::allowedIf(FALSE)->addCacheContexts(['user.permissions']); + default: + return AccessResult::allowedIf(FALSE); + } } /** @@ -854,16 +904,16 @@ public function testAllowedIfHasPermissions($permissions, $conjunction, $expecte */ 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'])], + [[], 'AND', 'default'], + [[], 'OR', 'default'], + [['allowed'], 'OR', 'allowed'], + [['allowed'], 'AND', 'allowed'], + [['denied'], 'OR', 'neutral'], + [['denied'], 'AND', 'neutral'], + [['allowed', 'denied'], 'OR', 'allowed'], + [['denied', 'allowed'], 'OR', 'allowed'], + [['allowed', 'denied', 'other'], 'OR', 'allowed'], + [['allowed', 'denied'], 'AND', 'neutral'], ]; } diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php index 7e0564f..5b2a292 100644 --- a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php +++ b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityStorageTest.php @@ -190,7 +190,6 @@ protected function setUp() { $container->set('language_manager', $this->languageManager); $container->set('cache_contexts_manager', $this->cacheContextsManager); \Drupal::setContainer($container); - } /**