3 namespace Drupal\Tests\views\Functional\Plugin;
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;
15 * Tests exposed forms functionality.
19 class ExposedFormCheckboxesTest extends ViewTestBase {
21 use EntityReferenceTestTrait;
26 public static $testViews = ['test_exposed_form_checkboxes'];
31 public static $modules = ['node', 'views_ui', 'taxonomy'];
41 * Vocabulary for testing checkbox options.
43 * @var \Drupal\taxonomy\Entity\Vocabulary
50 protected function setUp($import_test_views = TRUE) {
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'],
62 $this->vocabulary = $vocabulary;
64 ViewTestData::createTestViews(self::class, ['views_test_config']);
65 $this->enableViewsTestModule();
67 // Create two content types.
68 $this->drupalCreateContentType(['type' => 'article']);
69 $this->drupalCreateContentType(['type' => 'page']);
71 // Create some random nodes: 5 articles, one page.
72 for ($i = 0; $i < 5; $i++) {
73 $this->drupalCreateNode(['type' => 'article']);
75 $this->drupalCreateNode(['type' => 'page']);
79 * Tests overriding the default render option with checkboxes.
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')
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;
94 $this->drupalGet('test_exposed_form_checkboxes');
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');
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.');
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.');
112 * Tests that "is all of" filters work with checkboxes.
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(),
122 $this->terms[] = $term;
126 $field_name = Unicode::strtolower($this->randomMachineName());
127 $handler_settings = [
128 'target_bundles' => [
129 $this->vocabulary->id() => $this->vocabulary->id(),
131 'auto_create' => FALSE,
133 $this->createEntityReferenceField('node', 'article', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
135 // Add some test nodes.
138 $field_name => [$this->terms[0]->id(), $this->terms[1]->id()],
142 $field_name => [$this->terms[0]->id(), $this->terms[2]->id()],
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')
151 $this->drupalGet('test_exposed_form_checkboxes');
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.');
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'));
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.');