/** * 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); }
/** * 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']); }
/** * 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); }