You are here

protected function StyleMappingTest::mappedOutputHelper in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Kernel/Plugin/StyleMappingTest.php \Drupal\Tests\views\Kernel\Plugin\StyleMappingTest::mappedOutputHelper()

Tests the mapping of fields.

Parameters

\Drupal\views\ViewExecutable $view: The view to test.

Return value

string The view rendered as HTML.

1 call to StyleMappingTest::mappedOutputHelper()
StyleMappingTest::testMappedOutput in core/modules/views/tests/src/Kernel/Plugin/StyleMappingTest.php
Verifies that the fields were mapped correctly.

File

core/modules/views/tests/src/Kernel/Plugin/StyleMappingTest.php, line 49

Class

StyleMappingTest
Tests mapping style functionality.

Namespace

Drupal\Tests\views\Kernel\Plugin

Code

protected function mappedOutputHelper(ViewExecutable $view) {
  $output = $view
    ->preview();
  $rendered_output = \Drupal::service('renderer')
    ->renderRoot($output);
  $this
    ->storeViewPreview($rendered_output);
  $rows = $this->elements->body->div->div;
  $data_set = $this
    ->dataSet();
  $count = 0;
  foreach ($rows as $row) {
    $attributes = $row
      ->attributes();
    $class = (string) $attributes['class'][0];
    $this
      ->assertStringContainsString('views-row-mapping-test', $class, 'Make sure that each row has the correct CSS class.');
    foreach ($row->div as $field) {

      // Split up the field-level class, the first part is the mapping name
      // and the second is the field ID.
      $field_attributes = $field
        ->attributes();
      $name = strtok((string) $field_attributes['class'][0], '-');
      $field_id = strtok('-');

      // The expected result is the mapping name and the field value,
      // separated by ':'.
      $expected_result = $name . ':' . $data_set[$count][$field_id];
      $actual_result = (string) $field;
      $this
        ->assertSame($expected_result, $actual_result, new FormattableMarkup('The fields were mapped successfully: %name => %field_id', [
        '%name' => $name,
        '%field_id' => $field_id,
      ]));
    }
    $count++;
  }
  return $rendered_output;
}