Version 1
[yaffs-website] / web / core / modules / views / src / Tests / Plugin / StyleGridTest.php
1 <?php
2
3 namespace Drupal\views\Tests\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 PluginTestBase {
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   protected $alignmentsTested = [];
27
28   /**
29    * {@inheritdoc}
30    */
31   protected function setUp() {
32     parent::setUp();
33     $this->enableViewsTestModule();
34   }
35
36   /**
37    * Tests the grid style.
38    */
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);
47     }
48
49     // Ensure styles are properly added for grid views.
50     $this->drupalGet('test-grid');
51     $this->assertRaw('stable/css/views/views.module.css');
52   }
53
54   /**
55    * Generates a grid and asserts that it is displaying correctly.
56    *
57    * @param \Drupal\views\ViewExecutable $view
58    *   The executable to prepare.
59    * @param string $alignment
60    *   The alignment of the grid to test.
61    * @param int $columns
62    *   The number of columns in the grid to test.
63    */
64   protected function assertGrid(ViewExecutable $view, $alignment, $columns) {
65     $view->setDisplay('default');
66     $view->initStyle();
67     $view->initHandlers();
68     $view->initQuery();
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;
79     }
80     $width = '0';
81     switch ($columns) {
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;
87     }
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.");
99   }
100
101 }