252a3091cae5e17e5eaaf2b7aeb544389468168e
[yaffs-website] / web / core / modules / views / tests / src / Functional / Plugin / StyleSummaryTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Plugin;
4
5 use Drupal\entity_test\Entity\EntityTest;
6 use Drupal\Tests\views\Functional\ViewTestBase;
7
8 /**
9  * Tests the summary style plugin.
10  *
11  * @group views
12  */
13 class StyleSummaryTest extends ViewTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public static $modules = ['entity_test', 'views_ui'];
19
20   /**
21    * {@inheritdoc}
22    */
23   public static $testViews = ['test_summary'];
24
25   /**
26    * @var \Drupal\entity_test\Entity\EntityTest[]
27    */
28   protected $entities = [];
29
30   /**
31    * {@inheritdoc}
32    */
33   protected function setUp($import_test_views = TRUE) {
34     parent::setUp($import_test_views);
35
36     // Create 5 entities per bundle, to allow a summary overview per bundle.
37     for ($i = 0; $i < 5; $i++) {
38       for ($j = 0; $j < 5; $j++) {
39         $this->entities[] = $entity = EntityTest::create([
40           'name' => 'Entity ' . ($i * 5 + $j),
41           'type' => 'type' . $i,
42         ]);
43         $entity->save();
44       }
45     }
46
47     $views_user = $this->drupalCreateUser(['administer views']);
48     $this->drupalLogin($views_user);
49   }
50
51   /**
52    * Tests a summary view.
53    */
54   public function testSummaryView() {
55     $this->drupalGet('test-summary');
56
57     $summary_list = $this->cssSelect('ul.views-summary li');
58     $this->assertEqual(4, count($summary_list));
59
60     foreach ($summary_list as $summary_list_item) {
61       $this->assertEqual('(5)', trim(explode(' ', $summary_list_item->getText())[1]));
62     }
63
64     $summary_links = $this->cssSelect('ul.views-summary a');
65     $this->assertEqual(4, count($summary_links));
66     foreach ($summary_links as $index => $summary_link) {
67       $this->assertEqual('type' . $index, trim($summary_link->getText()));
68     }
69
70     $this->clickLink('type1');
71     $entries = $this->cssSelect('div.view-content div.views-row');
72     $this->assertEqual(2, count($entries));
73
74     // Add a base path to the summary settings.
75     $edit = [
76       'options[summary][options][default_summary][base_path]' => 'test-summary',
77     ];
78     $this->drupalPostForm('admin/structure/views/nojs/handler/test_summary/page_1/argument/type', $edit, t('Apply'));
79     $this->drupalPostForm(NULL, [], t('Save'));
80
81     // Test that the links still work.
82     $this->drupalGet('test-summary');
83     $this->clickLink('type1');
84     $entries = $this->cssSelect('div.view-content div.views-row');
85     $this->assertEqual(2, count($entries));
86
87     // Change the summary display to an unformatted list displaying 3 items.
88     $edit = [
89       'options[summary][format]' => 'unformatted_summary',
90       'options[summary][options][unformatted_summary][override]' => '1',
91       'options[summary][options][unformatted_summary][items_per_page]' => '3',
92     ];
93     $this->drupalPostForm('admin/structure/views/nojs/handler/test_summary/page_1/argument/type', $edit, t('Apply'));
94     $this->drupalPostForm(NULL, [], t('Save'));
95
96     $this->drupalGet('admin/structure/views/nojs/handler/test_summary/page_1/argument/type');
97     $this->drupalGet('test-summary');
98
99     $summary_list = $this->cssSelect('.views-summary-unformatted');
100     $this->assertEqual(3, count($summary_list));
101
102     foreach ($summary_list as $summary_list_item) {
103       $this->assertEqual('(5)', trim(explode(' ', $summary_list_item->getText())[1]));
104     }
105
106     $summary_links = $this->cssSelect('.views-summary-unformatted a');
107     $this->assertEqual(3, count($summary_links));
108     foreach ($summary_links as $index => $summary_link) {
109       $this->assertEqual('type' . $index, trim($summary_link->getText()));
110     }
111
112     $this->clickLink('type1');
113     $entries = $this->cssSelect('div.view-content div.views-row');
114     $this->assertEqual(2, count($entries));
115
116     // Add a base path to the summary settings.
117     $edit = [
118       'options[summary][options][unformatted_summary][base_path]' => 'test-summary',
119     ];
120     $this->drupalPostForm('admin/structure/views/nojs/handler/test_summary/page_1/argument/type', $edit, t('Apply'));
121     $this->drupalPostForm(NULL, [], t('Save'));
122
123     // Test that the links still work.
124     $this->drupalGet('test-summary');
125     $this->clickLink('type1');
126     $entries = $this->cssSelect('div.view-content div.views-row');
127     $this->assertEqual(2, count($entries));
128
129     // Set base_path to an unknown path and test that the links lead to the
130     // front page.
131     $edit = [
132       'options[summary][options][unformatted_summary][base_path]' => 'unknown-path',
133     ];
134     $this->drupalPostForm('admin/structure/views/nojs/handler/test_summary/page_1/argument/type', $edit, t('Apply'));
135     $this->drupalPostForm(NULL, [], t('Save'));
136     $this->drupalGet('test-summary');
137     $this->assertLinkByHref('/');
138   }
139
140 }