function TaggedWithTest::testTaggedWithByNodeType

Tests the "tagged with" form element.

Confirm that the element only shows for node types that support it.

File

core/modules/views/tests/src/Functional/Wizard/TaggedWithTest.php, line 203

Class

TaggedWithTest
Tests the ability of the views wizard to create views filtered by taxonomy.

Namespace

Drupal\Tests\views\Functional\Wizard

Code

public function testTaggedWithByNodeType() : void {
  // The tagging field is associated with one of our node types only. So the
  // "tagged with" form element on the view wizard should appear on the form
  // by default (when the wizard is configured to display all content) and
  // also when the node type that has the tagging field is selected, but not
  // when the node type that doesn't have the tagging field is selected.
  $this->drupalGet('admin/structure/views/add');
  $this->assertSession()
    ->fieldExists("show[tagged_with]");
  $view['show[type]'] = $this->nodeTypeWithTags
    ->id();
  $this->drupalGet('admin/structure/views/add');
  $this->submitForm($view, 'Update "of type" choice');
  $this->assertSession()
    ->fieldExists("show[tagged_with]");
  $view['show[type]'] = $this->nodeTypeWithoutTags
    ->id();
  $this->submitForm($view, 'Update "of type" choice (2)');
  $this->assertSession()
    ->fieldNotExists("show[tagged_with]");
  // If we add an instance of the tagging field to the second node type, the
  // "tagged with" form element should not appear for it too.
  FieldConfig::create([
    'field_name' => $this->tagFieldName,
    'entity_type' => 'node',
    'bundle' => $this->nodeTypeWithoutTags
      ->id(),
    'settings' => [
      'handler' => 'default',
      'handler_settings' => [
        'target_bundles' => [
          $this->tagVocabulary
            ->id() => $this->tagVocabulary
            ->id(),
        ],
        'auto_create' => TRUE,
      ],
    ],
  ])
    ->save();
  \Drupal::service('entity_display.repository')->getFormDisplay('node', $this->nodeTypeWithoutTags
    ->id())
    ->setComponent($this->tagFieldName, [
    'type' => 'entity_reference_autocomplete_tags',
  ])
    ->save();
  $view['show[type]'] = $this->nodeTypeWithTags
    ->id();
  $this->drupalGet('admin/structure/views/add');
  $this->submitForm($view, 'Update "of type" choice');
  $this->assertSession()
    ->fieldExists("show[tagged_with]");
  $view['show[type]'] = $this->nodeTypeWithoutTags
    ->id();
  $this->submitForm($view, 'Update "of type" choice (2)');
  $this->assertSession()
    ->fieldExists("show[tagged_with]");
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.