comparison core/modules/comment/tests/src/Kernel/CommentFieldAccessTest.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
1 <?php 1 <?php
2 2
3 namespace Drupal\Tests\comment\Kernel; 3 namespace Drupal\Tests\comment\Kernel;
4 4
5 use Drupal\comment\CommentInterface;
5 use Drupal\comment\Entity\Comment; 6 use Drupal\comment\Entity\Comment;
6 use Drupal\comment\Entity\CommentType; 7 use Drupal\comment\Entity\CommentType;
7 use Drupal\comment\Tests\CommentTestTrait; 8 use Drupal\comment\Tests\CommentTestTrait;
8 use Drupal\Component\Render\FormattableMarkup; 9 use Drupal\Component\Render\FormattableMarkup;
9 use Drupal\Core\Session\AnonymousUserSession; 10 use Drupal\Core\Session\AnonymousUserSession;
140 $this->addDefaultCommentField('entity_test', 'entity_test', 'comment_other'); 141 $this->addDefaultCommentField('entity_test', 'entity_test', 'comment_other');
141 142
142 // Change the second field's anonymous contact setting. 143 // Change the second field's anonymous contact setting.
143 $instance = FieldConfig::loadByName('entity_test', 'entity_test', 'comment_other'); 144 $instance = FieldConfig::loadByName('entity_test', 'entity_test', 'comment_other');
144 // Default is 'May not contact', for this field - they may contact. 145 // Default is 'May not contact', for this field - they may contact.
145 $instance->setSetting('anonymous', COMMENT_ANONYMOUS_MAY_CONTACT); 146 $instance->setSetting('anonymous', CommentInterface::ANONYMOUS_MAY_CONTACT);
146 $instance->save(); 147 $instance->save();
147 148
148 // Create three "Comments". One is owned by our edit-enabled user. 149 // Create three "Comments". One is owned by our edit-enabled user.
149 $comment1 = Comment::create([ 150 $comment1 = Comment::create([
150 'entity_type' => 'entity_test', 151 'entity_type' => 'entity_test',
210 foreach ($this->administrativeFields as $field) { 211 foreach ($this->administrativeFields as $field) {
211 foreach ($permutations as $set) { 212 foreach ($permutations as $set) {
212 $may_view = $set['comment']->{$field}->access('view', $set['user']); 213 $may_view = $set['comment']->{$field}->access('view', $set['user']);
213 $may_update = $set['comment']->{$field}->access('edit', $set['user']); 214 $may_update = $set['comment']->{$field}->access('edit', $set['user']);
214 $this->assertTrue($may_view, new FormattableMarkup('User @user can view field @field on comment @comment', [ 215 $this->assertTrue($may_view, new FormattableMarkup('User @user can view field @field on comment @comment', [
215 '@user' => $set['user']->getUsername(), 216 '@user' => $set['user']->getAccountName(),
216 '@comment' => $set['comment']->getSubject(), 217 '@comment' => $set['comment']->getSubject(),
217 '@field' => $field, 218 '@field' => $field,
218 ])); 219 ]));
219 $this->assertEqual($may_update, $set['user']->hasPermission('administer comments'), new FormattableMarkup('User @user @state update field @field on comment @comment', [ 220 $this->assertEqual($may_update, $set['user']->hasPermission('administer comments'), new FormattableMarkup('User @user @state update field @field on comment @comment', [
220 '@user' => $set['user']->getUsername(), 221 '@user' => $set['user']->getAccountName(),
221 '@state' => $may_update ? 'can' : 'cannot', 222 '@state' => $may_update ? 'can' : 'cannot',
222 '@comment' => $set['comment']->getSubject(), 223 '@comment' => $set['comment']->getSubject(),
223 '@field' => $field, 224 '@field' => $field,
224 ])); 225 ]));
225 } 226 }
227 228
228 // Check access to normal field. 229 // Check access to normal field.
229 foreach ($permutations as $set) { 230 foreach ($permutations as $set) {
230 $may_update = $set['comment']->access('update', $set['user']) && $set['comment']->subject->access('edit', $set['user']); 231 $may_update = $set['comment']->access('update', $set['user']) && $set['comment']->subject->access('edit', $set['user']);
231 $this->assertEqual($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', [ 232 $this->assertEqual($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', [
232 '@user' => $set['user']->getUsername(), 233 '@user' => $set['user']->getAccountName(),
233 '@state' => $may_update ? 'can' : 'cannot', 234 '@state' => $may_update ? 'can' : 'cannot',
234 '@comment' => $set['comment']->getSubject(), 235 '@comment' => $set['comment']->getSubject(),
235 ])); 236 ]));
236 } 237 }
237 238
249 else { 250 else {
250 $view_access = TRUE; 251 $view_access = TRUE;
251 $state = 'can'; 252 $state = 'can';
252 } 253 }
253 $this->assertEqual($may_view, $view_access, new FormattableMarkup('User @user @state view field @field on comment @comment', [ 254 $this->assertEqual($may_view, $view_access, new FormattableMarkup('User @user @state view field @field on comment @comment', [
254 '@user' => $set['user']->getUsername(), 255 '@user' => $set['user']->getAccountName(),
255 '@comment' => $set['comment']->getSubject(), 256 '@comment' => $set['comment']->getSubject(),
256 '@field' => $field, 257 '@field' => $field,
257 '@state' => $state, 258 '@state' => $state,
258 ])); 259 ]));
259 $this->assertFalse($may_update, new FormattableMarkup('User @user @state update field @field on comment @comment', [ 260 $this->assertFalse($may_update, new FormattableMarkup('User @user @state update field @field on comment @comment', [
260 '@user' => $set['user']->getUsername(), 261 '@user' => $set['user']->getAccountName(),
261 '@state' => $may_update ? 'can' : 'cannot', 262 '@state' => $may_update ? 'can' : 'cannot',
262 '@comment' => $set['comment']->getSubject(), 263 '@comment' => $set['comment']->getSubject(),
263 '@field' => $field, 264 '@field' => $field,
264 ])); 265 ]));
265 } 266 }
270 // Check view operation. 271 // Check view operation.
271 foreach ($permutations as $set) { 272 foreach ($permutations as $set) {
272 $may_view = $set['comment']->{$field}->access('view', $set['user']); 273 $may_view = $set['comment']->{$field}->access('view', $set['user']);
273 $may_update = $set['comment']->{$field}->access('edit', $set['user']); 274 $may_update = $set['comment']->{$field}->access('edit', $set['user']);
274 $this->assertEqual($may_view, TRUE, new FormattableMarkup('User @user can view field @field on comment @comment', [ 275 $this->assertEqual($may_view, TRUE, new FormattableMarkup('User @user can view field @field on comment @comment', [
275 '@user' => $set['user']->getUsername(), 276 '@user' => $set['user']->getAccountName(),
276 '@comment' => $set['comment']->getSubject(), 277 '@comment' => $set['comment']->getSubject(),
277 '@field' => $field, 278 '@field' => $field,
278 ])); 279 ]));
279 $this->assertEqual($may_update, $set['user']->hasPermission('post comments') && $set['comment']->isNew(), new FormattableMarkup('User @user @state update field @field on comment @comment', [ 280 $this->assertEqual($may_update, $set['user']->hasPermission('post comments') && $set['comment']->isNew(), new FormattableMarkup('User @user @state update field @field on comment @comment', [
280 '@user' => $set['user']->getUsername(), 281 '@user' => $set['user']->getAccountName(),
281 '@state' => $may_update ? 'can' : 'cannot', 282 '@state' => $may_update ? 'can' : 'cannot',
282 '@comment' => $set['comment']->getSubject(), 283 '@comment' => $set['comment']->getSubject(),
283 '@field' => $field, 284 '@field' => $field,
284 ])); 285 ]));
285 } 286 }
297 $set['user']->isAnonymous() && 298 $set['user']->isAnonymous() &&
298 $set['comment']->isNew() && 299 $set['comment']->isNew() &&
299 $set['user']->hasPermission('post comments') && 300 $set['user']->hasPermission('post comments') &&
300 $set['comment']->getFieldName() == 'comment_other' 301 $set['comment']->getFieldName() == 'comment_other'
301 ), new FormattableMarkup('User @user @state update field @field on comment @comment', [ 302 ), new FormattableMarkup('User @user @state update field @field on comment @comment', [
302 '@user' => $set['user']->getUsername(), 303 '@user' => $set['user']->getAccountName(),
303 '@state' => $may_update ? 'can' : 'cannot', 304 '@state' => $may_update ? 'can' : 'cannot',
304 '@comment' => $set['comment']->getSubject(), 305 '@comment' => $set['comment']->getSubject(),
305 '@field' => $field, 306 '@field' => $field,
306 ])); 307 ]));
307 } 308 }