Verified Commit 7b063440 authored by Dave Long's avatar Dave Long
Browse files

Issue #3402297 by dww: Fix strict type errors in CommentFieldAccessTest

(cherry picked from commit 8310b5cc)
parent 56e616d6
Loading
Loading
Loading
Loading
Loading
+62 −46
Original line number Diff line number Diff line
@@ -7,7 +7,6 @@
use Drupal\comment\Entity\CommentType;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
@@ -233,28 +232,29 @@ public function testAccessToAdministrativeFields() {
      foreach ($permutations as $set) {
        $may_view = $set['comment']->{$field}->access('view', $set['user']);
        $may_update = $set['comment']->{$field}->access('edit', $set['user']);
        $this->assertTrue($may_view, new FormattableMarkup('User @user can view field @field on comment @comment', [
          '@user' => $set['user']->getAccountName(),
          '@comment' => $set['comment']->getSubject(),
          '@field' => $field,
        ]));
        $this->assertEquals($may_update, $set['user']->hasPermission('administer comments'), new FormattableMarkup('User @user @state update field @field on comment @comment', [
          '@user' => $set['user']->getAccountName(),
          '@state' => $may_update ? 'can' : 'cannot',
          '@comment' => $set['comment']->getSubject(),
          '@field' => $field,
        ]));
        $account_name = $set['user']->getAccountName();
        $comment_subject = $set['comment']->getSubject();
        $this->assertTrue($may_view, "User $account_name can view field $field on comment $comment_subject");
        $this->assertEquals(
          $may_update,
          $set['user']->hasPermission('administer comments'),
          "User $account_name" . ($may_update ? 'can' : 'cannot') . "update field $field on comment $comment_subject"
        );
      }
    }

    // Check access to normal field.
    foreach ($permutations as $set) {
      $may_update = $set['comment']->access('update', $set['user']) && $set['comment']->subject->access('edit', $set['user']);
      $this->assertEquals($may_update, $set['user']->hasPermission('administer comments') || ($set['user']->hasPermission('edit own comments') && $set['user']->id() == $set['comment']->getOwnerId()), new FormattableMarkup('User @user @state update field subject on comment @comment', [
        '@user' => $set['user']->getAccountName(),
        '@state' => $may_update ? 'can' : 'cannot',
        '@comment' => $set['comment']->getSubject(),
      ]));
      $this->assertEquals(
        $may_update,
        $set['user']->hasPermission('administer comments') || ($set['user']->hasPermission('edit own comments') && $set['user']->id() == $set['comment']->getOwnerId()),
        sprintf('User %s %s update field subject on comment %s',
          $set['user']->getAccountName(),
          $may_update ? 'can' : 'cannot',
          $set['comment']->getSubject(),
        ),
      );
    }

    // Check read-only fields.
@@ -272,18 +272,25 @@ public function testAccessToAdministrativeFields() {
          $view_access = TRUE;
          $state = 'can';
        }
        $this->assertEquals($may_view, $view_access, new FormattableMarkup('User @user @state view field @field on comment @comment', [
          '@user' => $set['user']->getAccountName(),
          '@comment' => $set['comment']->getSubject(),
          '@field' => $field,
          '@state' => $state,
        ]));
        $this->assertFalse($may_update, new FormattableMarkup('User @user @state update field @field on comment @comment', [
          '@user' => $set['user']->getAccountName(),
          '@state' => $may_update ? 'can' : 'cannot',
          '@comment' => $set['comment']->getSubject(),
          '@field' => $field,
        ]));
        $this->assertEquals(
          $may_view,
          $view_access,
          sprintf('User %s %s view field %s on comment %s',
            $set['user']->getAccountName(),
            $state,
            $field,
            $set['comment']->getSubject(),
          ),
        );
        $this->assertFalse(
          $may_update,
          sprintf('User %s %s update field %s on comment %s',
            $set['user']->getAccountName(),
            $may_update ? 'can' : 'cannot',
            $field,
            $set['comment']->getSubject(),
          ),
        );
      }
    }

@@ -293,18 +300,25 @@ public function testAccessToAdministrativeFields() {
      foreach ($permutations as $set) {
        $may_view = $set['comment']->{$field}->access('view', $set['user']);
        $may_update = $set['comment']->{$field}->access('edit', $set['user']);
        $this->assertTrue($may_view, new FormattableMarkup('User @user can view field @field on comment @comment', [
          '@user' => $set['user']->getAccountName(),
          '@comment' => $set['comment']->getSubject(),
          '@field' => $field,
        ]));
        $this->assertTrue(
          $may_view,
          sprintf('User %s can view field %s on comment %s',
            $set['user']->getAccountName(),
            $field,
            $set['comment']->getSubject(),
          ),
        );
        $expected = $set['user']->hasPermission('post comments') && $set['comment']->isNew() && (int) $set['comment']->getCommentedEntity()->get($set['comment']->getFieldName())->status !== CommentItemInterface::CLOSED;
        $this->assertEquals($expected, $may_update, new FormattableMarkup('User @user @state update field @field on comment @comment', [
          '@user' => $set['user']->getAccountName(),
          '@state' => $expected ? 'can' : 'cannot',
          '@comment' => $set['comment']->getSubject(),
          '@field' => $field,
        ]));
        $this->assertEquals(
          $expected,
          $may_update,
          sprintf('User %s %s update field %s on comment %s',
            $set['user']->getAccountName(),
            $expected ? 'can' : 'cannot',
            $field,
            $set['comment']->getSubject(),
          ),
        );
      }
    }

@@ -321,12 +335,14 @@ public function testAccessToAdministrativeFields() {
            $set['comment']->isNew() &&
            $set['user']->hasPermission('post comments') &&
            $set['comment']->getFieldName() == 'comment_other'
          ), new FormattableMarkup('User @user @state update field @field on comment @comment', [
            '@user' => $set['user']->getAccountName(),
            '@state' => $may_update ? 'can' : 'cannot',
            '@comment' => $set['comment']->getSubject(),
            '@field' => $field,
          ]));
          ),
          sprintf('User %s %s update field %s on comment %s',
            $set['user']->getAccountName(),
            $may_update ? 'can' : 'cannot',
            $field,
            $set['comment']->getSubject(),
          ),
        );
      }
    }
    foreach ($permutations as $set) {