View source
<?php
namespace Drupal\KernelTests\Core\ParamConverter;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\entity_test\Entity\EntityTestMulRev;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\user\Traits\UserCreationTrait;
class EntityConverterLatestRevisionTest extends KernelTestBase {
use UserCreationTrait;
protected static $modules = [
'entity_test',
'user',
'language',
'system',
];
protected $converter;
protected function setUp() : void {
parent::setUp();
$this
->setUpCurrentUser();
$this
->installEntitySchema('entity_test_mulrev');
$this
->installEntitySchema('entity_test');
$this
->installConfig([
'system',
'language',
]);
$this->converter = $this->container
->get('paramconverter.entity');
ConfigurableLanguage::createFromLangcode('de')
->save();
}
public function testNoEntity() {
$converted = $this->converter
->convert(1, [
'load_latest_revision' => TRUE,
'type' => 'entity:entity_test_mulrev',
], 'foo', []);
$this
->assertEquals(NULL, $converted);
}
public function testEntityNoPendingRevision() {
$entity = EntityTestMulRev::create();
$entity
->save();
$converted = $this->converter
->convert(1, [
'load_latest_revision' => TRUE,
'type' => 'entity:entity_test_mulrev',
], 'foo', []);
$this
->assertEquals($entity
->getLoadedRevisionId(), $converted
->getLoadedRevisionId());
}
public function testEntityWithPendingRevision() {
$entity = EntityTestMulRev::create();
$entity
->save();
$entity
->isDefaultRevision(FALSE);
$entity
->setNewRevision(TRUE);
$entity
->save();
$converted = $this->converter
->convert(1, [
'load_latest_revision' => TRUE,
'type' => 'entity:entity_test_mulrev',
], 'foo', []);
$this
->assertEquals($entity
->getLoadedRevisionId(), $converted
->getLoadedRevisionId());
}
public function testWithTranslatedPendingRevision() {
$this->container
->get('state')
->set('entity_test.translation', TRUE);
$this->container
->get('entity_type.bundle.info')
->clearCachedBundles();
$entity = EntityTestMulRev::create();
$entity
->save();
$entity_type_id = 'entity_test_mulrev';
$storage = $this->container
->get('entity_type.manager')
->getStorage($entity_type_id);
$translated_entity = $storage
->createRevision($entity
->addTranslation('de'), FALSE);
$translated_entity
->save();
$this
->config('system.site')
->set('default_langcode', 'de')
->save();
EntityTestMulRev::load($entity
->id());
$this
->assertEquals('en', $entity
->language()
->getId());
$converted = $this->converter
->convert(1, [
'load_latest_revision' => TRUE,
'type' => 'entity:entity_test_mulrev',
], 'foo', []);
$this
->assertEquals('de', $converted
->language()
->getId());
$this
->assertEquals($translated_entity
->getLoadedRevisionId(), $converted
->getLoadedRevisionId());
$this
->config('system.site')
->set('default_langcode', 'en')
->save();
$converted = $this->converter
->convert(1, [
'load_latest_revision' => TRUE,
'type' => 'entity:entity_test_mulrev',
], 'foo', []);
$this
->assertEquals('en', $converted
->language()
->getId());
$this
->assertEquals($entity
->getLoadedRevisionId(), $converted
->getLoadedRevisionId());
}
public function testOptimizedConvert() {
$entity = EntityTestMulRev::create();
$entity
->save();
$entity = EntityTestMulRev::load($entity
->id());
$database = $this->container
->get('database');
$database
->delete('entity_test_mulrev')
->condition('id', $entity
->id())
->execute();
$converted = $this->converter
->convert(1, [
'load_latest_revision' => TRUE,
'type' => 'entity:entity_test_mulrev',
], 'foo', []);
$this
->assertEquals($entity
->getLoadedRevisionId(), $converted
->getLoadedRevisionId());
}
public function testConvertNonRevisionableEntityType() {
$entity = EntityTest::create();
$entity
->save();
$converted = $this->converter
->convert(1, [
'load_latest_revision' => TRUE,
'type' => 'entity:entity_test',
], 'foo', []);
$this
->assertEquals($entity
->id(), $converted
->id());
}
}