View source
<?php
namespace Drupal\Tests\field_ui\Functional;
use Drupal\Core\Url;
use Behat\Mink\Element\NodeElement;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\node\Entity\NodeType;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
class ManageDisplayTest extends BrowserTestBase {
use FieldUiTestTrait;
public static $modules = [
'node',
'field_ui',
'taxonomy',
'search',
'field_test',
'field_third_party_test',
'block',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('system_breadcrumb_block');
$this
->drupalPlaceBlock('local_tasks_block');
$admin_user = $this
->drupalCreateUser([
'access content',
'administer content types',
'administer node fields',
'administer node form display',
'administer node display',
'administer taxonomy',
'administer taxonomy_term fields',
'administer taxonomy_term display',
'administer users',
'administer account settings',
'administer user display',
'bypass node access',
]);
$this
->drupalLogin($admin_user);
$type_name = strtolower($this
->randomMachineName(8)) . '_test';
$type = $this
->drupalCreateContentType([
'name' => $type_name,
'type' => $type_name,
]);
$this->type = $type
->id();
$vocabulary = Vocabulary::create([
'name' => $this
->randomMachineName(),
'description' => $this
->randomMachineName(),
'vid' => mb_strtolower($this
->randomMachineName()),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'help' => '',
'nodes' => [
'article' => 'article',
],
'weight' => mt_rand(0, 10),
]);
$vocabulary
->save();
$this->vocabulary = $vocabulary
->id();
}
public function testViewModeCustom() {
$this
->fieldUIAddNewField('admin/structure/types/manage/' . $this->type, 'test', 'Test field');
\Drupal::service('entity_field.manager')
->clearCachedFieldDefinitions();
$value = 12345;
$settings = [
'type' => $this->type,
'field_test' => [
[
'value' => $value,
],
],
];
$node = $this
->drupalCreateNode($settings);
$formatter_plugin_manager = \Drupal::service('plugin.manager.field.formatter');
$field_test_default_settings = $formatter_plugin_manager
->getDefaultSettings('field_test_default');
$field_test_with_prepare_view_settings = $formatter_plugin_manager
->getDefaultSettings('field_test_with_prepare_view');
$output = [
'field_test_default' => $field_test_default_settings['test_formatter_setting'] . '|' . $value,
'field_test_with_prepare_view' => $field_test_with_prepare_view_settings['test_formatter_setting_additional'] . '|' . $value . '|' . ($value + 1),
];
$this
->assertNodeViewText($node, 'rss', $output['field_test_default'], "The field is displayed as expected in view modes that use 'default' settings.");
$this
->assertNodeViewNoText($node, 'teaser', $value, "The field is hidden in view modes that use custom settings.");
$edit = [
'fields[field_test][type]' => 'field_test_with_prepare_view',
'fields[field_test][region]' => 'content',
];
$this
->drupalPostForm('admin/structure/types/manage/' . $this->type . '/display', $edit, t('Save'));
$this
->assertNodeViewText($node, 'rss', $output['field_test_with_prepare_view'], "The field is displayed as expected in view modes that use 'default' settings.");
$edit = [
"display_modes_custom[rss]" => TRUE,
];
$this
->drupalPostForm('admin/structure/types/manage/' . $this->type . '/display', $edit, t('Save'));
$this
->assertNodeViewText($node, 'rss', $output['field_test_with_prepare_view'], "The field is displayed as expected in newly specialized 'rss' mode.");
$edit = [
'fields[field_test][region]' => 'hidden',
];
$this
->drupalPostForm('admin/structure/types/manage/' . $this->type . '/display/rss', $edit, t('Save'));
$this
->assertNodeViewNoText($node, 'rss', $value, "The field is hidden in 'rss' mode.");
$edit = [
"display_modes_custom[rss]" => FALSE,
];
$this
->drupalPostForm('admin/structure/types/manage/' . $this->type . '/display', $edit, t('Save'));
$this
->assertNodeViewText($node, 'rss', $output['field_test_with_prepare_view'], "The field is displayed as expected when 'rss' mode is set back to 'default' settings.");
$edit = [
"display_modes_custom[rss]" => TRUE,
];
$this
->drupalPostForm('admin/structure/types/manage/' . $this->type . '/display', $edit, t('Save'));
$this
->assertNodeViewNoText($node, 'rss', $value, "The previous settings are kept when 'rss' mode is specialized again.");
}
public function testViewModeLocalTasks() {
$manage_display = 'admin/structure/types/manage/' . $this->type . '/display';
$this
->drupalGet($manage_display);
$this
->assertSession()
->linkNotExists('Full content');
$this
->assertSession()
->linkExists('Teaser');
$this
->drupalGet($manage_display . '/teaser');
$this
->assertSession()
->linkNotExists('Full content');
$this
->assertSession()
->linkExists('Default');
}
public function testNonInitializedFields() {
$this
->fieldUIAddNewField('admin/structure/types/manage/' . $this->type, 'test', 'Test');
$this
->drupalGet('admin/structure/types/manage/' . $this->type . '/display/teaser');
$this
->assertFieldByName('fields[field_test][region]', 'hidden', 'The field is displayed as \'hidden \'.');
}
public function testSingleViewMode() {
$this
->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary . '/display');
$this
->assertNoText('Use custom display settings for the following view modes', 'Custom display settings fieldset found.');
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary . '/overview/display', [], t('Save'));
}
public function testNoFieldsDisplayOverview() {
NodeType::create([
'type' => 'no_fields',
'name' => 'No fields',
])
->save();
$this
->drupalGet('admin/structure/types/manage/no_fields/display');
$this
->assertRaw(t('There are no fields yet added. You can add new fields on the <a href=":link">Manage fields</a> page.', [
':link' => Url::fromRoute('entity.node.field_ui_fields', [
'node_type' => 'no_fields',
])
->toString(),
]));
}
public function assertNodeViewText(EntityInterface $node, $view_mode, $text, $message) {
return $this
->assertNodeViewTextHelper($node, $view_mode, $text, $message, FALSE);
}
public function assertNodeViewNoText(EntityInterface $node, $view_mode, $text, $message) {
return $this
->assertNodeViewTextHelper($node, $view_mode, $text, $message, TRUE);
}
public function assertNodeViewTextHelper(EntityInterface $node, $view_mode, $text, $message, $not_exists) {
\Drupal::service('entity_field.manager')
->clearCachedFieldDefinitions();
$clone = clone $node;
$element = \Drupal::entityTypeManager()
->getViewBuilder('node')
->view($clone, $view_mode);
$output = (string) \Drupal::service('renderer')
->renderRoot($element);
$this
->verbose(t('Rendered node - view mode: @view_mode', [
'@view_mode' => $view_mode,
]) . '<hr />' . $output);
if ($not_exists) {
$this
->assertStringNotContainsString((string) $text, $output, $message);
}
else {
$this
->assertStringContainsString((string) $text, $output, $message);
}
}
protected function assertFieldSelectOptions($name, array $expected_options) {
$xpath = $this
->buildXPathQuery('//select[@name=:name]', [
':name' => $name,
]);
$fields = $this
->xpath($xpath);
if ($fields) {
$field = $fields[0];
$options = $this
->getAllOptionsList($field);
sort($options);
sort($expected_options);
$this
->assertIdentical($options, $expected_options);
}
else {
$this
->fail('Unable to find field ' . $name);
}
}
protected function getAllOptionsList(NodeElement $element) {
$options = [];
foreach ($element->option as $option) {
$options[] = $option
->getValue();
}
foreach ($element->optgroup as $optgroup) {
$options = array_merge($this
->getAllOptionsList($optgroup), $options);
}
return $options;
}
}