Version 1
[yaffs-website] / web / core / modules / views / tests / src / Kernel / Plugin / StyleMappingTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel\Plugin;
4
5 use Drupal\views\Views;
6
7 /**
8  * Tests mapping style functionality.
9  *
10  * @group views
11  */
12 class StyleMappingTest extends StyleTestBase {
13
14   public static $modules = ['system'];
15
16   /**
17    * Views used by this test.
18    *
19    * @var array
20    */
21   public static $testViews = ['test_style_mapping'];
22
23   /**
24    * Verifies that the fields were mapped correctly.
25    */
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.');
30     $view->destroy();
31
32     $view->setDisplay();
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.');
36   }
37
38   /**
39    * Tests the mapping of fields.
40    *
41    * @param \Drupal\views\ViewExecutable $view
42    *   The view to test.
43    *
44    * @return string
45    *   The view rendered as HTML.
46    */
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();
53
54     $count = 0;
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.');
59
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('-');
66
67         // The expected result is the mapping name and the field value,
68         // separated by ':'.
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]));
72       }
73
74       $count++;
75     }
76
77     return $rendered_output;
78   }
79
80 }