diff --git a/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php b/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php index e9b61af..509cbb5 100644 --- a/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/Sql/SqlContentEntityStorageSchemaTest.php @@ -1319,4 +1319,93 @@ public function setUpStorageDefinition($field_name, array $schema) { } } + /** + * Data provider for testPerformFieldSchemaOperation(). + * + * @return array + * - Value to mock for + * TableMappingInterface::requiresDedicatedTableStorage(). + * - Value to mock for TableMappingInterface::allowsSharedTableStorage(). + * - Operation string. First argument for performFieldSchemaOperation(). + * - Mocked FieldStorageDefinitionInterface for $storage_definition. Second + * argument for performFieldSchemaOperation(). + * - Mocked FieldStorageDefinitionInterface for $original. Third argument + * for performFieldSchemaOperation(). + */ + public function providerPerformFieldSchemaOperation() { + $storage_definition = $this->getMockForAbstractClass('Drupal\Core\Field\FieldStorageDefinitionInterface'); + $original = $this->getMockForAbstractClass('Drupal\Core\Field\FieldStorageDefinitionInterface'); + return [ + [TRUE, FALSE, 'operation', $storage_definition, $original], + [FALSE, TRUE, 'operation', $storage_definition, $original], + [FALSE, FALSE, 'operation', $storage_definition, $original], + [TRUE, TRUE, 'operation', $storage_definition, $original], + ]; + } + + /** + * @covers ::performFieldSchemaOperation + * @dataProvider providerPerformFieldSchemaOperation + */ + public function testPerformFieldSchemaOperation($requires_dedicated_table_storage, $allows_shared_table_storage, $operation, $storage_definition, $original) { + // Mock TableMappingInterface dependency. + $mock_table_mapping_interface = $this->getMockBuilder('Drupal\Core\Entity\Sql\TableMappingInterface') + ->setMethods(array('requiresDedicatedTableStorage', 'allowsSharedTableStorage')) + ->getMockForAbstractClass(); + // Set expectations for these two methods. + $mock_table_mapping_interface->expects($this->once()) + ->method('requiresDedicatedTableStorage') + ->willReturn($requires_dedicated_table_storage); + // If requiresDedicatedTableStorage() is true, allowsSharedTableStorage() + // will never be called. + $mock_table_mapping_interface->expects($this->exactly( + $requires_dedicated_table_storage ? 0 : 1 + )) + ->method('allowsSharedTableStorage') + ->willReturn($allows_shared_table_storage); + + // Mock SqlContentEntityStorage dependency. + $mock_sql_content_entity_storage = $this->getMockBuilder('Drupal\Core\Entity\Sql\SqlContentEntityStorage') + ->disableOriginalConstructor() + ->setMethods(array('getTableMapping')) + ->getMock(); + $mock_sql_content_entity_storage->expects($this->once()) + ->method('getTableMapping')->willReturn($mock_table_mapping_interface); + + // Set up naming-convention methods for our SqlContentEntityStorageSchema + // mock. + $methods = array( + $operation . 'DedicatedTableSchema', + $operation . 'SharedTableSchema', + ); + // Mock the SqlContentEntityStorageSchema. + $mock_schema = $this->getMockBuilder('Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema') + ->disableOriginalConstructor() + ->setMethods($methods) + ->getMock(); + // Set expectations for our methods, similar to those for the table mapping + // but not the same. + $mock_schema->expects($this->exactly( + $requires_dedicated_table_storage ? 1 : 0 + )) + ->method($operation . 'DedicatedTableSchema') + ->with($storage_definition, $original); + $mock_schema->expects($this->exactly( + (!$requires_dedicated_table_storage) && $allows_shared_table_storage ? 1 : 0 + )) + ->method($operation . 'SharedTableSchema') + ->with($storage_definition, $original); + + // Poke our storage mock into $mock_schema. + $ref_storage = new \ReflectionProperty($mock_schema, 'storage'); + $ref_storage->setAccessible(TRUE); + $ref_storage->setValue($mock_schema, $mock_sql_content_entity_storage); + + // Make the method accessible. + $ref_perform = new \ReflectionMethod($mock_schema, 'performFieldSchemaOperation'); + $ref_perform->setAccessible(TRUE); + // Finally, call the method on our mocked object. + $ref_perform->invoke($mock_schema, $operation, $storage_definition, $original); + } + }