You are here

function HistoryTest::testHistory in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/history/src/Tests/HistoryTest.php \Drupal\history\Tests\HistoryTest::testHistory()

Verifies that the history endpoints work.

File

core/modules/history/src/Tests/HistoryTest.php, line 109
Contains \Drupal\history\Tests\HistoryTest.

Class

HistoryTest
Tests the History endpoints.

Namespace

Drupal\history\Tests

Code

function testHistory() {
  $nid = $this->testNode
    ->id();

  // Retrieve "last read" timestamp for test node, for the current user.
  $response = $this
    ->getNodeReadTimestamps(array(
    $nid,
  ));
  $this
    ->assertResponse(200);
  $json = Json::decode($response);
  $this
    ->assertIdentical(array(
    1 => 0,
  ), $json, 'The node has not yet been read.');

  // View the node.
  $this
    ->drupalGet('node/' . $nid);
  $this
    ->assertCacheContext('user.roles:authenticated');

  // JavaScript present to record the node read.
  $settings = $this
    ->getDrupalSettings();
  $libraries = explode(',', $settings['ajaxPageState']['libraries']);
  $this
    ->assertTrue(in_array('history/mark-as-read', $libraries), 'history/mark-as-read library is present.');
  $this
    ->assertEqual([
    $nid => TRUE,
  ], $settings['history']['nodesToMarkAsRead'], 'drupalSettings to mark node as read are present.');

  // Simulate JavaScript: perform HTTP request to mark node as read.
  $response = $this
    ->markNodeAsRead($nid);
  $this
    ->assertResponse(200);
  $timestamp = Json::decode($response);
  $this
    ->assertTrue(is_numeric($timestamp), 'Node has been marked as read. Timestamp received.');

  // Retrieve "last read" timestamp for test node, for the current user.
  $response = $this
    ->getNodeReadTimestamps(array(
    $nid,
  ));
  $this
    ->assertResponse(200);
  $json = Json::decode($response);
  $this
    ->assertIdentical(array(
    1 => $timestamp,
  ), $json, 'The node has been read.');

  // Failing to specify node IDs for the first endpoint should return a 404.
  $this
    ->getNodeReadTimestamps(array());
  $this
    ->assertResponse(404);

  // Accessing either endpoint as the anonymous user should return a 403.
  $this
    ->drupalLogout();
  $this
    ->getNodeReadTimestamps(array(
    $nid,
  ));
  $this
    ->assertResponse(403);
  $this
    ->getNodeReadTimestamps(array());
  $this
    ->assertResponse(403);
  $this
    ->markNodeAsRead($nid);
  $this
    ->assertResponse(403);
}