Commit 7f1b02ec authored by alexpott's avatar alexpott

Issue #2419065 by jhedstrom: Switching the entity storage class should not be...

Issue #2419065 by jhedstrom: Switching the entity storage class should not be considered a definition update that requires schema changes
parent 024a6a96
......@@ -127,7 +127,6 @@ protected function installedStorageSchema() {
*/
public function requiresEntityStorageSchemaChanges(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
return
$entity_type->getStorageClass() != $original->getStorageClass() ||
$entity_type->isRevisionable() != $original->isRevisionable() ||
$entity_type->isTranslatable() != $original->isTranslatable() ||
$this->hasSharedTableNameChanges($entity_type, $original) ||
......
......@@ -8,6 +8,7 @@
namespace Drupal\Tests\Core\Entity\Sql;
use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\Sql\DefaultTableMapping;
use Drupal\Tests\UnitTestCase;
......@@ -48,7 +49,7 @@ class SqlContentEntityStorageSchemaTest extends UnitTestCase {
/**
* The storage schema handler used in this test.
*
* @var \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema.
* @var \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema|\PHPUnit_Framework_MockObject_MockObject.
*/
protected $storageSchema;
......@@ -1041,29 +1042,21 @@ public function testDedicatedTableSchemaForEntityWithStringIdentifier() {
}
public function providerTestRequiresEntityDataMigration() {
$updated_entity_type_definition = $this->getMockBuilder('\Drupal\Core\Entity\EntityTypeInterface')
->disableOriginalConstructor()
->getMock();
$updated_entity_type_definition = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
$updated_entity_type_definition->expects($this->any())
->method('getStorageClass')
// A class that exists, *any* class.
->willReturn('\Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema');
$original_entity_type_definition = $this->getMockBuilder('\Drupal\Core\Entity\EntityTypeInterface')
->disableOriginalConstructor()
->getMock();
$original_entity_type_definition = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
$original_entity_type_definition->expects($this->any())
->method('getStorageClass')
// A class that exists, *any* class.
->willReturn('\Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema');
$original_entity_type_definition_other_nonexisting = $this->getMockBuilder('\Drupal\Core\Entity\EntityTypeInterface')
->disableOriginalConstructor()
->getMock();
$original_entity_type_definition_other_nonexisting = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
$original_entity_type_definition_other_nonexisting->expects($this->any())
->method('getStorageClass')
->willReturn('bar');
$original_entity_type_definition_other_existing = $this->getMockBuilder('\Drupal\Core\Entity\EntityTypeInterface')
->disableOriginalConstructor()
->getMock();
$original_entity_type_definition_other_existing = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
$original_entity_type_definition_other_existing->expects($this->any())
->method('getStorageClass')
// A class that exists, *any* class.
......@@ -1122,6 +1115,118 @@ public function testRequiresEntityDataMigration($updated_entity_type_definition,
$this->assertEquals($migration_required, $this->storageSchema->requiresEntityDataMigration($updated_entity_type_definition, $original_entity_type_definition));
}
/**
* Data provider for ::testRequiresEntityStorageSchemaChanges().
*/
public function providerTestRequiresEntityStorageSchemaChanges() {
$cases = [];
$updated_entity_type_definition = $this->getMock('\Drupal\Core\Entity\ContentEntityTypeInterface');
$original_entity_type_definition = $this->getMock('\Drupal\Core\Entity\ContentEntityTypeInterface');
$updated_entity_type_definition->expects($this->any())
->method('id')
->willReturn('entity_test');
$updated_entity_type_definition->expects($this->any())
->method('getKey')
->willReturn('id');
$original_entity_type_definition->expects($this->any())
->method('id')
->willReturn('entity_test');
$original_entity_type_definition->expects($this->any())
->method('getKey')
->willReturn('id');
// Storage class changes should not impact this at all, and should not be
// checked.
$updated = clone $updated_entity_type_definition;
$original = clone $original_entity_type_definition;
$updated->expects($this->never())
->method('getStorageClass');
$original->expects($this->never())
->method('getStorageClass');
// Case 1: No shared table changes should not require change.
$cases[] = [$updated, $original, FALSE, FALSE, FALSE];
// Case 2: A change in the entity schema should result in required changes.
$cases[] = [$updated, $original, TRUE, TRUE, FALSE];
// Case 3: Has shared table changes should result in required changes.
$cases[] = [$updated, $original, TRUE, FALSE, TRUE];
// Case 4: Changing translation should result in required changes.
$updated = clone $updated_entity_type_definition;
$updated->expects($this->once())
->method('isTranslatable')
->willReturn(FALSE);
$original = clone $original_entity_type_definition;
$original->expects($this->once())
->method('isTranslatable')
->willReturn(TRUE);
$cases[] = [$updated, $original, TRUE, FALSE, FALSE];
// Case 5: Changing revisionable should result in required changes.
$updated = clone $updated_entity_type_definition;
$updated->expects($this->once())
->method('isRevisionable')
->willReturn(FALSE);
$original = clone $original_entity_type_definition;
$original->expects($this->once())
->method('isRevisionable')
->willReturn(TRUE);
$cases[] = [$updated, $original, TRUE, FALSE, FALSE];
return $cases;
}
/**
* @covers ::requiresEntityStorageSchemaChanges
*
* @dataProvider providerTestRequiresEntityStorageSchemaChanges
*/
public function testRequiresEntityStorageSchemaChanges(ContentEntityTypeInterface $updated, ContentEntityTypeInterface $original, $requires_change, $change_schema, $change_shared_table) {
$this->entityType = new ContentEntityType(array(
'id' => 'entity_test',
'entity_keys' => array('id' => 'id'),
));
$this->setUpStorageSchema();
$table_mapping = new DefaultTableMapping($this->entityType, $this->storageDefinitions);
$table_mapping->setFieldNames('entity_test', array_keys($this->storageDefinitions));
$table_mapping->setExtraColumns('entity_test', array('default_langcode'));
$this->storage->expects($this->any())
->method('getTableMapping')
->will($this->returnValue($table_mapping));
// Setup storage schema.
if ($change_schema) {
$this->storageSchema->expects($this->once())
->method('loadEntitySchemaData')
->willReturn(array());
}
else {
$expected = [
'entity_test' => [
'primary key' => ['id'],
],
];
$this->storageSchema->expects($this->any())
->method('loadEntitySchemaData')
->willReturn($expected);
}
if ($change_shared_table) {
$this->storageSchema->expects($this->once())
->method('hasSharedTableNameChanges')
->willReturn(TRUE);
}
$this->assertEquals($requires_change, $this->storageSchema->requiresEntityStorageSchemaChanges($updated, $original));
}
/**
* Sets up the storage schema object to test.
*
......@@ -1176,7 +1281,7 @@ protected function setUpStorageSchema(array $expected = array()) {
$key_value = $this->getMock('Drupal\Core\KeyValueStore\KeyValueStoreInterface');
$this->storageSchema = $this->getMockBuilder('Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema')
->setConstructorArgs(array($this->entityManager, $this->entityType, $this->storage, $connection))
->setMethods(array('installedStorageSchema'))
->setMethods(array('installedStorageSchema', 'loadEntitySchemaData', 'hasSharedTableNameChanges'))
->getMock();
$this->storageSchema
->expects($this->any())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment