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

merge-requests/5799/merge
Dave Long 2024-01-12 18:11:27 +00:00
parent 3d1fd96ad0
commit 8310b5cc76
No known key found for this signature in database
GPG Key ID: ED52AE211E142771
1 changed files with 62 additions and 46 deletions

View File

@ -7,7 +7,6 @@ use Drupal\comment\Entity\Comment;
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 @@ class CommentFieldAccessTest extends EntityKernelTestBase {
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 @@ class CommentFieldAccessTest extends EntityKernelTestBase {
$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 @@ class CommentFieldAccessTest extends EntityKernelTestBase {
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 @@ class CommentFieldAccessTest extends EntityKernelTestBase {
$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) {