EntityReferenceSelectionAccessTest.php 19 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\system\Functional\Entity\EntityReferenceSelection;
4

5
use Drupal\comment\Tests\CommentTestTrait;
6
use Drupal\Component\Utility\Html;
7
use Drupal\Core\Language\LanguageInterface;
8
use Drupal\comment\CommentInterface;
9
use Drupal\KernelTests\KernelTestBase;
10
use Drupal\node\Entity\Node;
11 12
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
13
use Drupal\node\NodeInterface;
14 15
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
16
use Drupal\user\Entity\User;
17
use Drupal\comment\Entity\Comment;
18 19

/**
20 21 22
 * Tests for the base handlers provided by Entity Reference.
 *
 * @group entity_reference
23
 */
24
class EntityReferenceSelectionAccessTest extends KernelTestBase {
25

26
  use CommentTestTrait;
27 28
  use ContentTypeCreationTrait;
  use UserCreationTrait;
29

30 31 32 33 34
  /**
   * Modules to enable.
   *
   * @var array
   */
35
  public static $modules = ['comment', 'field', 'node', 'system', 'taxonomy', 'text', 'user'];
36

37 38 39
  /**
   * {@inheritdoc}
   */
40
  protected function setUp() {
41 42
    parent::setUp();

43 44 45 46 47
    $this->installSchema('system', 'sequences');
    $this->installSchema('comment', ['comment_entity_statistics']);

    $this->installEntitySchema('comment');
    $this->installEntitySchema('node');
48
    $this->installEntitySchema('taxonomy_term');
49 50
    $this->installEntitySchema('user');

51
    $this->installConfig(['comment', 'field', 'node', 'taxonomy', 'user']);
52 53 54 55 56 57 58 59 60 61 62 63 64

    // Create the anonymous and the admin users.
    $anonymous_user = User::create([
      'uid' => 0,
      'name' => '',
    ]);
    $anonymous_user->save();
    $admin_user = User::create([
      'uid' => 1,
      'name' => 'admin',
      'status' => 1,
    ]);
    $admin_user->save();
65 66
  }

67 68 69 70 71 72 73 74 75 76 77 78
  /**
   * Checks that a selection plugin returns the expected results.
   *
   * @param array $selection_options
   *   An array of options as required by entity reference selection plugins.
   * @param array $tests
   *   An array of tests to run.
   * @param string $handler_name
   *   The name of the entity type selection handler being tested.
   */
  protected function assertReferenceable(array $selection_options, $tests, $handler_name) {
    $handler = \Drupal::service('plugin.manager.entity_reference_selection')->getInstance($selection_options);
79 80 81

    foreach ($tests as $test) {
      foreach ($test['arguments'] as $arguments) {
82 83
        $result = call_user_func_array([$handler, 'getReferenceableEntities'], $arguments);
        $this->assertEqual($result, $test['result'], format_string('Valid result set returned by @handler.', ['@handler' => $handler_name]));
84

85
        $result = call_user_func_array([$handler, 'countReferenceableEntities'], $arguments);
86 87 88 89 90 91 92 93
        if (!empty($test['result'])) {
          $bundle = key($test['result']);
          $count = count($test['result'][$bundle]);
        }
        else {
          $count = 0;
        }

94
        $this->assertEqual($result, $count, format_string('Valid count returned by @handler.', ['@handler' => $handler_name]));
95 96 97 98 99 100 101 102
      }
    }
  }

