/**
  * Tests the views data generation.
  */
 public function testViewsData()
 {
     // Test that the field is not exposed to views, since contact_message
     // entities have no storage.
     $table_name = ContentEntityDatabaseStorage::_fieldTableName($this->field_storage);
     $data = $this->container->get('views.views_data')->get($table_name);
     $this->assertFalse($data, 'The field is not exposed to Views.');
 }
 /**
  * Checks whether field languages are correctly stored for the given entity.
  *
  * @param \Drupal\Core\Entity\ContentEntityInterface $entity
  *   The entity fields are attached to.
  * @param string $message
  *   (optional) A message to display with the assertion.
  */
 protected function assertFieldStorageLangcode(ContentEntityInterface $entity, $message = '')
 {
     $status = TRUE;
     $entity_type = $entity->getEntityTypeId();
     $id = $entity->id();
     $langcode = $entity->getUntranslated()->language()->id;
     $fields = array($this->field_name, $this->untranslatable_field_name);
     foreach ($fields as $field_name) {
         $field_storage = FieldStorageConfig::loadByName($entity_type, $field_name);
         $tables = array(ContentEntityDatabaseStorage::_fieldTableName($field_storage), ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage));
         foreach ($tables as $table) {
             $record = \Drupal::database()->select($table, 'f')->fields('f')->condition('f.entity_id', $id)->condition('f.revision_id', $id)->execute()->fetchObject();
             if ($record->langcode != $langcode) {
                 $status = FALSE;
                 break;
             }
         }
     }
     return $this->assertTrue($status, $message);
 }
Beispiel #3
0
 /**
  * Unit testing the views data structure.
  *
  * We check data structure for both node and node revision tables.
  */
 function testViewsData()
 {
     $views_data = $this->container->get('views.views_data');
     $data = array();
     // Check the table and the joins of the first field.
     // Attached to node only.
     $field_storage = $this->fieldStorages[0];
     $current_table = ContentEntityDatabaseStorage::_fieldTableName($field_storage);
     $revision_table = ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage);
     $data[$current_table] = $views_data->get($current_table);
     $data[$revision_table] = $views_data->get($revision_table);
     $this->assertTrue(isset($data[$current_table]));
     $this->assertTrue(isset($data[$revision_table]));
     // The node field should join against node.
     $this->assertTrue(isset($data[$current_table]['table']['join']['node']));
     $this->assertTrue(isset($data[$revision_table]['table']['join']['node_revision']));
     $expected_join = array('left_field' => 'nid', 'field' => 'entity_id', 'extra' => array(array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE)));
     $this->assertEqual($expected_join, $data[$current_table]['table']['join']['node']);
     $expected_join = array('left_field' => 'vid', 'field' => 'revision_id', 'extra' => array(array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE)));
     $this->assertEqual($expected_join, $data[$revision_table]['table']['join']['node_revision']);
 }
