class DateFilterTest in Drupal 10
Same name and namespace in other branches
- 8 core/modules/datetime/tests/src/Functional/DateFilterTest.php \Drupal\Tests\datetime\Functional\DateFilterTest
- 9 core/modules/datetime/tests/src/Functional/DateFilterTest.php \Drupal\Tests\datetime\Functional\DateFilterTest
Test exposed datetime filters functionality.
@group views
Hierarchy
- class \Drupal\Tests\BrowserTestBase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, FunctionalTestSetupTrait, TestSetupTrait, BlockCreationTrait, ConfigTestTrait, ExtensionListTestTrait, ContentTypeCreationTrait, NodeCreationTrait, RandomGeneratorTrait, TestRequirementsTrait, PhpUnitWarnings, UiHelperTrait, UserCreationTrait, XdebugRequestTrait
- class \Drupal\Tests\views\Functional\ViewTestBase uses ViewResultAssertionTrait
- class \Drupal\Tests\datetime\Functional\DateFilterTest
- class \Drupal\Tests\views\Functional\ViewTestBase uses ViewResultAssertionTrait
Expanded class hierarchy of DateFilterTest
See also
\Drupal\datetime\Plugin\views\filter\Date
File
- core/
modules/ datetime/ tests/ src/ Functional/ DateFilterTest.php, line 17
Namespace
Drupal\Tests\datetime\FunctionalView source
class DateFilterTest extends ViewTestBase {
/**
* A user with permission to administer views.
*
* @var \Drupal\user\Entity\User
*/
protected $adminUser;
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = [
'test_exposed_filter_datetime',
];
/**
* {@inheritdoc}
*/
protected static $modules = [
'datetime_test',
'node',
'datetime',
'field',
'views_ui',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE, $modules = [
'datetime_test',
]) : void {
parent::setUp($import_test_views, $modules);
// Add a date field to page nodes.
$node_type = NodeType::create([
'type' => 'page',
'name' => 'page',
]);
$node_type
->save();
$fieldStorage = FieldStorageConfig::create([
'field_name' => 'field_date',
'entity_type' => 'node',
'type' => 'datetime',
'settings' => [
'datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME,
],
]);
$fieldStorage
->save();
$field = FieldConfig::create([
'field_storage' => $fieldStorage,
'bundle' => 'page',
'required' => TRUE,
]);
$field
->save();
$this->adminUser = $this
->drupalCreateUser([
'administer views',
]);
$this
->drupalLogin($this->adminUser);
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
}
/**
* Tests the limit of the expose operator functionality.
*/
public function testLimitExposedOperators() {
$this
->drupalGet('test_exposed_filter_datetime');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->optionExists('edit-field-date-value-op', '=');
$this
->assertSession()
->optionNotExists('edit-field-date-value-op', '>');
$this
->assertSession()
->optionNotExists('edit-field-date-value-op', '>=');
// Because there are not operators that use the min and max fields, those
// fields should not be in the exposed form.
$this
->assertSession()
->fieldExists('edit-field-date-value-value');
$this
->assertSession()
->fieldNotExists('edit-field-date-value-min');
$this
->assertSession()
->fieldNotExists('edit-field-date-value-max');
$edit = [];
$edit['options[operator]'] = '>';
$edit['options[expose][operator_list][]'] = [
'>',
'>=',
'between',
];
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_filter_datetime/default/filter/field_date_value');
$this
->submitForm($edit, 'Apply');
$this
->drupalGet('admin/structure/views/view/test_exposed_filter_datetime/edit/default');
$this
->submitForm([], 'Save');
$this
->drupalGet('test_exposed_filter_datetime');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->optionNotExists('edit-field-date-value-op', '<');
$this
->assertSession()
->optionNotExists('edit-field-date-value-op', '<=');
$this
->assertSession()
->optionNotExists('edit-field-date-value-op', '=');
$this
->assertSession()
->optionExists('edit-field-date-value-op', '>');
$this
->assertSession()
->optionExists('edit-field-date-value-op', '>=');
$this
->assertSession()
->fieldExists('edit-field-date-value-value');
$this
->assertSession()
->fieldExists('edit-field-date-value-min');
$this
->assertSession()
->fieldExists('edit-field-date-value-max');
// Set the default to an excluded operator.
$edit = [];
$edit['options[operator]'] = '=';
$edit['options[expose][operator_list][]'] = [
'<',
'>',
];
$this
->drupalGet('admin/structure/views/nojs/handler/test_exposed_filter_datetime/default/filter/field_date_value');
$this
->submitForm($edit, 'Apply');
$this
->assertSession()
->pageTextContains('You selected the "Is equal to" operator as the default value but is not included in the list of limited operators.');
}
}