  /**
   * Test the node-specific overrides of the entity handler.
   */
  public function testNodeHandler() {
103
    $selection_options = [
104 105
      'target_type' => 'node',
      'handler' => 'default',
106
      'target_bundles' => NULL,
107
    ];
108 109 110

    // Build a set of test data.
    // Titles contain HTML-special characters to test escaping.
111 112
    $node_values = [
      'published1' => [
113
        'type' => 'article',
114
        'status' => NodeInterface::PUBLISHED,
115 116
        'title' => 'Node published1 (<&>)',
        'uid' => 1,
117 118
      ],
      'published2' => [
119
        'type' => 'article',
120
        'status' => NodeInterface::PUBLISHED,
121 122
        'title' => 'Node published2 (<&>)',
        'uid' => 1,
123 124
      ],
      'unpublished' => [
125
        'type' => 'article',
126
        'status' => NodeInterface::NOT_PUBLISHED,
127 128
        'title' => 'Node unpublished (<&>)',
        'uid' => 1,
129 130
      ],
    ];
131

132 133
    $nodes = [];
    $node_labels = [];
134
    foreach ($node_values as $key => $values) {
135
      $node = Node::create($values);
136 137
      $node->save();
      $nodes[$key] = $node;
138
      $node_labels[$key] = Html::escape($node->label());
139 140 141
    }

    // Test as a non-admin.
142 143
    $normal_user = $this->createUser(['access content']);
    $this->setCurrentUser($normal_user);
144 145 146 147 148 149 150
    $referenceable_tests = [
      [
        'arguments' => [
          [NULL, 'CONTAINS'],
        ],
        'result' => [
          'article' => [
151 152
            $nodes['published1']->id() => $node_labels['published1'],
            $nodes['published2']->id() => $node_labels['published2'],
153 154 155 156 157 158 159 160 161 162
          ],
        ],
      ],
      [
        'arguments' => [
          ['published1', 'CONTAINS'],
          ['Published1', 'CONTAINS'],
        ],
        'result' => [
          'article' => [
163
            $nodes['published1']->id() => $node_labels['published1'],
164 165 166 167 168 169 170 171 172 173
          ],
        ],
      ],
      [
        'arguments' => [
          ['published2', 'CONTAINS'],
          ['Published2', 'CONTAINS'],
        ],
        'result' => [
          'article' => [
174
            $nodes['published2']->id() => $node_labels['published2'],
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
          ],
        ],
      ],
      [
        'arguments' => [
          ['invalid node', 'CONTAINS'],
        ],
        'result' => [],
      ],
      [
        'arguments' => [
          ['Node unpublished', 'CONTAINS'],
        ],
        'result' => [],
      ],
    ];
191
    $this->assertReferenceable($selection_options, $referenceable_tests, 'Node handler');
192 193

    // Test as an admin.
194 195
    $content_admin = $this->createUser(['access content', 'bypass node access']);
    $this->setCurrentUser($content_admin);
196 197 198 199 200 201 202
    $referenceable_tests = [
      [
        'arguments' => [
          [NULL, 'CONTAINS'],
        ],
        'result' => [
          'article' => [
203 204 205
            $nodes['published1']->id() => $node_labels['published1'],
            $nodes['published2']->id() => $node_labels['published2'],
            $nodes['unpublished']->id() => $node_labels['unpublished'],
206 207 208 209 210 211 212 213 214
          ],
        ],
      ],
      [
        'arguments' => [
          ['Node unpublished', 'CONTAINS'],
        ],
        'result' => [
          'article' => [
215
            $nodes['unpublished']->id() => $node_labels['unpublished'],
216 217 218 219
          ],
        ],
      ],
    ];
220
    $this->assertReferenceable($selection_options, $referenceable_tests, 'Node handler (admin)');
221 222 223 224 225 226
  }

