From 000dbabc5712dfe0c2a448bb615e1b57d900bb2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=CC=81bor=20Hojtsy?= Date: Mon, 11 Feb 2019 11:45:08 +0100 Subject: [PATCH] Issue #3015697 by alexpott, andypost: Properly deprecate COMMENT_ANONYMOUS_* constants --- .../comment/src/CommentAccessControlHandler.php | 2 +- core/modules/comment/src/CommentForm.php | 10 +++++----- .../comment/src/Plugin/Field/FieldType/CommentItem.php | 9 +++++---- .../Constraint/CommentNameConstraintValidator.php | 2 +- .../tests/src/Functional/CommentAnonymousTest.php | 7 ++++--- .../tests/src/Functional/CommentNonNodeTest.php | 4 ++-- .../tests/src/Kernel/CommentFieldAccessTest.php | 3 ++- .../comment/tests/src/Kernel/CommentValidationTest.php | 2 +- .../rdf/tests/src/Functional/CommentAttributesTest.php | 2 +- 9 files changed, 22 insertions(+), 19 deletions(-) diff --git a/core/modules/comment/src/CommentAccessControlHandler.php b/core/modules/comment/src/CommentAccessControlHandler.php index 6e811a97d31f..516a2912329b 100644 --- a/core/modules/comment/src/CommentAccessControlHandler.php +++ b/core/modules/comment/src/CommentAccessControlHandler.php @@ -126,7 +126,7 @@ class CommentAccessControlHandler extends EntityAccessControlHandler { $commented_entity = $entity->getCommentedEntity(); $anonymous_contact = $commented_entity->get($entity->getFieldName())->getFieldDefinition()->getSetting('anonymous'); $admin_access = AccessResult::allowedIfHasPermission($account, 'administer comments'); - $anonymous_access = AccessResult::allowedIf($entity->isNew() && $account->isAnonymous() && ($anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT || $is_name) && $account->hasPermission('post comments')) + $anonymous_access = AccessResult::allowedIf($entity->isNew() && $account->isAnonymous() && ($anonymous_contact != CommentInterface::ANONYMOUS_MAYNOT_CONTACT || $is_name) && $account->hasPermission('post comments')) ->cachePerPermissions() ->addCacheableDependency($entity) ->addCacheableDependency($field_definition->getConfig($commented_entity->bundle())) diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index 57e59d1b41db..f3a33b8b8a51 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -108,7 +108,7 @@ class CommentForm extends ContentEntityForm { $anonymous_contact = $field_definition->getSetting('anonymous'); $is_admin = $comment->id() && $this->currentUser->hasPermission('administer comments'); - if (!$this->currentUser->isAuthenticated() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT) { + if (!$this->currentUser->isAuthenticated() && $anonymous_contact != CommentInterface::ANONYMOUS_MAYNOT_CONTACT) { $form['#attached']['library'][] = 'core/drupal.form'; $form['#attributes']['data-user-info-from-browser'] = TRUE; } @@ -178,7 +178,7 @@ class CommentForm extends ContentEntityForm { '#type' => 'textfield', '#title' => $is_admin ? $this->t('Name for @anonymous', ['@anonymous' => $config->get('anonymous')]) : $this->t('Your name'), '#default_value' => $author, - '#required' => ($this->currentUser->isAnonymous() && $anonymous_contact == COMMENT_ANONYMOUS_MUST_CONTACT), + '#required' => ($this->currentUser->isAnonymous() && $anonymous_contact == CommentInterface::ANONYMOUS_MUST_CONTACT), '#maxlength' => 60, '#access' => $this->currentUser->isAnonymous() || $is_admin, '#size' => 30, @@ -202,11 +202,11 @@ class CommentForm extends ContentEntityForm { '#type' => 'email', '#title' => $this->t('Email'), '#default_value' => $comment->getAuthorEmail(), - '#required' => ($this->currentUser->isAnonymous() && $anonymous_contact == COMMENT_ANONYMOUS_MUST_CONTACT), + '#required' => ($this->currentUser->isAnonymous() && $anonymous_contact == CommentInterface::ANONYMOUS_MUST_CONTACT), '#maxlength' => 64, '#size' => 30, '#description' => $this->t('The content of this field is kept private and will not be shown publicly.'), - '#access' => ($comment->getOwner()->isAnonymous() && $is_admin) || ($this->currentUser->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT), + '#access' => ($comment->getOwner()->isAnonymous() && $is_admin) || ($this->currentUser->isAnonymous() && $anonymous_contact != CommentInterface::ANONYMOUS_MAYNOT_CONTACT), ]; $form['author']['homepage'] = [ @@ -215,7 +215,7 @@ class CommentForm extends ContentEntityForm { '#default_value' => $comment->getHomepage(), '#maxlength' => 255, '#size' => 30, - '#access' => $is_admin || ($this->currentUser->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT), + '#access' => $is_admin || ($this->currentUser->isAnonymous() && $anonymous_contact != CommentInterface::ANONYMOUS_MAYNOT_CONTACT), ]; // Add administrative comment publishing options. diff --git a/core/modules/comment/src/Plugin/Field/FieldType/CommentItem.php b/core/modules/comment/src/Plugin/Field/FieldType/CommentItem.php index 10ad410a056a..255b5ab7c13c 100644 --- a/core/modules/comment/src/Plugin/Field/FieldType/CommentItem.php +++ b/core/modules/comment/src/Plugin/Field/FieldType/CommentItem.php @@ -2,6 +2,7 @@ namespace Drupal\comment\Plugin\Field\FieldType; +use Drupal\comment\CommentInterface; use Drupal\comment\CommentManagerInterface; use Drupal\comment\Entity\CommentType; use Drupal\Core\Field\FieldDefinitionInterface; @@ -44,7 +45,7 @@ class CommentItem extends FieldItemBase implements CommentItemInterface { 'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED, 'per_page' => 50, 'form_location' => CommentItemInterface::FORM_BELOW, - 'anonymous' => COMMENT_ANONYMOUS_MAYNOT_CONTACT, + 'anonymous' => CommentInterface::ANONYMOUS_MAYNOT_CONTACT, 'preview' => DRUPAL_OPTIONAL, ] + parent::defaultFieldSettings(); } @@ -124,9 +125,9 @@ class CommentItem extends FieldItemBase implements CommentItemInterface { '#title' => t('Anonymous commenting'), '#default_value' => $settings['anonymous'], '#options' => [ - COMMENT_ANONYMOUS_MAYNOT_CONTACT => t('Anonymous posters may not enter their contact information'), - COMMENT_ANONYMOUS_MAY_CONTACT => t('Anonymous posters may leave their contact information'), - COMMENT_ANONYMOUS_MUST_CONTACT => t('Anonymous posters must leave their contact information'), + CommentInterface::ANONYMOUS_MAYNOT_CONTACT => t('Anonymous posters may not enter their contact information'), + CommentInterface::ANONYMOUS_MAY_CONTACT => t('Anonymous posters may leave their contact information'), + CommentInterface::ANONYMOUS_MUST_CONTACT => t('Anonymous posters must leave their contact information'), ], '#access' => $anonymous_user->hasPermission('post comments'), ]; diff --git a/core/modules/comment/src/Plugin/Validation/Constraint/CommentNameConstraintValidator.php b/core/modules/comment/src/Plugin/Validation/Constraint/CommentNameConstraintValidator.php index 5842854eecaa..ebc1a9d03f5d 100644 --- a/core/modules/comment/src/Plugin/Validation/Constraint/CommentNameConstraintValidator.php +++ b/core/modules/comment/src/Plugin/Validation/Constraint/CommentNameConstraintValidator.php @@ -76,7 +76,7 @@ class CommentNameConstraintValidator extends ConstraintValidator implements Cont // can't validate this without a valid commented entity, which will fail // the validation elsewhere. if ($owner_id === 0 && empty($author_name) && $entity->getCommentedEntity() && $entity->getFieldName() && - $this->getAnonymousContactDetailsSetting($entity) === COMMENT_ANONYMOUS_MUST_CONTACT) { + $this->getAnonymousContactDetailsSetting($entity) === CommentInterface::ANONYMOUS_MUST_CONTACT) { $this->context->buildViolation($constraint->messageRequired) ->atPath('name') ->addViolation(); diff --git a/core/modules/comment/tests/src/Functional/CommentAnonymousTest.php b/core/modules/comment/tests/src/Functional/CommentAnonymousTest.php index 95e1d11ae8b0..82108496ce9c 100644 --- a/core/modules/comment/tests/src/Functional/CommentAnonymousTest.php +++ b/core/modules/comment/tests/src/Functional/CommentAnonymousTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\comment\Functional; +use Drupal\comment\CommentInterface; use Drupal\user\RoleInterface; /** @@ -32,7 +33,7 @@ class CommentAnonymousTest extends CommentTestBase { */ public function testAnonymous() { $this->drupalLogin($this->adminUser); - $this->setCommentAnonymous(COMMENT_ANONYMOUS_MAYNOT_CONTACT); + $this->setCommentAnonymous(CommentInterface::ANONYMOUS_MAYNOT_CONTACT); $this->drupalLogout(); // Preview comments (with `skip comment approval` permission). @@ -77,7 +78,7 @@ class CommentAnonymousTest extends CommentTestBase { // Allow contact info. $this->drupalLogin($this->adminUser); - $this->setCommentAnonymous(COMMENT_ANONYMOUS_MAY_CONTACT); + $this->setCommentAnonymous(CommentInterface::ANONYMOUS_MAY_CONTACT); // Attempt to edit anonymous comment. $this->drupalGet('comment/' . $anonymous_comment1->id() . '/edit'); @@ -110,7 +111,7 @@ class CommentAnonymousTest extends CommentTestBase { // Require contact info. $this->drupalLogin($this->adminUser); - $this->setCommentAnonymous(COMMENT_ANONYMOUS_MUST_CONTACT); + $this->setCommentAnonymous(CommentInterface::ANONYMOUS_MUST_CONTACT); $this->drupalLogout(); // Try to post comment with contact info (required). diff --git a/core/modules/comment/tests/src/Functional/CommentNonNodeTest.php b/core/modules/comment/tests/src/Functional/CommentNonNodeTest.php index 860218f908ab..acd1f9b4c880 100644 --- a/core/modules/comment/tests/src/Functional/CommentNonNodeTest.php +++ b/core/modules/comment/tests/src/Functional/CommentNonNodeTest.php @@ -396,14 +396,14 @@ class CommentNonNodeTest extends BrowserTestBase { // Test comment option change in field settings. $edit = [ 'default_value_input[comment][0][status]' => CommentItemInterface::CLOSED, - 'settings[anonymous]' => COMMENT_ANONYMOUS_MAY_CONTACT, + 'settings[anonymous]' => CommentInterface::ANONYMOUS_MAY_CONTACT, ]; $this->drupalPostForm(NULL, $edit, t('Save settings')); $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment'); $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-0'); $this->assertFieldChecked('edit-default-value-input-comment-0-status-1'); $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-2'); - $this->assertFieldByName('settings[anonymous]', COMMENT_ANONYMOUS_MAY_CONTACT); + $this->assertFieldByName('settings[anonymous]', CommentInterface::ANONYMOUS_MAY_CONTACT); // Add a new comment-type. $bundle = CommentType::create([ diff --git a/core/modules/comment/tests/src/Kernel/CommentFieldAccessTest.php b/core/modules/comment/tests/src/Kernel/CommentFieldAccessTest.php index 0a7848718d66..41acbf4e1d15 100644 --- a/core/modules/comment/tests/src/Kernel/CommentFieldAccessTest.php +++ b/core/modules/comment/tests/src/Kernel/CommentFieldAccessTest.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\comment\Tests\CommentTestTrait; @@ -142,7 +143,7 @@ class CommentFieldAccessTest extends EntityKernelTestBase { // Change the second field's anonymous contact setting. $instance = FieldConfig::loadByName('entity_test', 'entity_test', 'comment_other'); // Default is 'May not contact', for this field - they may contact. - $instance->setSetting('anonymous', COMMENT_ANONYMOUS_MAY_CONTACT); + $instance->setSetting('anonymous', CommentInterface::ANONYMOUS_MAY_CONTACT); $instance->save(); // Create three "Comments". One is owned by our edit-enabled user. diff --git a/core/modules/comment/tests/src/Kernel/CommentValidationTest.php b/core/modules/comment/tests/src/Kernel/CommentValidationTest.php index 1d81d55ececa..4c2481c9846a 100644 --- a/core/modules/comment/tests/src/Kernel/CommentValidationTest.php +++ b/core/modules/comment/tests/src/Kernel/CommentValidationTest.php @@ -135,7 +135,7 @@ class CommentValidationTest extends EntityKernelTestBase { $comment->set('thread', NULL); // Force anonymous users to enter contact details. - $field->setSetting('anonymous', COMMENT_ANONYMOUS_MUST_CONTACT); + $field->setSetting('anonymous', CommentInterface::ANONYMOUS_MUST_CONTACT); $field->save(); // Reset the node entity. \Drupal::entityManager()->getStorage('node')->resetCache([$node->id()]); diff --git a/core/modules/rdf/tests/src/Functional/CommentAttributesTest.php b/core/modules/rdf/tests/src/Functional/CommentAttributesTest.php index b278c35aac38..45039cd43d08 100644 --- a/core/modules/rdf/tests/src/Functional/CommentAttributesTest.php +++ b/core/modules/rdf/tests/src/Functional/CommentAttributesTest.php @@ -46,7 +46,7 @@ class CommentAttributesTest extends CommentTestBase { 'skip comment approval' => TRUE, ]); // Allows anonymous to leave their contact information. - $this->setCommentAnonymous(COMMENT_ANONYMOUS_MAY_CONTACT); + $this->setCommentAnonymous(CommentInterface::ANONYMOUS_MAY_CONTACT); $this->setCommentPreview(DRUPAL_OPTIONAL); $this->setCommentForm(TRUE); $this->setCommentSubject(TRUE);