function FilterDateTest::setUp

Same name in this branch
  1. 11.x core/modules/views/tests/src/Functional/Handler/FilterDateTest.php \Drupal\Tests\views\Functional\Handler\FilterDateTest::setUp()
  2. 11.x core/modules/datetime_range/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime_range\Kernel\Views\FilterDateTest::setUp()
  3. 11.x core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime\Kernel\Views\FilterDateTest::setUp()
Same name in other branches
  1. 9 core/modules/views/tests/src/Functional/Handler/FilterDateTest.php \Drupal\Tests\views\Functional\Handler\FilterDateTest::setUp()
  2. 9 core/modules/datetime_range/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime_range\Kernel\Views\FilterDateTest::setUp()
  3. 9 core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime\Kernel\Views\FilterDateTest::setUp()
  4. 9 core/modules/datetime/tests/src/Functional/Views/FilterDateTest.php \Drupal\Tests\datetime\Functional\Views\FilterDateTest::setUp()
  5. 8.9.x core/modules/views/tests/src/Functional/Handler/FilterDateTest.php \Drupal\Tests\views\Functional\Handler\FilterDateTest::setUp()
  6. 8.9.x core/modules/datetime_range/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime_range\Kernel\Views\FilterDateTest::setUp()
  7. 8.9.x core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime\Kernel\Views\FilterDateTest::setUp()
  8. 8.9.x core/modules/datetime/tests/src/Functional/Views/FilterDateTest.php \Drupal\Tests\datetime\Functional\Views\FilterDateTest::setUp()
  9. 10 core/modules/views/tests/src/Functional/Handler/FilterDateTest.php \Drupal\Tests\views\Functional\Handler\FilterDateTest::setUp()
  10. 10 core/modules/datetime_range/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime_range\Kernel\Views\FilterDateTest::setUp()
  11. 10 core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php \Drupal\Tests\datetime\Kernel\Views\FilterDateTest::setUp()
  12. 10 core/modules/datetime/tests/src/Functional/Views/FilterDateTest.php \Drupal\Tests\datetime\Functional\Views\FilterDateTest::setUp()

Create nodes with relative dates of yesterday, today, and tomorrow.

Overrides ViewTestBase::setUp

File

core/modules/datetime/tests/src/Functional/Views/FilterDateTest.php, line 70

Class

FilterDateTest
Tests Views filters for datetime fields.

Namespace

Drupal\Tests\datetime\Functional\Views

Code

protected function setUp($import_test_views = TRUE, $modules = [
    'views_test_config',
]) : void {
    parent::setUp($import_test_views, $modules);
    $now = \Drupal::time()->getRequestTime();
    $admin_user = $this->drupalCreateUser([
        'administer views',
    ]);
    $this->drupalLogin($admin_user);
    $this->drupalCreateContentType([
        'type' => 'page',
        'name' => 'Basic page',
    ]);
    // Add a date field to page nodes.
    $fieldStorage = FieldStorageConfig::create([
        'field_name' => $this->fieldName,
        '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();
    // Create some nodes.
    $this->dates = [
        // Tomorrow.
DrupalDateTime::createFromTimestamp($now + 86400, DateTimeItemInterface::STORAGE_TIMEZONE)->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
        // Today.
DrupalDateTime::createFromTimestamp($now, DateTimeItemInterface::STORAGE_TIMEZONE)->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
        // Yesterday.
DrupalDateTime::createFromTimestamp($now - 86400, DateTimeItemInterface::STORAGE_TIMEZONE)->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
    ];
    $this->nodes = [];
    foreach ($this->dates as $date) {
        $this->nodes[] = $this->drupalCreateNode([
            $this->fieldName => [
                'value' => $date,
            ],
        ]);
    }
    // Add a node where the date field is empty.
    $this->nodes[] = $this->drupalCreateNode();
    // Views needs to be aware of the new field.
    $this->container
        ->get('views.views_data')
        ->clear();
    // Load test views.
    ViewTestData::createTestViews(static::class, [
        'datetime_test',
    ]);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.