View source
<?php
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\user\Traits\UserCreationTrait;
class EntityRepositoryTest extends KernelTestBase {
use UserCreationTrait;
public static $modules = [
'entity_test',
'user',
'language',
'system',
];
protected $entityTypeManager;
protected $entityRepository;
public function setUp() {
parent::setUp();
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this->entityRepository = $this->container
->get('entity.repository');
$this
->setUpCurrentUser();
$this
->installEntitySchema('entity_test');
$this
->installEntitySchema('entity_test_rev');
$this
->installEntitySchema('entity_test_mul');
$this
->installEntitySchema('entity_test_mulrev');
$this
->installConfig([
'system',
'language',
]);
ConfigurableLanguage::createFromLangcode('it')
->setWeight(1)
->save();
ConfigurableLanguage::createFromLangcode('ro')
->setWeight(2)
->save();
$this->container
->get('state')
->set('entity_test.translation', TRUE);
$this->container
->get('entity_type.bundle.info')
->clearCachedBundles();
}
public function testGetActive() {
$en_contexts = $this
->getLanguageContexts('en');
$entity_type_id = 'entity_test';
$active = $this->entityRepository
->getActive($entity_type_id, -1);
$this
->assertNull($active);
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
$values = [
'name' => $this
->randomString(),
];
$entity = $storage
->create($values);
$storage
->save($entity);
$entity = $storage
->load($entity
->id());
$active = $this->entityRepository
->getActive($entity_type_id, $entity
->id(), $en_contexts);
$this
->assertSame($entity, $active);
$entity_type_id = 'entity_test_rev';
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
$values = [
'name' => $this
->randomString(),
];
$entity = $storage
->create($values);
$storage
->save($entity);
$revision = $storage
->createRevision($entity, FALSE);
$revision
->save();
$active = $this->entityRepository
->getActive($entity_type_id, $entity
->id(), $en_contexts);
$this
->assertEntityType($active, $entity_type_id);
$this
->assertSame($revision
->getLoadedRevisionId(), $active
->getLoadedRevisionId());
$revision2 = $storage
->createRevision($revision);
$revision2
->save();
$active = $this->entityRepository
->getActive($entity_type_id, $entity
->id(), $en_contexts);
$this
->assertSame($revision2
->getLoadedRevisionId(), $active
->getLoadedRevisionId());
$entity_type_id = 'entity_test_mul';
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
$values = [
'name' => $this
->randomString(),
];
$entity = $storage
->create($values);
$storage
->save($entity);
$langcode = 'it';
$translation = $entity
->addTranslation($langcode, $values);
$storage
->save($translation);
$active = $this->entityRepository
->getActive($entity_type_id, $entity
->id(), $en_contexts);
$this
->assertEntityType($active, $entity_type_id);
$this
->assertSame($entity
->language()
->getId(), $active
->language()
->getId());
$it_contexts = $this
->getLanguageContexts($langcode);
$active = $this->entityRepository
->getActive($entity_type_id, $entity
->id(), $it_contexts);
$this
->assertSame($translation
->language()
->getId(), $active
->language()
->getId());
$entity_type_id = 'entity_test_mulrev';
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
$values = [
'name' => $this
->randomString(),
];
$entity = $storage
->create($values);
$storage
->save($entity);
$en_revision = $storage
->createRevision($entity, FALSE);
$storage
->save($en_revision);
$active = $this->entityRepository
->getActive($entity_type_id, $entity
->id(), $en_contexts);
$this
->assertEntityType($active, $entity_type_id);
$this
->assertSame($en_revision
->getLoadedRevisionId(), $active
->getLoadedRevisionId());
$revision_translation = $en_revision
->addTranslation($langcode, $values);
$it_revision = $storage
->createRevision($revision_translation, FALSE);
$storage
->save($it_revision);
$active = $this->entityRepository
->getActive($entity_type_id, $entity
->id(), $en_contexts);
$this
->assertSame($en_revision
->getLoadedRevisionId(), $active
->getLoadedRevisionId());
$this
->assertSame($en_revision
->language()
->getId(), $active
->language()
->getId());
$active = $this->entityRepository
->getActive($entity_type_id, $entity
->id(), $it_contexts);
$this
->assertSame($it_revision
->getLoadedRevisionId(), $active
->getLoadedRevisionId());
$this
->assertSame($it_revision
->language()
->getId(), $active
->language()
->getId());
$en_revision2 = $storage
->createRevision($en_revision);
$storage
->save($en_revision2);
$active = $this->entityRepository
->getActive($entity_type_id, $entity
->id(), $en_contexts);
$this
->assertSame($en_revision2
->getLoadedRevisionId(), $active
->getLoadedRevisionId());
$this
->assertSame($en_revision2
->language()
->getId(), $active
->language()
->getId());
$active = $this->entityRepository
->getActive($entity_type_id, $entity
->id(), $it_contexts);
$this
->assertSame($it_revision
->getLoadedRevisionId(), $active
->getLoadedRevisionId());
$this
->assertSame($it_revision
->language()
->getId(), $active
->language()
->getId());
$it_revision2 = $storage
->createRevision($it_revision);
$storage
->save($it_revision2);
$active = $this->entityRepository
->getActive($entity_type_id, $entity
->id(), $en_contexts);
$this
->assertSame($it_revision2
->getLoadedRevisionId(), $active
->getLoadedRevisionId());
$this
->assertSame($it_revision2
->getUntranslated()
->language()
->getId(), $active
->language()
->getId());
$active = $this->entityRepository
->getActive($entity_type_id, $entity
->id(), $it_contexts);
$this
->assertSame($it_revision2
->getLoadedRevisionId(), $active
->getLoadedRevisionId());
$this
->assertSame($it_revision2
->language()
->getId(), $active
->language()
->getId());
$entity2 = $storage
->create($values);
$storage
->save($entity2);
$active = $this->entityRepository
->getActiveMultiple($entity_type_id, [
$entity
->id(),
$entity2
->id(),
], $it_contexts);
$this
->assertSame($it_revision2
->getLoadedRevisionId(), $active[$entity
->id()]
->getLoadedRevisionId());
$this
->assertSame($it_revision2
->language()
->getId(), $active[$entity
->id()]
->language()
->getId());
$this
->assertSame($entity2
->getLoadedRevisionId(), $active[$entity2
->id()]
->getLoadedRevisionId());
$this
->assertSame($entity2
->language()
->getId(), $active[$entity2
->id()]
->language()
->getId());
$this
->doTestLanguageFallback('getActive');
}
public function testGetCanonical() {
$entity_type_id = 'entity_test_mul';
$canonical = $this->entityRepository
->getActive($entity_type_id, -1);
$this
->assertNull($canonical);
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
$values = [
'name' => $this
->randomString(),
];
$entity = $storage
->create($values);
$storage
->save($entity);
$langcode = 'it';
$it_contexts = $this
->getLanguageContexts($langcode);
$canonical = $this->entityRepository
->getCanonical($entity_type_id, $entity
->id(), $it_contexts);
$this
->assertSame($entity
->getUntranslated()
->language()
->getId(), $canonical
->language()
->getId());
$translation = $entity
->addTranslation($langcode, $values);
$storage
->save($translation);
$canonical = $this->entityRepository
->getCanonical($entity_type_id, $entity
->id(), $it_contexts);
$this
->assertSame($translation
->language()
->getId(), $canonical
->language()
->getId());
$canonical = $this->entityRepository
->getCanonical($entity_type_id, $entity
->id());
$this
->assertSame($entity
->getUntranslated()
->language()
->getId(), $canonical
->language()
->getId());
$entity2 = $storage
->create($values);
$storage
->save($entity2);
$canonical = $this->entityRepository
->getCanonicalMultiple($entity_type_id, [
$entity
->id(),
$entity2
->id(),
], $it_contexts);
$this
->assertSame($translation
->language()
->getId(), $canonical[$entity
->id()]
->language()
->getId());
$this
->assertSame($entity2
->language()
->getId(), $canonical[$entity2
->id()]
->language()
->getId());
$this
->doTestLanguageFallback('getCanonical');
}
protected function doTestLanguageFallback($method_name) {
$entity_type_id = 'entity_test_mulrev';
$en_contexts = $this
->getLanguageContexts('en');
$it_contexts = $this
->getLanguageContexts('it');
$ro_contexts = $this
->getLanguageContexts('ro');
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
$values = [
'name' => $this
->randomString(),
];
$entity3 = $storage
->create([
'langcode' => 'it',
] + $values);
$entity3
->addTranslation('ro', $values);
$storage
->save($entity3);
$active = $this->entityRepository
->{$method_name}($entity_type_id, $entity3
->id(), $en_contexts);
$this
->assertSame('it', $active
->language()
->getId());
$active = $this->entityRepository
->{$method_name}($entity_type_id, $entity3
->id(), $ro_contexts);
$this
->assertSame('ro', $active
->language()
->getId());
$entity4 = $storage
->create([
'langcode' => 'ro',
] + $values);
$entity4
->addTranslation('en', $values);
$storage
->save($entity4);
$active = $this->entityRepository
->{$method_name}($entity_type_id, $entity4
->id(), $it_contexts);
$this
->assertSame('en', $active
->language()
->getId());
$entity5 = $storage
->create([
'langcode' => 'ro',
] + $values);
$storage
->save($entity5);
$active = $this->entityRepository
->{$method_name}($entity_type_id, $entity5
->id(), $it_contexts);
$this
->assertSame('ro', $active
->language()
->getId());
$active = $this->entityRepository
->{$method_name}($entity_type_id, $entity5
->id(), $en_contexts);
$this
->assertSame('ro', $active
->language()
->getId());
}
protected function assertEntityType($entity, $expected_entity_type_id) {
$this
->assertInstanceOf(EntityTest::class, $entity);
$this
->assertEquals($expected_entity_type_id, $entity
->getEntityTypeId());
}
protected function getLanguageContexts($langcode) {
$prefix = '@language.current_language_context:';
return [
$prefix . LanguageInterface::TYPE_INTERFACE => new Context(new ContextDefinition('language'), $langcode),
$prefix . LanguageInterface::TYPE_CONTENT => new Context(new ContextDefinition('language'), $langcode),
];
}
}