You are here

public function StyleTableTest::testGrouping in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Functional/Plugin/StyleTableTest.php \Drupal\Tests\views\Functional\Plugin\StyleTableTest::testGrouping()
  2. 9 core/modules/views/tests/src/Functional/Plugin/StyleTableTest.php \Drupal\Tests\views\Functional\Plugin\StyleTableTest::testGrouping()

Tests grouping by a field.

File

core/modules/views/tests/src/Functional/Plugin/StyleTableTest.php, line 170

Class

StyleTableTest
Tests the table style views plugin.

Namespace

Drupal\Tests\views\Functional\Plugin

Code

public function testGrouping() {

  /** @var \Drupal\views\ViewEntityInterface $view */
  $view = \Drupal::entityTypeManager()
    ->getStorage('view')
    ->load('test_table');

  // Get a reference to the display configuration so we can alter some
  // specific style options.
  $display =& $view
    ->getDisplay('default');

  // Set job as the grouping field.
  $display['display_options']['style']['options']['grouping'][0] = [
    'field' => 'job',
    'rendered' => TRUE,
    'rendered_strip' => FALSE,
  ];

  // Clear the caption text, the rendered job field will be used as a caption.
  $display['display_options']['style']['options']['caption'] = '';
  $display['display_options']['style']['options']['summary'] = '';
  $display['display_options']['style']['options']['description'] = '';
  $view
    ->save();

  // Add a record containing unsafe markup to be sure it's filtered out.
  $unsafe_markup = '<script>alert("Rapper");</script>';
  $unsafe_markup_data = [
    'name' => 'Marshall',
    'age' => 42,
    'job' => $unsafe_markup,
    'created' => gmmktime(0, 0, 0, 2, 15, 2001),
    'status' => 1,
  ];
  $database = $this->container
    ->get('database');
  $database
    ->insert('views_test_data')
    ->fields(array_keys($unsafe_markup_data))
    ->values($unsafe_markup_data)
    ->execute();
  $this
    ->drupalGet('test-table');
  $expected_captions = [
    'Job: Speaker',
    'Job: Songwriter',
    'Job: Drummer',
    'Job: Singer',
    'Job: ' . $unsafe_markup,
  ];

  // Ensure that we don't find the caption containing unsafe markup.
  $this
    ->assertSession()
    ->responseNotContains($unsafe_markup);

  // Ensure that the summary isn't shown.
  $this
    ->assertEmpty($this
    ->xpath('//caption/details'));

  // Ensure that all expected captions are found.
  foreach ($expected_captions as $raw_caption) {
    $this
      ->assertSession()
      ->assertEscaped($raw_caption);
  }
  $display =& $view
    ->getDisplay('default');

  // Remove the label from the grouping field.
  $display['display_options']['fields']['job']['label'] = '';
  $view
    ->save();
  $this
    ->drupalGet('test-table');
  $expected_captions = [
    'Speaker',
    'Songwriter',
    'Drummer',
    'Singer',
    $unsafe_markup,
  ];

  // Ensure that we don't find the caption containing unsafe markup.
  $this
    ->assertSession()
    ->responseNotContains($unsafe_markup);

  // Ensure that all expected captions are found.
  foreach ($expected_captions as $raw_caption) {
    $this
      ->assertSession()
      ->assertEscaped($raw_caption);
  }
}