X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=web%2Fcore%2Fmodules%2Fcontent_translation%2Ftests%2Fsrc%2FUnit%2FAccess%2FContentTranslationManageAccessCheckTest.php;fp=web%2Fcore%2Fmodules%2Fcontent_translation%2Ftests%2Fsrc%2FUnit%2FAccess%2FContentTranslationManageAccessCheckTest.php;h=ad7317f3ad695496a977d0e8d59bab85cc90b509;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/content_translation/tests/src/Unit/Access/ContentTranslationManageAccessCheckTest.php b/web/core/modules/content_translation/tests/src/Unit/Access/ContentTranslationManageAccessCheckTest.php new file mode 100644 index 000000000..ad7317f3a --- /dev/null +++ b/web/core/modules/content_translation/tests/src/Unit/Access/ContentTranslationManageAccessCheckTest.php @@ -0,0 +1,133 @@ +cacheContextsManager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager') + ->disableOriginalConstructor() + ->getMock(); + $this->cacheContextsManager->method('assertValidTokens')->willReturn(TRUE); + + $container = new ContainerBuilder(); + $container->set('cache_contexts_manager', $this->cacheContextsManager); + \Drupal::setContainer($container); + } + + /** + * Tests the create access method. + * + * @covers ::access + */ + public function testCreateAccess() { + // Set the mock translation handler. + $translation_handler = $this->getMock('\Drupal\content_translation\ContentTranslationHandlerInterface'); + $translation_handler->expects($this->once()) + ->method('getTranslationAccess') + ->will($this->returnValue(AccessResult::allowed())); + + $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface'); + $entity_manager->expects($this->once()) + ->method('getHandler') + ->withAnyParameters() + ->will($this->returnValue($translation_handler)); + + // Set our source and target languages. + $source = 'en'; + $target = 'it'; + + // Set the mock language manager. + $language_manager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface'); + $language_manager->expects($this->at(0)) + ->method('getLanguage') + ->with($this->equalTo($source)) + ->will($this->returnValue(new Language(['id' => 'en']))); + $language_manager->expects($this->at(1)) + ->method('getLanguages') + ->will($this->returnValue(['en' => [], 'it' => []])); + $language_manager->expects($this->at(2)) + ->method('getLanguage') + ->with($this->equalTo($source)) + ->will($this->returnValue(new Language(['id' => 'en']))); + $language_manager->expects($this->at(3)) + ->method('getLanguage') + ->with($this->equalTo($target)) + ->will($this->returnValue(new Language(['id' => 'it']))); + + // Set the mock entity. We need to use ContentEntityBase for mocking due to + // issues with phpunit and multiple interfaces. + $entity = $this->getMockBuilder('Drupal\Core\Entity\ContentEntityBase') + ->disableOriginalConstructor() + ->getMock(); + $entity->expects($this->once()) + ->method('getEntityTypeId'); + $entity->expects($this->once()) + ->method('getTranslationLanguages') + ->with() + ->will($this->returnValue([])); + $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(['node:1337'])); + $entity->expects($this->once()) + ->method('getCacheContexts') + ->willReturn([]); + + // Set the route requirements. + $route = new Route('test_route'); + $route->setRequirement('_access_content_translation_manage', 'create'); + + // Set up the route match. + $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface'); + $route_match->expects($this->once()) + ->method('getParameter') + ->with('node') + ->will($this->returnValue($entity)); + + // Set the mock account. + $account = $this->getMock('Drupal\Core\Session\AccountInterface'); + + // The access check under test. + $check = new ContentTranslationManageAccessCheck($entity_manager, $language_manager); + + // The request params. + $language = 'en'; + $entity_type_id = 'node'; + + $this->assertTrue($check->access($route, $route_match, $account, $source, $target, $language, $entity_type_id)->isAllowed(), "The access check matches"); + } + +}