Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / views / tests / src / Functional / Plugin / ExposedFormCheckboxesTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Plugin;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Field\FieldStorageDefinitionInterface;
7 use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
8 use Drupal\taxonomy\Entity\Term;
9 use Drupal\taxonomy\Entity\Vocabulary;
10 use Drupal\Tests\views\Functional\ViewTestBase;
11 use Drupal\views\Tests\ViewTestData;
12 use Drupal\views\Views;
13
14 /**
15  * Tests exposed forms functionality.
16  *
17  * @group views
18  */
19 class ExposedFormCheckboxesTest extends ViewTestBase {
20
21   use EntityReferenceTestTrait;
22
23   /**
24    * {@inheritdoc}
25    */
26   public static $testViews = ['test_exposed_form_checkboxes'];
27
28   /**
29    * {@inheritdoc}
30    */
31   public static $modules = ['node', 'views_ui', 'taxonomy'];
32
33   /**
34    * Test terms.
35    *
36    * @var array
37    */
38   public $terms = [];
39
40   /**
41    * Vocabulary for testing checkbox options.
42    *
43    * @var \Drupal\taxonomy\Entity\Vocabulary
44    */
45   public $vocabulary;
46
47   /**
48    * {@inheritdoc}
49    */
50   protected function setUp($import_test_views = TRUE) {
51     parent::setUp(FALSE);
52
53     // Create a vocabulary and entity reference field so we can test the "is all
54     // of" filter operator. Must be done ahead of the view import so the
55     // vocabulary is in place to meet the view dependencies.
56     $vocabulary = Vocabulary::create([
57       'name' => 'test_exposed_checkboxes',
58       'vid' => 'test_exposed_checkboxes',
59       'nodes' => ['article' => 'article'],
60     ]);
61     $vocabulary->save();
62     $this->vocabulary = $vocabulary;
63
64     ViewTestData::createTestViews(self::class, ['views_test_config']);
65     $this->enableViewsTestModule();
66
67     // Create two content types.
68     $this->drupalCreateContentType(['type' => 'article']);
69     $this->drupalCreateContentType(['type' => 'page']);
70
71     // Create some random nodes: 5 articles, one page.
72     for ($i = 0; $i < 5; $i++) {
73       $this->drupalCreateNode(['type' => 'article']);
74     }
75     $this->drupalCreateNode(['type' => 'page']);
76   }
77
78   /**
79    * Tests overriding the default render option with checkboxes.
80    */
81   public function testExposedFormRenderCheckboxes() {
82     // Use a test theme to convert multi-select elements into checkboxes.
83     \Drupal::service('theme_handler')->install(['views_test_checkboxes_theme']);
84     $this->config('system.theme')
85       ->set('default', 'views_test_checkboxes_theme')
86       ->save();
87
88     // Only display 5 items per page so we can test that paging works.
89     $view = Views::getView('test_exposed_form_checkboxes');
90     $display = &$view->storage->getDisplay('default');
91     $display['display_options']['pager']['options']['items_per_page'] = 5;
92
93     $view->save();
94     $this->drupalGet('test_exposed_form_checkboxes');
95
96     $actual = $this->xpath('//form//input[@type="checkbox" and @name="type[article]"]');
97     $this->assertEqual(count($actual), 1, 'Article option renders as a checkbox.');
98     $actual = $this->xpath('//form//input[@type="checkbox" and @name="type[page]"]');
99     $this->assertEqual(count($actual), 1, 'Page option renders as a checkbox');
100
101     // Ensure that all results are displayed.
102     $rows = $this->xpath("//div[contains(@class, 'views-row')]");
103     $this->assertEqual(count($rows), 5, '5 rows are displayed by default on the first page when no options are checked.');
104
105     $this->clickLink('Page 2');
106     $rows = $this->xpath("//div[contains(@class, 'views-row')]");
107     $this->assertEqual(count($rows), 1, '1 row is displayed by default on the second page when no options are checked.');
108     $this->assertNoText('An illegal choice has been detected. Please contact the site administrator.');
109   }
110
111   /**
112    * Tests that "is all of" filters work with checkboxes.
113    */
114   public function testExposedIsAllOfFilter() {
115     foreach (['Term 1', 'Term 2', 'Term 3'] as $term_name) {
116       // Add a few terms to the new vocabulary.
117       $term = Term::create([
118         'name' => $term_name,
119         'vid' => $this->vocabulary->id(),
120       ]);
121       $term->save();
122       $this->terms[] = $term;
123     }
124
125     // Create a field.
126     $field_name = Unicode::strtolower($this->randomMachineName());
127     $handler_settings = [
128       'target_bundles' => [
129         $this->vocabulary->id() => $this->vocabulary->id(),
130       ],
131       'auto_create' => FALSE,
132     ];
133     $this->createEntityReferenceField('node', 'article', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
134
135     // Add some test nodes.
136     $this->createNode([
137       'type' => 'article',
138       $field_name => [$this->terms[0]->id(), $this->terms[1]->id()],
139     ]);
140     $this->createNode([
141       'type' => 'article',
142       $field_name => [$this->terms[0]->id(), $this->terms[2]->id()],
143     ]);
144
145     // Use a test theme to convert multi-select elements into checkboxes.
146     \Drupal::service('theme_handler')->install(['views_test_checkboxes_theme']);
147     $this->config('system.theme')
148       ->set('default', 'views_test_checkboxes_theme')
149       ->save();
150
151     $this->drupalGet('test_exposed_form_checkboxes');
152
153     // Ensure that all results are displayed.
154     $rows = $this->xpath("//div[contains(@class, 'views-row')]");
155     $this->assertEqual(count($rows), 8, 'All rows are displayed by default on the first page when no options are checked.');
156     $this->assertNoText('An illegal choice has been detected. Please contact the site administrator.');
157
158     // Select one option and ensure we still have results.
159     $tid = $this->terms[0]->id();
160     $this->drupalPostForm(NULL, ["tid[$tid]" => $tid], t('Apply'));
161
162     // Ensure only nodes tagged with $tid are displayed.
163     $rows = $this->xpath("//div[contains(@class, 'views-row')]");
164     $this->assertEqual(count($rows), 2, 'Correct rows are displayed when a tid is selected.');
165     $this->assertNoText('An illegal choice has been detected. Please contact the site administrator.');
166   }
167
168 }