3 namespace Drupal\Tests\views\Functional\Plugin;
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;
14 * Tests exposed forms functionality.
18 class ExposedFormCheckboxesTest extends ViewTestBase {
20 use EntityReferenceTestTrait;
25 public static $testViews = ['test_exposed_form_checkboxes'];
30 public static $modules = ['node', 'views_ui', 'taxonomy'];
40 * Vocabulary for testing checkbox options.
42 * @var \Drupal\taxonomy\Entity\Vocabulary
49 protected function setUp($import_test_views = TRUE) {
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'],
61 $this->vocabulary = $vocabulary;
63 ViewTestData::createTestViews(self::class, ['views_test_config']);
64 $this->enableViewsTestModule();
66 // Create two content types.
67 $this->drupalCreateContentType(['type' => 'article']);
68 $this->drupalCreateContentType(['type' => 'page']);
70 // Create some random nodes: 5 articles, one page.
71 for ($i = 0; $i < 5; $i++) {
72 $this->drupalCreateNode(['type' => 'article']);
74 $this->drupalCreateNode(['type' => 'page']);
78 * Tests overriding the default render option with checkboxes.
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')
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;
93 $this->drupalGet('test_exposed_form_checkboxes');
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');
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.');
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.');
111 * Tests that "is all of" filters work with checkboxes.
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(),
121 $this->terms[] = $term;
125 $field_name = mb_strtolower($this->randomMachineName());
126 $handler_settings = [
127 'target_bundles' => [
128 $this->vocabulary->id() => $this->vocabulary->id(),
130 'auto_create' => FALSE,
132 $this->createEntityReferenceField('node', 'article', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
134 // Add some test nodes.
137 $field_name => [$this->terms[0]->id(), $this->terms[1]->id()],
141 $field_name => [$this->terms[0]->id(), $this->terms[2]->id()],
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')
150 $this->drupalGet('test_exposed_form_checkboxes');
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.');
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'));
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.');