/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $this->submitCustomerForm($form, $form_state); $values = $form_state->getValues(); $order = $this->storage->create(['type' => $values['type'], 'mail' => $values['mail'], 'uid' => [$values['uid']], 'store_id' => [$values['store_id']]]); $order->save(); // Redirect to the edit form to complete the order. $form_state->setRedirect('entity.commerce_order.edit_form', ['commerce_order' => $order->id()]); }
/** * @inheritDoc */ public function getValue(ResultRow $values, $field = NULL) { $value = NULL; $cat_id = parent::getValue($values, $field); if ($cat_id) { $value = $this->entityStorage->load($cat_id)->getName(); } return $value; }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $this->submitCustomerForm($form, $form_state); $values = $form_state->getValues(); $order_data = ['type' => $values['type'], 'mail' => $values['mail'], 'uid' => [$values['uid']], 'store_id' => [$values['store_id']]]; if (!empty($values['custom_placed_date']) && !empty($values['placed'])) { $order_data['placed'] = $values['placed']->getTimestamp(); } $order = $this->storage->create($order_data); $order->save(); // Redirect to the edit form to complete the order. $form_state->setRedirect('entity.commerce_order.edit_form', ['commerce_order' => $order->id()]); }
/** * ::onEntityTypeUpdate */ public function testonEntityTypeUpdateWithNewIndex() { $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(['entity_test' => ['indexes' => ['entity_test__b588603cb9' => ['longer_index_name'], 'entity_test__removed_field' => ['removed_field']]]]); // The original indexes should be dropped before the new one is added. $this->dbSchemaHandler->expects($this->at(0))->method('dropIndex')->with('entity_test', 'entity_test__b588603cb9'); $this->dbSchemaHandler->expects($this->at(1))->method('dropIndex')->with('entity_test', 'entity_test__removed_field'); $this->dbSchemaHandler->expects($this->atLeastOnce())->method('fieldExists')->willReturn(TRUE); $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)); }
/** * {@inheritdoc} */ public function onBundleRename($bundle, $bundle_new, $entity_type_id) { parent::onBundleRename($bundle, $bundle_new, $entity_type_id); // Update media entities with a new bundle. $this->database->update('media')->fields(array('bundle' => $bundle_new))->condition('bundle', $bundle)->execute(); $this->database->update('media_field_data')->fields(array('bundle' => $bundle_new))->condition('bundle', $bundle)->execute(); }
/** * {@inheritdoc} */ protected function invokeHook($hook, EntityInterface $entity) { parent::invokeHook($hook, $entity); $event_class = $this->entityType->getHandlerClass('event'); if ($event_class) { $this->eventDispatcher->dispatch($this->getEventName($hook), new $event_class($entity)); } }
/** * {@inheritdoc} */ public function getSchema() { $schema = parent::getSchema(); // Marking the respective fields as NOT NULL makes the indexes more // performant. $schema['uc_cart_products']['fields']['cart_id']['not null'] = TRUE; $schema['uc_cart_products']['indexes'] += array('cart_id' => array('cart_id')); $schema['uc_cart_products']['foreign keys'] += array('node' => array('table' => 'node', 'columns' => array('nid' => 'nid'))); return $schema; }
/** * {@inheritdoc} */ protected function doSaveFieldItems(ContentEntityInterface $entity, array $names = []) { // The anonymous user account is saved with the fixed user ID of 0. // Therefore we need to check for NULL explicitly. if ($entity->id() === NULL) { $entity->uid->value = $this->database->nextId($this->database->query('SELECT MAX(uid) FROM {users}')->fetchField()); $entity->enforceIsNew(); } return parent::doSaveFieldItems($entity, $names); }
/** * {@inheritdoc} */ public function save(EntityInterface $entity) { // The anonymous user account is saved with the fixed user ID of 0. // Therefore we need to check for NULL explicitly. if ($entity->id() === NULL) { $entity->uid->value = $this->database->nextId($this->database->query('SELECT MAX(uid) FROM {users}')->fetchField()); $entity->enforceIsNew(); } return parent::save($entity); }
/** * Test external destinations. */ public function testExternal() { $redirect = $this->storage->create(); $redirect->setSource('a-path'); // @todo Redirect::setRedirect() assumes that all redirects are internal. $redirect->redirect_redirect->set(0, ['uri' => 'https://www.example.org']); $redirect->setStatusCode(301); $redirect->save(); $this->assertRedirect('a-path', 'https://www.example.org'); }
/** * Tests groupby with filters. */ public function testGroupByCountOnlyFilters() { // Check if GROUP BY and HAVING are included when a view // doesn't display SUM, COUNT, MAX, etc. functions in SELECT statement. for ($x = 0; $x < 10; $x++) { $this->storage->create(array('name' => 'name1'))->save(); } $view = Views::getView('test_group_by_in_filters'); $this->executeView($view); $this->assertTrue(strpos($view->build_info['query'], 'GROUP BY'), 'Make sure that GROUP BY is in the query'); $this->assertTrue(strpos($view->build_info['query'], 'HAVING'), 'Make sure that HAVING is in the query'); }
/** * Test external destinations. */ public function testExternal() { $redirect = $this->storage->create(); $redirect->setSource('a-path'); // @todo Redirect::setRedirect() assumes that all redirects are internal. $redirect->redirect_redirect->set(0, ['uri' => 'https://www.example.org']); $redirect->setStatusCode(301); $redirect->save(); $this->assertRedirect('a-path', 'https://www.example.org'); $this->drupalLogin($this->adminUser); $this->drupalPostForm('admin/config/search/redirect/settings', ['redirect_deslash' => 1], t('Save configuration')); $this->drupalGet('/2015/10/10/'); $this->assertResponse(404); }
/** * {@inheritdoc} */ public function getSchema() { $schema = parent::getSchema(); // @todo Create a numeric field type and use that instead. $schema['uc_orders']['fields']['order_total']['type'] = 'numeric'; $schema['uc_orders']['fields']['order_total']['precision'] = 16; $schema['uc_orders']['fields']['order_total']['scale'] = 5; // Marking the respective fields as NOT NULL makes the indexes more // performant. $schema['uc_orders']['fields']['uid']['not null'] = TRUE; $schema['uc_orders']['fields']['order_status']['not null'] = TRUE; $schema['uc_orders']['indexes'] += array('uid' => array('uid'), 'order_status' => array('order_status')); $schema['uc_orders']['foreign keys'] += array('users' => array('table' => 'users', 'columns' => array('uid' => 'uid'))); return $schema; }
/** * @covers ::doLoadMultiple * @covers ::buildCacheId * @covers ::getFromPersistentCache */ public function testLoadMultiplePersistentCached() { $this->setUpModuleHandlerNoImplementations(); $key = 'values:' . $this->entityTypeId . ':1'; $id = 1; $entity = $this->getMockBuilder('\\Drupal\\Tests\\Core\\Entity\\Sql\\SqlContentEntityStorageTestEntityInterface')->getMockForAbstractClass(); $entity->expects($this->any())->method('id')->will($this->returnValue($id)); $this->entityType->expects($this->atLeastOnce())->method('isPersistentlyCacheable')->will($this->returnValue(TRUE)); $this->entityType->expects($this->atLeastOnce())->method('id')->will($this->returnValue($this->entityTypeId)); $this->entityType->expects($this->atLeastOnce())->method('getClass')->will($this->returnValue(get_class($entity))); $this->cache->expects($this->once())->method('getMultiple')->with(array($key))->will($this->returnValue(array($key => (object) array('data' => $entity)))); $this->cache->expects($this->never())->method('set'); $this->setUpEntityStorage(); $entities = $this->entityStorage->loadMultiple(array($id)); $this->assertEquals($entity, $entities[$id]); }
/** * @covers ::hasData */ public function testHasData() { $query = $this->getMock('Drupal\\Core\\Entity\\Query\\QueryInterface'); $query->expects($this->once())->method('accessCheck')->with(FALSE)->willReturn($query); $query->expects($this->once())->method('range')->with(0, 1)->willReturn($query); $query->expects($this->once())->method('execute')->willReturn(array(5)); $factory = $this->getMockBuilder('Drupal\\Core\\Entity\\Query\\QueryFactory')->disableOriginalConstructor()->getMock(); $factory->expects($this->once())->method('get')->with($this->entityType, 'AND')->willReturn($query); $this->container->set('entity.query.sql', $factory); $database = $this->getMockBuilder('Drupal\\Core\\Database\\Connection')->disableOriginalConstructor()->getMock(); $this->entityManager->expects($this->any())->method('getDefinition')->will($this->returnValue($this->entityType)); $this->entityManager->expects($this->any())->method('getFieldStorageDefinitions')->will($this->returnValue($this->fieldDefinitions)); $this->entityManager->expects($this->any())->method('getBaseFieldDefinitions')->will($this->returnValue($this->fieldDefinitions)); $this->entityStorage = new SqlContentEntityStorage($this->entityType, $database, $this->entityManager, $this->cache, $this->languageManager); $result = $this->entityStorage->hasData(); $this->assertTrue($result, 'hasData returned TRUE'); }
/** * @covers ::requiresEntityStorageSchemaChanges * * @dataProvider providerTestRequiresEntityStorageSchemaChanges */ public function testRequiresEntityStorageSchemaChanges(ContentEntityTypeInterface $updated, ContentEntityTypeInterface $original, $requires_change, $change_schema, $change_shared_table) { $this->entityType = new ContentEntityType(array('id' => 'entity_test', 'entity_keys' => array('id' => 'id'))); $this->setUpStorageSchema(); $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)); // Setup storage schema. if ($change_schema) { $this->storageSchema->expects($this->once())->method('loadEntitySchemaData')->willReturn(array()); } else { $expected = ['entity_test' => ['primary key' => ['id']]]; $this->storageSchema->expects($this->any())->method('loadEntitySchemaData')->willReturn($expected); } if ($change_shared_table) { $this->storageSchema->expects($this->once())->method('hasSharedTableNameChanges')->willReturn(TRUE); } $this->assertEquals($requires_change, $this->storageSchema->requiresEntityStorageSchemaChanges($updated, $original)); }
/** * Tests the schema for a field dedicated table for an entity with a string identifier. * * @covers ::getDedicatedTableSchema() * @covers ::createDedicatedTableSchema() */ public function testDedicatedTableSchemaForEntityWithStringIdentifier() { $entity_type_id = 'entity_test'; $this->entityType = new ContentEntityType(array('id' => 'entity_test', 'entity_keys' => array('id' => 'id'))); // Setup a field having a dedicated schema. $field_name = $this->getRandomGenerator()->name(); $this->setUpStorageDefinition($field_name, array('columns' => array('shape' => array('type' => 'varchar', 'length' => 32, 'not null' => FALSE), 'color' => array('type' => 'varchar', 'length' => 32, 'not null' => FALSE)), 'foreign keys' => array('color' => array('table' => 'color', 'columns' => array('color' => 'id'))), 'unique keys' => array(), 'indexes' => array())); $field_storage = $this->storageDefinitions[$field_name]; $field_storage->expects($this->any())->method('getType')->will($this->returnValue('shape')); $field_storage->expects($this->any())->method('getTargetEntityTypeId')->will($this->returnValue($entity_type_id)); $field_storage->expects($this->any())->method('isMultiple')->will($this->returnValue(TRUE)); $this->storageDefinitions['id']->expects($this->any())->method('getType')->will($this->returnValue('string')); $expected = array($entity_type_id . '__' . $field_name => array('description' => "Data storage for {$entity_type_id} field {$field_name}.", 'fields' => array('bundle' => array('type' => 'varchar', 'length' => 128, 'not null' => true, 'default' => '', 'description' => 'The field instance bundle to which this row belongs, used when deleting a field instance'), 'deleted' => array('type' => 'int', 'size' => 'tiny', 'not null' => true, 'default' => 0, 'description' => 'A boolean indicating whether this data item has been deleted'), 'entity_id' => array('type' => 'varchar', 'length' => 128, 'not null' => true, 'description' => 'The entity id this data is attached to'), 'revision_id' => array('type' => 'varchar', 'length' => 128, 'not null' => true, 'description' => 'The entity revision id this data is attached to, which for an unversioned entity type is the same as the entity id'), 'langcode' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'default' => '', 'description' => 'The language code for this data item.'), 'delta' => array('type' => 'int', 'unsigned' => true, 'not null' => true, 'description' => 'The sequence number for this data item, used for multi-value fields'), $field_name . '_shape' => array('type' => 'varchar', 'length' => 32, 'not null' => false), $field_name . '_color' => array('type' => 'varchar', 'length' => 32, 'not null' => false)), 'primary key' => array('entity_id', 'deleted', 'delta', 'langcode'), 'indexes' => array('bundle' => array('bundle'), 'deleted' => array('deleted'), 'entity_id' => array('entity_id'), 'revision_id' => array('revision_id'), 'langcode' => array('langcode')), 'foreign keys' => array($field_name . '_color' => array('table' => 'color', 'columns' => array($field_name . '_color' => 'id'))))); $this->setUpStorageSchema($expected); $table_mapping = new DefaultTableMapping($this->storageDefinitions); $table_mapping->setFieldNames($entity_type_id, array_keys($this->storageDefinitions)); $table_mapping->setExtraColumns($entity_type_id, array('default_langcode')); $this->storage->expects($this->any())->method('getTableMapping')->will($this->returnValue($table_mapping)); $this->storageSchema->onFieldStorageDefinitionCreate($field_storage); }
/** * Constructs a CommentStorage object. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_info * An array of entity info for the entity type. * @param \Drupal\Core\Database\Connection $database * The database connection to be used. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. * @param \Drupal\Core\Session\AccountInterface $current_user * The current user. * @param \Drupal\Core\Cache\CacheBackendInterface $cache * Cache backend instance to use. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. */ public function __construct(EntityTypeInterface $entity_info, Connection $database, EntityManagerInterface $entity_manager, AccountInterface $current_user, CacheBackendInterface $cache, LanguageManagerInterface $language_manager) { parent::__construct($entity_info, $database, $entity_manager, $cache, $language_manager); $this->currentUser = $current_user; }
/** * {@inheritdoc} */ public function __wakeup() { parent::__wakeup(); // Initialize static caches. $this->parents = array(); $this->parentsAll = array(); $this->children = array(); $this->treeChildren = array(); $this->treeParents = array(); $this->treeTerms = array(); $this->trees = array(); }
/** * {@inheritdoc} */ protected function mapToStorageRecord(ContentEntityInterface $entity, $table_name = NULL) { /** @var \Drupal\payment\Entity\PaymentInterface $payment */ $payment = $entity; $record = parent::mapToStorageRecord($entity, $table_name); $deltas = []; foreach ($payment->getPaymentStatuses() as $delta => $item) { $deltas[] = $delta; } $record->current_payment_status_delta = max($deltas); return $record; }
/** * Gets the SQL schema for a dedicated table. * * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition * The field storage definition. * @param \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type * (optional) The entity type definition. Defaults to the one returned by * the entity manager. * * @return array * The schema definition for the table with the following keys: * - fields: The schema definition for the each field columns. * - indexes: The schema definition for the indexes. * - unique keys: The schema definition for the unique keys. * - foreign keys: The schema definition for the foreign keys. * * @throws \Drupal\Core\Field\FieldException * Exception thrown if the schema contains reserved column names. * * @see hook_schema() */ protected function getDedicatedTableSchema(FieldStorageDefinitionInterface $storage_definition, ContentEntityTypeInterface $entity_type = NULL) { $description_current = "Data storage for {$storage_definition->getTargetEntityTypeId()} field {$storage_definition->getName()}."; $description_revision = "Revision archive storage for {$storage_definition->getTargetEntityTypeId()} field {$storage_definition->getName()}."; $id_definition = $this->fieldStorageDefinitions[$this->entityType->getKey('id')]; if ($id_definition->getType() == 'integer') { $id_schema = array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'The entity id this data is attached to'); } else { $id_schema = array('type' => 'varchar_ascii', 'length' => 128, 'not null' => TRUE, 'description' => 'The entity id this data is attached to'); } // Define the revision ID schema. if (!$this->entityType->isRevisionable()) { $revision_id_schema = $id_schema; $revision_id_schema['description'] = 'The entity revision id this data is attached to, which for an unversioned entity type is the same as the entity id'; } elseif ($this->fieldStorageDefinitions[$this->entityType->getKey('revision')]->getType() == 'integer') { $revision_id_schema = array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'The entity revision id this data is attached to'); } else { $revision_id_schema = array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'description' => 'The entity revision id this data is attached to'); } $data_schema = array('description' => $description_current, 'fields' => array('bundle' => array('type' => 'varchar_ascii', 'length' => 128, 'not null' => TRUE, 'default' => '', 'description' => 'The field instance bundle to which this row belongs, used when deleting a field instance'), 'deleted' => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0, 'description' => 'A boolean indicating whether this data item has been deleted'), 'entity_id' => $id_schema, 'revision_id' => $revision_id_schema, 'langcode' => array('type' => 'varchar_ascii', 'length' => 32, 'not null' => TRUE, 'default' => '', 'description' => 'The language code for this data item.'), 'delta' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'The sequence number for this data item, used for multi-value fields')), 'primary key' => array('entity_id', 'deleted', 'delta', 'langcode'), 'indexes' => array('bundle' => array('bundle'), 'revision_id' => array('revision_id'))); // Check that the schema does not include forbidden column names. $schema = $storage_definition->getSchema(); $properties = $storage_definition->getPropertyDefinitions(); $table_mapping = $this->storage->getTableMapping(); if (array_intersect(array_keys($schema['columns']), $table_mapping->getReservedColumns())) { throw new FieldException("Illegal field column names on {$storage_definition->getName()}"); } // Add field columns. foreach ($schema['columns'] as $column_name => $attributes) { $real_name = $table_mapping->getFieldColumnName($storage_definition, $column_name); $data_schema['fields'][$real_name] = $attributes; // A dedicated table only contain rows for actual field values, and no // rows for entities where the field is empty. Thus, we can safely // enforce 'not null' on the columns for the field's required properties. $data_schema['fields'][$real_name]['not null'] = $properties[$column_name]->isRequired(); } // Add indexes. foreach ($schema['indexes'] as $index_name => $columns) { $real_name = $this->getFieldIndexName($storage_definition, $index_name); foreach ($columns as $column_name) { // Indexes can be specified as either a column name or an array with // column name and length. Allow for either case. if (is_array($column_name)) { $data_schema['indexes'][$real_name][] = array($table_mapping->getFieldColumnName($storage_definition, $column_name[0]), $column_name[1]); } else { $data_schema['indexes'][$real_name][] = $table_mapping->getFieldColumnName($storage_definition, $column_name); } } } // Add unique keys. foreach ($schema['unique keys'] as $index_name => $columns) { $real_name = $this->getFieldIndexName($storage_definition, $index_name); foreach ($columns as $column_name) { // Unique keys can be specified as either a column name or an array with // column name and length. Allow for either case. if (is_array($column_name)) { $data_schema['unique keys'][$real_name][] = array($table_mapping->getFieldColumnName($storage_definition, $column_name[0]), $column_name[1]); } else { $data_schema['unique keys'][$real_name][] = $table_mapping->getFieldColumnName($storage_definition, $column_name); } } } // Add foreign keys. foreach ($schema['foreign keys'] as $specifier => $specification) { $real_name = $this->getFieldIndexName($storage_definition, $specifier); $data_schema['foreign keys'][$real_name]['table'] = $specification['table']; foreach ($specification['columns'] as $column_name => $referenced) { $sql_storage_column = $table_mapping->getFieldColumnName($storage_definition, $column_name); $data_schema['foreign keys'][$real_name]['columns'][$sql_storage_column] = $referenced; } } $dedicated_table_schema = array($table_mapping->getDedicatedDataTableName($storage_definition) => $data_schema); // If the entity type is revisionable, construct the revision table. $entity_type = $entity_type ?: $this->entityType; if ($entity_type->isRevisionable()) { $revision_schema = $data_schema; $revision_schema['description'] = $description_revision; $revision_schema['primary key'] = array('entity_id', 'revision_id', 'deleted', 'delta', 'langcode'); $revision_schema['fields']['revision_id']['not null'] = TRUE; $revision_schema['fields']['revision_id']['description'] = 'The entity revision id this data is attached to'; $dedicated_table_schema += array($table_mapping->getDedicatedRevisionTableName($storage_definition) => $revision_schema); } return $dedicated_table_schema; }
/** * {@inheritdoc} */ public function __construct(EntityTypeInterface $entity_type, Connector $moodle_connector, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, RouteMatchInterface $route_match) { $database = $moodle_connector->connect(); $this->routeMatch = $route_match; parent::__construct($entity_type, $database, $entity_manager, $cache, $language_manager); }
/** * Tests fields on the revision table. */ public function testRevisionTableFields() { $entity_type = $this->baseEntityType->set('base_table', 'entity_test_mulrev')->set('revision_table', 'entity_test_mulrev_revision')->set('data_table', 'entity_test_mulrev_property_data')->set('revision_data_table', 'entity_test_mulrev_property_revision')->set('id', 'entity_test_mulrev'); $base_field_definitions = $this->setupBaseFields(EntityTestMulRev::baseFieldDefinitions($this->baseEntityType)); $this->entityManager->expects($this->once())->method('getBaseFieldDefinitions')->with('entity_test_mulrev')->willReturn($base_field_definitions); $this->viewsData->setEntityType($entity_type); // Setup the table mapping. $table_mapping = $this->getMock('Drupal\\Core\\Entity\\Sql\\TableMappingInterface'); $table_mapping->expects($this->any())->method('getTableNames')->willReturn(['entity_test_mulrev', 'entity_test_mulrev_revision', 'entity_test_mulrev_property_data', 'entity_test_mulrev_property_revision']); $table_mapping->expects($this->any())->method('getColumnNames')->willReturnMap([['id', ['value' => 'id']], ['uuid', ['value' => 'uuid']], ['type', ['value' => 'type']], ['langcode', ['value' => 'langcode']], ['name', ['value' => 'name']], ['description', ['value' => 'description__value', 'format' => 'description__format']], ['homepage', ['value' => 'homepage']], ['user_id', ['target_id' => 'user_id']], ['revision_id', ['value' => 'id']]]); $table_mapping->expects($this->any())->method('getFieldNames')->willReturnMap([['entity_test_mulrev', ['id', 'revision_id', 'uuid', 'type']], ['entity_test_mulrev_revision', ['id', 'revision_id', 'langcode']], ['entity_test_mulrev_property_data', ['id', 'revision_id', 'langcode', 'name', 'description', 'homepage', 'user_id']], ['entity_test_mulrev_property_revision', ['id', 'revision_id', 'langcode', 'name', 'description', 'homepage', 'user_id']]]); $this->entityStorage->expects($this->once())->method('getTableMapping')->willReturn($table_mapping); $this->setupFieldStorageDefinition(); $data = $this->viewsData->getViewsData(); // Check the base fields. $this->assertNumericField($data['entity_test_mulrev']['id']); $this->assertNumericField($data['entity_test_mulrev']['revision_id']); $this->assertUuidField($data['entity_test_mulrev']['uuid']); $this->assertStringField($data['entity_test_mulrev']['type']); // Also ensure that field_data only fields don't appear on the base table. $this->assertFalse(isset($data['entity_test_mulrev']['name'])); $this->assertFalse(isset($data['entity_test_mul']['description'])); $this->assertFalse(isset($data['entity_test_mul']['description__value'])); $this->assertFalse(isset($data['entity_test_mul']['description__format'])); $this->assertFalse(isset($data['entity_test_mul']['homepage'])); $this->assertFalse(isset($data['entity_test_mulrev']['langcode'])); $this->assertFalse(isset($data['entity_test_mulrev']['user_id'])); // Check the revision fields. $this->assertNumericField($data['entity_test_mulrev_revision']['id']); $this->assertNumericField($data['entity_test_mulrev_revision']['revision_id']); $this->assertLanguageField($data['entity_test_mulrev_revision']['langcode']); $this->assertEquals('Original language', $data['entity_test_mulrev_revision']['langcode']['title']); // Also ensure that field_data only fields don't appear on the revision table. $this->assertFalse(isset($data['entity_test_mulrev_revision']['name'])); $this->assertFalse(isset($data['entity_test_mulrev_revision']['description'])); $this->assertFalse(isset($data['entity_test_mulrev_revision']['description__value'])); $this->assertFalse(isset($data['entity_test_mulrev_revision']['description__format'])); $this->assertFalse(isset($data['entity_test_mulrev_revision']['homepage'])); $this->assertFalse(isset($data['entity_test_mulrev_revision']['user_id'])); // Check the data fields. $this->assertNumericField($data['entity_test_mulrev_property_data']['id']); $this->assertLanguageField($data['entity_test_mulrev_property_data']['langcode']); $this->assertStringField($data['entity_test_mulrev_property_data']['name']); $this->assertLongTextField($data['entity_test_mulrev_property_data'], 'description'); $this->assertUriField($data['entity_test_mulrev_property_data']['homepage']); $this->assertEntityReferenceField($data['entity_test_mulrev_property_data']['user_id']); $relationship = $data['entity_test_mulrev_property_data']['user_id']['relationship']; $this->assertEquals('users', $relationship['base']); $this->assertEquals('uid', $relationship['base field']); // Check the property data fields. $this->assertNumericField($data['entity_test_mulrev_property_revision']['id']); $this->assertLanguageField($data['entity_test_mulrev_property_revision']['langcode']); $this->assertEquals('Translation language', $data['entity_test_mulrev_property_revision']['langcode']['title']); $this->assertStringField($data['entity_test_mulrev_property_revision']['name']); $this->assertLongTextField($data['entity_test_mulrev_property_revision'], 'description'); $this->assertUriField($data['entity_test_mulrev_property_revision']['homepage']); $this->assertEntityReferenceField($data['entity_test_mulrev_property_revision']['user_id']); $relationship = $data['entity_test_mulrev_property_revision']['user_id']['relationship']; $this->assertEquals('users', $relationship['base']); $this->assertEquals('uid', $relationship['base field']); }
/** * Tests fields on the revision table. */ public function testRevisionTableFields() { $entity_type = $this->baseEntityType->set('base_table', 'entity_test_mulrev')->set('revision_table', 'entity_test_mulrev_revision')->set('data_table', 'entity_test_mulrev_property_data')->set('revision_data_table', 'entity_test_mulrev_property_revision')->set('id', 'entity_test_mulrev')->set('translatable', TRUE); $base_field_definitions = $this->setupBaseFields(EntityTestMulRev::baseFieldDefinitions($this->baseEntityType)); $user_base_field_definitions = ['uid' => BaseFieldDefinition::create('integer')->setLabel('ID')->setDescription('The ID of the user entity.')->setReadOnly(TRUE)->setSetting('unsigned', TRUE)]; $this->entityManager->expects($this->any())->method('getBaseFieldDefinitions')->will($this->returnValueMap([['user', $user_base_field_definitions], ['entity_test_mulrev', $base_field_definitions]])); $this->viewsData->setEntityType($entity_type); // Setup the table mapping. $table_mapping = $this->getMock('Drupal\\Core\\Entity\\Sql\\TableMappingInterface'); $table_mapping->expects($this->any())->method('getTableNames')->willReturn(['entity_test_mulrev', 'entity_test_mulrev_revision', 'entity_test_mulrev_property_data', 'entity_test_mulrev_property_revision']); $table_mapping->expects($this->any())->method('getColumnNames')->willReturnMap([['id', ['value' => 'id']], ['uuid', ['value' => 'uuid']], ['type', ['value' => 'type']], ['langcode', ['value' => 'langcode']], ['name', ['value' => 'name']], ['description', ['value' => 'description__value', 'format' => 'description__format']], ['homepage', ['value' => 'homepage']], ['user_id', ['target_id' => 'user_id']], ['revision_id', ['value' => 'id']]]); $table_mapping->expects($this->any())->method('getFieldNames')->willReturnMap([['entity_test_mulrev', ['id', 'revision_id', 'uuid', 'type']], ['entity_test_mulrev_revision', ['id', 'revision_id', 'langcode']], ['entity_test_mulrev_property_data', ['id', 'revision_id', 'langcode', 'name', 'description', 'homepage', 'user_id']], ['entity_test_mulrev_property_revision', ['id', 'revision_id', 'langcode', 'name', 'description', 'homepage', 'user_id']]]); $table_mapping->expects($this->any())->method('getFieldTableName')->willReturnCallback(function ($field) { if ($field == 'uuid') { return 'entity_test_mulrev'; } return 'entity_test_mulrev_property_data'; }); $this->entityStorage->expects($this->once())->method('getTableMapping')->willReturn($table_mapping); $this->setupFieldStorageDefinition(); $data = $this->viewsData->getViewsData(); // Check the base fields. $this->assertFalse(isset($data['entity_test_mulrev']['id'])); $this->assertFalse(isset($data['entity_test_mulrev']['type'])); $this->assertFalse(isset($data['entity_test_mulrev']['revision_id'])); $this->assertUuidField($data['entity_test_mulrev']['uuid']); $this->assertField($data['entity_test_mulrev']['uuid'], 'uuid'); // Also ensure that field_data only fields don't appear on the base table. $this->assertFalse(isset($data['entity_test_mulrev']['name'])); $this->assertFalse(isset($data['entity_test_mul']['description'])); $this->assertFalse(isset($data['entity_test_mul']['description__value'])); $this->assertFalse(isset($data['entity_test_mul']['description__format'])); $this->assertFalse(isset($data['entity_test_mul']['homepage'])); $this->assertFalse(isset($data['entity_test_mulrev']['langcode'])); $this->assertFalse(isset($data['entity_test_mulrev']['user_id'])); // Check the revision fields. The revision ID should only appear in the data // table. $this->assertFalse(isset($data['entity_test_mulrev_revision']['revision_id'])); // Also ensure that field_data only fields don't appear on the revision table. $this->assertFalse(isset($data['entity_test_mulrev_revision']['id'])); $this->assertFalse(isset($data['entity_test_mulrev_revision']['name'])); $this->assertFalse(isset($data['entity_test_mulrev_revision']['description'])); $this->assertFalse(isset($data['entity_test_mulrev_revision']['description__value'])); $this->assertFalse(isset($data['entity_test_mulrev_revision']['description__format'])); $this->assertFalse(isset($data['entity_test_mulrev_revision']['homepage'])); $this->assertFalse(isset($data['entity_test_mulrev_revision']['user_id'])); // Check the data fields. $this->assertNumericField($data['entity_test_mulrev_property_data']['id']); $this->assertField($data['entity_test_mulrev_property_data']['id'], 'id'); $this->assertNumericField($data['entity_test_mulrev_property_data']['revision_id']); $this->assertField($data['entity_test_mulrev_property_data']['revision_id'], 'revision_id'); $this->assertLanguageField($data['entity_test_mulrev_property_data']['langcode']); $this->assertField($data['entity_test_mulrev_property_data']['langcode'], 'langcode'); $this->assertStringField($data['entity_test_mulrev_property_data']['name']); $this->assertField($data['entity_test_mulrev_property_data']['name'], 'name'); $this->assertLongTextField($data['entity_test_mulrev_property_data'], 'description'); $this->assertField($data['entity_test_mulrev_property_data']['description__value'], 'description'); $this->assertField($data['entity_test_mulrev_property_data']['description__format'], 'description'); $this->assertUriField($data['entity_test_mulrev_property_data']['homepage']); $this->assertField($data['entity_test_mulrev_property_data']['homepage'], 'homepage'); $this->assertEntityReferenceField($data['entity_test_mulrev_property_data']['user_id']); $this->assertField($data['entity_test_mulrev_property_data']['user_id'], 'user_id'); $relationship = $data['entity_test_mulrev_property_data']['user_id']['relationship']; $this->assertEquals('users_field_data', $relationship['base']); $this->assertEquals('uid', $relationship['base field']); // Check the property data fields. $this->assertNumericField($data['entity_test_mulrev_property_revision']['id']); $this->assertField($data['entity_test_mulrev_property_revision']['id'], 'id'); $this->assertLanguageField($data['entity_test_mulrev_property_revision']['langcode']); $this->assertField($data['entity_test_mulrev_property_revision']['langcode'], 'langcode'); $this->assertEquals('Translation language', $data['entity_test_mulrev_property_revision']['langcode']['title']); $this->assertStringField($data['entity_test_mulrev_property_revision']['name']); $this->assertField($data['entity_test_mulrev_property_revision']['name'], 'name'); $this->assertLongTextField($data['entity_test_mulrev_property_revision'], 'description'); $this->assertField($data['entity_test_mulrev_property_revision']['description__value'], 'description'); $this->assertField($data['entity_test_mulrev_property_revision']['description__format'], 'description'); $this->assertUriField($data['entity_test_mulrev_property_revision']['homepage']); $this->assertField($data['entity_test_mulrev_property_revision']['homepage'], 'homepage'); $this->assertEntityReferenceField($data['entity_test_mulrev_property_revision']['user_id']); $this->assertField($data['entity_test_mulrev_property_revision']['user_id'], 'user_id'); $relationship = $data['entity_test_mulrev_property_revision']['user_id']['relationship']; $this->assertEquals('users_field_data', $relationship['base']); $this->assertEquals('uid', $relationship['base field']); }