function ArgumentSummaryTest::testArgumentSummary

Same name and namespace in other branches
  1. 11.x core/modules/views/tests/src/Kernel/Handler/ArgumentSummaryTest.php \Drupal\Tests\views\Kernel\Handler\ArgumentSummaryTest::testArgumentSummary()

Tests the argument summary feature.

File

core/modules/views/tests/src/Kernel/Handler/ArgumentSummaryTest.php, line 119

Class

ArgumentSummaryTest
Tests the summary of results when an argument is not provided.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testArgumentSummary() : void {
  // Setup 2 tags.
  $tags = [];
  for ($i = 0; $i < 2; $i++) {
    $tags[$i] = $this->createTag();
  }
  // Setup 4 nodes with different tags.
  for ($i = 0; $i < 4; $i++) {
    Node::create([
      'type' => $this->nodeType
        ->id(),
      'title' => $this->randomMachineName(),
      // For odd numbered nodes, use both tags, even only get 1 tag.
'field_tags' => $i % 2 ? $tags : [
        $tags[0]->id(),
      ],
    ])
      ->save();
  }
  $view = Views::getView('test_argument_summary');
  $result = $view->preview('default');
  // For the purposes of this test, we don't care about any markup or
  // formatting, only that the summary is showing the tag labels and the
  // correct counts. So strip all tags and extra whitespace to make the
  // assertions more clear.
  $renderer = $this->container
    ->get('renderer');
  $output = (string) $renderer->renderRoot($result);
  $output = trim(preg_replace('/\\s+/', ' ', strip_tags($output)));
  // Output should show first tag on 4 nodes, the second tag on only 2.
  $this->assertStringContainsString($tags[0]->label() . ' (4)', $output);
  $this->assertStringContainsString($tags[1]->label() . ' (2)', $output);
}

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