View source
<?php
namespace Drupal\Tests\dynamic_entity_reference\Functional\Update;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\entity_test\Entity\EntityTestMul;
use Drupal\entity_test\Entity\EntityTestStringId;
use Drupal\FunctionalTests\Update\UpdatePathTestBase;
class DerUpdateTest extends UpdatePathTestBase {
protected $installProfile = 'testing';
protected function setDatabaseDumpFiles() {
$this->databaseDumpFiles = [
__DIR__ . '/../../../fixtures/update/der_dump.php.gz',
];
}
public function testUpdate8201() {
$connection = \Drupal::database();
if ($connection
->driver() == 'mysql') {
$connection
->query('ALTER TABLE {entity_test__field_test} ROW_FORMAT=compact');
}
$this
->runUpdates();
$this
->assertEquals([
NULL,
1,
1,
], $connection
->query('SELECT dynamic_references__target_id FROM {entity_test} ORDER BY id')
->fetchCol());
$this
->assertEquals([
NULL,
1,
1,
], $connection
->query('SELECT dynamic_references__target_id_int FROM {entity_test} ORDER BY id')
->fetchCol());
$this
->assertEquals([
1,
1,
1,
1,
], $connection
->query('SELECT field_test_target_id FROM {entity_test__field_test} ORDER BY entity_id, delta')
->fetchCol());
$this
->assertEquals([
1,
1,
1,
1,
], $connection
->query('SELECT field_test_target_id_int FROM {entity_test__field_test} ORDER BY entity_id, delta')
->fetchCol());
$this
->assertEquals([
NULL,
1,
1,
], $connection
->query('SELECT dynamic_references__target_id FROM {entity_test_mul_property_data} ORDER BY id')
->fetchCol());
$this
->assertEquals([
NULL,
1,
1,
], $connection
->query('SELECT dynamic_references__target_id_int FROM {entity_test_mul_property_data} ORDER BY id')
->fetchCol());
$this
->assertEquals([
1,
1,
1,
1,
], $connection
->query('SELECT field_test_mul_target_id FROM {entity_test_mul__field_test_mul} ORDER BY entity_id, delta')
->fetchCol());
$this
->assertEquals([
1,
1,
1,
1,
], $connection
->query('SELECT field_test_mul_target_id_int FROM {entity_test_mul__field_test_mul} ORDER BY entity_id, delta')
->fetchCol());
$referenced_entity = EntityTestStringId::create([
'id' => 'test',
]);
$referenced_entity
->save();
$entity = EntityTest::load(3);
$entity->field_test[] = $referenced_entity;
$entity
->save();
$this
->assertEquals([
1,
1,
1,
1,
0,
], $connection
->query('SELECT field_test_target_id_int FROM {entity_test__field_test} ORDER BY entity_id, delta')
->fetchCol());
$this
->assertEquals([
1,
1,
1,
1,
'test',
], $connection
->query('SELECT field_test_target_id FROM {entity_test__field_test} ORDER BY entity_id, delta')
->fetchCol());
$entity = EntityTestMul::load(3);
$entity->field_test_mul[] = $referenced_entity;
$entity
->save();
$this
->assertEquals([
1,
1,
1,
1,
0,
], $connection
->query('SELECT field_test_mul_target_id_int FROM {entity_test_mul__field_test_mul} ORDER BY entity_id, delta')
->fetchCol());
$this
->assertEquals([
1,
1,
1,
1,
'test',
], $connection
->query('SELECT field_test_mul_target_id FROM {entity_test_mul__field_test_mul} ORDER BY entity_id, delta')
->fetchCol());
$referenced_entity = EntityTest::load(1);
$referenced_entity_mul = EntityTestMul::load(1);
$entity = EntityTest::create();
$entity
->save();
$entity = EntityTestMul::create();
$entity
->save();
$entity = EntityTest::create();
$entity->field_test[] = $referenced_entity;
$entity->field_test[] = $referenced_entity_mul;
$entity->dynamic_references[] = $referenced_entity_mul;
$entity
->save();
$entity = EntityTestMul::create();
$entity->field_test_mul[] = $referenced_entity;
$entity->field_test_mul[] = $referenced_entity_mul;
$entity->dynamic_references[] = $referenced_entity;
$entity
->save();
$this
->assertEquals([
NULL,
1,
1,
NULL,
1,
], $connection
->query('SELECT dynamic_references__target_id FROM {entity_test} ORDER BY id')
->fetchCol());
$this
->assertEquals([
NULL,
1,
1,
NULL,
1,
], $connection
->query('SELECT dynamic_references__target_id_int FROM {entity_test} ORDER BY id')
->fetchCol());
$this
->assertEquals([
1,
1,
1,
1,
0,
1,
1,
], $connection
->query('SELECT field_test_target_id_int FROM {entity_test__field_test} ORDER BY entity_id, delta')
->fetchCol());
$this
->assertEquals([
1,
1,
1,
1,
'test',
1,
1,
], $connection
->query('SELECT field_test_target_id FROM {entity_test__field_test} ORDER BY entity_id, delta')
->fetchCol());
$this
->assertEquals([
NULL,
1,
1,
NULL,
1,
], $connection
->query('SELECT dynamic_references__target_id FROM {entity_test_mul_property_data} ORDER BY id')
->fetchCol());
$this
->assertEquals([
NULL,
1,
1,
NULL,
1,
], $connection
->query('SELECT dynamic_references__target_id_int FROM {entity_test_mul_property_data} ORDER BY id')
->fetchCol());
$this
->assertEquals([
1,
1,
1,
1,
0,
1,
1,
], $connection
->query('SELECT field_test_mul_target_id_int FROM {entity_test_mul__field_test_mul} ORDER BY entity_id, delta')
->fetchCol());
$this
->assertEquals([
1,
1,
1,
1,
'test',
1,
1,
], $connection
->query('SELECT field_test_mul_target_id FROM {entity_test_mul__field_test_mul} ORDER BY entity_id, delta')
->fetchCol());
$this
->assertTrue(\Drupal::database()
->schema()
->indexExists('entity_test', 'dynamic_references__target_id_int'));
$this
->assertTrue(\Drupal::database()
->schema()
->indexExists('entity_test__field_test', 'field_test_target_id_int'));
$this
->assertTrue(\Drupal::database()
->schema()
->indexExists('entity_test_mul__field_test_mul', 'field_test_mul_target_id_int'));
}
}