View source
<?php
namespace Drupal\Tests\jsonapi\Kernel\Revisions;
use Drupal\Core\Http\Exception\CacheableBadRequestHttpException;
use Drupal\Core\Http\Exception\CacheableNotFoundHttpException;
use Drupal\jsonapi\Revisions\VersionById;
use Drupal\jsonapi\Revisions\VersionByRel;
use Drupal\jsonapi\Revisions\VersionNegotiator;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\jsonapi\Kernel\JsonapiKernelTestBase;
use Drupal\user\Entity\User;
class VersionNegotiatorTest extends JsonapiKernelTestBase {
protected $user;
protected $node;
protected $nodePreviousRevisionId;
protected $versionNegotiator;
protected $node2;
public static $modules = [
'node',
'field',
'jsonapi',
'serialization',
'system',
'user',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installSchema('system', [
'sequences',
]);
$this
->installSchema('node', [
'node_access',
]);
$this
->installSchema('user', [
'users_data',
]);
$type = NodeType::create([
'type' => 'dummy',
'new_revision' => TRUE,
]);
$type
->save();
$this->user = User::create([
'name' => 'user1',
'mail' => 'user@localhost',
'status' => 1,
]);
$this->user
->save();
$this->node = Node::create([
'title' => 'dummy_title',
'type' => 'dummy',
'uid' => $this->user
->id(),
]);
$this->node
->save();
$this->nodePreviousRevisionId = $this->node
->getRevisionId();
$this->node
->setNewRevision();
$this->node
->setTitle('revised_dummy_title');
$this->node
->save();
$this->node2 = Node::create([
'type' => 'dummy',
'title' => 'Another test node',
'uid' => $this->user
->id(),
]);
$this->node2
->save();
$entity_type_manager = \Drupal::entityTypeManager();
$version_negotiator = new VersionNegotiator();
$version_negotiator
->addVersionNegotiator(new VersionById($entity_type_manager), 'id');
$version_negotiator
->addVersionNegotiator(new VersionByRel($entity_type_manager), 'rel');
$this->versionNegotiator = $version_negotiator;
}
public function testOldRevision() {
$revision = $this->versionNegotiator
->getRevision($this->node, 'id:' . $this->nodePreviousRevisionId);
$this
->assertEquals($this->node
->id(), $revision
->id());
$this
->assertEquals($this->nodePreviousRevisionId, $revision
->getRevisionId());
}
public function testInvalidRevisionId() {
$this
->expectException(CacheableNotFoundHttpException::class);
$this
->expectExceptionMessage(sprintf('The requested version, identified by `id:%s`, could not be found.', $this->node2
->getRevisionId()));
$this->versionNegotiator
->getRevision($this->node, 'id:' . $this->node2
->getRevisionId());
}
public function testLatestVersion() {
$revision = $this->versionNegotiator
->getRevision($this->node, 'rel:' . VersionByRel::LATEST_VERSION);
$this
->assertEquals($this->node
->id(), $revision
->id());
$this
->assertEquals($this->node
->getRevisionId(), $revision
->getRevisionId());
}
public function testCurrentVersion() {
$revision = $this->versionNegotiator
->getRevision($this->node, 'rel:' . VersionByRel::WORKING_COPY);
$this
->assertEquals($this->node
->id(), $revision
->id());
$this
->assertEquals($this->node
->id(), $revision
->id());
$this
->assertEquals($this->node
->getRevisionId(), $revision
->getRevisionId());
}
public function testInvalidRevisionRel() {
$this
->expectException(CacheableBadRequestHttpException::class);
$this
->expectExceptionMessage('An invalid resource version identifier, `rel:erroneous-revision-name`, was provided.');
$this->versionNegotiator
->getRevision($this->node, 'rel:erroneous-revision-name');
}
}