function DateArgumentDefaultTest::testArgumentDefaultNodeCreated

Test the 'Current node created time' default argument handler.

See also

\Drupal\node\Plugin\views\argument_default\NodeCreated

File

core/modules/node/tests/src/Functional/Views/DateArgumentDefaultTest.php, line 104

Class

DateArgumentDefaultTest
Tests pluggable argument_default for views.

Namespace

Drupal\Tests\node\Functional\Views

Code

public function testArgumentDefaultNodeCreated() : void {
  $this->drupalPlaceBlock('views_block:test_argument_node_date-block_1', [
    'label' => 'test_argument_node_date-block_1:1',
  ]);
  $assert = $this->assertSession();
  // Assert that only nodes with the same creation time as the current node
  // are shown in the block.
  $this->drupalGet($this->currentNode
    ->toUrl());
  $assert->pageTextContains($this->currentNode
    ->getTitle());
  $assert->pageTextContains($this->sameTimeNode
    ->getTitle());
  $assert->pageTextNotContains($this->otherTimeNode
    ->getTitle());
  // Update the View to use the Y-m format argument.
  $view = View::load('test_argument_node_date');
  $display =& $view->getDisplay('block_1');
  $display['display_options']['arguments']['created']['plugin_id'] = 'date_year_month';
  $display['display_options']['arguments']['created']['field'] = 'created_year_month';
  $display['display_options']['arguments']['created']['id'] = 'created_year_month';
  $view->save();
  // Test that the nodes with a create date in the same month are shown.
  $this->drupalGet($this->fixedTimeNode
    ->toUrl());
  $assert->pageTextContains($this->fixedTimeNode
    ->getTitle());
  $assert->pageTextContains($this->sameMonthNode
    ->getTitle());
  $assert->pageTextNotContains($this->currentNode
    ->getTitle());
  // Update the View to use the date format argument for non-date field.
  $display['display_options']['arguments']['created']['field'] = 'title';
  $view->save();
  // Test that the nodes with a title in the same create date are shown.
  $nodeTitleFixed = $this->drupalCreateNode([
    'type' => 'page',
    'title' => '1975-05-18',
  ]);
  $this->drupalGet($this->fixedTimeNode
    ->toUrl());
  $assert->pageTextContains($nodeTitleFixed->getTitle());
  $assert->pageTextNotContains($this->sameMonthNode
    ->getTitle());
  // Test the getDefaultArgument() outside of node page.
  $view = Views::getView('test_argument_node_date');
  $view->setDisplay('block_1');
  $view->initHandlers();
  $this->assertFalse($view->argument['created']
    ->getDefaultArgument());
}

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