You are here

public function ContentTranslationManageAccessCheckTest::testCreateAccess in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/content_translation/tests/src/Unit/Access/ContentTranslationManageAccessCheckTest.php \Drupal\Tests\content_translation\Unit\Access\ContentTranslationManageAccessCheckTest::testCreateAccess()
  2. 9 core/modules/content_translation/tests/src/Unit/Access/ContentTranslationManageAccessCheckTest.php \Drupal\Tests\content_translation\Unit\Access\ContentTranslationManageAccessCheckTest::testCreateAccess()

Tests the create access method.

@covers ::access

File

core/modules/content_translation/tests/src/Unit/Access/ContentTranslationManageAccessCheckTest.php, line 51

Class

ContentTranslationManageAccessCheckTest
Tests for content translation manage check.

Namespace

Drupal\Tests\content_translation\Unit\Access

Code

public function testCreateAccess() {

  // Set the mock translation handler.
  $translation_handler = $this
    ->createMock('\\Drupal\\content_translation\\ContentTranslationHandlerInterface');
  $translation_handler
    ->expects($this
    ->once())
    ->method('getTranslationAccess')
    ->will($this
    ->returnValue(AccessResult::allowed()));
  $entity_type_manager = $this
    ->createMock(EntityTypeManagerInterface::class);
  $entity_type_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
    ->createMock('Drupal\\Core\\Language\\LanguageManagerInterface');
  $language_manager
    ->expects($this
    ->once())
    ->method('getLanguages')
    ->willReturn([
    $source => [],
    $target => [],
  ]);
  $language_manager
    ->expects($this
    ->any())
    ->method('getLanguage')
    ->willReturnMap([
    [
      $source,
      new Language([
        'id' => $source,
      ]),
    ],
    [
      $target,
      new Language([
        'id' => $target,
      ]),
    ],
  ]);

  // 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
    ->createMock('Drupal\\Core\\Routing\\RouteMatchInterface');
  $route_match
    ->expects($this
    ->once())
    ->method('getParameter')
    ->with('node')
    ->will($this
    ->returnValue($entity));

  // Set the mock account.
  $account = $this
    ->createMock('Drupal\\Core\\Session\\AccountInterface');

  // The access check under test.
  $check = new ContentTranslationManageAccessCheck($entity_type_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");
}