diff --git a/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php b/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php index 3bc3ede..6dba5e0 100644 --- a/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php @@ -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; @@ -1081,29 +1082,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. @@ -1163,89 +1156,115 @@ public function testRequiresEntityDataMigration($updated_entity_type_definition, } /** - * @covers ::requiresEntityStorageSchemaChanges + * Data provider for ::testRequiresEntityStorageSchemaChanges(). */ - public function testRequiresEntityStorageSchemaChanges() { - $this->entityType = new ContentEntityType(array( - 'id' => 'entity_test', - 'entity_keys' => array('id' => 'id'), - )); + public function providerTestRequiresEntityStorageSchemaChanges() { - $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)); + $cases = []; - $updated_entity_type_definition = $this->getMockBuilder('\Drupal\Core\Entity\ContentEntityTypeInterface') - ->getMock(); - $original_entity_type_definition = $this->getMockBuilder('\Drupal\Core\Entity\ContentEntityTypeInterface') - ->getMock(); + $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($this->entityType->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($this->entityType->id()); + ->willReturn('entity_test'); $original_entity_type_definition->expects($this->any()) ->method('getKey') ->willReturn('id'); // Case 1: Storage class changes should not impact this at all. - $updated_entity_type_definition->expects($this->never()) + $updated = clone $updated_entity_type_definition; + $original = clone $original_entity_type_definition; + $updated->expects($this->never()) ->method('getStorageClass'); - $original_entity_type_definition->expects($this->never()) + $original->expects($this->never()) ->method('getStorageClass'); - $expected = [ - 'entity_test' => [ - 'primary key' => ['id'], - ], - ]; - $this->storageSchema->expects($this->any()) - ->method('loadEntitySchemaData') - ->willReturn($expected); - $this->assertFalse($this->storageSchema->requiresEntityStorageSchemaChanges($updated_entity_type_definition, $original_entity_type_definition)); + $cases[] = [$updated, $original, FALSE]; // Case 2: A change in the entity schema should result in required changes. - $this->setUpStorageSchema(); - $this->storageSchema->expects($this->once()) - ->method('loadEntitySchemaData') - ->willReturn(array()); - $this->assertTrue($this->storageSchema->requiresEntityStorageSchemaChanges($updated_entity_type_definition, $original_entity_type_definition)); + $cases[] = [$updated, $original, TRUE, TRUE]; // Case 3: Has shared table changes should result in required changes. - $this->setUpStorageSchema(); - $this->storageSchema->expects($this->once()) - ->method('hasSharedTableNameChanges') - ->willReturn(TRUE); - $this->assertTrue($this->storageSchema->requiresEntityStorageSchemaChanges($updated_entity_type_definition, $original_entity_type_definition)); + $cases[] = [$updated, $original, TRUE, FALSE, TRUE]; - // Case 4: Changing translation should result in required changes. - $this->setUpStorageSchema(); - $updated_entity_type_definition->expects($this->once()) + // Case 4: No shared table changes should not require change. + $cases[] = [$updated, $original, FALSE, FALSE, FALSE]; + + // Case 5: Changing translation should result in required changes. + $updated = clone $updated_entity_type_definition; + $updated->expects($this->once()) ->method('isTranslatable') ->willReturn(FALSE); - $original_entity_type_definition->expects($this->once()) + $original = clone $original_entity_type_definition; + $original->expects($this->once()) ->method('isTranslatable') ->willReturn(TRUE); - $this->assertTrue($this->storageSchema->requiresEntityStorageSchemaChanges($updated_entity_type_definition, $original_entity_type_definition)); + $cases[] = [$updated, $original, TRUE, FALSE, FALSE]; - // Case 5: Changing revisionable should result in required changes. - $this->setUpStorageSchema(); - $updated_entity_type_definition->expects($this->any()) + // Case 6: Changing revisionable should result in required changes. + $updated = clone $updated_entity_type_definition; + $updated->expects($this->once()) ->method('isRevisionable') ->willReturn(FALSE); - $original_entity_type_definition->expects($this->any()) + $original = clone $original_entity_type_definition; + $original->expects($this->once()) ->method('isRevisionable') ->willReturn(TRUE); - $this->assertTrue($this->storageSchema->requiresEntityStorageSchemaChanges($updated_entity_type_definition, $original_entity_type_definition)); + $cases[] = [$updated, $original, TRUE, FALSE, FALSE]; + + return $cases; + } + + /** + * @covers ::requiresEntityStorageSchemaChanges + * + * @dataProvider providerTestRequiresEntityStorageSchemaChanges + */ + public function testRequiresEntityStorageSchemaChanges(ContentEntityTypeInterface $updated, ContentEntityTypeInterface $original, $requires_change, $change_schema = FALSE, $change_shared_table = FALSE) { + + $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)); } /**