function DateArgumentDefaultTest::testArgumentDefaultNodeChanged

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

See also

\Drupal\node\Plugin\views\argument_default\NodeChanged

File

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

Class

DateArgumentDefaultTest
Tests pluggable argument_default for views.

Namespace

Drupal\Tests\node\Functional\Views

Code

public function testArgumentDefaultNodeChanged() : void {
  $this->drupalPlaceBlock('views_block:test_argument_node_date-block_2', [
    'label' => 'test_argument_node_date-block_2:2',
  ]);
  $assert = $this->assertSession();
  // Assert that only nodes with the same changed 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_2');
  $display['display_options']['arguments']['changed']['plugin_id'] = 'date_year_month';
  $display['display_options']['arguments']['changed']['field'] = 'changed_year_month';
  $display['display_options']['arguments']['changed']['id'] = 'changed_year_month';
  $view->save();
  // Test that the nodes with a changed 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']['changed']['field'] = 'title';
  $view->save();
  // Test that the nodes with a title in the same changed 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_2');
  $view->initHandlers();
  $this->assertFalse($view->argument['changed']
    ->getDefaultArgument());
}

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