You are here

public function EntityRepositoryTest::testGetTranslationFromContext in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 63
Contains \Drupal\Tests\Core\Entity\EntityRepositoryTest.

Class

EntityRepositoryTest
@coversDefaultClass \Drupal\Core\Entity\EntityManager @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 = array();
    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'));
}