Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / content_translation / tests / src / Unit / Access / ContentTranslationManageAccessCheckTest.php
1 <?php
2
3 namespace Drupal\Tests\content_translation\Unit\Access;
4
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;
12
13 /**
14  * Tests for content translation manage check.
15  *
16  * @coversDefaultClass \Drupal\content_translation\Access\ContentTranslationManageAccessCheck
17  * @group Access
18  * @group content_translation
19  */
20 class ContentTranslationManageAccessCheckTest extends UnitTestCase {
21
22   /**
23    * The cache contexts manager.
24    *
25    * @var \Drupal\Core\Cache\Context\CacheContextsManager|\PHPUnit_Framework_MockObject_MockObject
26    */
27   protected $cacheContextsManager;
28
29   /**
30    * {@inheritdoc}
31    */
32   protected function setUp() {
33     parent::setUp();
34
35     $this->cacheContextsManager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
36       ->disableOriginalConstructor()
37       ->getMock();
38     $this->cacheContextsManager->method('assertValidTokens')->willReturn(TRUE);
39
40     $container = new ContainerBuilder();
41     $container->set('cache_contexts_manager', $this->cacheContextsManager);
42     \Drupal::setContainer($container);
43   }
44
45   /**
46    * Tests the create access method.
47    *
48    * @covers ::access
49    */
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()));
56
57     $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
58     $entity_manager->expects($this->once())
59       ->method('getHandler')
60       ->withAnyParameters()
61       ->will($this->returnValue($translation_handler));
62
63     // Set our source and target languages.
64     $source = 'en';
65     $target = 'it';
66
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'])));
84
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()
89       ->getMock();
90     $entity->expects($this->once())
91       ->method('getEntityTypeId');
92     $entity->expects($this->once())
93       ->method('getTranslationLanguages')
94       ->with()
95       ->will($this->returnValue([]));
96     $entity->expects($this->once())
97       ->method('getCacheContexts')
98       ->willReturn([]);
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')
107       ->willReturn([]);
108
109     // Set the route requirements.
110     $route = new Route('test_route');
111     $route->setRequirement('_access_content_translation_manage', 'create');
112
113     // Set up the route match.
114     $route_match = $this->getMock('Drupal\Core\Routing\RouteMatchInterface');
115     $route_match->expects($this->once())
116       ->method('getParameter')
117       ->with('node')
118       ->will($this->returnValue($entity));
119
120     // Set the mock account.
121     $account = $this->getMock('Drupal\Core\Session\AccountInterface');
122
123     // The access check under test.
124     $check = new ContentTranslationManageAccessCheck($entity_manager, $language_manager);
125
126     // The request params.
127     $language = 'en';
128     $entity_type_id = 'node';
129
130     $this->assertTrue($check->access($route, $route_match, $account, $source, $target, $language, $entity_type_id)->isAllowed(), "The access check matches");
131   }
132
133 }