3 namespace Drupal\Tests\system\Functional\Entity\EntityReferenceSelection;
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\media\Entity\Media;
11 use Drupal\node\Entity\Node;
12 use Drupal\taxonomy\Entity\Term;
13 use Drupal\taxonomy\Entity\Vocabulary;
14 use Drupal\node\NodeInterface;
15 use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
16 use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
17 use Drupal\Tests\user\Traits\UserCreationTrait;
18 use Drupal\user\Entity\User;
19 use Drupal\comment\Entity\Comment;
22 * Tests for the base handlers provided by Entity Reference.
24 * @group entity_reference
26 class EntityReferenceSelectionAccessTest extends KernelTestBase {
29 use ContentTypeCreationTrait;
30 use MediaTypeCreationTrait;
31 use UserCreationTrait;
38 public static $modules = ['comment', 'field', 'file', 'image', 'node', 'media', 'system', 'taxonomy', 'text', 'user'];
43 protected function setUp() {
46 $this->installSchema('system', 'sequences');
47 $this->installSchema('comment', ['comment_entity_statistics']);
48 $this->installSchema('file', ['file_usage']);
50 $this->installEntitySchema('comment');
51 $this->installEntitySchema('file');
52 $this->installEntitySchema('media');
53 $this->installEntitySchema('node');
54 $this->installEntitySchema('taxonomy_term');
55 $this->installEntitySchema('user');
57 $this->installConfig(['comment', 'field', 'media', 'node', 'taxonomy', 'user']);
59 // Create the anonymous and the admin users.
60 $anonymous_user = User::create([
64 $anonymous_user->save();
65 $admin_user = User::create([
74 * Checks that a selection plugin returns the expected results.
76 * @param array $selection_options
77 * An array of options as required by entity reference selection plugins.
79 * An array of tests to run.
80 * @param string $handler_name
81 * The name of the entity type selection handler being tested.
83 protected function assertReferenceable(array $selection_options, $tests, $handler_name) {
84 $handler = \Drupal::service('plugin.manager.entity_reference_selection')->getInstance($selection_options);
86 foreach ($tests as $test) {
87 foreach ($test['arguments'] as $arguments) {
88 $result = call_user_func_array([$handler, 'getReferenceableEntities'], $arguments);
89 $this->assertEqual($result, $test['result'], format_string('Valid result set returned by @handler.', ['@handler' => $handler_name]));
91 $result = call_user_func_array([$handler, 'countReferenceableEntities'], $arguments);
92 if (!empty($test['result'])) {
93 $bundle = key($test['result']);
94 $count = count($test['result'][$bundle]);
100 $this->assertEqual($result, $count, format_string('Valid count returned by @handler.', ['@handler' => $handler_name]));
106 * Test the node-specific overrides of the entity handler.
108 public function testNodeHandler() {
109 $selection_options = [
110 'target_type' => 'node',
111 'handler' => 'default',
112 'target_bundles' => NULL,
115 // Build a set of test data.
116 // Titles contain HTML-special characters to test escaping.
120 'status' => NodeInterface::PUBLISHED,
121 'title' => 'Node published1 (<&>)',
126 'status' => NodeInterface::PUBLISHED,
127 'title' => 'Node published2 (<&>)',
132 'status' => NodeInterface::NOT_PUBLISHED,
133 'title' => 'Node unpublished (<&>)',
140 foreach ($node_values as $key => $values) {
141 $node = Node::create($values);
143 $nodes[$key] = $node;
144 $node_labels[$key] = Html::escape($node->label());
147 // Test as a non-admin.
148 $normal_user = $this->createUser(['access content']);
149 $this->setCurrentUser($normal_user);
150 $referenceable_tests = [
157 $nodes['published1']->id() => $node_labels['published1'],
158 $nodes['published2']->id() => $node_labels['published2'],
164 ['published1', 'CONTAINS'],
165 ['Published1', 'CONTAINS'],
169 $nodes['published1']->id() => $node_labels['published1'],
175 ['published2', 'CONTAINS'],
176 ['Published2', 'CONTAINS'],
180 $nodes['published2']->id() => $node_labels['published2'],
186 ['invalid node', 'CONTAINS'],
192 ['Node unpublished', 'CONTAINS'],
197 $this->assertReferenceable($selection_options, $referenceable_tests, 'Node handler');
200 $content_admin = $this->createUser(['access content', 'bypass node access']);
201 $this->setCurrentUser($content_admin);
202 $referenceable_tests = [
209 $nodes['published1']->id() => $node_labels['published1'],
210 $nodes['published2']->id() => $node_labels['published2'],
211 $nodes['unpublished']->id() => $node_labels['unpublished'],
217 ['Node unpublished', 'CONTAINS'],
221 $nodes['unpublished']->id() => $node_labels['unpublished'],
226 $this->assertReferenceable($selection_options, $referenceable_tests, 'Node handler (admin)');
230 * Test the user-specific overrides of the entity handler.
232 public function testUserHandler() {
233 $selection_options = [
234 'target_type' => 'user',
235 'handler' => 'default',
236 'target_bundles' => NULL,
237 'include_anonymous' => TRUE,
240 // Build a set of test data.
242 'anonymous' => User::load(0),
243 'admin' => User::load(1),
245 'name' => 'non_admin <&>',
246 'mail' => 'non_admin@example.com',
248 'pass' => user_password(),
252 'name' => 'blocked <&>',
253 'mail' => 'blocked@example.com',
255 'pass' => user_password(),
260 $user_values['anonymous']->name = $this->config('user.settings')->get('anonymous');
264 foreach ($user_values as $key => $values) {
265 if (is_array($values)) {
266 $account = User::create($values);
272 $users[$key] = $account;
273 $user_labels[$key] = Html::escape($account->getUsername());
276 // Test as a non-admin.
277 $this->setCurrentUser($users['non_admin']);
278 $referenceable_tests = [
285 $users['admin']->id() => $user_labels['admin'],
286 $users['non_admin']->id() => $user_labels['non_admin'],
292 ['non_admin', 'CONTAINS'],
293 ['NON_ADMIN', 'CONTAINS'],
297 $users['non_admin']->id() => $user_labels['non_admin'],
303 ['invalid user', 'CONTAINS'],
309 ['blocked', 'CONTAINS'],
314 $this->assertReferenceable($selection_options, $referenceable_tests, 'User handler');
316 $this->setCurrentUser($users['admin']);
317 $referenceable_tests = [
324 $users['anonymous']->id() => $user_labels['anonymous'],
325 $users['admin']->id() => $user_labels['admin'],
326 $users['non_admin']->id() => $user_labels['non_admin'],
327 $users['blocked']->id() => $user_labels['blocked'],
333 ['blocked', 'CONTAINS'],
337 $users['blocked']->id() => $user_labels['blocked'],
343 ['Anonymous', 'CONTAINS'],
344 ['anonymous', 'CONTAINS'],
348 $users['anonymous']->id() => $user_labels['anonymous'],
353 $this->assertReferenceable($selection_options, $referenceable_tests, 'User handler (admin)');
355 // Test the 'include_anonymous' option.
356 $selection_options['include_anonymous'] = FALSE;
357 $referenceable_tests = [
360 ['Anonymous', 'CONTAINS'],
361 ['anonymous', 'CONTAINS'],
366 $this->assertReferenceable($selection_options, $referenceable_tests, 'User handler (does not include anonymous)');
368 // Check that the Anonymous user is not included in the results when no
369 // label matching is done, for example when using the 'options_select'
371 $referenceable_tests = [
378 $users['admin']->id() => $user_labels['admin'],
379 $users['non_admin']->id() => $user_labels['non_admin'],
380 $users['blocked']->id() => $user_labels['blocked'],
385 $this->assertReferenceable($selection_options, $referenceable_tests, 'User handler (does not include anonymous)');
389 * Test the comment-specific overrides of the entity handler.
391 public function testCommentHandler() {
392 $selection_options = [
393 'target_type' => 'comment',
394 'handler' => 'default',
395 'target_bundles' => NULL,
398 // Build a set of test data.
399 $this->createContentType(['type' => 'article', 'name' => 'Article']);
404 'title' => 'Node published',
410 'title' => 'Node unpublished',
415 foreach ($node_values as $key => $values) {
416 $node = Node::create($values);
418 $nodes[$key] = $node;
421 // Create comment field on article.
422 $this->addDefaultCommentField('node', 'article');
425 'published_published' => [
426 'entity_id' => $nodes['published']->id(),
427 'entity_type' => 'node',
428 'field_name' => 'comment',
432 'status' => CommentInterface::PUBLISHED,
433 'subject' => 'Comment Published <&>',
434 'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
436 'published_unpublished' => [
437 'entity_id' => $nodes['published']->id(),
438 'entity_type' => 'node',
439 'field_name' => 'comment',
443 'status' => CommentInterface::NOT_PUBLISHED,
444 'subject' => 'Comment Unpublished <&>',
445 'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
447 'unpublished_published' => [
448 'entity_id' => $nodes['unpublished']->id(),
449 'entity_type' => 'node',
450 'field_name' => 'comment',
454 'status' => CommentInterface::NOT_PUBLISHED,
455 'subject' => 'Comment Published on Unpublished node <&>',
456 'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
461 $comment_labels = [];
462 foreach ($comment_values as $key => $values) {
463 $comment = Comment::create($values);
465 $comments[$key] = $comment;
466 $comment_labels[$key] = Html::escape($comment->label());
469 // Test as a non-admin.
470 $normal_user = $this->createUser(['access content', 'access comments']);
471 $this->setCurrentUser($normal_user);
472 $referenceable_tests = [
479 $comments['published_published']->cid->value => $comment_labels['published_published'],
485 ['Published', 'CONTAINS'],
489 $comments['published_published']->cid->value => $comment_labels['published_published'],
495 ['invalid comment', 'CONTAINS'],
501 ['Comment Unpublished', 'CONTAINS'],
506 $this->assertReferenceable($selection_options, $referenceable_tests, 'Comment handler');
508 // Test as a comment admin.
509 $admin_user = $this->createUser(['access content', 'access comments', 'administer comments']);
510 $this->setCurrentUser($admin_user);
511 $referenceable_tests = [
518 $comments['published_published']->cid->value => $comment_labels['published_published'],
519 $comments['published_unpublished']->cid->value => $comment_labels['published_unpublished'],
524 $this->assertReferenceable($selection_options, $referenceable_tests, 'Comment handler (comment admin)');
526 // Test as a node and comment admin.
527 $admin_user = $this->createUser(['access content', 'access comments', 'administer comments', 'bypass node access']);
528 $this->setCurrentUser($admin_user);
529 $referenceable_tests = [
536 $comments['published_published']->cid->value => $comment_labels['published_published'],
537 $comments['published_unpublished']->cid->value => $comment_labels['published_unpublished'],
538 $comments['unpublished_published']->cid->value => $comment_labels['unpublished_published'],
543 $this->assertReferenceable($selection_options, $referenceable_tests, 'Comment handler (comment + node admin)');
547 * Test the term-specific overrides of the selection handler.
549 public function testTermHandler() {
550 // Create a 'Tags' vocabulary.
553 'description' => $this->randomMachineName(),
557 $selection_options = [
558 'target_type' => 'taxonomy_term',
559 'handler' => 'default',
560 'target_bundles' => NULL,
563 // Build a set of test data.
568 'name' => 'Term published1',
573 'name' => 'Term published2',
578 'name' => 'Term unpublished',
583 'name' => 'Term published3',
584 'parent' => 'unpublished',
589 'name' => 'Term published4',
590 'parent' => 'published3',
596 foreach ($term_values as $key => $values) {
597 $term = Term::create($values);
598 if (isset($values['parent'])) {
599 $term->parent->entity = $terms[$values['parent']];
602 $terms[$key] = $term;
603 $term_labels[$key] = Html::escape($term->label());
606 // Test as a non-admin.
607 $normal_user = $this->createUser(['access content']);
608 $this->setCurrentUser($normal_user);
609 $referenceable_tests = [
616 $terms['published1']->id() => $term_labels['published1'],
617 $terms['published2']->id() => $term_labels['published2'],
623 ['published1', 'CONTAINS'],
624 ['Published1', 'CONTAINS'],
628 $terms['published1']->id() => $term_labels['published1'],
634 ['published2', 'CONTAINS'],
635 ['Published2', 'CONTAINS'],
639 $terms['published2']->id() => $term_labels['published2'],
645 ['invalid term', 'CONTAINS'],
651 ['Term unpublished', 'CONTAINS'],
656 $this->assertReferenceable($selection_options, $referenceable_tests, 'Term handler');
659 $admin_user = $this->createUser(['access content', 'administer taxonomy']);
660 $this->setCurrentUser($admin_user);
661 $referenceable_tests = [
668 $terms['published1']->id() => $term_labels['published1'],
669 $terms['published2']->id() => $term_labels['published2'],
670 $terms['unpublished']->id() => $term_labels['unpublished'],
671 $terms['published3']->id() => '-' . $term_labels['published3'],
672 $terms['published4']->id() => '--' . $term_labels['published4'],
678 ['Term unpublished', 'CONTAINS'],
682 $terms['unpublished']->id() => $term_labels['unpublished'],
687 $this->assertReferenceable($selection_options, $referenceable_tests, 'Term handler (admin)');
691 * Tests the selection handler for the media entity type.
693 public function testMediaHandler() {
694 $selection_options = [
695 'target_type' => 'media',
696 'handler' => 'default',
697 'target_bundles' => NULL,
700 // Build a set of test data.
701 $media_type = $this->createMediaType('file');
704 'bundle' => $media_type->id(),
706 'name' => 'Media published',
710 'bundle' => $media_type->id(),
712 'name' => 'Media unpublished',
717 $media_entities = [];
719 foreach ($media_values as $key => $values) {
720 $media = Media::create($values);
722 $media_entities[$key] = $media;
723 $media_labels[$key] = Html::escape($media->label());
726 // Test as a non-admin.
727 $normal_user = $this->createUser(['view media']);
728 $this->setCurrentUser($normal_user);
729 $referenceable_tests = [
735 $media_type->id() => [
736 $media_entities['published']->id() => $media_labels['published'],
742 ['Media unpublished', 'CONTAINS'],
747 $this->assertReferenceable($selection_options, $referenceable_tests, 'Media handler');
750 $admin_user = $this->createUser(['view media', 'administer media']);
751 $this->setCurrentUser($admin_user);
752 $referenceable_tests = [
758 $media_type->id() => [
759 $media_entities['published']->id() => $media_labels['published'],
760 $media_entities['unpublished']->id() => $media_labels['unpublished'],
766 ['Media unpublished', 'CONTAINS'],
769 $media_type->id() => [
770 $media_entities['unpublished']->id() => $media_labels['unpublished'],
775 $this->assertReferenceable($selection_options, $referenceable_tests, 'Media handler (admin)');