3 namespace Drupal\Tests\views\Kernel\Plugin;
5 use Drupal\views\Views;
8 * Tests mapping style functionality.
12 class StyleMappingTest extends StyleTestBase {
14 public static $modules = ['system'];
17 * Views used by this test.
21 public static $testViews = ['test_style_mapping'];
24 * Verifies that the fields were mapped correctly.
26 public function testMappedOutput() {
27 $view = Views::getView('test_style_mapping');
28 $output = $this->mappedOutputHelper($view);
29 $this->assertTrue(strpos($output, 'job') === FALSE, 'The job field is added to the view but not in the mapping.');
33 $view->displayHandlers->get('default')->options['style']['options']['mapping']['name_field'] = 'job';
34 $output = $this->mappedOutputHelper($view);
35 $this->assertTrue(strpos($output, 'job') !== FALSE, 'The job field is added to the view and is in the mapping.');
39 * Tests the mapping of fields.
41 * @param \Drupal\views\ViewExecutable $view
45 * The view rendered as HTML.
47 protected function mappedOutputHelper($view) {
48 $output = $view->preview();
49 $rendered_output = \Drupal::service('renderer')->renderRoot($output);
50 $this->storeViewPreview($rendered_output);
51 $rows = $this->elements->body->div->div;
52 $data_set = $this->dataSet();
55 foreach ($rows as $row) {
56 $attributes = $row->attributes();
57 $class = (string) $attributes['class'][0];
58 $this->assertTrue(strpos($class, 'views-row-mapping-test') !== FALSE, 'Make sure that each row has the correct CSS class.');
60 foreach ($row->div as $field) {
61 // Split up the field-level class, the first part is the mapping name
62 // and the second is the field ID.
63 $field_attributes = $field->attributes();
64 $name = strtok((string) $field_attributes['class'][0], '-');
65 $field_id = strtok('-');
67 // The expected result is the mapping name and the field value,
69 $expected_result = $name . ':' . $data_set[$count][$field_id];
70 $actual_result = (string) $field;
71 $this->assertIdentical($expected_result, $actual_result, format_string('The fields were mapped successfully: %name => %field_id', ['%name' => $name, '%field_id' => $field_id]));
77 return $rendered_output;