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