  /**
   * Test the user-specific overrides of the entity handler.
   */
  public function testUserHandler() {
227
    $selection_options = [
228 229
      'target_type' => 'user',
      'handler' => 'default',
230 231
      'target_bundles' => NULL,
      'include_anonymous' => TRUE,
232
    ];
233 234

    // Build a set of test data.
235
    $user_values = [
236 237
      'anonymous' => User::load(0),
      'admin' => User::load(1),
238
      'non_admin' => [
239 240
        'name' => 'non_admin <&>',
        'mail' => 'non_admin@example.com',
241
        'roles' => [],
242 243
        'pass' => user_password(),
        'status' => 1,
244 245
      ],
      'blocked' => [
246 247
        'name' => 'blocked <&>',
        'mail' => 'blocked@example.com',
248
        'roles' => [],
249 250
        'pass' => user_password(),
        'status' => 0,
251 252
      ],
    ];
253

254
    $user_values['anonymous']->name = $this->config('user.settings')->get('anonymous');
255
    $users = [];
256

257
    $user_labels = [];
258 259
    foreach ($user_values as $key => $values) {
      if (is_array($values)) {
260
        $account = User::create($values);
261 262 263 264 265 266
        $account->save();
      }
      else {
        $account = $values;
      }
      $users[$key] = $account;
267
      $user_labels[$key] = Html::escape($account->getUsername());
268 269 270
    }

    // Test as a non-admin.
271
    $this->setCurrentUser($users['non_admin']);
272 273 274 275 276 277 278
    $referenceable_tests = [
      [
        'arguments' => [
          [NULL, 'CONTAINS'],
        ],
        'result' => [
          'user' => [
279 280
            $users['admin']->id() => $user_labels['admin'],
            $users['non_admin']->id() => $user_labels['non_admin'],
281 282 283 284 285 286 287 288 289 290
          ],
        ],
      ],
      [
        'arguments' => [
          ['non_admin', 'CONTAINS'],
          ['NON_ADMIN', 'CONTAINS'],
        ],
        'result' => [
          'user' => [
291
            $users['non_admin']->id() => $user_labels['non_admin'],
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
          ],
        ],
      ],
      [
        'arguments' => [
          ['invalid user', 'CONTAINS'],
        ],
        'result' => [],
      ],
      [
        'arguments' => [
          ['blocked', 'CONTAINS'],
        ],
        'result' => [],
      ],
    ];
308
    $this->assertReferenceable($selection_options, $referenceable_tests, 'User handler');
309

310
    $this->setCurrentUser($users['admin']);
311 312 313 314 315 316 317
    $referenceable_tests = [
      [
        'arguments' => [
          [NULL, 'CONTAINS'],
        ],
        'result' => [
          'user' => [
318 319 320 321
            $users['anonymous']->id() => $user_labels['anonymous'],
            $users['admin']->id() => $user_labels['admin'],
            $users['non_admin']->id() => $user_labels['non_admin'],
            $users['blocked']->id() => $user_labels['blocked'],
322 323 324 325 326 327 328 329 330
          ],
        ],
      ],
      [
        'arguments' => [
          ['blocked', 'CONTAINS'],
        ],
        'result' => [
          'user' => [
331
            $users['blocked']->id() => $user_labels['blocked'],
332 333 334 335 336 337 338 339 340 341
          ],
        ],
      ],
      [
        'arguments' => [
          ['Anonymous', 'CONTAINS'],
          ['anonymous', 'CONTAINS'],
        ],
        'result' => [
          'user' => [
342
            $users['anonymous']->id() => $user_labels['anonymous'],
343 344 345 346
          ],
        ],
      ],
    ];
347
    $this->assertReferenceable($selection_options, $referenceable_tests, 'User handler (admin)');
348 349

    // Test the 'include_anonymous' option.
350
    $selection_options['include_anonymous'] = FALSE;
351 352 353 354 355 356 357 358 359
    $referenceable_tests = [
      [
        'arguments' => [
          ['Anonymous', 'CONTAINS'],
          ['anonymous', 'CONTAINS'],
        ],
        'result' => [],
      ],
    ];
360
    $this->assertReferenceable($selection_options, $referenceable_tests, 'User handler (does not include anonymous)');
361 362 363 364

    // Check that the Anonymous user is not included in the results when no
    // label matching is done, for example when using the 'options_select'
    // widget.
365 366 367 368 369 370 371
    $referenceable_tests = [
      [
        'arguments' => [
          [NULL],
        ],
        'result' => [
          'user' => [
372 373 374
            $users['admin']->id() => $user_labels['admin'],
            $users['non_admin']->id() => $user_labels['non_admin'],
            $users['blocked']->id() => $user_labels['blocked'],
375 376 377 378
          ],
        ],
      ],
    ];
379
    $this->assertReferenceable($selection_options, $referenceable_tests, 'User handler (does not include anonymous)');
380 381 382 383 384 385
  }

