/** * Asserts various aspects of a view mode entity. * * @param string $id * The entity ID. * @param string $label * The expected label of the view mode. * @param string $entity_type * The expected entity type ID which owns the view mode. * @param bool $status * The expected status of the view mode. */ protected function assertEntity($id, $label, $entity_type) { /** @var \Drupal\Core\Entity\EntityViewModeInterface $view_mode */ $view_mode = EntityViewMode::load($id); $this->assertTrue($view_mode instanceof EntityViewModeInterface); $this->assertIdentical($label, $view_mode->label()); $this->assertIdentical($entity_type, $view_mode->getTargetType()); }
/** * Tests Drupal 6 view modes to Drupal 8 migration. */ public function testViewModes() { // Test a new view mode. $view_mode = EntityViewMode::load('node.preview'); $this->assertIdentical(FALSE, is_null($view_mode), 'Preview view mode loaded.'); $this->assertIdentical('Preview', $view_mode->label(), 'View mode has correct label.'); // Test the Id Map. $this->assertIdentical(array('node', 'preview'), entity_load('migration', 'd6_view_modes')->getIdMap()->lookupDestinationID(array(1))); }
/** * Tests migrating D7 view modes, then rolling back. */ public function testMigration() { // Test that the view modes have migrated (prior to rollback). parent::testMigration(); $this->executeRollback('d7_view_modes'); // Check that view modes have been rolled back. $view_mode_ids = ['comment.full', 'node.teaser', 'node.full', 'user.full']; foreach ($view_mode_ids as $view_mode_id) { $this->assertNull(EntityViewMode::load($view_mode_id)); } }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->installConfig(['node']); entity_create('node_type', array('type' => 'test_page'))->save(); entity_create('node_type', array('type' => 'story'))->save(); // Create the node preview view mode. EntityViewMode::create(array('id' => 'node.preview', 'targetEntityType' => 'node'))->save(); // Add some id mappings for the dependant migrations. $id_mappings = array('d6_view_modes' => array(array(array(1), array('node', 'preview')), array(array(4), array('node', 'rss')), array(array('teaser'), array('node', 'teaser')), array(array('full'), array('node', 'full'))), 'd6_field_instance' => array(array(array('fieldname', 'page'), array('node', 'fieldname', 'page'))), 'd6_field' => array(array(array('field_test'), array('node', 'field_test')), array(array('field_test_two'), array('node', 'field_test_two')), array(array('field_test_three'), array('node', 'field_test_three')), array(array('field_test_email'), array('node', 'field_test_email')), array(array('field_test_link'), array('node', 'field_test_link')), array(array('field_test_filefield'), array('node', 'field_test_filefield')), array(array('field_test_imagefield'), array('node', 'field_test_imagefield')), array(array('field_test_phone'), array('node', 'field_test_phone')), array(array('field_test_date'), array('node', 'field_test_date')), array(array('field_test_datestamp'), array('node', 'field_test_datestamp')), array(array('field_test_datetime'), array('node', 'field_test_datetime')), array(array('field_test_exclude_unset'), array('node', 'field_test_exclude_unset')))); $this->prepareMigrations($id_mappings); $this->executeMigration('d6_field_formatter_settings'); }
/** * Tests changing the row plugin and changing some options of a row. */ public function testRowUI() { $view_name = 'test_view'; $view_edit_url = "admin/structure/views/view/{$view_name}/edit"; $row_plugin_url = "admin/structure/views/nojs/display/{$view_name}/default/row"; $row_options_url = "admin/structure/views/nojs/display/{$view_name}/default/row_options"; $this->drupalGet($row_plugin_url); $this->assertFieldByName('row[type]', 'fields', 'The default row plugin selected in the UI should be fields.'); $edit = array('row[type]' => 'test_row'); $this->drupalPostForm(NULL, $edit, t('Apply')); $this->assertFieldByName('row_options[test_option]', NULL, 'Make sure the custom settings form from the test plugin appears.'); $random_name = $this->randomMachineName(); $edit = array('row_options[test_option]' => $random_name); $this->drupalPostForm(NULL, $edit, t('Apply')); $this->drupalGet($row_options_url); $this->assertFieldByName('row_options[test_option]', $random_name, 'Make sure the custom settings form field has the expected value stored.'); $this->drupalPostForm($view_edit_url, array(), t('Save')); $this->assertLink(t('Test row plugin'), 0, 'Make sure the test row plugin is shown in the UI'); $view = Views::getView($view_name); $view->initDisplay(); $row = $view->display_handler->getOption('row'); $this->assertEqual($row['type'], 'test_row', 'Make sure that the test_row got saved as used row plugin.'); $this->assertEqual($row['options']['test_option'], $random_name, 'Make sure that the custom settings field got saved as expected.'); // Change the row plugin to fields using ajax. // Note: this is the best approximation we can achieve, because we cannot // simulate the 'openDialog' command in // WebTestBase::drupalProcessAjaxResponse(), hence we have to make do. $row_plugin_url_ajax = str_replace('/nojs/', '/ajax/', $row_plugin_url); $ajax_settings = ['accepts' => 'application/vnd.drupal-ajax', 'submit' => ['_triggering_element_name' => 'op', '_triggering_element_value' => 'Apply'], 'url' => $row_plugin_url_ajax]; $this->drupalPostAjaxForm($row_plugin_url, ['row[type]' => 'fields'], NULL, $row_plugin_url_ajax, [], [], NULL, $ajax_settings); $this->drupalGet($row_plugin_url); $this->assertResponse(200); $this->assertFieldByName('row[type]', 'fields', 'Make sure that the fields got saved as used row plugin.'); // Ensure that entity row plugins appear. $view_name = 'content'; $row_plugin_url = "admin/structure/views/nojs/display/{$view_name}/default/row"; $row_options_url = "admin/structure/views/nojs/display/{$view_name}/default/row_options"; $this->drupalGet($row_plugin_url); $this->assertFieldByName('row[type]', 'entity:node'); $this->drupalPostForm(NULL, ['row[type]' => 'entity:node'], t('Apply')); $this->assertUrl($row_options_url); $this->assertFieldByName('row_options[view_mode]', 'teaser'); // Change the teaser label to have markup so we can test escaping. $teaser = EntityViewMode::load('node.teaser'); $teaser->set('label', 'Teaser <em>markup</em>'); $teaser->save(); $this->drupalGet('admin/structure/views/view/frontpage/edit/default'); $this->assertEscaped('Teaser <em>markup</em>'); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); entity_create('node_type', array('type' => 'test_page'))->save(); entity_create('node_type', array('type' => 'story'))->save(); // Create the node preview view mode. EntityViewMode::create(array('id' => 'node.preview', 'targetEntityType' => 'node'))->save(); // Add some id mappings for the dependant migrations. $id_mappings = array('d6_view_modes' => array(array(array(1), array('node', 'preview')), array(array(4), array('node', 'rss')), array(array('teaser'), array('node', 'teaser')), array(array('full'), array('node', 'full'))), 'd6_field_instance' => array(array(array('fieldname', 'page'), array('node', 'fieldname', 'page'))), 'd6_field' => array(array(array('field_test'), array('node', 'field_test')), array(array('field_test_two'), array('node', 'field_test_two')), array(array('field_test_three'), array('node', 'field_test_three')), array(array('field_test_email'), array('node', 'field_test_email')), array(array('field_test_link'), array('node', 'field_test_link')), array(array('field_test_filefield'), array('node', 'field_test_filefield')), array(array('field_test_imagefield'), array('node', 'field_test_imagefield')), array(array('field_test_phone'), array('node', 'field_test_phone')), array(array('field_test_date'), array('node', 'field_test_date')), array(array('field_test_datestamp'), array('node', 'field_test_datestamp')), array(array('field_test_datetime'), array('node', 'field_test_datetime')), array(array('field_test_exclude_unset'), array('node', 'field_test_exclude_unset')))); $this->prepareMigrations($id_mappings); $migration = entity_load('migration', 'd6_field_formatter_settings'); $dumps = array($this->getDumpDirectory() . '/ContentNodeFieldInstance.php', $this->getDumpDirectory() . '/ContentNodeField.php', $this->getDumpDirectory() . '/ContentFieldTest.php', $this->getDumpDirectory() . '/ContentFieldTestTwo.php', $this->getDumpDirectory() . '/ContentFieldMultivalue.php'); $this->prepare($migration, $dumps); $executable = new MigrateExecutable($migration, $this); $executable->import(); }
/** * Ensures that entity types with bundles do not break following entity types. */ public function testFieldUIRoutes() { $this->drupalGet('entity_test_no_id/structure/entity_test/fields'); $this->assertText('No fields are present yet.'); $this->drupalGet('admin/config/people/accounts/fields'); $this->assertTitle('Manage fields | Drupal'); $this->assertLocalTasks(); // Test manage display tabs and titles. $this->drupalGet('admin/config/people/accounts/display/compact'); $this->assertResponse(403); $this->drupalGet('admin/config/people/accounts/display'); $this->assertTitle('Manage display | Drupal'); $this->assertLocalTasks(); $edit = array('display_modes_custom[compact]' => TRUE); $this->drupalPostForm(NULL, $edit, t('Save')); $this->drupalGet('admin/config/people/accounts/display/compact'); $this->assertTitle('Manage display | Drupal'); $this->assertLocalTasks(); // Test manage form display tabs and titles. $this->drupalGet('admin/config/people/accounts/form-display/register'); $this->assertResponse(403); $this->drupalGet('admin/config/people/accounts/form-display'); $this->assertTitle('Manage form display | Drupal'); $this->assertLocalTasks(); $edit = array('display_modes_custom[register]' => TRUE); $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertResponse(200); $this->drupalGet('admin/config/people/accounts/form-display/register'); $this->assertTitle('Manage form display | Drupal'); $this->assertLocalTasks(); $this->assert(count($this->xpath('//ul/li[1]/a[contains(text(), :text)]', array(':text' => 'Default'))) == 1, 'Default secondary tab is in first position.'); // Create new view mode and verify it's available on the Manage Display // screen after enabling it. EntityViewMode::create(array('id' => 'user.test', 'label' => 'Test', 'targetEntityType' => 'user'))->save(); $this->container->get('router.builder')->rebuildIfNeeded(); $edit = array('display_modes_custom[test]' => TRUE); $this->drupalPostForm('admin/config/people/accounts/display', $edit, t('Save')); $this->assertLink('Test'); // Create new form mode and verify it's available on the Manage Form // Display screen after enabling it. EntityFormMode::create(array('id' => 'user.test', 'label' => 'Test', 'targetEntityType' => 'user'))->save(); $this->container->get('router.builder')->rebuildIfNeeded(); $edit = array('display_modes_custom[test]' => TRUE); $this->drupalPostForm('admin/config/people/accounts/form-display', $edit, t('Save')); $this->assertLink('Test'); }
/** * {@inheritdoc} */ public function setUp() { parent::setUp(); $this->installEntitySchema('user'); $this->installEntitySchema('node'); // Create the article content type with a text field. $node_type = NodeType::create(['type' => 'article']); $node_type->save(); $field_storage = FieldStorageConfig::create(['field_name' => 'test_field', 'entity_type' => 'node', 'type' => 'text']); $field_storage->save(); $field = FieldConfig::create(['field_name' => 'test_field', 'entity_type' => 'node', 'bundle' => 'article', 'label' => 'Test field']); $field->save(); $view_mode = EntityViewMode::create(['id' => 'node.token', 'targetEntityType' => 'node']); $view_mode->save(); $entity_display = entity_get_display('node', 'article', 'token'); $entity_display->setComponent('test_field', ['type' => 'text_default']); $entity_display->save(); }
/** * {@inheritdoc} */ protected function setUpFixtures() { $this->installEntitySchema('user'); $this->installEntitySchema('entity_test'); $this->installConfig(['entity_test']); EntityViewMode::create(['id' => 'entity_test.foobar', 'targetEntityType' => 'entity_test', 'status' => TRUE, 'enabled' => TRUE, 'label' => 'My view mode'])->save(); $display = EntityViewDisplay::create(['targetEntityType' => 'entity_test', 'bundle' => 'entity_test', 'mode' => 'foobar', 'label' => 'My view mode', 'status' => TRUE]); $display->save(); $field_storage = FieldStorageConfig::create(['field_name' => 'test_field', 'entity_type' => 'entity_test', 'type' => 'string']); $field_storage->save(); $field_config = FieldConfig::create(['field_name' => 'test_field', 'entity_type' => 'entity_test', 'bundle' => 'entity_test']); $field_config->save(); // Create some test entities. for ($i = 1; $i <= 3; $i++) { EntityTest::create(['name' => "Article title {$i}", 'test_field' => "Test {$i}"])->save(); } $this->user = User::create(['name' => 'test user']); $this->user->save(); parent::setUpFixtures(); }
/** * Tests view mode setting integration. * * @see comment_entity_view_display_presave() * @see CommentDefaultFormatter::calculateDependencies() */ public function testViewMode() { $mode = Unicode::strtolower($this->randomMachineName()); // Create a new comment view mode and a view display entity. EntityViewMode::create(['id' => "comment.{$mode}", 'targetEntityType' => 'comment', 'settings' => ['comment_type' => 'comment']])->save(); EntityViewDisplay::create(['targetEntityType' => 'comment', 'bundle' => 'comment', 'mode' => $mode])->setStatus(TRUE)->save(); // Create a comment field attached to a host 'entity_test' entity. FieldStorageConfig::create(['entity_type' => 'entity_test', 'type' => 'comment', 'field_name' => $field_name = Unicode::strtolower($this->randomMachineName()), 'settings' => ['comment_type' => 'comment']])->save(); FieldConfig::create(['entity_type' => 'entity_test', 'bundle' => 'entity_test', 'field_name' => $field_name])->save(); $component = ['type' => 'comment_default', 'settings' => ['view_mode' => $mode, 'pager_id' => 0]]; // Create a new 'entity_test' view display on host entity that uses the // custom comment display in field formatter to show the field. EntityViewDisplay::create(['targetEntityType' => 'entity_test', 'bundle' => 'entity_test', 'mode' => 'default'])->setComponent($field_name, $component)->setStatus(TRUE)->save(); $host_display_id = 'entity_test.entity_test.default'; $comment_display_id = "comment.comment.{$mode}"; // Disable the "comment.comment.$mode" display. EntityViewDisplay::load($comment_display_id)->setStatus(FALSE)->save(); /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $host_display */ $host_display = EntityViewDisplay::load($host_display_id); // Check that the field formatter has been disabled on host view display. $this->assertNull($host_display->getComponent($field_name)); $this->assertTrue($host_display->get('hidden')[$field_name]); // Check that the proper warning has been logged. $arguments = ['@id' => $host_display_id, '@name' => $field_name, '@display' => EntityViewMode::load("comment.{$mode}")->label(), '@mode' => $mode]; $logged = (bool) Database::getConnection()->select('watchdog')->fields('watchdog', ['wid'])->condition('type', 'system')->condition('message', "View display '@id': Comment field formatter '@name' was disabled because it is using the comment view display '@display' (@mode) that was just disabled.")->condition('variables', serialize($arguments))->execute()->fetchField(); $this->assertTrue($logged); // Re-enable the comment view display. EntityViewDisplay::load($comment_display_id)->setStatus(TRUE)->save(); // Re-enable the comment field formatter on host entity view display. EntityViewDisplay::load($host_display_id)->setComponent($field_name, $component)->save(); // Delete the "comment.$mode" view mode. EntityViewMode::load("comment.{$mode}")->delete(); // Check that the comment view display entity has been deleted too. $this->assertNull(EntityViewDisplay::load($comment_display_id)); /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */ $host_display = EntityViewDisplay::load($host_display_id); // Check that the field formatter has been disabled on host view display. $this->assertNull($host_display->getComponent($field_name)); $this->assertTrue($host_display->get('hidden')[$field_name]); }
protected function setUp() { parent::setUp(); // Create a field and its storage. $this->fieldName = 'test_field'; $this->label = $this->randomMachineName(); $this->cardinality = 4; $field_storage = array('field_name' => $this->fieldName, 'entity_type' => 'entity_test', 'type' => 'test_field', 'cardinality' => $this->cardinality); $field = array('field_name' => $this->fieldName, 'entity_type' => 'entity_test', 'bundle' => 'entity_test', 'label' => $this->label); $this->displayOptions = array('default' => array('type' => 'field_test_default', 'settings' => array('test_formatter_setting' => $this->randomMachineName())), 'teaser' => array('type' => 'field_test_default', 'settings' => array('test_formatter_setting' => $this->randomMachineName()))); FieldStorageConfig::create($field_storage)->save(); FieldConfig::create($field)->save(); // Create a display for the default view mode. entity_get_display($field['entity_type'], $field['bundle'], 'default')->setComponent($this->fieldName, $this->displayOptions['default'])->save(); // Create a display for the teaser view mode. EntityViewMode::create(array('id' => 'entity_test.teaser', 'targetEntityType' => 'entity_test'))->save(); entity_get_display($field['entity_type'], $field['bundle'], 'teaser')->setComponent($this->fieldName, $this->displayOptions['teaser'])->save(); // Create an entity with values. $this->values = $this->_generateTestFieldValues($this->cardinality); $this->entity = EntityTest::create(); $this->entity->{$this->fieldName}->setValue($this->values); $this->entity->save(); }
/** * Tests deleting field. */ public function testDeleteField() { $field_name = 'test_field'; // Create a field storage and a field. $field_storage = FieldStorageConfig::create(array('field_name' => $field_name, 'entity_type' => 'entity_test', 'type' => 'test_field')); $field_storage->save(); $field = FieldConfig::create(array('field_storage' => $field_storage, 'bundle' => 'entity_test')); $field->save(); // Create default and teaser entity display. EntityViewMode::create(array('id' => 'entity_test.teaser', 'targetEntityType' => 'entity_test'))->save(); EntityViewDisplay::create(array('targetEntityType' => 'entity_test', 'bundle' => 'entity_test', 'mode' => 'default'))->setComponent($field_name)->save(); EntityViewDisplay::create(array('targetEntityType' => 'entity_test', 'bundle' => 'entity_test', 'mode' => 'teaser'))->setComponent($field_name)->save(); // Check the component exists. $display = entity_get_display('entity_test', 'entity_test', 'default'); $this->assertTrue($display->getComponent($field_name)); $display = entity_get_display('entity_test', 'entity_test', 'teaser'); $this->assertTrue($display->getComponent($field_name)); // Delete the field. $field->delete(); // Check that the component has been removed from the entity displays. $display = entity_get_display('entity_test', 'entity_test', 'default'); $this->assertFalse($display->getComponent($field_name)); $display = entity_get_display('entity_test', 'entity_test', 'teaser'); $this->assertFalse($display->getComponent($field_name)); }
/** * {@inheritdoc} */ public function onDependencyRemoval(array $dependencies) { // All dependencies of this processor are entity view modes, so we go // through our configuration and remove the settings for all datasources or // bundles which were set to one of the removed view modes. This will always // result in the removal of all those dependencies. // The code is highly similar to calculateDependencies(), only that we // remove the setting (if necessary) instead of adding a dependency. $view_modes = $this->configuration['view_mode']; foreach ($this->index->getDatasources() as $datasource_id => $datasource) { if ($entity_type_id = $datasource->getEntityTypeId() && !empty($view_modes[$datasource_id])) { foreach ($view_modes[$datasource_id] as $bundle => $view_mode_id) { if ($view_mode_id) { /** @var \Drupal\Core\Entity\EntityViewModeInterface $view_mode */ $view_mode = EntityViewMode::load($entity_type_id . '.' . $view_mode_id); if ($view_mode) { $dependency_key = $view_mode->getConfigDependencyKey(); $dependency_name = $view_mode->getConfigDependencyName(); if (!empty($dependencies[$dependency_key][$dependency_name])) { unset($this->configuration['view_mode'][$datasource_id][$bundle]); } } } } } } return TRUE; }
/** * Tests the comment formatter configured with a custom comment view mode. */ public function testViewMode() { $this->drupalLogin($this->webUser); $this->drupalGet($this->node->toUrl()); $comment_text = $this->randomMachineName(); // Post a comment. $this->postComment($this->node, $comment_text); // Comment displayed in 'default' display mode found and has body text. $comment_element = $this->cssSelect('.comment-wrapper'); $this->assertTrue(!empty($comment_element)); $this->assertRaw('<p>' . $comment_text . '</p>'); // Create a new comment entity view mode. $mode = Unicode::strtolower($this->randomMachineName()); EntityViewMode::create(['targetEntityType' => 'comment', 'id' => "comment.{$mode}"])->save(); // Create the corresponding entity view display for article node-type. Note // that this new view display mode doesn't contain the comment body. EntityViewDisplay::create(['targetEntityType' => 'comment', 'bundle' => 'comment', 'mode' => $mode])->setStatus(TRUE)->save(); /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $node_display */ $node_display = EntityViewDisplay::load('node.article.default'); $formatter = $node_display->getComponent('comment'); // Change the node comment field formatter to use $mode mode instead of // 'default' mode. $formatter['settings']['view_mode'] = $mode; $node_display->setComponent('comment', $formatter)->save(); // Reloading the node page to show the same node with its same comment but // with a different display mode. $this->drupalGet($this->node->toUrl()); // The comment should exist but without the body text because we used $mode // mode this time. $comment_element = $this->cssSelect('.comment-wrapper'); $this->assertTrue(!empty($comment_element)); $this->assertNoRaw('<p>' . $comment_text . '</p>'); }
/** * Test tokens on node with the token view mode overriding default formatters. */ public function testTokenViewMode() { $value = 'A really long string that should be trimmed by the special formatter on token view we are going to have.'; // The formatter we are going to use will eventually call Unicode::strlen. // This expects that the Unicode has already been explicitly checked, which // happens in DrupalKernel. But since that doesn't run in kernel tests, we // explicitly call this here. Unicode::check(); // Create a node with a value in the text field and test its token. $entity = Node::create(['title' => 'Test node title', 'type' => 'article', 'test_field' => ['value' => $value, 'format' => $this->testFormat->id()]]); $entity->save(); $this->assertTokens('node', ['node' => $entity], ['test_field' => Markup::create($value)]); // Now, create a token view mode which sets a different format for // test_field. When replacing tokens, this formatter should be picked over // the default formatter for the field type. // @see field_tokens(). $view_mode = EntityViewMode::create(['id' => 'node.token', 'targetEntityType' => 'node']); $view_mode->save(); $entity_display = entity_get_display('node', 'article', 'token'); $entity_display->setComponent('test_field', ['type' => 'text_trimmed', 'settings' => ['trim_length' => 50]]); $entity_display->save(); $this->assertTokens('node', ['node' => $entity], ['test_field' => Markup::create(substr($value, 0, 50))]); }