.../ContentTranslationManageAccessCheckTest.php | 22 ++++++++++++++++++++++ .../tests/src/Unit/PermissionAccessCheckTest.php | 21 +++++++++++++++++++++ .../src/Unit/UserAccessControlHandlerTest.php | 20 ++++++++++++++++++++ .../Tests/Core/Entity/EntityAccessCheckTest.php | 22 ++++++++++++++++++++++ .../Tests/Core/Route/RoleAccessCheckTest.php | 22 ++++++++++++++++++++++ 5 files changed, 107 insertions(+) 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 0487d08..3bb8cf9 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; @@ -22,6 +23,27 @@ */ class ContentTranslationManageAccessCheckTest extends UnitTestCase { + + /** + * {@inheritdoc} + * + * Mocks the current user service, so that ::cachePerRole() can work. + * + * @see AccessResult::cachePerRole + * + * @todo remove this when https://www.drupal.org/node/2428703 lands. + */ + protected function setUp() { + parent::setUp(); + $container = new ContainerBuilder(); + $current_user = $this->getMock('\Drupal\Core\Session\AccountProxyInterface'); + $current_user->expects($this->any()) + ->method('getRoles') + ->willReturn(['authenticated', 'llama_herder']); + $container->set('current_user', $current_user); + \Drupal::setContainer($container); + } + /** * Tests the create access method. * diff --git a/core/modules/user/tests/src/Unit/PermissionAccessCheckTest.php b/core/modules/user/tests/src/Unit/PermissionAccessCheckTest.php index a6e57e1..ad96953 100644 --- a/core/modules/user/tests/src/Unit/PermissionAccessCheckTest.php +++ b/core/modules/user/tests/src/Unit/PermissionAccessCheckTest.php @@ -8,6 +8,7 @@ namespace Drupal\Tests\user\Unit; use Drupal\Core\Access\AccessResult; +use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Tests\UnitTestCase; use Drupal\user\Access\PermissionAccessCheck; use Symfony\Component\Routing\Route; @@ -26,12 +27,32 @@ class PermissionAccessCheckTest extends UnitTestCase { */ public $accessCheck; + + /** + * Mocks the current user service, so that ::cachePerRole() can work. + * + * @see ::cachePerRole + * + * @todo remove this when https://www.drupal.org/node/2428703 lands. + */ + protected function mockCurrentUserService() { + $container = new ContainerBuilder(); + $current_user = $this->getMock('\Drupal\Core\Session\AccountProxyInterface'); + $current_user->expects($this->any()) + ->method('getRoles') + ->willReturn(['authenticated', 'llama_herder']); + $container->set('current_user', $current_user); + \Drupal::setContainer($container); + } + /** * {@inheritdoc} */ protected function setUp() { parent::setUp(); + $this->mockCurrentUserService(); + $this->accessCheck = new PermissionAccessCheck(); } diff --git a/core/modules/user/tests/src/Unit/UserAccessControlHandlerTest.php b/core/modules/user/tests/src/Unit/UserAccessControlHandlerTest.php index 04cdef9..9dfdbf0 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\String; use Drupal\Core\Access\AccessResult; +use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Tests\UnitTestCase; use Drupal\user\UserAccessControlHandler; @@ -58,6 +59,23 @@ class UserAccessControlHandlerTest extends UnitTestCase { protected $items; /** + * Mocks the current user service, so that ::cachePerRole() can work. + * + * @see ::cachePerRole + * + * @todo remove this when https://www.drupal.org/node/2428703 lands. + */ + protected function mockCurrentUserService() { + $container = new ContainerBuilder(); + $current_user = $this->getMock('\Drupal\Core\Session\AccountProxyInterface'); + $current_user->expects($this->any()) + ->method('getRoles') + ->willReturn(['authenticated', 'llama_herder']); + $container->set('current_user', $current_user); + \Drupal::setContainer($container); + } + + /** * {@inheritdoc} */ public function setUp() { @@ -108,6 +126,8 @@ public function setUp() { ->expects($this->any()) ->method('defaultAccess') ->will($this->returnValue(AccessResult::allowed())); + + $this->mockCurrentUserService(); } /** diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityAccessCheckTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityAccessCheckTest.php index 2b53bb2..04e09e9 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\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\Routing\Route; use Drupal\Core\Access\AccessResult; @@ -22,6 +23,27 @@ class EntityAccessCheckTest extends UnitTestCase { /** + * {@inheritdoc} + * + * Mocks the current user service, so that ::cachePerRole() can work. + * + * @see AccessResult::cachePerRole + * + * @todo remove this when https://www.drupal.org/node/2428703 lands. + */ + protected function setUp() { + parent::setUp(); + + $container = new ContainerBuilder(); + $current_user = $this->getMock('\Drupal\Core\Session\AccountProxyInterface'); + $current_user->expects($this->any()) + ->method('getRoles') + ->willReturn(['authenticated', 'llama_herder']); + $container->set('current_user', $current_user); + \Drupal::setContainer($container); + } + + /** * Tests the method for checking access to routes. */ public function testAccess() { diff --git a/core/tests/Drupal/Tests/Core/Route/RoleAccessCheckTest.php b/core/tests/Drupal/Tests/Core/Route/RoleAccessCheckTest.php index ec07d8e..046276b 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\DependencyInjection\ContainerBuilder; use Drupal\Core\Session\UserSession; use Drupal\Tests\UnitTestCase; use Drupal\user\Access\RoleAccessCheck; @@ -22,6 +23,27 @@ class RoleAccessCheckTest extends UnitTestCase { /** + * {@inheritdoc} + * + * Mocks the current user service, so that ::cachePerRole() can work. + * + * @see AccessResult::cachePerRole + * + * @todo remove this when https://www.drupal.org/node/2428703 lands. + */ + protected function setUp() { + parent::setUp(); + + $container = new ContainerBuilder(); + $current_user = $this->getMock('\Drupal\Core\Session\AccountProxyInterface'); + $current_user->expects($this->any()) + ->method('getRoles') + ->willReturn(['authenticated', 'llama_herder']); + $container->set('current_user', $current_user); + \Drupal::setContainer($container); + } + + /** * Generates the test route collection. * * @return \Symfony\Component\Routing\RouteCollection