/**
  * Tests field SQL schema generation for an entity with a string identifier.
  *
  * @covers ::_fieldSqlSchema()
  */
 public function testFieldSqlSchemaForEntityWithStringIdentifier()
 {
     $field_type_manager = $this->getMock('Drupal\\Core\\Field\\FieldTypePluginManagerInterface');
     $this->container->set('plugin.manager.field.field_type', $field_type_manager);
     $this->container->set('entity.manager', $this->entityManager);
     $this->entityType->expects($this->any())->method('getKey')->will($this->returnValueMap(array(array('id', 'id'), array('revision', 'revision'))));
     $this->entityType->expects($this->once())->method('hasKey')->with('revision')->will($this->returnValue(TRUE));
     $field_type_manager->expects($this->exactly(2))->method('getDefaultSettings')->will($this->returnValue(array()));
     $field_type_manager->expects($this->exactly(2))->method('getDefaultInstanceSettings')->will($this->returnValue(array()));
     $this->fieldDefinitions['id'] = FieldDefinition::create('string')->setName('id');
     $this->fieldDefinitions['revision'] = FieldDefinition::create('string')->setName('revision');
     $this->entityManager->expects($this->any())->method('getDefinition')->with('test_entity')->will($this->returnValue($this->entityType));
     $this->entityManager->expects($this->any())->method('getBaseFieldDefinitions')->will($this->returnValue($this->fieldDefinitions));
     // Define a field definition for a test_field field.
     $field_storage = $this->getMock('\\Drupal\\field\\FieldStorageConfigInterface');
     $field_storage->deleted = FALSE;
     $field_storage->expects($this->any())->method('getName')->will($this->returnValue('test_field'));
     $field_storage->expects($this->any())->method('getTargetEntityTypeId')->will($this->returnValue('test_entity'));
     $field_schema = array('columns' => array('value' => array('type' => 'varchar', 'length' => 10, 'not null' => FALSE)), 'unique keys' => array(), 'indexes' => array(), 'foreign keys' => array());
     $field_storage->expects($this->any())->method('getSchema')->will($this->returnValue($field_schema));
     $schema = ContentEntityDatabaseStorage::_fieldSqlSchema($field_storage);
     // Make sure that the entity_id schema field if of type varchar.
     $this->assertEquals($schema['test_entity__test_field']['fields']['entity_id']['type'], 'varchar');
     $this->assertEquals($schema['test_entity__test_field']['fields']['revision_id']['type'], 'varchar');
 }
Esempio n. 2
0
 /**
  * Test foreign key support.
  */
 function testFieldSqlStorageForeignKeys()
 {
     // Create a 'shape' field, with a configurable foreign key (see
     // field_test_field_schema()).
     $field_name = 'testfield';
     $foreign_key_name = 'shape';
     $field_storage = entity_create('field_storage_config', array('name' => $field_name, 'entity_type' => 'entity_test', 'type' => 'shape', 'settings' => array('foreign_key_name' => $foreign_key_name)));
     $field_storage->save();
     // Get the field schema.
     $schema = $field_storage->getSchema();
     // Retrieve the field definition and check that the foreign key is in place.
     $this->assertEqual($schema['foreign keys'][$foreign_key_name]['table'], $foreign_key_name, 'Foreign key table name preserved through CRUD');
     $this->assertEqual($schema['foreign keys'][$foreign_key_name]['columns'][$foreign_key_name], 'id', 'Foreign key column name preserved through CRUD');
     // Update the field settings, it should update the foreign key definition too.
     $foreign_key_name = 'color';
     $field_storage->settings['foreign_key_name'] = $foreign_key_name;
     $field_storage->save();
     // Reload the field schema after the update.
     $schema = $field_storage->getSchema();
     // Retrieve the field definition and check that the foreign key is in place.
     $field_storage = FieldStorageConfig::loadByName('entity_test', $field_name);
     $this->assertEqual($schema['foreign keys'][$foreign_key_name]['table'], $foreign_key_name, 'Foreign key table name modified after update');
     $this->assertEqual($schema['foreign keys'][$foreign_key_name]['columns'][$foreign_key_name], 'id', 'Foreign key column name modified after update');
     // Verify the SQL schema.
     $schemas = ContentEntityDatabaseStorage::_fieldSqlSchema($field_storage);
     $schema = $schemas[ContentEntityDatabaseStorage::_fieldTableName($field_storage)];
     $this->assertEqual(count($schema['foreign keys']), 1, 'There is 1 foreign key in the schema');
     $foreign_key = reset($schema['foreign keys']);
     $foreign_key_column = ContentEntityDatabaseStorage::_fieldColumnName($field_storage, $foreign_key_name);
     $this->assertEqual($foreign_key['table'], $foreign_key_name, 'Foreign key table name preserved in the schema');
     $this->assertEqual($foreign_key['columns'][$foreign_key_column], 'id', 'Foreign key column name preserved in the schema');
 }
Esempio n. 3
0
 /**
  * Verify that deleting an instance leaves the field data items in
  * the database and that the appropriate Field API functions can
  * operate on the deleted data and instance.
  *
  * This tests how EntityFieldQuery interacts with field instance deletion and
  * could be moved to FieldCrudTestCase, but depends on this class's setUp().
  */
 function testDeleteFieldInstance()
 {
     $bundle = reset($this->bundles);
     $field_storage = reset($this->fieldStorages);
     $field_name = $field_storage->name;
     $factory = \Drupal::service('entity.query');
     // There are 10 entities of this bundle.
     $found = $factory->get('entity_test')->condition('type', $bundle)->execute();
     $this->assertEqual(count($found), 10, 'Correct number of entities found before deleting');
     // Delete the instance.
     $instance = FieldInstanceConfig::loadByName($this->entity_type, $bundle, $field_storage->name);
     $instance->delete();
     // The instance still exists, deleted.
     $instances = entity_load_multiple_by_properties('field_instance_config', array('field_storage_uuid' => $field_storage->uuid(), 'deleted' => TRUE, 'include_deleted' => TRUE));
     $this->assertEqual(count($instances), 1, 'There is one deleted instance');
     $instance = $instances[$instance->uuid()];
     $this->assertEqual($instance->bundle, $bundle, 'The deleted instance is for the correct bundle');
     // Check that the actual stored content did not change during delete.
     $schema = ContentEntityDatabaseStorage::_fieldSqlSchema($field_storage);
     $table = ContentEntityDatabaseStorage::_fieldTableName($field_storage);
     $column = ContentEntityDatabaseStorage::_fieldColumnName($field_storage, 'value');
     $result = db_select($table, 't')->fields('t', array_keys($schema[$table]['fields']))->execute();
     foreach ($result as $row) {
         $this->assertEqual($this->entities[$row->entity_id]->{$field_storage->name}->value, $row->{$column});
     }
     // There are 0 entities of this bundle with non-deleted data.
     $found = $factory->get('entity_test')->condition('type', $bundle)->condition("{$field_name}.deleted", 0)->execute();
     $this->assertFalse($found, 'No entities found after deleting');
     // There are 10 entities of this bundle when deleted fields are allowed, and
     // their values are correct.
     $found = $factory->get('entity_test')->condition('type', $bundle)->condition("{$field_name}.deleted", 1)->sort('id')->execute();
     $this->assertEqual(count($found), 10, 'Correct number of entities found after deleting');
     $this->assertFalse(array_diff($found, array_keys($this->entities)));
 }