X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fviews%2Ftests%2Fsrc%2FFunctional%2FPlugin%2FExposedFormCheckboxesTest.php;fp=web%2Fcore%2Fmodules%2Fviews%2Ftests%2Fsrc%2FFunctional%2FPlugin%2FExposedFormCheckboxesTest.php;h=80584d796e2b79716a758c2b3597c22ee599297c;hp=0000000000000000000000000000000000000000;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hpb=aea91e65e895364e460983b890e295aa5d5540a5 diff --git a/web/core/modules/views/tests/src/Functional/Plugin/ExposedFormCheckboxesTest.php b/web/core/modules/views/tests/src/Functional/Plugin/ExposedFormCheckboxesTest.php new file mode 100644 index 000000000..80584d796 --- /dev/null +++ b/web/core/modules/views/tests/src/Functional/Plugin/ExposedFormCheckboxesTest.php @@ -0,0 +1,168 @@ + 'test_exposed_checkboxes', + 'vid' => 'test_exposed_checkboxes', + 'nodes' => ['article' => 'article'], + ]); + $vocabulary->save(); + $this->vocabulary = $vocabulary; + + ViewTestData::createTestViews(self::class, ['views_test_config']); + $this->enableViewsTestModule(); + + // Create two content types. + $this->drupalCreateContentType(['type' => 'article']); + $this->drupalCreateContentType(['type' => 'page']); + + // Create some random nodes: 5 articles, one page. + for ($i = 0; $i < 5; $i++) { + $this->drupalCreateNode(['type' => 'article']); + } + $this->drupalCreateNode(['type' => 'page']); + } + + /** + * Tests overriding the default render option with checkboxes. + */ + public function testExposedFormRenderCheckboxes() { + // Use a test theme to convert multi-select elements into checkboxes. + \Drupal::service('theme_handler')->install(['views_test_checkboxes_theme']); + $this->config('system.theme') + ->set('default', 'views_test_checkboxes_theme') + ->save(); + + // Only display 5 items per page so we can test that paging works. + $view = Views::getView('test_exposed_form_checkboxes'); + $display = &$view->storage->getDisplay('default'); + $display['display_options']['pager']['options']['items_per_page'] = 5; + + $view->save(); + $this->drupalGet('test_exposed_form_checkboxes'); + + $actual = $this->xpath('//form//input[@type="checkbox" and @name="type[article]"]'); + $this->assertEqual(count($actual), 1, 'Article option renders as a checkbox.'); + $actual = $this->xpath('//form//input[@type="checkbox" and @name="type[page]"]'); + $this->assertEqual(count($actual), 1, 'Page option renders as a checkbox'); + + // Ensure that all results are displayed. + $rows = $this->xpath("//div[contains(@class, 'views-row')]"); + $this->assertEqual(count($rows), 5, '5 rows are displayed by default on the first page when no options are checked.'); + + $this->clickLink('Page 2'); + $rows = $this->xpath("//div[contains(@class, 'views-row')]"); + $this->assertEqual(count($rows), 1, '1 row is displayed by default on the second page when no options are checked.'); + $this->assertNoText('An illegal choice has been detected. Please contact the site administrator.'); + } + + /** + * Tests that "is all of" filters work with checkboxes. + */ + public function testExposedIsAllOfFilter() { + foreach (['Term 1', 'Term 2', 'Term 3'] as $term_name) { + // Add a few terms to the new vocabulary. + $term = Term::create([ + 'name' => $term_name, + 'vid' => $this->vocabulary->id(), + ]); + $term->save(); + $this->terms[] = $term; + } + + // Create a field. + $field_name = Unicode::strtolower($this->randomMachineName()); + $handler_settings = [ + 'target_bundles' => [ + $this->vocabulary->id() => $this->vocabulary->id(), + ], + 'auto_create' => FALSE, + ]; + $this->createEntityReferenceField('node', 'article', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); + + // Add some test nodes. + $this->createNode([ + 'type' => 'article', + $field_name => [$this->terms[0]->id(), $this->terms[1]->id()], + ]); + $this->createNode([ + 'type' => 'article', + $field_name => [$this->terms[0]->id(), $this->terms[2]->id()], + ]); + + // Use a test theme to convert multi-select elements into checkboxes. + \Drupal::service('theme_handler')->install(['views_test_checkboxes_theme']); + $this->config('system.theme') + ->set('default', 'views_test_checkboxes_theme') + ->save(); + + $this->drupalGet('test_exposed_form_checkboxes'); + + // Ensure that all results are displayed. + $rows = $this->xpath("//div[contains(@class, 'views-row')]"); + $this->assertEqual(count($rows), 8, 'All rows are displayed by default on the first page when no options are checked.'); + $this->assertNoText('An illegal choice has been detected. Please contact the site administrator.'); + + // Select one option and ensure we still have results. + $tid = $this->terms[0]->id(); + $this->drupalPostForm(NULL, ["tid[$tid]" => $tid], t('Apply')); + + // Ensure only nodes tagged with $tid are displayed. + $rows = $this->xpath("//div[contains(@class, 'views-row')]"); + $this->assertEqual(count($rows), 2, 'Correct rows are displayed when a tid is selected.'); + $this->assertNoText('An illegal choice has been detected. Please contact the site administrator.'); + } + +}