function EntityRepositoryTest::testGetTranslationFromContext

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Entity/EntityRepositoryTest.php \Drupal\Tests\Core\Entity\EntityRepositoryTest::testGetTranslationFromContext()
  2. 8.9.x core/tests/Drupal/Tests/Core/Entity/EntityRepositoryTest.php \Drupal\Tests\Core\Entity\EntityRepositoryTest::testGetTranslationFromContext()
  3. 10 core/tests/Drupal/Tests/Core/Entity/EntityRepositoryTest.php \Drupal\Tests\Core\Entity\EntityRepositoryTest::testGetTranslationFromContext()

Tests the getTranslationFromContext() method.

@covers ::getTranslationFromContext

File

core/tests/Drupal/Tests/Core/Entity/EntityRepositoryTest.php, line 69

Class

EntityRepositoryTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Entity%21EntityRepository.php/class/EntityRepository/11.x" title="Provides several mechanisms for retrieving entities." class="local">\Drupal\Core\Entity\EntityRepository</a> @group Entity

Namespace

Drupal\Tests\Core\Entity

Code

public function testGetTranslationFromContext() : void {
    $language = new Language([
        'id' => 'en',
    ]);
    $this->languageManager
        ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
        ->willReturn($language)
        ->shouldBeCalledTimes(1);
    $this->languageManager
        ->getFallbackCandidates(Argument::type('array'))
        ->will(function ($args) {
        $context = $args[0];
        $candidates = [];
        if (!empty($context['langcode'])) {
            $candidates[$context['langcode']] = $context['langcode'];
        }
        return $candidates;
    })
        ->shouldBeCalledTimes(1);
    $translated_entity = $this->prophesize(ContentEntityInterface::class);
    $entity = $this->prophesize(ContentEntityInterface::class);
    $entity->getUntranslated()
        ->willReturn($entity);
    $entity->language()
        ->willReturn($language);
    $entity->hasTranslation(LanguageInterface::LANGCODE_DEFAULT)
        ->willReturn(FALSE);
    $entity->hasTranslation('custom_langcode')
        ->willReturn(TRUE);
    $entity->getTranslation('custom_langcode')
        ->willReturn($translated_entity->reveal());
    $entity->getTranslationLanguages()
        ->willReturn([
        new Language([
            'id' => 'en',
        ]),
        new Language([
            'id' => 'custom_langcode',
        ]),
    ]);
    $entity->addCacheContexts([
        'languages:language_content',
    ])
        ->shouldBeCalled();
    $this->assertSame($entity->reveal(), $this->entityRepository
        ->getTranslationFromContext($entity->reveal()));
    $this->assertSame($translated_entity->reveal(), $this->entityRepository
        ->getTranslationFromContext($entity->reveal(), 'custom_langcode'));
}

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