function TaggedWithTest::testTaggedWith

Same name and namespace in other branches
  1. 8.9.x core/modules/views/tests/src/Functional/Wizard/TaggedWithTest.php \Drupal\Tests\views\Functional\Wizard\TaggedWithTest::testTaggedWith()
  2. 10 core/modules/views/tests/src/Functional/Wizard/TaggedWithTest.php \Drupal\Tests\views\Functional\Wizard\TaggedWithTest::testTaggedWith()
  3. 11.x core/modules/views/tests/src/Functional/Wizard/TaggedWithTest.php \Drupal\Tests\views\Functional\Wizard\TaggedWithTest::testTaggedWith()

Tests the "tagged with" functionality.

File

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

Class

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

Namespace

Drupal\Tests\views\Functional\Wizard

Code

public function testTaggedWith() {
    // In this test we will only create nodes that have an instance of the tag
    // field.
    $node_add_path = 'node/add/' . $this->nodeTypeWithTags
        ->id();
    // Create three nodes, with different tags.
    $edit = [];
    $edit['title[0][value]'] = $node_tag1_title = $this->randomMachineName();
    $edit[$this->tagFieldName . '[target_id]'] = 'tag1';
    $this->drupalGet($node_add_path);
    $this->submitForm($edit, 'Save');
    $edit = [];
    $edit['title[0][value]'] = $node_tag1_tag2_title = $this->randomMachineName();
    $edit[$this->tagFieldName . '[target_id]'] = 'tag1, tag2';
    $this->drupalGet($node_add_path);
    $this->submitForm($edit, 'Save');
    $edit = [];
    $edit['title[0][value]'] = $node_no_tags_title = $this->randomMachineName();
    $this->drupalGet($node_add_path);
    $this->submitForm($edit, 'Save');
    // Create a view that filters by taxonomy term "tag1". It should show only
    // the two nodes from above that are tagged with "tag1".
    $view1 = [];
    // First select the node type and update the form so the correct tag field
    // is used.
    $view1['show[type]'] = $this->nodeTypeWithTags
        ->id();
    $this->drupalGet('admin/structure/views/add');
    $this->submitForm($view1, 'Update "of type" choice');
    // Now resubmit the entire form to the same URL.
    $view1['label'] = $this->randomMachineName(16);
    $view1['id'] = strtolower($this->randomMachineName(16));
    $view1['description'] = $this->randomMachineName(16);
    $view1['show[tagged_with]'] = 'tag1';
    $view1['page[create]'] = 1;
    $view1['page[title]'] = $this->randomMachineName(16);
    $view1['page[path]'] = $this->randomMachineName(16);
    $this->submitForm($view1, 'Save and edit');
    // Visit the page and check that the nodes we expect are present and the
    // ones we don't expect are absent.
    $this->drupalGet($view1['page[path]']);
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextContains($node_tag1_title);
    $this->assertSession()
        ->pageTextContains($node_tag1_tag2_title);
    $this->assertSession()
        ->pageTextNotContains($node_no_tags_title);
    // Create a view that filters by taxonomy term "tag2". It should show only
    // the one node from above that is tagged with "tag2".
    $view2 = [];
    $view2['show[type]'] = $this->nodeTypeWithTags
        ->id();
    $this->drupalGet('admin/structure/views/add');
    $this->submitForm($view2, 'Update "of type" choice');
    $this->assertSession()
        ->statusCodeEquals(200);
    $view2['label'] = $this->randomMachineName(16);
    $view2['id'] = strtolower($this->randomMachineName(16));
    $view2['description'] = $this->randomMachineName(16);
    $view2['show[tagged_with]'] = 'tag2';
    $view2['page[create]'] = 1;
    $view2['page[title]'] = $this->randomMachineName(16);
    $view2['page[path]'] = $this->randomMachineName(16);
    $this->submitForm($view2, 'Save and edit');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->drupalGet($view2['page[path]']);
    $this->assertSession()
        ->pageTextNotContains($node_tag1_title);
    $this->assertSession()
        ->pageTextContains($node_tag1_tag2_title);
    $this->assertSession()
        ->pageTextNotContains($node_no_tags_title);
}

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