You are here

public function TaggedWithTest::testTaggedWithByNodeType in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Functional/Wizard/TaggedWithTest.php \Drupal\Tests\views\Functional\Wizard\TaggedWithTest::testTaggedWithByNodeType()

Tests that the "tagged with" form element only shows for node types that support it.

File

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

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() {

  // 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.
  $tags_xpath = '//input[@name="show[tagged_with]"]';
  $this
    ->drupalGet('admin/structure/views/add');
  $this
    ->assertFieldByXpath($tags_xpath);
  $view['show[type]'] = $this->nodeTypeWithTags
    ->id();
  $this
    ->drupalPostForm('admin/structure/views/add', $view, t('Update "of type" choice'));
  $this
    ->assertFieldByXpath($tags_xpath);
  $view['show[type]'] = $this->nodeTypeWithoutTags
    ->id();
  $this
    ->drupalPostForm(NULL, $view, t('Update "of type" choice (2)'));
  $this
    ->assertNoFieldByXpath($tags_xpath);

  // 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
    ->drupalPostForm('admin/structure/views/add', $view, t('Update "of type" choice'));
  $this
    ->assertFieldByXpath($tags_xpath);
  $view['show[type]'] = $this->nodeTypeWithoutTags
    ->id();
  $this
    ->drupalPostForm(NULL, $view, t('Update "of type" choice (2)'));
  $this
    ->assertFieldByXpath($tags_xpath);
}