View source
<?php
namespace Drupal\Tests\views\Functional\Plugin;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;
class ExposedFormCheckboxesTest extends ViewTestBase {
use EntityReferenceTestTrait;
public static $testViews = [
'test_exposed_form_checkboxes',
];
protected static $modules = [
'node',
'views_ui',
'taxonomy',
];
protected $defaultTheme = 'stark';
public $terms = [];
public $vocabulary;
protected function setUp($import_test_views = TRUE) : void {
parent::setUp(FALSE);
$vocabulary = Vocabulary::create([
'name' => 'test_exposed_checkboxes',
'vid' => 'test_exposed_checkboxes',
'nodes' => [
'article' => 'article',
],
]);
$vocabulary
->save();
$this->vocabulary = $vocabulary;
ViewTestData::createTestViews(self::class, [
'views_test_config',
]);
$this
->enableViewsTestModule();
$this
->drupalCreateContentType([
'type' => 'article',
]);
$this
->drupalCreateContentType([
'type' => 'page',
]);
for ($i = 0; $i < 5; $i++) {
$this
->drupalCreateNode([
'type' => 'article',
]);
}
$this
->drupalCreateNode([
'type' => 'page',
]);
}
public function testExposedFormRenderCheckboxes() {
\Drupal::service('theme_installer')
->install([
'views_test_checkboxes_theme',
]);
$this
->config('system.theme')
->set('default', 'views_test_checkboxes_theme')
->save();
$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');
$this
->assertSame('checkbox', $this
->assertSession()
->fieldExists('type[article]')
->getAttribute('type'));
$this
->assertSame('checkbox', $this
->assertSession()
->fieldExists('type[page]')
->getAttribute('type'));
$this
->assertSession()
->elementsCount('xpath', "//div[contains(@class, 'views-row')]", 5);
$this
->clickLink('Page 2');
$this
->assertSession()
->elementsCount('xpath', "//div[contains(@class, 'views-row')]", 1);
$this
->assertSession()
->pageTextNotContains('An illegal choice has been detected. Please contact the site administrator.');
}
public function testExposedIsAllOfFilter() {
foreach ([
'Term 1',
'Term 2',
'Term 3',
] as $term_name) {
$term = Term::create([
'name' => $term_name,
'vid' => $this->vocabulary
->id(),
]);
$term
->save();
$this->terms[] = $term;
}
$field_name = mb_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);
$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(),
],
]);
\Drupal::service('theme_installer')
->install([
'views_test_checkboxes_theme',
]);
$this
->config('system.theme')
->set('default', 'views_test_checkboxes_theme')
->save();
$this
->drupalGet('test_exposed_form_checkboxes');
$this
->assertSession()
->elementsCount('xpath', "//div[contains(@class, 'views-row')]", 8);
$this
->assertSession()
->pageTextNotContains('An illegal choice has been detected. Please contact the site administrator.');
$tid = $this->terms[0]
->id();
$this
->submitForm([
"tid[{$tid}]" => $tid,
], 'Apply');
$this
->assertSession()
->elementsCount('xpath', "//div[contains(@class, 'views-row')]", 2);
$this
->assertSession()
->pageTextNotContains('An illegal choice has been detected. Please contact the site administrator.');
}
}