function EntityRepositoryTest::testGetTranslationFromContext

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Entity/EntityRepositoryTest.php \Drupal\Tests\Core\Entity\EntityRepositoryTest::testGetTranslationFromContext()
  2. 10 core/tests/Drupal/Tests/Core/Entity/EntityRepositoryTest.php \Drupal\Tests\Core\Entity\EntityRepositoryTest::testGetTranslationFromContext()
  3. 11.x 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 67

Class

EntityRepositoryTest
@coversDefaultClass \Drupal\Core\Entity\EntityRepository @group Entity

Namespace

Drupal\Tests\Core\Entity

Code

public function testGetTranslationFromContext() {
    $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.