annotate core/modules/comment/tests/src/Kernel/CommentFieldAccessTest.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\comment\Kernel;
Chris@0 4
Chris@0 5 use Drupal\comment\Entity\Comment;
Chris@0 6 use Drupal\comment\Entity\CommentType;
Chris@0 7 use Drupal\comment\Tests\CommentTestTrait;
Chris@0 8 use Drupal\Component\Utility\SafeMarkup;
Chris@0 9 use Drupal\Core\Session\AnonymousUserSession;
Chris@0 10 use Drupal\entity_test\Entity\EntityTest;
Chris@0 11 use Drupal\field\Entity\FieldConfig;
Chris@0 12 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
Chris@0 13 use Drupal\Tests\Traits\Core\GeneratePermutationsTrait;
Chris@0 14 use Drupal\user\Entity\Role;
Chris@0 15 use Drupal\user\RoleInterface;
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Tests comment field level access.
Chris@0 19 *
Chris@0 20 * @group comment
Chris@0 21 * @group Access
Chris@0 22 */
Chris@0 23 class CommentFieldAccessTest extends EntityKernelTestBase {
Chris@0 24
Chris@0 25 use CommentTestTrait;
Chris@0 26 use GeneratePermutationsTrait;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * Modules to install.
Chris@0 30 *
Chris@0 31 * @var array
Chris@0 32 */
Chris@0 33 public static $modules = ['comment', 'entity_test', 'user'];
Chris@0 34
Chris@0 35 /**
Chris@0 36 * Fields that only users with administer comments permissions can change.
Chris@0 37 *
Chris@0 38 * @var array
Chris@0 39 */
Chris@0 40 protected $administrativeFields = [
Chris@0 41 'uid',
Chris@0 42 'status',
Chris@0 43 'created',
Chris@0 44 ];
Chris@0 45
Chris@0 46 /**
Chris@0 47 * These fields are automatically managed and can not be changed by any user.
Chris@0 48 *
Chris@0 49 * @var array
Chris@0 50 */
Chris@0 51 protected $readOnlyFields = [
Chris@0 52 'changed',
Chris@0 53 'hostname',
Chris@0 54 'cid',
Chris@0 55 'thread',
Chris@0 56 ];
Chris@0 57
Chris@0 58 /**
Chris@0 59 * These fields can be edited on create only.
Chris@0 60 *
Chris@0 61 * @var array
Chris@0 62 */
Chris@0 63 protected $createOnlyFields = [
Chris@0 64 'uuid',
Chris@0 65 'pid',
Chris@0 66 'comment_type',
Chris@0 67 'entity_id',
Chris@0 68 'entity_type',
Chris@0 69 'field_name',
Chris@0 70 ];
Chris@0 71
Chris@0 72 /**
Chris@0 73 * These fields can only be edited by the admin or anonymous users if allowed.
Chris@0 74 *
Chris@0 75 * @var array
Chris@0 76 */
Chris@0 77 protected $contactFields = [
Chris@0 78 'name',
Chris@0 79 'mail',
Chris@0 80 'homepage',
Chris@0 81 ];
Chris@0 82
Chris@0 83 /**
Chris@0 84 * {@inheritdoc}
Chris@0 85 */
Chris@0 86 protected function setUp() {
Chris@0 87 parent::setUp();
Chris@0 88 $this->installConfig(['user', 'comment']);
Chris@0 89 $this->installSchema('comment', ['comment_entity_statistics']);
Chris@0 90 }
Chris@0 91
Chris@0 92 /**
Chris@0 93 * Test permissions on comment fields.
Chris@0 94 */
Chris@0 95 public function testAccessToAdministrativeFields() {
Chris@0 96 // Create a comment type.
Chris@0 97 $comment_type = CommentType::create([
Chris@0 98 'id' => 'comment',
Chris@0 99 'label' => 'Default comments',
Chris@0 100 'description' => 'Default comment field',
Chris@0 101 'target_entity_type_id' => 'entity_test',
Chris@0 102 ]);
Chris@0 103 $comment_type->save();
Chris@0 104
Chris@0 105 // Create a comment against a test entity.
Chris@0 106 $host = EntityTest::create();
Chris@0 107 $host->save();
Chris@0 108
Chris@0 109 // An administrator user. No user exists yet, ensure that the first user
Chris@0 110 // does not have UID 1.
Chris@0 111 $comment_admin_user = $this->createUser(['uid' => 2, 'name' => 'admin'], [
Chris@0 112 'administer comments',
Chris@0 113 'access comments',
Chris@0 114 ]);
Chris@0 115
Chris@0 116 // Two comment enabled users, one with edit access.
Chris@0 117 $comment_enabled_user = $this->createUser(['name' => 'enabled'], [
Chris@0 118 'post comments',
Chris@0 119 'skip comment approval',
Chris@0 120 'edit own comments',
Chris@0 121 'access comments',
Chris@0 122 ]);
Chris@0 123 $comment_no_edit_user = $this->createUser(['name' => 'no edit'], [
Chris@0 124 'post comments',
Chris@0 125 'skip comment approval',
Chris@0 126 'access comments',
Chris@0 127 ]);
Chris@0 128
Chris@0 129 // An unprivileged user.
Chris@0 130 $comment_disabled_user = $this->createUser(['name' => 'disabled'], ['access content']);
Chris@0 131
Chris@0 132 $role = Role::load(RoleInterface::ANONYMOUS_ID);
Chris@0 133 $role->grantPermission('post comments')
Chris@0 134 ->save();
Chris@0 135
Chris@0 136 $anonymous_user = new AnonymousUserSession();
Chris@0 137
Chris@0 138 // Add two fields.
Chris@0 139 $this->addDefaultCommentField('entity_test', 'entity_test', 'comment');
Chris@0 140 $this->addDefaultCommentField('entity_test', 'entity_test', 'comment_other');
Chris@0 141
Chris@0 142 // Change the second field's anonymous contact setting.
Chris@0 143 $instance = FieldConfig::loadByName('entity_test', 'entity_test', 'comment_other');
Chris@0 144 // Default is 'May not contact', for this field - they may contact.
Chris@0 145 $instance->setSetting('anonymous', COMMENT_ANONYMOUS_MAY_CONTACT);
Chris@0 146 $instance->save();
Chris@0 147
Chris@0 148 // Create three "Comments". One is owned by our edit-enabled user.
Chris@0 149 $comment1 = Comment::create([
Chris@0 150 'entity_type' => 'entity_test',
Chris@0 151 'name' => 'Tony',
Chris@0 152 'hostname' => 'magic.example.com',
Chris@0 153 'mail' => 'tonythemagicalpony@example.com',
Chris@0 154 'subject' => 'Bruce the Mesopotamian moose',
Chris@0 155 'entity_id' => $host->id(),
Chris@0 156 'comment_type' => 'comment',
Chris@0 157 'field_name' => 'comment',
Chris@0 158 'pid' => 0,
Chris@0 159 'uid' => 0,
Chris@0 160 'status' => 1,
Chris@0 161 ]);
Chris@0 162 $comment1->save();
Chris@0 163 $comment2 = Comment::create([
Chris@0 164 'entity_type' => 'entity_test',
Chris@0 165 'hostname' => 'magic.example.com',
Chris@0 166 'subject' => 'Brian the messed up lion',
Chris@0 167 'entity_id' => $host->id(),
Chris@0 168 'comment_type' => 'comment',
Chris@0 169 'field_name' => 'comment',
Chris@0 170 'status' => 1,
Chris@0 171 'pid' => 0,
Chris@0 172 'uid' => $comment_enabled_user->id(),
Chris@0 173 ]);
Chris@0 174 $comment2->save();
Chris@0 175 $comment3 = Comment::create([
Chris@0 176 'entity_type' => 'entity_test',
Chris@0 177 'hostname' => 'magic.example.com',
Chris@0 178 // Unpublished.
Chris@0 179 'status' => 0,
Chris@0 180 'subject' => 'Gail the minky whale',
Chris@0 181 'entity_id' => $host->id(),
Chris@0 182 'comment_type' => 'comment',
Chris@0 183 'field_name' => 'comment_other',
Chris@0 184 'pid' => $comment2->id(),
Chris@0 185 'uid' => $comment_no_edit_user->id(),
Chris@0 186 ]);
Chris@0 187 $comment3->save();
Chris@0 188 // Note we intentionally don't save this comment so it remains 'new'.
Chris@0 189 $comment4 = Comment::create([
Chris@0 190 'entity_type' => 'entity_test',
Chris@0 191 'hostname' => 'magic.example.com',
Chris@0 192 // Unpublished.
Chris@0 193 'status' => 0,
Chris@0 194 'subject' => 'Daniel the Cocker-Spaniel',
Chris@0 195 'entity_id' => $host->id(),
Chris@0 196 'comment_type' => 'comment',
Chris@0 197 'field_name' => 'comment_other',
Chris@0 198 'pid' => 0,
Chris@0 199 'uid' => $anonymous_user->id(),
Chris@0 200 ]);
Chris@0 201
Chris@0 202 // Generate permutations.
Chris@0 203 $combinations = [
Chris@0 204 'comment' => [$comment1, $comment2, $comment3, $comment4],
Chris@0 205 'user' => [$comment_admin_user, $comment_enabled_user, $comment_no_edit_user, $comment_disabled_user, $anonymous_user]
Chris@0 206 ];
Chris@0 207 $permutations = $this->generatePermutations($combinations);
Chris@0 208
Chris@0 209 // Check access to administrative fields.
Chris@0 210 foreach ($this->administrativeFields as $field) {
Chris@0 211 foreach ($permutations as $set) {
Chris@0 212 $may_view = $set['comment']->{$field}->access('view', $set['user']);
Chris@0 213 $may_update = $set['comment']->{$field}->access('edit', $set['user']);
Chris@0 214 $this->assertTrue($may_view, SafeMarkup::format('User @user can view field @field on comment @comment', [
Chris@0 215 '@user' => $set['user']->getUsername(),
Chris@0 216 '@comment' => $set['comment']->getSubject(),
Chris@0 217 '@field' => $field,
Chris@0 218 ]));
Chris@0 219 $this->assertEqual($may_update, $set['user']->hasPermission('administer comments'), SafeMarkup::format('User @user @state update field @field on comment @comment', [
Chris@0 220 '@user' => $set['user']->getUsername(),
Chris@0 221 '@state' => $may_update ? 'can' : 'cannot',
Chris@0 222 '@comment' => $set['comment']->getSubject(),
Chris@0 223 '@field' => $field,
Chris@0 224 ]));
Chris@0 225 }
Chris@0 226 }
Chris@0 227
Chris@0 228 // Check access to normal field.
Chris@0 229 foreach ($permutations as $set) {
Chris@0 230 $may_update = $set['comment']->access('update', $set['user']) && $set['comment']->subject->access('edit', $set['user']);
Chris@0 231 $this->assertEqual($may_update, $set['user']->hasPermission('administer comments') || ($set['user']->hasPermission('edit own comments') && $set['user']->id() == $set['comment']->getOwnerId()), SafeMarkup::format('User @user @state update field subject on comment @comment', [
Chris@0 232 '@user' => $set['user']->getUsername(),
Chris@0 233 '@state' => $may_update ? 'can' : 'cannot',
Chris@0 234 '@comment' => $set['comment']->getSubject(),
Chris@0 235 ]));
Chris@0 236 }
Chris@0 237
Chris@0 238 // Check read-only fields.
Chris@0 239 foreach ($this->readOnlyFields as $field) {
Chris@0 240 // Check view operation.
Chris@0 241 foreach ($permutations as $set) {
Chris@0 242 $may_view = $set['comment']->{$field}->access('view', $set['user']);
Chris@0 243 $may_update = $set['comment']->{$field}->access('edit', $set['user']);
Chris@0 244 // Nobody has access to view the hostname field.
Chris@0 245 if ($field === 'hostname') {
Chris@0 246 $view_access = FALSE;
Chris@0 247 $state = 'cannot';
Chris@0 248 }
Chris@0 249 else {
Chris@0 250 $view_access = TRUE;
Chris@0 251 $state = 'can';
Chris@0 252 }
Chris@0 253 $this->assertEqual($may_view, $view_access, SafeMarkup::format('User @user @state view field @field on comment @comment', [
Chris@0 254 '@user' => $set['user']->getUsername(),
Chris@0 255 '@comment' => $set['comment']->getSubject(),
Chris@0 256 '@field' => $field,
Chris@0 257 '@state' => $state,
Chris@0 258 ]));
Chris@0 259 $this->assertFalse($may_update, SafeMarkup::format('User @user @state update field @field on comment @comment', [
Chris@0 260 '@user' => $set['user']->getUsername(),
Chris@0 261 '@state' => $may_update ? 'can' : 'cannot',
Chris@0 262 '@comment' => $set['comment']->getSubject(),
Chris@0 263 '@field' => $field,
Chris@0 264 ]));
Chris@0 265 }
Chris@0 266 }
Chris@0 267
Chris@0 268 // Check create-only fields.
Chris@0 269 foreach ($this->createOnlyFields as $field) {
Chris@0 270 // Check view operation.
Chris@0 271 foreach ($permutations as $set) {
Chris@0 272 $may_view = $set['comment']->{$field}->access('view', $set['user']);
Chris@0 273 $may_update = $set['comment']->{$field}->access('edit', $set['user']);
Chris@0 274 $this->assertEqual($may_view, TRUE, SafeMarkup::format('User @user can view field @field on comment @comment', [
Chris@0 275 '@user' => $set['user']->getUsername(),
Chris@0 276 '@comment' => $set['comment']->getSubject(),
Chris@0 277 '@field' => $field,
Chris@0 278 ]));
Chris@0 279 $this->assertEqual($may_update, $set['user']->hasPermission('post comments') && $set['comment']->isNew(), SafeMarkup::format('User @user @state update field @field on comment @comment', [
Chris@0 280 '@user' => $set['user']->getUsername(),
Chris@0 281 '@state' => $may_update ? 'can' : 'cannot',
Chris@0 282 '@comment' => $set['comment']->getSubject(),
Chris@0 283 '@field' => $field,
Chris@0 284 ]));
Chris@0 285 }
Chris@0 286 }
Chris@0 287
Chris@0 288 // Check contact fields.
Chris@0 289 foreach ($this->contactFields as $field) {
Chris@0 290 // Check view operation.
Chris@0 291 foreach ($permutations as $set) {
Chris@0 292 $may_update = $set['comment']->{$field}->access('edit', $set['user']);
Chris@0 293 // To edit the 'mail' or 'name' field, either the user has the
Chris@0 294 // "administer comments" permissions or the user is anonymous and
Chris@0 295 // adding a new comment using a field that allows contact details.
Chris@0 296 $this->assertEqual($may_update, $set['user']->hasPermission('administer comments') || (
Chris@0 297 $set['user']->isAnonymous() &&
Chris@0 298 $set['comment']->isNew() &&
Chris@0 299 $set['user']->hasPermission('post comments') &&
Chris@0 300 $set['comment']->getFieldName() == 'comment_other'
Chris@0 301 ), SafeMarkup::format('User @user @state update field @field on comment @comment', [
Chris@0 302 '@user' => $set['user']->getUsername(),
Chris@0 303 '@state' => $may_update ? 'can' : 'cannot',
Chris@0 304 '@comment' => $set['comment']->getSubject(),
Chris@0 305 '@field' => $field,
Chris@0 306 ]));
Chris@0 307 }
Chris@0 308 }
Chris@0 309 foreach ($permutations as $set) {
Chris@0 310 // Check no view-access to mail field for other than admin.
Chris@0 311 $may_view = $set['comment']->mail->access('view', $set['user']);
Chris@0 312 $this->assertEqual($may_view, $set['user']->hasPermission('administer comments'));
Chris@0 313 }
Chris@0 314 }
Chris@0 315
Chris@0 316 }