d4381a6329ab3231fbb58d8262610dd2acf13494
[yaffs-website] / web / core / modules / views / tests / src / Kernel / Plugin / StyleGridTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel\Plugin;
4
5 use Drupal\views\Views;
6 use Drupal\views\ViewExecutable;
7
8 /**
9  * Tests the grid style plugin.
10  *
11  * @group views
12  * @see \Drupal\views\Plugin\views\style\Grid
13  */
14 class StyleGridTest extends PluginKernelTestBase {
15
16   /**
17    * Views used by this test.
18    *
19    * @var array
20    */
21   public static $testViews = ['test_grid'];
22
23   /**
24    * Keeps track of which alignments have been tested.
25    *
26    * @var array
27    */
28   protected $alignmentsTested = [];
29
30   /**
31    * Tests the grid style.
32    */
33   public function testGrid() {
34     $view = Views::getView('test_grid');
35     foreach (['horizontal', 'vertical'] as $alignment) {
36       $this->assertGrid($view, $alignment, 5);
37       $this->assertGrid($view, $alignment, 4);
38       $this->assertGrid($view, $alignment, 3);
39       $this->assertGrid($view, $alignment, 2);
40       $this->assertGrid($view, $alignment, 1);
41     }
42   }
43
44   /**
45    * Generates a grid and asserts that it is displaying correctly.
46    *
47    * @param \Drupal\views\ViewExecutable $view
48    *   The executable to prepare.
49    * @param string $alignment
50    *   The alignment of the grid to test.
51    * @param int $columns
52    *   The number of columns in the grid to test.
53    */
54   protected function assertGrid(ViewExecutable $view, $alignment, $columns) {
55     $view->setDisplay('default');
56     $view->initStyle();
57     $view->initHandlers();
58     $view->initQuery();
59     $view->style_plugin->options['alignment'] = $alignment;
60     $view->style_plugin->options['columns'] = $columns;
61     $this->executeView($view);
62     $output = $view->preview();
63     $output = \Drupal::service('renderer')->renderRoot($output);
64     $this->setRawContent($output);
65     if (!in_array($alignment, $this->alignmentsTested)) {
66       $result = $this->xpath('//div[contains(@class, "views-view-grid") and contains(@class, :alignment) and contains(@class, :columns)]', [':alignment' => $alignment, ':columns' => 'cols-' . $columns]);
67       $this->assertTrue(count($result), ucfirst($alignment) . " grid markup detected.");
68       $this->alignmentsTested[] = $alignment;
69     }
70     $width = '0';
71     switch ($columns) {
72       case 5: $width = '20';
73         break;
74
75       case 4: $width = '25';
76         break;
77
78       case 3: $width = '33.3333';
79         break;
80
81       case 2: $width = '50';
82         break;
83
84       case 1: $width = '100';
85         break;
86     }
87     // Ensure last column exists.
88     $result = $this->xpath('//div[contains(@class, "views-col") and contains(@class, :columns) and starts-with(@style, :width)]', [':columns' => 'col-' . $columns, ':width' => 'width: ' . $width]);
89     $this->assertTrue(count($result), ucfirst($alignment) . " $columns column grid: last column exists and automatic width calculated correctly.");
90     // Ensure no extra columns were generated.
91     $result = $this->xpath('//div[contains(@class, "views-col") and contains(@class, :columns)]', [':columns' => 'col-' . ($columns + 1)]);
92     $this->assertFalse(count($result), ucfirst($alignment) . " $columns column grid: no extraneous columns exist.");
93     // Ensure tokens are being replaced in custom row/column classes.
94     $result = $this->xpath('//div[contains(@class, "views-col") and contains(@class, "name-John")]');
95     $this->assertTrue(count($result), ucfirst($alignment) . " $columns column grid: Token replacement verified in custom column classes.");
96     $result = $this->xpath('//div[contains(@class, "views-row") and contains(@class, "age-25")]');
97     $this->assertTrue(count($result), ucfirst($alignment) . " $columns column grid: Token replacement verified in custom row classes.");
98   }
99
100 }