Beispiel #4
0
 /**
  * Join field table if necessary.
  *
  * @param $field_name
  *   Name of the field.
  * @return string
  * @throws \Drupal\Core\Entity\Query\QueryException
  */
 protected function ensureFieldTable($index_prefix, &$field, $type, $langcode, $base_table, $entity_id_field, $field_id_field)
 {
     $field_name = $field->getName();
     if (!isset($this->fieldTables[$index_prefix . $field_name])) {
         $table = $this->sqlQuery->getMetaData('age') == EntityStorageInterface::FIELD_LOAD_CURRENT ? ContentEntityDatabaseStorage::_fieldTableName($field) : ContentEntityDatabaseStorage::_fieldRevisionTableName($field);
         if ($field->getCardinality() != 1) {
             $this->sqlQuery->addMetaData('simple_query', FALSE);
         }
         $entity_type = $this->sqlQuery->getMetaData('entity_type');
         $this->fieldTables[$index_prefix . $field_name] = $this->addJoin($type, $table, "%alias.{$field_id_field} = {$base_table}.{$entity_id_field}", $langcode);
     }
     return $this->fieldTables[$index_prefix . $field_name];
 }
 /**
  * Tests table name generation.
  */
 public function testTableNames()
 {
     // Note: we need to test entity types with long names. We therefore use
     // fields on imaginary entity types (works as long as we don't actually save
     // them), and just check the generated table names.
     // Short entity type and field name.
     $entity_type = 'short_entity_type';
     $field_name = 'short_field_name';
     $field_storage = entity_create('field_storage_config', array('entity_type' => $entity_type, 'name' => $field_name, 'type' => 'test_field'));
     $expected = 'short_entity_type__short_field_name';
     $this->assertEqual(ContentEntityDatabaseStorage::_fieldTableName($field_storage), $expected);
     $expected = 'short_entity_type_revision__short_field_name';
     $this->assertEqual(ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage), $expected);
     // Short entity type, long field name
     $entity_type = 'short_entity_type';
     $field_name = 'long_field_name_abcdefghijklmnopqrstuvwxyz';
     $field_storage = entity_create('field_storage_config', array('entity_type' => $entity_type, 'name' => $field_name, 'type' => 'test_field'));
     $expected = 'short_entity_type__' . substr(hash('sha256', $field_storage->uuid()), 0, 10);
     $this->assertEqual(ContentEntityDatabaseStorage::_fieldTableName($field_storage), $expected);
     $expected = 'short_entity_type_r__' . substr(hash('sha256', $field_storage->uuid()), 0, 10);
     $this->assertEqual(ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage), $expected);
     // Long entity type, short field name
     $entity_type = 'long_entity_type_abcdefghijklmnopqrstuvwxyz';
     $field_name = 'short_field_name';
     $field_storage = entity_create('field_storage_config', array('entity_type' => $entity_type, 'name' => $field_name, 'type' => 'test_field'));
     $expected = 'long_entity_type_abcdefghijklmnopq__' . substr(hash('sha256', $field_storage->uuid()), 0, 10);
     $this->assertEqual(ContentEntityDatabaseStorage::_fieldTableName($field_storage), $expected);
     $expected = 'long_entity_type_abcdefghijklmnopq_r__' . substr(hash('sha256', $field_storage->uuid()), 0, 10);
     $this->assertEqual(ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage), $expected);
     // Long entity type and field name.
     $entity_type = 'long_entity_type_abcdefghijklmnopqrstuvwxyz';
     $field_name = 'long_field_name_abcdefghijklmnopqrstuvwxyz';
     $field_storage = entity_create('field_storage_config', array('entity_type' => $entity_type, 'name' => $field_name, 'type' => 'test_field'));
     $expected = 'long_entity_type_abcdefghijklmnopq__' . substr(hash('sha256', $field_storage->uuid()), 0, 10);
     $this->assertEqual(ContentEntityDatabaseStorage::_fieldTableName($field_storage), $expected);
     $expected = 'long_entity_type_abcdefghijklmnopq_r__' . substr(hash('sha256', $field_storage->uuid()), 0, 10);
     $this->assertEqual(ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage), $expected);
     // Try creating a second field and check there are no clashes.
     $field_storage2 = entity_create('field_storage_config', array('entity_type' => $entity_type, 'name' => $field_name . '2', 'type' => 'test_field'));
     $this->assertNotEqual(ContentEntityDatabaseStorage::_fieldTableName($field_storage), ContentEntityDatabaseStorage::_fieldTableName($field_storage2));
     $this->assertNotEqual(ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage), ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage2));
     // Deleted field.
     $field_storage = entity_create('field_storage_config', array('entity_type' => 'some_entity_type', 'name' => 'some_field_name', 'type' => 'test_field', 'deleted' => TRUE));
     $expected = 'field_deleted_data_' . substr(hash('sha256', $field_storage->uuid()), 0, 10);
     $this->assertEqual(ContentEntityDatabaseStorage::_fieldTableName($field_storage, TRUE), $expected);
     $expected = 'field_deleted_revision_' . substr(hash('sha256', $field_storage->uuid()), 0, 10);
     $this->assertEqual(ContentEntityDatabaseStorage::_fieldRevisionTableName($field_storage, TRUE), $expected);
 }
 /**
  * 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)));
 }