예제 #1
0
 /**
  * Tests the getTranslationFromContext() method.
  *
  * @covers ::getTranslationFromContext
  */
 public function testGetTranslationFromContext()
 {
     $this->setUpEntityManager();
     $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->entityManager->getTranslationFromContext($entity->reveal()));
     $this->assertSame($translated_entity->reveal(), $this->entityManager->getTranslationFromContext($entity->reveal(), 'custom_langcode'));
 }
예제 #2
0
 /**
  * Tests the getTranslationFromContext() method.
  *
  * @covers ::getTranslationFromContext()
  */
 public function testGetTranslationFromContext()
 {
     $this->setUpEntityManager();
     $this->languageManager->expects($this->exactly(2))->method('getFallbackCandidates')->will($this->returnCallback(function ($langcode = NULL, array $context = array()) {
         $candidates = array();
         if ($langcode) {
             $candidates[$langcode] = $langcode;
         }
         return $candidates;
     }));
     $entity = $this->getMock('Drupal\\Tests\\Core\\Entity\\TestContentEntityInterface');
     $entity->expects($this->exactly(2))->method('getUntranslated')->will($this->returnValue($entity));
     $entity->expects($this->exactly(2))->method('language')->will($this->returnValue((object) array('id' => 'en')));
     $entity->expects($this->exactly(2))->method('hasTranslation')->will($this->returnValueMap(array(array(LanguageInterface::LANGCODE_DEFAULT, FALSE), array('custom_langcode', TRUE))));
     $translated_entity = $this->getMock('Drupal\\Tests\\Core\\Entity\\TestContentEntityInterface');
     $entity->expects($this->once())->method('getTranslation')->with('custom_langcode')->will($this->returnValue($translated_entity));
     $this->assertSame($entity, $this->entityManager->getTranslationFromContext($entity));
     $this->assertSame($translated_entity, $this->entityManager->getTranslationFromContext($entity, 'custom_langcode'));
 }
예제 #3
0
 /**
  * Tests the getTranslationFromContext() method.
  *
  * @covers ::getTranslationFromContext
  */
 public function testGetTranslationFromContext()
 {
     $this->setUpEntityManager();
     $this->languageManager->expects($this->exactly(1))->method('getFallbackCandidates')->will($this->returnCallback(function (array $context = array()) {
         $candidates = array();
         if (!empty($context['langcode'])) {
             $candidates[$context['langcode']] = $context['langcode'];
         }
         return $candidates;
     }));
     $entity = $this->getMock('Drupal\\Tests\\Core\\Entity\\TestContentEntityInterface');
     $entity->expects($this->exactly(1))->method('getUntranslated')->will($this->returnValue($entity));
     $language = $this->getMock('\\Drupal\\Core\\Language\\LanguageInterface');
     $language->expects($this->any())->method('getId')->will($this->returnValue('en'));
     $entity->expects($this->exactly(3))->method('language')->will($this->returnValue($language));
     $entity->expects($this->exactly(1))->method('hasTranslation')->will($this->returnValueMap(array(array(LanguageInterface::LANGCODE_DEFAULT, FALSE), array('custom_langcode', TRUE))));
     $translated_entity = $this->getMock('Drupal\\Tests\\Core\\Entity\\TestContentEntityInterface');
     $entity->expects($this->once())->method('getTranslation')->with('custom_langcode')->will($this->returnValue($translated_entity));
     $entity->expects($this->any())->method('getTranslationLanguages')->will($this->returnValue([new Language(['id' => 'en']), new Language(['id' => 'custom_langcode'])]));
     $this->assertSame($entity, $this->entityManager->getTranslationFromContext($entity));
     $this->assertSame($translated_entity, $this->entityManager->getTranslationFromContext($entity, 'custom_langcode'));
 }