  /**
   * Test the comment-specific overrides of the entity handler.
   */
  public function testCommentHandler() {
386
    $selection_options = [
387 388
      'target_type' => 'comment',
      'handler' => 'default',
389
      'target_bundles' => NULL,
390
    ];
391 392

    // Build a set of test data.
393
    $this->createContentType(['type' => 'article', 'name' => 'Article']);
394 395
    $node_values = [
      'published' => [
396 397 398 399
        'type' => 'article',
        'status' => 1,
        'title' => 'Node published',
        'uid' => 1,
400 401
      ],
      'unpublished' => [
402 403 404 405
        'type' => 'article',
        'status' => 0,
        'title' => 'Node unpublished',
        'uid' => 1,
406 407 408
      ],
    ];
    $nodes = [];
409
    foreach ($node_values as $key => $values) {
410
      $node = Node::create($values);
411 412 413 414
      $node->save();
      $nodes[$key] = $node;
    }

415
    // Create comment field on article.
416
    $this->addDefaultCommentField('node', 'article');
417

418 419
    $comment_values = [
      'published_published' => [
420 421 422
        'entity_id' => $nodes['published']->id(),
        'entity_type' => 'node',
        'field_name' => 'comment',
423 424 425
        'uid' => 1,
        'cid' => NULL,
        'pid' => 0,
426
        'status' => CommentInterface::PUBLISHED,
427
        'subject' => 'Comment Published <&>',
428
        'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
429 430
      ],
      'published_unpublished' => [
431 432 433
        'entity_id' => $nodes['published']->id(),
        'entity_type' => 'node',
        'field_name' => 'comment',
434 435 436
        'uid' => 1,
        'cid' => NULL,
        'pid' => 0,
437
        'status' => CommentInterface::NOT_PUBLISHED,
438
        'subject' => 'Comment Unpublished <&>',
439
        'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
440 441
      ],
      'unpublished_published' => [
442 443 444
        'entity_id' => $nodes['unpublished']->id(),
        'entity_type' => 'node',
        'field_name' => 'comment',
445 446 447
        'uid' => 1,
        'cid' => NULL,
        'pid' => 0,
448
        'status' => CommentInterface::NOT_PUBLISHED,
449
        'subject' => 'Comment Published on Unpublished node <&>',
450
        'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
451 452
      ],
    ];
453

454 455
    $comments = [];
    $comment_labels = [];
456
    foreach ($comment_values as $key => $values) {
457
      $comment = Comment::create($values);
458 459
      $comment->save();
      $comments[$key] = $comment;
460
      $comment_labels[$key] = Html::escape($comment->label());
461 462 463
    }

    // Test as a non-admin.
464 465
    $normal_user = $this->createUser(['access content', 'access comments']);
    $this->setCurrentUser($normal_user);
466 467 468 469 470 471 472
    $referenceable_tests = [
      [
        'arguments' => [
          [NULL, 'CONTAINS'],
        ],
        'result' => [
          'comment' => [
473
            $comments['published_published']->cid->value => $comment_labels['published_published'],
474 475 476 477 478 479 480 481 482
          ],
        ],
      ],
      [
        'arguments' => [
          ['Published', 'CONTAINS'],
        ],
        'result' => [
          'comment' => [
483
            $comments['published_published']->cid->value => $comment_labels['published_published'],
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
          ],
        ],
      ],
      [
        'arguments' => [
          ['invalid comment', 'CONTAINS'],
        ],
        'result' => [],
      ],
      [
        'arguments' => [
          ['Comment Unpublished', 'CONTAINS'],
        ],
        'result' => [],
      ],
    ];
500
    $this->assertReferenceable($selection_options, $referenceable_tests, 'Comment handler');
501 502

    // Test as a comment admin.
503 504
    $admin_user = $this->createUser(['access content', 'access comments', 'administer comments']);
    $this->setCurrentUser($admin_user);
505 506 507 508 509 510 511
    $referenceable_tests = [
      [
        'arguments' => [
          [NULL, 'CONTAINS'],
        ],
        'result' => [
          'comment' => [
512 513
            $comments['published_published']->cid->value => $comment_labels['published_published'],
            $comments['published_unpublished']->cid->value => $comment_labels['published_unpublished'],
514 515 516 517
          ],
        ],
      ],
    ];
518
    $this->assertReferenceable($selection_options, $referenceable_tests, 'Comment handler (comment admin)');
519 520

    // Test as a node and comment admin.
521 522
    $admin_user = $this->createUser(['access content', 'access comments', 'administer comments', 'bypass node access']);
    $this->setCurrentUser($admin_user);
523 524 525 526 527 528 529
    $referenceable_tests = [
      [
        'arguments' => [
          [NULL, 'CONTAINS'],
        ],
        'result' => [
          'comment' => [
530 531 532
            $comments['published_published']->cid->value => $comment_labels['published_published'],
            $comments['published_unpublished']->cid->value => $comment_labels['published_unpublished'],
            $comments['unpublished_published']->cid->value => $comment_labels['unpublished_published'],
533 534 535 536
          ],
        ],
      ],
    ];
537
    $this->assertReferenceable($selection_options, $referenceable_tests, 'Comment handler (comment + node admin)');
538
  }
539

540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
  /**
   * Test the term-specific overrides of the selection handler.
   */
  public function testTermHandler() {
    // Create a 'Tags' vocabulary.
    Vocabulary::create([
      'name' => 'Tags',
      'description' => $this->randomMachineName(),
      'vid' => 'tags',
    ])->save();

    $selection_options = [
      'target_type' => 'taxonomy_term',
      'handler' => 'default',
      'target_bundles' => NULL,
    ];

    // Build a set of test data.
    $term_values = [
      'published1' => [
        'vid' => 'tags',
        'status' => 1,
        'name' => 'Term published1',
      ],
      'published2' => [
        'vid' => 'tags',
        'status' => 1,
        'name' => 'Term published2',
      ],
      'unpublished' => [
        'vid' => 'tags',
        'status' => 0,
        'name' => 'Term unpublished',
      ],
      'published3' => [
        'vid' => 'tags',
        'status' => 1,
        'name' => 'Term published3',
        'parent' => 'unpublished',
      ],
      'published4' => [
        'vid' => 'tags',
        'status' => 1,
        'name' => 'Term published4',
        'parent' => 'published3',
      ],
    ];

    $terms = [];
    $term_labels = [];
    foreach ($term_values as $key => $values) {
      $term = Term::create($values);
      if (isset($values['parent'])) {
        $term->parent->entity = $terms[$values['parent']];
      }
      $term->save();
      $terms[$key] = $term;
      $term_labels[$key] = Html::escape($term->label());
    }

    // Test as a non-admin.
    $normal_user = $this->createUser(['access content']);
    $this->setCurrentUser($normal_user);
    $referenceable_tests = [
      [
        'arguments' => [
          [NULL, 'CONTAINS'],
        ],
        'result' => [
          'tags' => [
            $terms['published1']->id() => $term_labels['published1'],
            $terms['published2']->id() => $term_labels['published2'],
          ],
        ],
      ],
      [
        'arguments' => [
          ['published1', 'CONTAINS'],
          ['Published1', 'CONTAINS'],
        ],
        'result' => [
          'tags' => [
            $terms['published1']->id() => $term_labels['published1'],
          ],
        ],
      ],
      [
        'arguments' => [
          ['published2', 'CONTAINS'],
          ['Published2', 'CONTAINS'],
        ],
        'result' => [
          'tags' => [
            $terms['published2']->id() => $term_labels['published2'],
          ],
        ],
      ],
      [
        'arguments' => [
          ['invalid term', 'CONTAINS'],
        ],
        'result' => [],
      ],
      [
        'arguments' => [
          ['Term unpublished', 'CONTAINS'],
        ],
        'result' => [],
      ],
    ];
    $this->assertReferenceable($selection_options, $referenceable_tests, 'Term handler');

    // Test as an admin.
    $admin_user = $this->createUser(['access content', 'administer taxonomy']);
    $this->setCurrentUser($admin_user);
    $referenceable_tests = [
      [
        'arguments' => [
          [NULL, 'CONTAINS'],
        ],
        'result' => [
          'tags' => [
            $terms['published1']->id() => $term_labels['published1'],
            $terms['published2']->id() => $term_labels['published2'],
            $terms['unpublished']->id() => $term_labels['unpublished'],
            $terms['published3']->id() => '-' . $term_labels['published3'],
            $terms['published4']->id() => '--' . $term_labels['published4'],
          ],
        ],
      ],
      [
        'arguments' => [
          ['Term unpublished', 'CONTAINS'],
        ],
        'result' => [
          'tags' => [
            $terms['unpublished']->id() => $term_labels['unpublished'],
          ],
        ],
      ],
    ];
    $this->assertReferenceable($selection_options, $referenceable_tests, 'Term handler (admin)');
  }

684
}