View source
<?php
namespace Drupal\Tests\history\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
class HistoryTest extends BrowserTestBase {
use AssertPageCacheContextsAndTagsTrait;
protected static $modules = [
'node',
'history',
];
protected $defaultTheme = 'stark';
protected $user;
protected $testNode;
protected function setUp() : void {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$this->user = $this
->drupalCreateUser([
'create page content',
'edit own page content',
'access content',
]);
$this
->drupalLogin($this->user);
$this->testNode = $this
->drupalCreateNode([
'type' => 'page',
'uid' => $this->user
->id(),
]);
}
protected function getNodeReadTimestamps(array $node_ids) {
$http_client = $this
->getHttpClient();
$url = Url::fromRoute('history.get_last_node_view')
->setAbsolute()
->toString();
return $http_client
->request('POST', $url, [
'form_params' => [
'node_ids' => $node_ids,
],
'cookies' => $this
->getSessionCookies(),
'http_errors' => FALSE,
]);
}
protected function markNodeAsRead($node_id) {
$http_client = $this
->getHttpClient();
$url = Url::fromRoute('history.read_node', [
'node' => $node_id,
], [
'absolute' => TRUE,
])
->toString();
return $http_client
->request('POST', $url, [
'cookies' => $this
->getSessionCookies(),
'http_errors' => FALSE,
]);
}
public function testHistory() {
$nid = $this->testNode
->id();
$this
->drupalGet("node/add/page");
$this
->submitForm([
'title[0][value]' => 'Unsaved page',
], 'Preview');
$this
->assertArrayNotHasKey('ajaxPageState', $this
->getDrupalSettings());
$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.');
$this
->drupalGet('node/' . $nid);
$this
->assertCacheContext('user.roles:authenticated');
$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.');
$response = $this
->markNodeAsRead($nid);
$this
->assertEquals(200, $response
->getStatusCode());
$timestamp = Json::decode($response
->getBody());
$this
->assertIsNumeric($timestamp);
$response = $this
->getNodeReadTimestamps([
$nid,
]);
$this
->assertEquals(200, $response
->getStatusCode());
$json = Json::decode($response
->getBody());
$this
->assertSame([
1 => $timestamp,
], $json, 'The node has been read.');
$response = $this
->getNodeReadTimestamps([]);
$this
->assertEquals(404, $response
->getStatusCode());
$this
->drupalGet("node/{$nid}/edit");
$this
->submitForm([], 'Preview');
$this
->assertArrayNotHasKey('ajaxPageState', $this
->getDrupalSettings());
$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());
$rows = \Drupal::database()
->query('SELECT * FROM {history}')
->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);
}
}