Unverified Commit 9f7df658 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2934026 by Sam152, govind.maloo, vacho, Jo Fitzgerald, amateescu,...

Issue #2934026 by Sam152, govind.maloo, vacho, Jo Fitzgerald, amateescu, Berdir, mikelutz: Deprecate isLatestRevision, getLatestRevision, getLatestRevisionId in the ModerationInformation service
parent 1d386c5d
Loading
Loading
Loading
Loading
+12 −16
Original line number Diff line number Diff line
@@ -86,26 +86,20 @@ public function shouldModerateEntitiesOfBundle(EntityTypeInterface $entity_type,
   * {@inheritdoc}
   */
  public function getLatestRevision($entity_type_id, $entity_id) {
    if ($latest_revision_id = $this->getLatestRevisionId($entity_type_id, $entity_id)) {
      return $this->entityTypeManager->getStorage($entity_type_id)->loadRevision($latest_revision_id);
    }
    @trigger_error(__METHOD__ . ' is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use RevisionableStorageInterface::getLatestRevisionId() and RevisionableStorageInterface::loadRevision() instead. See https://www.drupal.org/node/3087295', E_USER_DEPRECATED);
    /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
    $storage = $this->entityTypeManager->getStorage($entity_type_id);
    return $storage->loadRevision($storage->getLatestRevisionId($entity_id));
  }

  /**
   * {@inheritdoc}
   */
  public function getLatestRevisionId($entity_type_id, $entity_id) {
    @trigger_error(__METHOD__ . ' is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use RevisionableStorageInterface::getLatestRevisionId() instead. See https://www.drupal.org/node/3087295', E_USER_DEPRECATED);
    /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
    if ($storage = $this->entityTypeManager->getStorage($entity_type_id)) {
      $result = $storage->getQuery()
        ->latestRevision()
        ->condition($this->entityTypeManager->getDefinition($entity_type_id)->getKey('id'), $entity_id)
        // No access check is performed here since this is an API function and
        // should return the same ID regardless of the current user.
        ->accessCheck(FALSE)
        ->execute();
      if ($result) {
        return key($result);
      }
      return $storage->getLatestRevisionId($entity_id);
    }
  }

@@ -143,7 +137,8 @@ public function getAffectedRevisionTranslation(ContentEntityInterface $entity) {
   * {@inheritdoc}
   */
  public function isLatestRevision(ContentEntityInterface $entity) {
    return $entity->getRevisionId() == $this->getLatestRevisionId($entity->getEntityTypeId(), $entity->id());
    @trigger_error(__METHOD__ . ' is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use RevisionableInterface::isLatestRevision() instead. See https://www.drupal.org/node/3087295', E_USER_DEPRECATED);
    return $entity->isLatestRevision();
  }

  /**
@@ -171,7 +166,7 @@ public function hasPendingRevision(ContentEntityInterface $entity) {
   */
  public function isLiveRevision(ContentEntityInterface $entity) {
    $workflow = $this->getWorkflowForEntity($entity);
    return $this->isLatestRevision($entity)
    return $entity->isLatestRevision()
      && $entity->isDefaultRevision()
      && $entity->moderation_state->value
      && $workflow->getTypePlugin()->getState($entity->moderation_state->value)->isPublishedState();
@@ -272,7 +267,8 @@ public function getOriginalState(ContentEntityInterface $entity) {
   *   TRUE if this is the entity's first time being moderated, FALSE otherwise.
   */
  protected function isFirstTimeModeration(ContentEntityInterface $entity) {
    $original_entity = $this->getLatestRevision($entity->getEntityTypeId(), $entity->id());
    $storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
    $original_entity = $storage->loadRevision($storage->getLatestRevisionId($entity->id()));

    if ($original_entity) {
      $original_id = $original_entity->moderation_state;
+16 −0
Original line number Diff line number Diff line
@@ -69,6 +69,12 @@ public function isModeratedEntityType(EntityTypeInterface $entity_type);
   * @return \Drupal\Core\Entity\ContentEntityInterface|null
   *   The latest entity revision or NULL, if the entity type / entity doesn't
   *   exist.
   *
   * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use
   *   RevisionableStorageInterface::getLatestRevisionId and
   *   RevisionableStorageInterface::loadRevision instead.
   *
   * @see https://www.drupal.org/node/3087295
   */
  public function getLatestRevision($entity_type_id, $entity_id);

@@ -83,6 +89,11 @@ public function getLatestRevision($entity_type_id, $entity_id);
   * @return int
   *   The revision ID of the latest revision for the specified entity, or
   *   NULL if there is no such entity.
   *
   * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use
   *   RevisionableStorageInterface::getLatestRevisionId instead.
   *
   * @see https://www.drupal.org/node/3087295
   */
  public function getLatestRevisionId($entity_type_id, $entity_id);

@@ -120,6 +131,11 @@ public function getAffectedRevisionTranslation(ContentEntityInterface $entity);
   * @return bool
   *   TRUE if the specified object is the latest revision of its entity,
   *   FALSE otherwise.
   *
   * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use
   *   RevisionableInterface::isLatestRevision instead.
   *
   * @see https://www.drupal.org/node/3087295
   */
  public function isLatestRevision(ContentEntityInterface $entity);

+60 −2
Original line number Diff line number Diff line
@@ -60,9 +60,8 @@ protected function setUp() {

  /**
   * @covers ::getDefaultRevisionId
   * @covers ::getLatestRevisionId
   */
  public function testDefaultAndLatestRevisionId() {
  public function testGetDefaultRevisionId() {
    $entity_test_rev = EntityTestRev::create([
      'name' => 'Default Revision',
      'moderation_state' => 'published',
@@ -77,6 +76,23 @@ public function testDefaultAndLatestRevisionId() {
    // revision ID.
    $default_revision_id = $this->moderationInformation->getDefaultRevisionId('entity_test_rev', $entity_test_rev->id());
    $this->assertSame(1, $default_revision_id);
  }

  /**
   * @covers ::getLatestRevisionId
   * @group legacy
   * @expectedDeprecation Drupal\content_moderation\ModerationInformation::getLatestRevisionId is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use RevisionableStorageInterface::getLatestRevisionId() instead. See https://www.drupal.org/node/3087295
   */
  public function testGetLatestRevisionId() {
    $entity_test_rev = EntityTestRev::create([
      'name' => 'Default Revision',
      'moderation_state' => 'published',
    ]);
    $entity_test_rev->save();

    $entity_test_rev->name = 'Pending revision';
    $entity_test_rev->moderation_state = 'draft';
    $entity_test_rev->save();

    // Check that moderation information service returns the correct latest
    // revision ID.
@@ -84,6 +100,48 @@ public function testDefaultAndLatestRevisionId() {
    $this->assertSame(2, $latest_revision_id);
  }

  /**
   * @covers ::getLatestRevision
   * @group legacy
   * @expectedDeprecation Drupal\content_moderation\ModerationInformation::getLatestRevision is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use RevisionableStorageInterface::getLatestRevisionId() and RevisionableStorageInterface::loadRevision() instead. See https://www.drupal.org/node/3087295
   */
  public function testGetLatestRevision() {
    $entity_test_rev = EntityTestRev::create([
      'name' => 'Default Revision',
      'moderation_state' => 'published',
    ]);
    $entity_test_rev->save();

    $entity_test_rev->name = 'Pending revision';
    $entity_test_rev->moderation_state = 'draft';
    $entity_test_rev->save();

    $latest_revision = $this->moderationInformation->getLatestRevision('entity_test_rev', $entity_test_rev->id());
    $this->assertEquals(2, $latest_revision->getRevisionId());
  }

  /**
   * @covers ::isLatestRevision
   * @group legacy
   * @expectedDeprecation Drupal\content_moderation\ModerationInformation::isLatestRevision is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use RevisionableInterface::isLatestRevision() instead. See https://www.drupal.org/node/3087295
   */
  public function testIsLatestRevision() {
    $entity_test_rev = EntityTestRev::create([
      'name' => 'Default Revision',
      'moderation_state' => 'published',
    ]);
    $entity_test_rev->save();

    $old_revision = clone $entity_test_rev;

    $entity_test_rev->name = 'Pending revision';
    $entity_test_rev->moderation_state = 'draft';
    $entity_test_rev->save();

    $this->assertFalse($this->moderationInformation->isLatestRevision($old_revision));
    $this->assertTrue($this->moderationInformation->isLatestRevision($entity_test_rev));
  }

  /**
   * @covers ::isDefaultRevisionPublished
   * @dataProvider isDefaultRevisionPublishedTestCases
+21 −3
Original line number Diff line number Diff line
@@ -67,9 +67,9 @@ protected function setUp() {
  }

  /**
   * Tests for moderation information methods with node access.
   * @covers \Drupal\content_moderation\ModerationInformation::getDefaultRevisionId
   */
  public function testModerationInformation() {
  public function testGetDefaultRevisionId() {
    // Create an admin user.
    $user = $this->createUser([], NULL, TRUE);
    \Drupal::currentUser()->setAccount($user);
@@ -77,12 +77,30 @@ public function testModerationInformation() {
    // Create a node.
    $node = $this->createNode(['type' => 'page']);
    $this->assertEquals($node->getRevisionId(), $this->moderationInformation->getDefaultRevisionId('node', $node->id()));
    $this->assertEquals($node->getRevisionId(), $this->moderationInformation->getLatestRevisionId('node', $node->id()));

    // Create a non-admin user.
    $user = $this->createUser();
    \Drupal::currentUser()->setAccount($user);
    $this->assertEquals($node->getRevisionId(), $this->moderationInformation->getDefaultRevisionId('node', $node->id()));
  }

  /**
   * @covers \Drupal\content_moderation\ModerationInformation::getLatestRevisionId
   * @group legacy
   * @expectedDeprecation Drupal\content_moderation\ModerationInformation::getLatestRevisionId is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use RevisionableStorageInterface::getLatestRevisionId() instead. See https://www.drupal.org/node/3087295
   */
  public function testGetLatestRevisionId() {
    // Create an admin user.
    $user = $this->createUser([], NULL, TRUE);
    \Drupal::currentUser()->setAccount($user);

    // Create a node.
    $node = $this->createNode(['type' => 'page']);
    $this->assertEquals($node->getRevisionId(), $this->moderationInformation->getLatestRevisionId('node', $node->id()));

    // Create a non-admin user.
    $user = $this->createUser();
    \Drupal::currentUser()->setAccount($user);
    $this->assertEquals($node->getRevisionId(), $this->moderationInformation->getLatestRevisionId('node', $node->id()));
  }