class ContentTranslationManageAccessCheckTest

Same name in other branches
  1. 9 core/modules/content_translation/tests/src/Unit/Access/ContentTranslationManageAccessCheckTest.php \Drupal\Tests\content_translation\Unit\Access\ContentTranslationManageAccessCheckTest
  2. 8.9.x core/modules/content_translation/tests/src/Unit/Access/ContentTranslationManageAccessCheckTest.php \Drupal\Tests\content_translation\Unit\Access\ContentTranslationManageAccessCheckTest
  3. 11.x core/modules/content_translation/tests/src/Unit/Access/ContentTranslationManageAccessCheckTest.php \Drupal\Tests\content_translation\Unit\Access\ContentTranslationManageAccessCheckTest

Tests for content translation manage check.

@coversDefaultClass \Drupal\content_translation\Access\ContentTranslationManageAccessCheck @group Access @group content_translation

Hierarchy

Expanded class hierarchy of ContentTranslationManageAccessCheckTest

File

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

Namespace

Drupal\Tests\content_translation\Unit\Access
View source
class ContentTranslationManageAccessCheckTest extends UnitTestCase {
    
    /**
     * The cache contexts manager.
     *
     * @var \Drupal\Core\Cache\Context\CacheContextsManager|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $cacheContextsManager;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->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() : void {
        // Set the mock translation handler.
        $translation_handler = $this->createMock('\\Drupal\\content_translation\\ContentTranslationHandlerInterface');
        $translation_handler->expects($this->once())
            ->method('getTranslationAccess')
            ->willReturn(AccessResult::allowed());
        $entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
        $entity_type_manager->expects($this->once())
            ->method('getHandler')
            ->withAnyParameters()
            ->willReturn($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(ContentEntityBaseMockableClass::class)
            ->disableOriginalConstructor()
            ->getMock();
        $entity->expects($this->once())
            ->method('getEntityTypeId');
        $entity->expects($this->once())
            ->method('getTranslationLanguages')
            ->with()
            ->willReturn([]);
        $entity->expects($this->once())
            ->method('getCacheContexts')
            ->willReturn([]);
        $entity->expects($this->once())
            ->method('getCacheMaxAge')
            ->willReturn(Cache::PERMANENT);
        $entity->expects($this->once())
            ->method('getCacheTags')
            ->willReturn([
            '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')
            ->willReturn($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");
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
ContentTranslationManageAccessCheckTest::$cacheContextsManager protected property The cache contexts manager.
ContentTranslationManageAccessCheckTest::setUp protected function Overrides UnitTestCase::setUp
ContentTranslationManageAccessCheckTest::testCreateAccess public function Tests the create access method.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.