/** * @covers \Drupal\Core\Entity\Routing\DefaultHtmlRouteProvider::getRoutes */ public function testHtmlRoutes() { /** @var \Drupal\Core\Routing\RouteProviderInterface $route_provider */ $route_provider = \Drupal::service('router.route_provider'); $route = $route_provider->getRouteByName('entity.entity_test_mul.canonical'); $this->assertEquals('entity_test_mul.full', $route->getDefault('_entity_view')); $this->assertEquals('\\Drupal\\Core\\Entity\\Controller\\EntityController::title', $route->getDefault('_title_callback')); $this->assertEquals('entity_test_mul.view', $route->getRequirement('_entity_access')); $this->assertFalse($route->hasOption('_admin_route')); $route = $route_provider->getRouteByName('entity.entity_test_mul.edit_form'); $this->assertEquals('entity_test_mul.default', $route->getDefault('_entity_form')); $this->assertEquals('\\Drupal\\Core\\Entity\\Controller\\EntityController::editTitle', $route->getDefault('_title_callback')); $this->assertEquals('entity_test_mul.update', $route->getRequirement('_entity_access')); $this->assertFalse($route->hasOption('_admin_route')); $route = $route_provider->getRouteByName('entity.entity_test_mul.delete_form'); $this->assertEquals('entity_test_mul.delete', $route->getDefault('_entity_form')); $this->assertEquals('\\Drupal\\Core\\Entity\\Controller\\EntityController::deleteTitle', $route->getDefault('_title_callback')); $this->assertEquals('entity_test_mul.delete', $route->getRequirement('_entity_access')); $this->assertFalse($route->hasOption('_admin_route')); $entity = EntityTestMul::create(['name' => 'Test title']); $entity->save(); $this->setRawContent($this->httpKernelHandle($entity->url())); $this->assertTitle('Test title | '); $this->setRawContent($this->httpKernelHandle($entity->url('edit-form'))); $this->assertTitle('Edit Test title | '); $this->setRawContent($this->httpKernelHandle($entity->url('delete-form'))); $this->assertTitle('Are you sure you want to delete the test entity - data table Test title? | '); }
/** * Tests that the flag for enforcing a new entity is not shared. */ public function testEnforceIsNewOnClonedEntityTranslation() { // Create a test entity. $entity = EntityTestMul::create(['name' => $this->randomString(), 'language' => 'en']); $entity->save(); $entity_translation = $entity->addTranslation('de'); $entity->save(); // The entity is not new anymore. $this->assertFalse($entity_translation->isNew()); // The clone should not be new as well. $clone = clone $entity_translation; $this->assertFalse($clone->isNew()); // After enforcing the clone to be new only it should be flagged as new, // but the original entity should not be flagged as new. $clone->enforceIsNew(); $this->assertTrue($clone->isNew()); $this->assertFalse($entity_translation->isNew()); }
/** * Tests the add page for an entity type not using bundle entities. */ public function testAddPageWithoutBundleEntities() { entity_test_create_bundle('test', 'Test label', 'entity_test_mul'); // Delete the default bundle, so that we can rely on our own. entity_test_delete_bundle('entity_test_mul', 'entity_test_mul'); // One bundle exists, confirm redirection to the add-form. $this->drupalGet('/entity_test_mul/add'); $this->assertUrl('/entity_test_mul/add/test'); // Two bundles exist, confirm both are shown. entity_test_create_bundle('test2', 'Test2 label', 'entity_test_mul'); $this->drupalGet('/entity_test_mul/add'); $this->assertLink('Test label'); $this->assertLink('Test2 label'); $this->clickLink('Test2 label'); $this->drupalGet('/entity_test_mul/add/test2'); $this->drupalPostForm(NULL, ['name[0][value]' => 'test name'], t('Save')); $entity = EntityTestMul::load(1); $this->assertEqual('test name', $entity->label()); }
/** * Tests if entity references on fields are still correct after cloning. */ public function testFieldEntityReferenceAfterClone() { $user = $this->createUser(); // Create a test entity. $entity = EntityTestMul::create(['name' => $this->randomString(), 'user_id' => $user->id(), 'language' => 'en']); $clone = clone $entity->addTranslation('de'); $this->assertEqual($entity->getTranslationLanguages(), $clone->getTranslationLanguages(), 'The entity and its clone have the same translation languages.'); $default_langcode = $entity->getUntranslated()->language()->getId(); foreach (array_keys($clone->getTranslationLanguages()) as $langcode) { $translation = $clone->getTranslation($langcode); foreach ($translation->getFields() as $field_name => $field) { if ($field->getFieldDefinition()->isTranslatable()) { $args = ['%field_name' => $field_name, '%langcode' => $langcode]; $this->assertEqual($langcode, $field->getEntity()->language()->getId(), format_string('Translatable field %field_name on translation %langcode has correct entity reference in translation %langcode after cloning.', $args)); } else { $args = ['%field_name' => $field_name, '%langcode' => $langcode, '%default_langcode' => $default_langcode]; $this->assertEqual($default_langcode, $field->getEntity()->language()->getId(), format_string('Non translatable field %field_name on translation %langcode has correct entity reference in the default translation %default_langcode after cloning.', $args)); } } } }
/** * Tests grouping a field with cardinality > 1. */ public function testGroupByFieldWithCardinality() { $field_storage = FieldStorageConfig::create(['type' => 'integer', 'field_name' => 'field_test', 'cardinality' => 4, 'entity_type' => 'entity_test_mul']); $field_storage->save(); $field = FieldConfig::create(['field_name' => 'field_test', 'entity_type' => 'entity_test_mul', 'bundle' => 'entity_test_mul']); $field->save(); $entities = []; $entity = EntityTestMul::create(['field_test' => [1, 1, 1]]); $entity->save(); $entities[] = $entity; $entity = EntityTestMul::create(['field_test' => [2, 2, 2]]); $entity->save(); $entities[] = $entity; $entity = EntityTestMul::create(['field_test' => [2, 2, 2]]); $entity->save(); $entities[] = $entity; $view = Views::getView('test_group_by_count_multicardinality'); $this->executeView($view); $this->assertEqual(2, count($view->result)); $this->assertEqual(3, $view->getStyle()->getField(0, 'id')); $this->assertEqual('1', $view->getStyle()->getField(0, 'field_test')); $this->assertEqual(6, $view->getStyle()->getField(1, 'id')); $this->assertEqual('2', $view->getStyle()->getField(1, 'field_test')); $entities[2]->field_test[0]->value = 3; $entities[2]->field_test[1]->value = 4; $entities[2]->field_test[2]->value = 5; $entities[2]->save(); $view = Views::getView('test_group_by_count_multicardinality'); $this->executeView($view); $this->assertEqual(5, count($view->result)); $this->assertEqual(3, $view->getStyle()->getField(0, 'id')); $this->assertEqual('1', $view->getStyle()->getField(0, 'field_test')); $this->assertEqual(3, $view->getStyle()->getField(1, 'id')); $this->assertEqual('2', $view->getStyle()->getField(1, 'field_test')); $this->assertEqual(1, $view->getStyle()->getField(2, 'id')); $this->assertEqual('3', $view->getStyle()->getField(2, 'field_test')); $this->assertEqual(1, $view->getStyle()->getField(3, 'id')); $this->assertEqual('4', $view->getStyle()->getField(3, 'field_test')); $this->assertEqual(1, $view->getStyle()->getField(4, 'id')); $this->assertEqual('5', $view->getStyle()->getField(4, 'field_test')); // Check that translated values are correctly retrieved and are not grouped // into the original entity. $translation = $entity->addTranslation('it'); $translation->field_test = [6, 6, 6]; $translation->save(); $view = Views::getView('test_group_by_count_multicardinality'); $this->executeView($view); $this->assertEqual(6, count($view->result)); $this->assertEqual(3, $view->getStyle()->getField(5, 'id')); $this->assertEqual('6', $view->getStyle()->getField(5, 'field_test')); }
/** * Tests views data generated for relationship. * * @see entity_reference_field_views_data() */ public function testDataTableRelationship() { // Create some test entities which link each other. $referenced_entity = EntityTest::create(); $referenced_entity->save(); $entity = EntityTestMul::create(); $entity->field_data_test->target_id = $referenced_entity->id(); $entity->save(); $this->assertEqual($entity->field_data_test[0]->entity->id(), $referenced_entity->id()); $this->entities[] = $entity; $entity = EntityTestMul::create(); $entity->field_data_test->target_id = $referenced_entity->id(); $entity->save(); $this->assertEqual($entity->field_data_test[0]->entity->id(), $referenced_entity->id()); $this->entities[] = $entity; Views::viewsData()->clear(); // Check the generated views data. $views_data = Views::viewsData()->get('entity_test_mul__field_data_test'); $this->assertEqual($views_data['field_data_test']['relationship']['id'], 'standard'); $this->assertEqual($views_data['field_data_test']['relationship']['base'], 'entity_test'); $this->assertEqual($views_data['field_data_test']['relationship']['base field'], 'id'); $this->assertEqual($views_data['field_data_test']['relationship']['relationship field'], 'field_data_test_target_id'); $this->assertEqual($views_data['field_data_test']['relationship']['entity type'], 'entity_test'); // Check the backwards reference. $views_data = Views::viewsData()->get('entity_test'); $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['id'], 'entity_reverse'); $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['base'], 'entity_test_mul_property_data'); $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['base field'], 'id'); $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['field table'], 'entity_test_mul__field_data_test'); $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['field field'], 'field_data_test_target_id'); $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['field_name'], 'field_data_test'); $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['entity_type'], 'entity_test_mul'); $this->assertEqual($views_data['reverse__entity_test_mul__field_data_test']['relationship']['join_extra'][0], ['field' => 'deleted', 'value' => 0, 'numeric' => TRUE]); // Check an actual test view. $view = Views::getView('test_entity_reference_entity_test_mul_view'); $this->executeView($view); /** @var \Drupal\views\ResultRow $row */ foreach ($view->result as $index => $row) { // Check that the actual ID of the entity is the expected one. $this->assertEqual($row->id, $this->entities[$index]->id()); // Also check that we have the correct result entity. $this->assertEqual($row->_entity->id(), $this->entities[$index]->id()); // Test the forward relationship. $this->assertEqual($row->entity_test_entity_test_mul__field_data_test_id, 1); // Test that the correct relationship entity is on the row. $this->assertEqual($row->_relationship_entities['field_data_test']->id(), 1); $this->assertEqual($row->_relationship_entities['field_data_test']->bundle(), 'entity_test'); } // Check the backwards reference view. $view = Views::getView('test_entity_reference_reverse_entity_test_mul_view'); $this->executeView($view); /** @var \Drupal\views\ResultRow $row */ foreach ($view->result as $index => $row) { $this->assertEqual($row->id, 1); $this->assertEqual($row->_entity->id(), 1); // Test the backwards relationship. $this->assertEqual($row->field_data_test_entity_test_id, $this->entities[$index]->id()); // Test that the correct relationship entity is on the row. $this->assertEqual($row->_relationship_entities['reverse__entity_test_mul__field_data_test']->id(), $this->entities[$index]->id()); $this->assertEqual($row->_relationship_entities['reverse__entity_test_mul__field_data_test']->bundle(), 'entity_test_mul'); } }
/** * Tests translation handling of the content entity datasource. */ public function testItemTranslations() { // Test retrieving language and translations when no translations are // available. $entity_1 = EntityTestMul::create(array('id' => 1, 'name' => 'test 1', 'user_id' => $this->container->get('current_user')->id())); $entity_1->save(); $this->assertEqual($entity_1->language()->getId(), 'en', SafeMarkup::format('%entity_type: Entity language set to site default.', array('%entity_type' => $this->testEntityTypeId))); $this->assertFalse($entity_1->getTranslationLanguages(FALSE), SafeMarkup::format('%entity_type: No translations are available', array('%entity_type' => $this->testEntityTypeId))); $entity_2 = EntityTestMul::create(array('id' => 2, 'name' => 'test 2', 'user_id' => $this->container->get('current_user')->id())); $entity_2->save(); $this->assertEqual($entity_2->language()->getId(), 'en', SafeMarkup::format('%entity_type: Entity language set to site default.', array('%entity_type' => $this->testEntityTypeId))); $this->assertFalse($entity_2->getTranslationLanguages(FALSE), SafeMarkup::format('%entity_type: No translations are available', array('%entity_type' => $this->testEntityTypeId))); // Test that the datasource returns the correct item IDs. $datasource = $this->index->getDatasource('entity:' . $this->testEntityTypeId); $datasource_item_ids = $datasource->getItemIds(); sort($datasource_item_ids); $expected = array('1:en', '2:en'); $this->assertEqual($datasource_item_ids, $expected, 'Datasource returns correct item ids.'); // Test indexing the new entity. $this->assertEqual($this->index->getTracker()->getIndexedItemsCount(), 0, 'The index is empty.'); $this->assertEqual($this->index->getTracker()->getTotalItemsCount(), 2, 'There are two items to be indexed.'); $this->index->index(); $this->assertEqual($this->index->getTracker()->getIndexedItemsCount(), 2, 'Two items have been indexed.'); // Now, make the first entity language-specific by assigning a language. $default_langcode = $this->langcodes[0]; $entity_1->get('langcode')->setValue($default_langcode); $entity_1->save(); $this->assertEqual($entity_1->language(), \Drupal::languageManager()->getLanguage($this->langcodes[0]), SafeMarkup::format('%entity_type: Entity language retrieved.', array('%entity_type' => $this->testEntityTypeId))); $this->assertFalse($entity_1->getTranslationLanguages(FALSE), SafeMarkup::format('%entity_type: No translations are available', array('%entity_type' => $this->testEntityTypeId))); // Test that the datasource returns the correct item IDs. $datasource_item_ids = $datasource->getItemIds(); sort($datasource_item_ids); $expected = array('1:' . $this->langcodes[0], '2:en'); $this->assertEqual($datasource_item_ids, $expected, 'Datasource returns correct item ids.'); // Test that the index needs to be updated. $this->assertEqual($this->index->getTracker()->getIndexedItemsCount(), 1, 'The updated item needs to be reindexed.'); $this->assertEqual($this->index->getTracker()->getTotalItemsCount(), 2, 'There are two items in total.'); // Set two translations for the first entity and test that the datasource // returns three separate item IDs, one for each translation. $translation = $entity_1->getTranslation($this->langcodes[1]); $translation->save(); $translation = $entity_1->getTranslation($this->langcodes[2]); $translation->save(); $this->assertTrue($entity_1->getTranslationLanguages(FALSE), SafeMarkup::format('%entity_type: Translations are available', array('%entity_type' => $this->testEntityTypeId))); $datasource_item_ids = $datasource->getItemIds(); sort($datasource_item_ids); $expected = array('1:' . $this->langcodes[0], '1:' . $this->langcodes[1], '1:' . $this->langcodes[2], '2:en'); $this->assertEqual($datasource_item_ids, $expected, 'Datasource returns correct item ids for a translated entity.'); // Test that the index needs to be updated. $this->assertEqual($this->index->getTracker()->getIndexedItemsCount(), 1, 'The updated items needs to be reindexed.'); $this->assertEqual($this->index->getTracker()->getTotalItemsCount(), 4, 'There are four items in total.'); // Delete one translation and test that the datasource returns only three // items. $entity_1->removeTranslation($this->langcodes[2]); $entity_1->save(); $datasource_item_ids = $datasource->getItemIds(); sort($datasource_item_ids); $expected = array('1:' . $this->langcodes[0], '1:' . $this->langcodes[1], '2:en'); $this->assertEqual($datasource_item_ids, $expected, 'Datasource returns correct item ids for a translated entity.'); // Test reindexing. $this->assertEqual($this->index->getTracker()->getTotalItemsCount(), 3, 'There are three items in total.'); $this->assertEqual($this->index->getTracker()->getIndexedItemsCount(), 1, 'The updated items needs to be reindexed.'); $this->index->index(); $this->assertEqual($this->index->getTracker()->getIndexedItemsCount(), 3, 'Three items are indexed.'); }
/** * Tests fields on the data table. */ public function testDataTableFields() { $entity_type = $this->baseEntityType->set('data_table', 'entity_test_mul_property_data')->set('base_table', 'entity_test_mul')->set('id', 'entity_test_mul')->setKey('bundle', 'type'); $base_field_definitions = $this->setupBaseFields(EntityTestMul::baseFieldDefinitions($this->baseEntityType)); $base_field_definitions['type'] = BaseFieldDefinition::create('entity_reference')->setLabel('entity test type')->setSetting('target_type', 'entity_test_bundle')->setTranslatable(TRUE); $base_field_definitions = $this->setupBaseFields($base_field_definitions); $user_base_field_definitions = ['uid' => BaseFieldDefinition::create('integer')->setLabel('ID')->setDescription('The ID of the user entity.')->setReadOnly(TRUE)->setSetting('unsigned', TRUE)]; $entity_test_type = new ConfigEntityType(['id' => 'entity_test_bundle']); $this->entityManager->expects($this->any())->method('getBaseFieldDefinitions')->will($this->returnValueMap([['user', $user_base_field_definitions], ['entity_test_mul', $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_mul', 'entity_test_mul_property_data']); $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']]]); $table_mapping->expects($this->any())->method('getFieldNames')->willReturnMap([['entity_test_mul', ['uuid']], ['entity_test_mul_property_data', ['id', 'type', 'langcode', 'name', 'description', 'homepage', 'user_id']]]); $table_mapping->expects($this->any())->method('getFieldTableName')->willReturnCallback(function ($field) { if ($field == 'uuid') { return 'entity_test_mul'; } return 'entity_test_mul_property_data'; }); $this->entityStorage->expects($this->once())->method('getTableMapping')->willReturn($table_mapping); $this->setupFieldStorageDefinition(); $user_entity_type = static::userEntityInfo(); $this->entityManager->expects($this->any())->method('getDefinition')->will($this->returnValueMap([['user', TRUE, $user_entity_type], ['entity_test_bundle', TRUE, $entity_test_type]])); $data = $this->viewsData->getViewsData(); // Check the base fields. $this->assertFalse(isset($data['entity_test_mul']['id'])); $this->assertFalse(isset($data['entity_test_mul']['type'])); $this->assertUuidField($data['entity_test_mul']['uuid']); $this->assertField($data['entity_test_mul']['uuid'], 'uuid'); $this->assertFalse(isset($data['entity_test_mul']['type']['relationship'])); // Also ensure that field_data only fields don't appear on the base table. $this->assertFalse(isset($data['entity_test_mul']['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']['user_id'])); $this->assertFalse(isset($data['entity_test_mul']['homepage'])); // Check the data fields. $this->assertNumericField($data['entity_test_mul_property_data']['id']); $this->assertField($data['entity_test_mul_property_data']['id'], 'id'); $this->assertBundleField($data['entity_test_mul_property_data']['type']); $this->assertField($data['entity_test_mul_property_data']['type'], 'type'); $this->assertLanguageField($data['entity_test_mul_property_data']['langcode']); $this->assertField($data['entity_test_mul_property_data']['langcode'], 'langcode'); $this->assertEquals('Translation language', $data['entity_test_mul_property_data']['langcode']['title']); $this->assertStringField($data['entity_test_mul_property_data']['name']); $this->assertField($data['entity_test_mul_property_data']['name'], 'name'); $this->assertLongTextField($data['entity_test_mul_property_data'], 'description'); $this->assertField($data['entity_test_mul_property_data']['description__value'], 'description'); $this->assertField($data['entity_test_mul_property_data']['description__format'], 'description'); $this->assertUriField($data['entity_test_mul_property_data']['homepage']); $this->assertField($data['entity_test_mul_property_data']['homepage'], 'homepage'); $this->assertEntityReferenceField($data['entity_test_mul_property_data']['user_id']); $this->assertField($data['entity_test_mul_property_data']['user_id'], 'user_id'); $relationship = $data['entity_test_mul_property_data']['user_id']['relationship']; $this->assertEquals('users_field_data', $relationship['base']); $this->assertEquals('uid', $relationship['base field']); }
/** * Tests groupby with a field not existing on some bundle. */ public function testGroupByWithFieldsNotExistingOnBundle() { $field_storage = FieldStorageConfig::create(['type' => 'integer', 'field_name' => 'field_test', 'cardinality' => 4, 'entity_type' => 'entity_test_mul']); $field_storage->save(); $field = FieldConfig::create(['field_name' => 'field_test', 'entity_type' => 'entity_test_mul', 'bundle' => 'entity_test_mul']); $field->save(); $entities = []; $entity = EntityTestMul::create(['field_test' => [1], 'type' => 'entity_test_mul']); $entity->save(); $entities[] = $entity; $entity = EntityTestMul::create(['type' => 'entity_test_mul2']); $entity->save(); $entities[] = $entity; $view = Views::getView('test_group_by_field_not_within_bundle'); $this->executeView($view); $this->assertEqual(2, count($view->result)); // The first result is coming from entity_test_mul2, so no field could be // rendered. $this->assertEqual('', $view->getStyle()->getField(0, 'field_test')); // The second result is coming from entity_test_mul, so its field value // could be rendered. $this->assertEqual('1', $view->getStyle()->getField(1, 'field_test')); }
/** * {@inheritdoc} */ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields = parent::baseFieldDefinitions($entity_type); $fields['parent_type'] = BaseFieldDefinition::create('string')->setLabel(t('Parent type'))->setDescription(t('The entity parent type to which this entity is referenced.')); return $fields; }