diff --git a/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php b/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php index 9b6f0e2..e5d6659 100644 --- a/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php @@ -19,6 +19,13 @@ class SqlContentEntityStorageSchemaTest extends UnitTestCase { /** + * The mocked DB schema handler. + * + * @var \Drupal\Core\Database\Schema|\PHPUnit_Framework_MockObject_MockObject + */ + protected $dbSchemaHandler; + + /** * The mocked entity manager used in this test. * * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject @@ -1298,7 +1305,7 @@ protected function setUpStorageSchema(array $expected = array()) { ->with($this->entityType->id()) ->will($this->returnValue($this->storageDefinitions)); - $db_schema_handler = $this->getMockBuilder('Drupal\Core\Database\Schema') + $this->dbSchemaHandler = $this->getMockBuilder('Drupal\Core\Database\Schema') ->disableOriginalConstructor() ->getMock(); @@ -1307,7 +1314,7 @@ protected function setUpStorageSchema(array $expected = array()) { $expected_table_names = array_keys($expected); $expected_table_schemas = array_values($expected); - $db_schema_handler->expects($this->any()) + $this->dbSchemaHandler->expects($this->any()) ->method('createTable') ->with( $this->callback(function($table_name) use (&$invocation_count, $expected_table_names) { @@ -1327,17 +1334,21 @@ protected function setUpStorageSchema(array $expected = array()) { ->getMock(); $connection->expects($this->any()) ->method('schema') - ->will($this->returnValue($db_schema_handler)); + ->will($this->returnValue($this->dbSchemaHandler)); $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', 'loadEntitySchemaData', 'hasSharedTableNameChanges')) + ->setMethods(array('installedStorageSchema', 'loadEntitySchemaData', 'hasSharedTableNameChanges', 'isTableEmpty')) ->getMock(); $this->storageSchema ->expects($this->any()) ->method('installedStorageSchema') ->will($this->returnValue($key_value)); + $this->storageSchema + ->expects($this->any()) + ->method('isTableEmpty') + ->willReturn(FALSE); } /** @@ -1380,4 +1391,76 @@ public function setUpStorageDefinition($field_name, array $schema) { } } + /** + * ::onEntityTypeUpdate + */ + public function testonEntityTypeUpdateWithNewIndex() { + $entity_type_id = 'entity_test'; + $this->entityType = $original_entity_type = new ContentEntityType(array( + 'id' => 'entity_test', + 'entity_keys' => array('id' => 'id'), + )); + + // Add a field with a really long index. + $this->setUpStorageDefinition('long_index_name', array( + 'columns' => array( + 'long_index_name' => array( + 'type' => 'int', + ), + ), + 'indexes' => array( + 'long_index_name_really_long_long_name' => array(array('long_index_name', 10)), + ), + )); + + $expected = array( + 'entity_test' => array( + 'description' => 'The base table for entity_test entities.', + 'fields' => array( + 'id' => array( + 'type' => 'serial', + 'not null' => TRUE, + ), + 'long_index_name' => array( + 'type' => 'int', + 'not null' => FALSE, + ), + ), + 'indexes' => array( + 'entity_test__b588603cb9' => array( + array('long_index_name', 10), + ), + ), + ), + ); + + $this->setUpStorageSchema($expected); + + $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)); + + $this->storageSchema->expects($this->any()) + ->method('loadEntitySchemaData') + ->willReturn([]); + + $this->dbSchemaHandler->expects($this->atLeastOnce()) + ->method('addIndex') + ->with('entity_test', 'entity_test__b588603cb9', [['long_index_name', 10]], $this->callback(function($actual_value) use ($expected) { + $this->assertEquals($expected['entity_test']['indexes'], $actual_value['indexes']); + $this->assertEquals($expected['entity_test']['fields'], $actual_value['fields']); + // If the parameters don't match, the assertions above will throw an + // exception. + return TRUE; + })); + + $this->assertNull( + $this->storageSchema->onEntityTypeUpdate($this->entityType, $original_entity_type) + ); + } + }