.../Sql/SqlContentEntityStorageSchemaTest.php | 73 +++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php b/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php index 3a28de0..1e033af 100644 --- a/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php @@ -14,7 +14,6 @@ /** * @coversDefaultClass \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema - * @group Entity */ class SqlContentEntityStorageSchemaTest extends UnitTestCase { @@ -1056,6 +1055,78 @@ public function testDedicatedTableSchemaForEntityWithStringIdentifier() { ); } + public function providerTestRequiresEntityDataMigration() { + $updated_entity_type_definition = $this->getMockBuilder('\Drupal\Core\Entity\EntityTypeInterface') + ->disableOriginalConstructor() + ->getMock(); + $updated_entity_type_definition->expects($this->any()) + ->method('getStorageClass') + ->willReturn('foo'); + $original_entity_type_definition = $this->getMockBuilder('\Drupal\Core\Entity\EntityTypeInterface') + ->disableOriginalConstructor() + ->getMock(); + $original_entity_type_definition->expects($this->any()) + ->method('getStorageClass') + ->willReturn('foo'); + $original_entity_type_definition_other_nonexisting = $this->getMockBuilder('\Drupal\Core\Entity\EntityTypeInterface') + ->disableOriginalConstructor() + ->getMock(); + $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->expects($this->any()) + ->method('getStorageClass') + ->willReturn('\Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema'); // A class that exists, *any* class. + + return [ + // Case 1: same storage class, ::hasData() === TRUE. + [$updated_entity_type_definition, $original_entity_type_definition, TRUE, TRUE], + // Case 2: same storage class, ::hasData() === FALSE. + [$updated_entity_type_definition, $original_entity_type_definition, FALSE, FALSE], + // Case 3: different storage class, original storage class does not exist. + [$updated_entity_type_definition, $original_entity_type_definition_other_nonexisting, NULL, TRUE], + // Case 4: different storage class, original storage class exists, ::hasData() === TRUE. + [$updated_entity_type_definition, $original_entity_type_definition_other_existing, TRUE, TRUE], + // Case 5: different storage class, original storage class exists, ::hasData() === FALSE. + [$updated_entity_type_definition, $original_entity_type_definition_other_existing, FALSE, FALSE], + ]; + } + + /** + * @covers ::requiresEntityDataMigration + * + * @dataProvider providerTestRequiresEntityDataMigration + */ + public function testRequiresEntityDataMigration($updated_entity_type_definition, $original_entity_type_definition, $storage_has_data, $migration_required) { + $this->entityType = new ContentEntityType(array( + 'id' => 'entity_test', + 'entity_keys' => array('id' => 'id'), + )); + + // Assert hasData() is not called when $storage_has_data === NULL. + $this->storage->expects($this->exactly(is_null($storage_has_data) ? 0 : 1)) + ->method('hasData') + ->willReturn($storage_has_data); + + $connection = $this->getMockBuilder('Drupal\Core\Database\Connection') + ->disableOriginalConstructor() + ->getMock(); + + $this->entityManager->expects($this->any()) + ->method('createHandlerInstance') + ->willReturn($this->storage); + + $this->storageSchema = $this->getMockBuilder('Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema') + ->setConstructorArgs(array($this->entityManager, $this->entityType, $this->storage, $connection)) + ->setMethods(array('installedStorageSchema')) + ->getMock(); + + $this->assertEquals($migration_required, $this->storageSchema->requiresEntityDataMigration($updated_entity_type_definition, $original_entity_type_definition)); + } + /** * Sets up the storage schema object to test. *