public function HistoryTest::testHistory in Drupal 8
Same name and namespace in other branches
- 9 core/modules/history/tests/src/Functional/HistoryTest.php \Drupal\Tests\history\Functional\HistoryTest::testHistory()
Verifies that the history endpoints work.
File
- core/
modules/ history/ tests/ src/ Functional/ HistoryTest.php, line 103
Class
- HistoryTest
- Tests the History endpoints.
Namespace
Drupal\Tests\history\FunctionalCode
public function testHistory() {
$nid = $this->testNode
->id();
// Retrieve "last read" timestamp for test node, for the current user.
$response = $this
->getNodeReadTimestamps([
$nid,
]);
$this
->assertEquals(200, $response
->getStatusCode());
$json = Json::decode($response
->getBody());
$this
->assertIdentical([
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
->assertContains('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
->assertEquals(200, $response
->getStatusCode());
$timestamp = Json::decode($response
->getBody());
$this
->assertIsNumeric($timestamp);
// Retrieve "last read" timestamp for test node, for the current user.
$response = $this
->getNodeReadTimestamps([
$nid,
]);
$this
->assertEquals(200, $response
->getStatusCode());
$json = Json::decode($response
->getBody());
$this
->assertIdentical([
1 => $timestamp,
], $json, 'The node has been read.');
// Failing to specify node IDs for the first endpoint should return a 404.
$response = $this
->getNodeReadTimestamps([]);
$this
->assertEquals(404, $response
->getStatusCode());
// Accessing either endpoint as the anonymous user should return a 403.
$this
->drupalLogout();
$response = $this
->getNodeReadTimestamps([
$nid,
]);
$this
->assertEquals(403, $response
->getStatusCode());
$response = $this
->getNodeReadTimestamps([]);
$this
->assertEquals(403, $response
->getStatusCode());
$response = $this
->markNodeAsRead($nid);
$this
->assertEquals(403, $response
->getStatusCode());
}