|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\datetime\Functional\Views; |
| 4 | + |
| 5 | +use Drupal\Core\Datetime\DrupalDateTime; |
| 6 | +use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem; |
| 7 | +use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface; |
| 8 | +use Drupal\field\Entity\FieldStorageConfig; |
| 9 | +use Drupal\field\Entity\FieldConfig; |
| 10 | +use Drupal\Tests\BrowserTestBase; |
| 11 | +use Drupal\views\Tests\ViewTestData; |
| 12 | + |
| 13 | +/** |
| 14 | + * Tests Views filters for datetime fields. |
| 15 | + * |
| 16 | + * @group datetime |
| 17 | + */ |
| 18 | +class FilterDateTest extends BrowserTestBase { |
| 19 | + |
| 20 | + /** |
| 21 | + * Name of the field. |
| 22 | + * |
| 23 | + * Note, this is used in the default test view. |
| 24 | + * |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + protected $fieldName = 'field_date'; |
| 28 | + |
| 29 | + /** |
| 30 | + * Nodes to test. |
| 31 | + * |
| 32 | + * @var \Drupal\node\NodeInterface[] |
| 33 | + */ |
| 34 | + protected $nodes = []; |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritdoc} |
| 38 | + */ |
| 39 | + public static $modules = [ |
| 40 | + 'datetime', |
| 41 | + 'datetime_test', |
| 42 | + 'node', |
| 43 | + 'views', |
| 44 | + 'views_ui', |
| 45 | + ]; |
| 46 | + |
| 47 | + /** |
| 48 | + * {@inheritdoc} |
| 49 | + */ |
| 50 | + public static $testViews = ['test_filter_datetime']; |
| 51 | + |
| 52 | + /** |
| 53 | + * {@inheritdoc} |
| 54 | + * |
| 55 | + * Create nodes with relative dates of yesterday, today, and tomorrow. |
| 56 | + */ |
| 57 | + protected function setUp() { |
| 58 | + parent::setUp(); |
| 59 | + |
| 60 | + $now = \Drupal::time()->getRequestTime(); |
| 61 | + |
| 62 | + $admin_user = $this->drupalCreateUser(['administer views']); |
| 63 | + $this->drupalLogin($admin_user); |
| 64 | + |
| 65 | + $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']); |
| 66 | + |
| 67 | + // Add a date field to page nodes. |
| 68 | + $fieldStorage = FieldStorageConfig::create([ |
| 69 | + 'field_name' => $this->fieldName, |
| 70 | + 'entity_type' => 'node', |
| 71 | + 'type' => 'datetime', |
| 72 | + 'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME], |
| 73 | + ]); |
| 74 | + $fieldStorage->save(); |
| 75 | + $field = FieldConfig::create([ |
| 76 | + 'field_storage' => $fieldStorage, |
| 77 | + 'bundle' => 'page', |
| 78 | + 'required' => TRUE, |
| 79 | + ]); |
| 80 | + $field->save(); |
| 81 | + |
| 82 | + // Create some nodes. |
| 83 | + $dates = [ |
| 84 | + // Tomorrow. |
| 85 | + DrupalDateTime::createFromTimestamp($now + 86400, DateTimeItemInterface::STORAGE_TIMEZONE)->format(DateTimeItemInterface::DATE_STORAGE_FORMAT), |
| 86 | + // Today. |
| 87 | + DrupalDateTime::createFromTimestamp($now, DateTimeItemInterface::STORAGE_TIMEZONE)->format(DateTimeItemInterface::DATE_STORAGE_FORMAT), |
| 88 | + // Yesterday. |
| 89 | + DrupalDateTime::createFromTimestamp($now - 86400, DateTimeItemInterface::STORAGE_TIMEZONE)->format(DateTimeItemInterface::DATE_STORAGE_FORMAT), |
| 90 | + ]; |
| 91 | + |
| 92 | + $this->nodes = []; |
| 93 | + foreach ($dates as $date) { |
| 94 | + $this->nodes[] = $this->drupalCreateNode([ |
| 95 | + $this->fieldName => [ |
| 96 | + 'value' => $date, |
| 97 | + ], |
| 98 | + ]); |
| 99 | + } |
| 100 | + // Add a node where the date field is empty. |
| 101 | + $this->nodes[] = $this->drupalCreateNode(); |
| 102 | + |
| 103 | + // Views needs to be aware of the new field. |
| 104 | + $this->container->get('views.views_data')->clear(); |
| 105 | + |
| 106 | + // Load test views. |
| 107 | + ViewTestData::createTestViews(get_class($this), ['datetime_test']); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Tests exposed grouped filters. |
| 112 | + */ |
| 113 | + public function testExposedGroupedFilters() { |
| 114 | + // Expose the empty and not empty operators in a grouped filter. |
| 115 | + $this->drupalPostForm('admin/structure/views/nojs/handler/test_filter_datetime/default/filter/' . $this->fieldName . '_value', [], t('Expose filter')); |
| 116 | + $this->drupalPostForm(NULL, [], 'Grouped filters'); |
| 117 | + |
| 118 | + $edit = []; |
| 119 | + $edit['options[group_info][group_items][1][title]'] = 'empty'; |
| 120 | + $edit['options[group_info][group_items][1][operator]'] = 'empty'; |
| 121 | + $edit['options[group_info][group_items][2][title]'] = 'not empty'; |
| 122 | + $edit['options[group_info][group_items][2][operator]'] = 'not empty'; |
| 123 | + |
| 124 | + $this->drupalPostForm(NULL, $edit, 'Apply'); |
| 125 | + |
| 126 | + // Test that the exposed filter works as expected. |
| 127 | + $path = 'test_filter_datetime-path'; |
| 128 | + $this->drupalPostForm('admin/structure/views/view/test_filter_datetime/edit', [], 'Add Page'); |
| 129 | + $this->drupalPostForm('admin/structure/views/nojs/display/test_filter_datetime/page_1/path', ['path' => $path], 'Apply'); |
| 130 | + $this->drupalPostForm(NULL, [], t('Save')); |
| 131 | + |
| 132 | + $this->drupalGet($path); |
| 133 | + |
| 134 | + // Filter the Preview by 'empty'. |
| 135 | + $this->getSession()->getPage()->findField($this->fieldName . '_value')->selectOption(1); |
| 136 | + $this->getSession()->getPage()->pressButton('Apply'); |
| 137 | + $results = $this->cssSelect('.view-content .field-content'); |
| 138 | + $this->assertEquals(1, count($results)); |
| 139 | + |
| 140 | + // Filter the Preview by 'not empty'. |
| 141 | + $this->getSession()->getPage()->findField($this->fieldName . '_value')->selectOption(2); |
| 142 | + $this->getSession()->getPage()->pressButton('Apply'); |
| 143 | + $results = $this->cssSelect('.view-content .field-content'); |
| 144 | + $this->assertEquals(3, count($results)); |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments