3 namespace Drupal\Tests\views\Kernel\Plugin;
5 use Drupal\views\Views;
6 use Drupal\views\ViewExecutable;
9 * Tests the grid style plugin.
12 * @see \Drupal\views\Plugin\views\style\Grid
14 class StyleGridTest extends PluginKernelTestBase {
17 * Views used by this test.
21 public static $testViews = ['test_grid'];
24 * Keeps track of which alignments have been tested.
28 protected $alignmentsTested = [];
31 * Tests the grid style.
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);
45 * Generates a grid and asserts that it is displaying correctly.
47 * @param \Drupal\views\ViewExecutable $view
48 * The executable to prepare.
49 * @param string $alignment
50 * The alignment of the grid to test.
52 * The number of columns in the grid to test.
54 protected function assertGrid(ViewExecutable $view, $alignment, $columns) {
55 $view->setDisplay('default');
57 $view->initHandlers();
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;
72 case 5: $width = '20';
74 case 4: $width = '25';
76 case 3: $width = '33.3333';
78 case 2: $width = '50';
80 case 1: $width = '100';
83 // Ensure last column exists.
84 $result = $this->xpath('//div[contains(@class, "views-col") and contains(@class, :columns) and starts-with(@style, :width)]', [':columns' => 'col-' . $columns, ':width' => 'width: ' . $width]);
85 $this->assertTrue(count($result), ucfirst($alignment) . " $columns column grid: last column exists and automatic width calculated correctly.");
86 // Ensure no extra columns were generated.
87 $result = $this->xpath('//div[contains(@class, "views-col") and contains(@class, :columns)]', [':columns' => 'col-' . ($columns + 1)]);
88 $this->assertFalse(count($result), ucfirst($alignment) . " $columns column grid: no extraneous columns exist.");
89 // Ensure tokens are being replaced in custom row/column classes.
90 $result = $this->xpath('//div[contains(@class, "views-col") and contains(@class, "name-John")]');
91 $this->assertTrue(count($result), ucfirst($alignment) . " $columns column grid: Token replacement verified in custom column classes.");
92 $result = $this->xpath('//div[contains(@class, "views-row") and contains(@class, "age-25")]');
93 $this->assertTrue(count($result), ucfirst($alignment) . " $columns column grid: Token replacement verified in custom row classes.");