From 4de7b7b46fe52f0e79cf50399e27b809ef8104de Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Tue, 23 Jul 2019 11:27:12 +0100 Subject: [PATCH] Issue #3068563 by mikelutz, Berdir: Properly deprecate Comment::getStatus() --- core/modules/comment/src/CommentForm.php | 2 +- core/modules/comment/src/CommentInterface.php | 9 ++++++--- core/modules/comment/src/Entity/Comment.php | 1 + .../Functional/Rest/CommentResourceTestBase.php | 4 ++-- .../comment/tests/src/Kernel/CommentLegacyTest.php | 14 ++++++++++++++ .../src/Kernel/Migrate/d7/MigrateCommentTest.php | 2 +- 6 files changed, 25 insertions(+), 7 deletions(-) diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index 8089c6d3bd5..48c72712df6 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -141,7 +141,7 @@ class CommentForm extends ContentEntityForm { if (!$comment->getOwnerId()) { $author = $comment->getAuthorName(); } - $status = $comment->getStatus(); + $status = $comment->isPublished() ? CommentInterface::PUBLISHED : CommentInterface::NOT_PUBLISHED; if (empty($comment_preview)) { $form['#title'] = $this->t('Edit comment %title', [ '%title' => $comment->getSubject(), diff --git a/core/modules/comment/src/CommentInterface.php b/core/modules/comment/src/CommentInterface.php index 55405629ce5..d08d850f284 100644 --- a/core/modules/comment/src/CommentInterface.php +++ b/core/modules/comment/src/CommentInterface.php @@ -211,11 +211,14 @@ interface CommentInterface extends ContentEntityInterface, EntityChangedInterfac /** * Returns the comment's status. * - * @return int - * One of CommentInterface::PUBLISHED or CommentInterface::NOT_PUBLISHED + * @return int|string|bool + * Either TRUE, '1', or CommentInterface::PUBLISHED(1) if the comment is + * published, or FALSE, '0', or CommentInterface::NOT_PUBLISHED(0) if the + * comment is not published. * - * @deprecated in Drupal 8.3.0, will be removed before Drupal 9.0.0. Use + * @deprecated in drupal:8.3.0 and is removed from drupal:9.0.0. Use * \Drupal\Core\Entity\EntityPublishedInterface::isPublished() instead. + * @see https://www.drupal.org/node/2830201 */ public function getStatus(); diff --git a/core/modules/comment/src/Entity/Comment.php b/core/modules/comment/src/Entity/Comment.php index 55c4d85d79c..9f404d708a9 100644 --- a/core/modules/comment/src/Entity/Comment.php +++ b/core/modules/comment/src/Entity/Comment.php @@ -490,6 +490,7 @@ class Comment extends ContentEntityBase implements CommentInterface { * {@inheritdoc} */ public function getStatus() { + @trigger_error(__NAMESPACE__ . '\Comment::getStatus() is deprecated in drupal:8.3.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Entity\EntityPublishedInterface::isPublished() instead. See https://www.drupal.org/node/2830201', E_USER_DEPRECATED); return $this->get('status')->value; } diff --git a/core/modules/comment/tests/src/Functional/Rest/CommentResourceTestBase.php b/core/modules/comment/tests/src/Functional/Rest/CommentResourceTestBase.php index 1711d6ebe3a..e552fb2f9e2 100644 --- a/core/modules/comment/tests/src/Functional/Rest/CommentResourceTestBase.php +++ b/core/modules/comment/tests/src/Functional/Rest/CommentResourceTestBase.php @@ -347,7 +347,7 @@ abstract class CommentResourceTestBase extends EntityResourceTestBase { $response = $this->request('POST', $url, $request_options); $unserialized = $this->serializer->deserialize((string) $response->getBody(), get_class($this->entity), static::$format); $this->assertResourceResponse(201, FALSE, $response); - $this->assertFalse($unserialized->getStatus()); + $this->assertFalse($unserialized->isPublished()); // Grant anonymous permission to skip comment approval. $this->grantPermissionsToTestedRole(['skip comment approval']); @@ -356,7 +356,7 @@ abstract class CommentResourceTestBase extends EntityResourceTestBase { $response = $this->request('POST', $url, $request_options); $unserialized = $this->serializer->deserialize((string) $response->getBody(), get_class($this->entity), static::$format); $this->assertResourceResponse(201, FALSE, $response); - $this->assertTrue($unserialized->getStatus()); + $this->assertTrue($unserialized->isPublished()); } /** diff --git a/core/modules/comment/tests/src/Kernel/CommentLegacyTest.php b/core/modules/comment/tests/src/Kernel/CommentLegacyTest.php index 793ba9ae35b..6d20b972858 100644 --- a/core/modules/comment/tests/src/Kernel/CommentLegacyTest.php +++ b/core/modules/comment/tests/src/Kernel/CommentLegacyTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\comment\Kernel; +use Drupal\comment\CommentInterface; use Drupal\comment\Entity\Comment; use Drupal\comment\Entity\CommentType; use Drupal\entity_test\Entity\EntityTest; @@ -90,4 +91,17 @@ class CommentLegacyTest extends EntityKernelTestBase { $this->assertEquals(4, count(comment_view_multiple($entities))); } + /** + * Tests the getStatus() method. + * + * @expectedDeprecation Drupal\comment\Entity\Comment::getStatus() is deprecated in drupal:8.3.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Entity\EntityPublishedInterface::isPublished() instead. See https://www.drupal.org/node/2830201 + */ + public function testGetStatus() { + $entity = $this->createComment(); + $entity->setPublished(); + $this->assertEquals(CommentInterface::PUBLISHED, $entity->getStatus()); + $entity->setUnPublished(); + $this->assertEquals(CommentInterface::NOT_PUBLISHED, $entity->getStatus()); + } + } diff --git a/core/modules/comment/tests/src/Kernel/Migrate/d7/MigrateCommentTest.php b/core/modules/comment/tests/src/Kernel/Migrate/d7/MigrateCommentTest.php index 73c211dc7fc..b5b873abb4f 100644 --- a/core/modules/comment/tests/src/Kernel/Migrate/d7/MigrateCommentTest.php +++ b/core/modules/comment/tests/src/Kernel/Migrate/d7/MigrateCommentTest.php @@ -71,7 +71,7 @@ class MigrateCommentTest extends MigrateDrupal7TestBase { $this->assertSame('Subject field in English', $comment->getSubject()); $this->assertSame('1421727536', $comment->getCreatedTime()); $this->assertSame('1421727536', $comment->getChangedTime()); - $this->assertTrue($comment->getStatus()); + $this->assertTrue($comment->isPublished()); $this->assertSame('admin', $comment->getAuthorName()); $this->assertSame('admin@local.host', $comment->getAuthorEmail()); $this->assertSame('This is a comment', $comment->comment_body->value);