You are here

public function FrontPageTest::testFrontPage in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/node/tests/src/Functional/Views/FrontPageTest.php \Drupal\Tests\node\Functional\Views\FrontPageTest::testFrontPage()

Tests the frontpage.

File

core/modules/node/tests/src/Functional/Views/FrontPageTest.php, line 61

Class

FrontPageTest
Tests the default frontpage provided by views.

Namespace

Drupal\Tests\node\Functional\Views

Code

public function testFrontPage() {
  $site_name = $this
    ->randomMachineName();
  $this
    ->config('system.site')
    ->set('name', $site_name)
    ->save();
  $view = Views::getView('frontpage');

  // Tests \Drupal\node\Plugin\views\row\RssPluginBase::calculateDependencies().
  $expected = [
    'config' => [
      'core.entity_view_mode.node.rss',
      'core.entity_view_mode.node.teaser',
    ],
    'module' => [
      'node',
      'user',
    ],
  ];
  $this
    ->assertIdentical($expected, $view
    ->getDependencies());
  $view
    ->setDisplay('page_1');
  $this
    ->executeView($view);
  $view
    ->preview();
  $this
    ->assertEqual($view
    ->getTitle(), new FormattableMarkup('Welcome to @site_name', [
    '@site_name' => $site_name,
  ]), 'The welcome title is used for the empty view.');
  $view
    ->destroy();

  // Create some nodes on the frontpage view. Add more than 10 nodes in order
  // to enable paging.
  $expected = [];
  for ($i = 0; $i < 20; $i++) {
    $values = [];
    $values['type'] = 'article';
    $values['title'] = $this
      ->randomMachineName();
    $values['promote'] = TRUE;
    $values['status'] = TRUE;

    // Test descending sort order.
    $values['created'] = REQUEST_TIME - $i;

    // Test the sticky order.
    if ($i == 5) {
      $values['sticky'] = TRUE;
      $node = $this->nodeStorage
        ->create($values);
      $node
        ->save();

      // Put the sticky on at the front.
      array_unshift($expected, [
        'nid' => $node
          ->id(),
      ]);
    }
    else {
      $values['sticky'] = FALSE;
      $node = $this->nodeStorage
        ->create($values);
      $node
        ->save();
      array_push($expected, [
        'nid' => $node
          ->id(),
      ]);
    }
  }

  // Create some nodes which aren't on the frontpage, either because they
  // aren't promoted or because they aren't published.
  $not_expected_nids = [];
  $values = [];
  $values['type'] = 'article';
  $values['title'] = $this
    ->randomMachineName();
  $values['status'] = TRUE;
  $values['promote'] = FALSE;
  $node = $this->nodeStorage
    ->create($values);
  $node
    ->save();
  $not_expected_nids[] = $node
    ->id();
  $values['promote'] = TRUE;
  $values['status'] = FALSE;
  $values['title'] = $this
    ->randomMachineName();
  $node = $this->nodeStorage
    ->create($values);
  $node
    ->save();
  $not_expected_nids[] = $node
    ->id();
  $values['promote'] = TRUE;
  $values['sticky'] = TRUE;
  $values['status'] = FALSE;
  $values['title'] = $this
    ->randomMachineName();
  $node = $this->nodeStorage
    ->create($values);
  $node
    ->save();
  $not_expected_nids[] = $node
    ->id();
  $column_map = [
    'nid' => 'nid',
  ];
  $view
    ->setDisplay('page_1');
  $this
    ->executeView($view);
  $this
    ->assertIdenticalResultset($view, array_slice($expected, 0, 10), $column_map, 'Ensure that the right nodes are displayed on the frontpage.');
  $this
    ->assertNotInResultSet($view, $not_expected_nids, 'Ensure no unexpected node is in the result.');
  $view
    ->destroy();
  $view
    ->setDisplay('page_1');
  $view
    ->setCurrentPage(1);
  $this
    ->executeView($view);
  $this
    ->assertIdenticalResultset($view, array_slice($expected, 10, 10), $column_map, 'Ensure that the right nodes are displayed on second page of the frontpage.');
  $this
    ->assertNotInResultSet($view, $not_expected_nids, 'Ensure no unexpected node is in the result.');
  $view
    ->destroy();
}