3 namespace Drupal\Tests\content_translation\Unit\Access;
5 use Drupal\content_translation\Access\ContentTranslationManageAccessCheck;
6 use Drupal\Core\Access\AccessResult;
7 use Drupal\Core\DependencyInjection\ContainerBuilder;
8 use Drupal\Core\Cache\Cache;
9 use Drupal\Core\Language\Language;
10 use Drupal\Tests\UnitTestCase;
11 use Symfony\Component\Routing\Route;
14 * Tests for content translation manage check.
16 * @coversDefaultClass \Drupal\content_translation\Access\ContentTranslationManageAccessCheck
18 * @group content_translation
20 class ContentTranslationManageAccessCheckTest extends UnitTestCase {
23 * The cache contexts manager.
25 * @var \Drupal\Core\Cache\Context\CacheContextsManager|\PHPUnit_Framework_MockObject_MockObject
27 protected $cacheContextsManager;
32 protected function setUp() {
35 $this->cacheContextsManager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
36 ->disableOriginalConstructor()
38 $this->cacheContextsManager->method('assertValidTokens')->willReturn(TRUE);
40 $container = new ContainerBuilder();
41 $container->set('cache_contexts_manager', $this->cacheContextsManager);
42 \Drupal::setContainer($container);
46 * Tests the create access method.
50 public function testCreateAccess() {
51 // Set the mock translation handler.
52 $translation_handler = $this->getMock('\Drupal\content_translation\ContentTranslationHandlerInterface');
53 $translation_handler->expects($this->once())
54 ->method('getTranslationAccess')
55 ->will($this->returnValue(AccessResult::allowed()));
57 $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
58 $entity_manager->expects($this->once())
59 ->method('getHandler')
61 ->will($this->returnValue($translation_handler));
63 // Set our source and target languages.
67 // Set the mock language manager.
68 $language_manager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
69 $language_manager->expects($this->at(0))
70 ->method('getLanguage')
71 ->with($this->equalTo($source))
72 ->will($this->returnValue(new Language(['id' => 'en'])));
73 $language_manager->expects($this->at(1))
74 ->method('getLanguages')
75 ->will($this->returnValue(['en' => [], 'it' => []]));
76 $language_manager->expects($this->at(2))
77 ->method('getLanguage')
78 ->with($this->equalTo($source))
79 ->will($this->returnValue(new Language(['id' => 'en'])));
80 $language_manager->expects($this->at(3))
81 ->method('getLanguage')
82 ->with($this->equalTo($target))
83 ->will($this->returnValue(new Language(['id' => 'it'])));
85 // Set the mock entity. We need to use ContentEntityBase for mocking due to
86 // issues with phpunit and multiple interfaces.
87 $entity = $this->getMockBuilder('Drupal\Core\Entity\ContentEntityBase')
88 ->disableOriginalConstructor()
90 $entity->expects($this->once())
91 ->method('getEntityTypeId');
92 $entity->expects($this->once())
93 ->method('getTranslationLanguages')
95 ->will($this->returnValue([]));
96 $entity->expects($this->once())
97 ->method('getCacheContexts')
99 $entity->expects($this->once())
100 ->method('getCacheMaxAge')
101 ->willReturn(Cache::PERMANENT);
102 $entity->expects($this->once())
103 ->method('getCacheTags')
104 ->will($this->returnValue(['node:1337']));
105 $entity->expects($this->once())
106 ->method('getCacheContexts')
109 // Set the route requirements.
110 $route = new Route('test_route');
111 $route->setRequirement('_access_content_translation_manage', 'create');
113 // Set up the route match.
114 $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
115 $route_match->expects($this->once())
116 ->method('getParameter')
118 ->will($this->returnValue($entity));
120 // Set the mock account.
121 $account = $this->getMock('Drupal\Core\Session\AccountInterface');
123 // The access check under test.
124 $check = new ContentTranslationManageAccessCheck($entity_manager, $language_manager);
126 // The request params.
128 $entity_type_id = 'node';
130 $this->assertTrue($check->access($route, $route_match, $account, $source, $target, $language, $entity_type_id)->isAllowed(), "The access check matches");