diff --git a/src/Plugin/VersionNegotiation/VersionByRel.php b/src/Plugin/VersionNegotiation/VersionByRel.php index 92b72de..ebc3d4d 100644 --- a/src/Plugin/VersionNegotiation/VersionByRel.php +++ b/src/Plugin/VersionNegotiation/VersionByRel.php @@ -46,10 +46,10 @@ class VersionByRel extends PluginNegotiationBase implements ContainerFactoryPlug */ protected function getRevisionId(EntityInterface $entity, $version_argument) { assert($entity instanceof RevisionableInterface); - $entity_storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId()); switch ($version_argument) { case static::WORKING_COPY: /* @var \Drupal\Core\Entity\RevisionableStorageInterface $entity_storage */ + $entity_storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId()); return static::ensureVersionFound($entity_storage->getLatestRevisionId($entity->id())); case static::LATEST_VERSION: diff --git a/src/Revisions/PluginNegotiationBase.php b/src/Revisions/PluginNegotiationBase.php index 9a56a70..fd3017b 100644 --- a/src/Revisions/PluginNegotiationBase.php +++ b/src/Revisions/PluginNegotiationBase.php @@ -87,9 +87,9 @@ abstract class PluginNegotiationBase extends PluginBase implements ContainerFact */ protected function loadRevision(EntityInterface $entity, $revision_id) { $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId()); - $revision = static::ensureVersionFound($storage->loadRevision($this->getRevisionId($entity, $revision_id))); + $revision = static::ensureVersionFound($storage->loadRevision($revision_id)); if ($revision->id() !== $entity->id()) { - throw new InvalidVersionIdentifierException('The requested resource does not have a version with the given ID.'); + throw new VersionNotFoundException(sprintf('The requested resource does not have a version with ID %s.', $revision_id)); } return $revision; } diff --git a/tests/src/Kernel/Plugin/VersionNegotiation/VersionByIdTest.php b/tests/src/Kernel/Plugin/VersionNegotiation/VersionByIdTest.php index 936ea01..1d9455d 100644 --- a/tests/src/Kernel/Plugin/VersionNegotiation/VersionByIdTest.php +++ b/tests/src/Kernel/Plugin/VersionNegotiation/VersionByIdTest.php @@ -2,6 +2,8 @@ namespace Drupal\Tests\jsonapi\Kernel\Plugin\VersionNegotiation; +use Drupal\jsonapi\Revisions\VersionNotFoundException; + /** * @coversDefaultClass \Drupal\jsonapi\Plugin\VersionNegotiation\VersionById * @group jsonapi @@ -14,7 +16,7 @@ class VersionByIdTest extends VersionNegotiationTestBase { * @covers ::getRevision */ public function testOldRevision() { - $plugin = $this->buildIdNegotationPlugin('id'); + $plugin = $this->getNegotiationPlugin('id'); $revision = $plugin->getRevision($this->node, $this->nodePreviousRevisionId); $this->assertEquals($this->node->id(), $revision->id()); $this->assertEquals($this->nodePreviousRevisionId, $revision->getRevisionId()); @@ -24,8 +26,8 @@ class VersionByIdTest extends VersionNegotiationTestBase { * @covers ::getRevision */ public function testInvalidRevision() { - $plugin = $this->buildIdNegotationPlugin('id'); - $this->setExpectedException(\InvalidArgumentException::class); + $plugin = $this->getNegotiationPlugin('id'); + $this->setExpectedException(VersionNotFoundException::class, sprintf('The requested resource does not have a version with ID %s.', $this->node2->getRevisionId())); $plugin->getRevision($this->node, $this->node2->getRevisionId()); } diff --git a/tests/src/Kernel/Plugin/VersionNegotiation/VersionByRelTest.php b/tests/src/Kernel/Plugin/VersionNegotiation/VersionByRelTest.php index b2ed899..a17e36a 100644 --- a/tests/src/Kernel/Plugin/VersionNegotiation/VersionByRelTest.php +++ b/tests/src/Kernel/Plugin/VersionNegotiation/VersionByRelTest.php @@ -3,6 +3,7 @@ namespace Drupal\Tests\jsonapi\Kernel\Plugin\VersionNegotiation; use Drupal\jsonapi\Plugin\VersionNegotiation\VersionByRel; +use Drupal\jsonapi\Revisions\InvalidVersionIdentifierException; /** * @coversDefaultClass \Drupal\jsonapi\Plugin\VersionNegotiation\VersionByRel @@ -16,7 +17,7 @@ class VersionByRelTest extends VersionNegotiationTestBase { * @covers ::getRevision */ public function testLatestVersion() { - $plugin = $this->buildIdNegotationPlugin('rel'); + $plugin = $this->getNegotiationPlugin('rel'); $revision = $plugin->getRevision($this->node, VersionByRel::LATEST_VERSION); $this->assertEquals($this->node->id(), $revision->id()); $this->assertEquals($this->node->getRevisionId(), $revision->getRevisionId()); @@ -26,7 +27,7 @@ class VersionByRelTest extends VersionNegotiationTestBase { * @covers ::getRevision */ public function testCurrentVersion() { - $plugin = $this->buildIdNegotationPlugin('rel'); + $plugin = $this->getNegotiationPlugin('rel'); $revision = $plugin->getRevision($this->node, VersionByRel::WORKING_COPY); $this->assertEquals($this->node->id(), $revision->id()); $this->assertEquals($this->node->getRevisionId(), $revision->getRevisionId()); @@ -36,8 +37,8 @@ class VersionByRelTest extends VersionNegotiationTestBase { * @covers ::getRevision */ public function testInvalidRevision() { - $plugin = $this->buildIdNegotationPlugin('rel'); - $this->setExpectedException(\InvalidArgumentException::class); + $plugin = $this->getNegotiationPlugin('rel'); + $this->setExpectedException(InvalidVersionIdentifierException::class, 'The version specifier must be either `latest-version` or `working-copy`, `erroneous-revision-name` given.'); $plugin->getRevision($this->node, 'erroneous-revision-name'); } diff --git a/tests/src/Kernel/Plugin/VersionNegotiation/VersionNegotiationTestBase.php b/tests/src/Kernel/Plugin/VersionNegotiation/VersionNegotiationTestBase.php index 42d6bdc..e1eacd4 100644 --- a/tests/src/Kernel/Plugin/VersionNegotiation/VersionNegotiationTestBase.php +++ b/tests/src/Kernel/Plugin/VersionNegotiation/VersionNegotiationTestBase.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\jsonapi\Kernel\Plugin\VersionNegotiation; +use Drupal\jsonapi\Revisions\VersionNegotiationManager; use Drupal\node\Entity\Node; use Drupal\node\Entity\NodeType; use Drupal\Tests\jsonapi\Kernel\JsonapiKernelTestBase; @@ -95,21 +96,27 @@ abstract class VersionNegotiationTestBase extends JsonapiKernelTestBase { 'uid' => $this->user->id(), ]); $this->node2->save(); + + $this->pluginManager = new VersionNegotiationManager( + $this->container->get('container.namespaces'), + $this->container->get('cache.discovery'), + $this->container->get('module_handler') + ); } /** * Returns an instance of a revision ID negotiation plugin. * - * @param string $pluginId - * The plugin id. + * @param string $negotiator_name + * The plugin ID. * - * @return VersionNegotiationInterface + * @return \Drupal\jsonapi\Revisions\VersionNegotiationInterface * The plugin instance. * * @throws \Exception */ - protected function buildIdNegotationPlugin($pluginId) { - return $this->container->get('plugin.manager.revision_id_negotiation')->createInstance($pluginId); + protected function getNegotiationPlugin($negotiator_name) { + return $this->pluginManager->createInstance($negotiator_name); } }