function HistoryTest::testHistory

Same name in other branches
  1. 9 core/modules/history/tests/src/Functional/HistoryTest.php \Drupal\Tests\history\Functional\HistoryTest::testHistory()
  2. 8.9.x core/modules/history/tests/src/Functional/HistoryTest.php \Drupal\Tests\history\Functional\HistoryTest::testHistory()
  3. 10 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 108

Class

HistoryTest
Tests the History endpoints.

Namespace

Drupal\Tests\history\Functional

Code

public function testHistory() : void {
    $nid = $this->testNode
        ->id();
    // Verify that previews of new entities do not create the history.
    $this->drupalGet("node/add/page");
    $this->submitForm([
        'title[0][value]' => 'Unsaved page',
    ], 'Preview');
    $this->assertArrayNotHasKey('ajaxPageState', $this->getDrupalSettings());
    // 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->assertSame([
        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->assertEquals([
        $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->assertSame([
        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());
    // Verify that previews of existing entities do not update the history.
    $this->drupalGet("node/{$nid}/edit");
    $this->submitForm([], 'Preview');
    $this->assertArrayNotHasKey('ajaxPageState', $this->getDrupalSettings());
    // 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());
    // Additional check to ensure that we did not forget to verify anything.
    $rows = \Drupal::database()->select('history')
        ->fields('history', [
        'nid',
        'uid',
        'timestamp',
    ])
        ->execute()
        ->fetchAll();
    $this->assertCount(1, $rows);
    $this->assertSame($this->user
        ->id(), $rows[0]->uid);
    $this->assertSame($this->testNode
        ->id(), $rows[0]->nid);
    $this->assertSame($timestamp, (int) $rows[0]->timestamp);
}

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