3 namespace Drupal\views\Tests\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 PluginTestBase {
17 * Views used by this test.
21 public static $testViews = ['test_grid'];
24 * Keeps track of which alignments have been tested.
26 protected $alignmentsTested = [];
31 protected function setUp() {
33 $this->enableViewsTestModule();
37 * Tests the grid style.
39 public function testGrid() {
40 $view = Views::getView('test_grid');
41 foreach (['horizontal', 'vertical'] as $alignment) {
42 $this->assertGrid($view, $alignment, 5);
43 $this->assertGrid($view, $alignment, 4);
44 $this->assertGrid($view, $alignment, 3);
45 $this->assertGrid($view, $alignment, 2);
46 $this->assertGrid($view, $alignment, 1);
49 // Ensure styles are properly added for grid views.
50 $this->drupalGet('test-grid');
51 $this->assertRaw('stable/css/views/views.module.css');
55 * Generates a grid and asserts that it is displaying correctly.
57 * @param \Drupal\views\ViewExecutable $view
58 * The executable to prepare.
59 * @param string $alignment
60 * The alignment of the grid to test.
62 * The number of columns in the grid to test.
64 protected function assertGrid(ViewExecutable $view, $alignment, $columns) {
65 $view->setDisplay('default');
67 $view->initHandlers();
69 $view->style_plugin->options['alignment'] = $alignment;
70 $view->style_plugin->options['columns'] = $columns;
71 $this->executeView($view);
72 $output = $view->preview();
73 $output = \Drupal::service('renderer')->renderRoot($output);
74 $this->setRawContent($output);
75 if (!in_array($alignment, $this->alignmentsTested)) {
76 $result = $this->xpath('//div[contains(@class, "views-view-grid") and contains(@class, :alignment) and contains(@class, :columns)]', [':alignment' => $alignment, ':columns' => 'cols-' . $columns]);
77 $this->assertTrue(count($result), ucfirst($alignment) . " grid markup detected.");
78 $this->alignmentsTested[] = $alignment;
82 case 5: $width = '20'; break;
83 case 4: $width = '25'; break;
84 case 3: $width = '33.3333'; break;
85 case 2: $width = '50'; break;
86 case 1: $width = '100'; break;
88 // Ensure last column exists.
89 $result = $this->xpath('//div[contains(@class, "views-col") and contains(@class, :columns) and starts-with(@style, :width)]', [':columns' => 'col-' . $columns, ':width' => 'width: ' . $width]);
90 $this->assertTrue(count($result), ucfirst($alignment) . " $columns column grid: last column exists and automatic width calculated correctly.");
91 // Ensure no extra columns were generated.
92 $result = $this->xpath('//div[contains(@class, "views-col") and contains(@class, :columns)]', [':columns' => 'col-' . ($columns + 1)]);
93 $this->assertFalse(count($result), ucfirst($alignment) . " $columns column grid: no extraneous columns exist.");
94 // Ensure tokens are being replaced in custom row/column classes.
95 $result = $this->xpath('//div[contains(@class, "views-col") and contains(@class, "name-John")]');
96 $this->assertTrue(count($result), ucfirst($alignment) . " $columns column grid: Token replacement verified in custom column classes.");
97 $result = $this->xpath('//div[contains(@class, "views-row") and contains(@class, "age-25")]');
98 $this->assertTrue(count($result), ucfirst($alignment) . " $columns column grid: Token replacement verified in custom row classes.");