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\node\Entity\Node;
10 use Drupal\Tests\BrowserTestBase;
11 use Drupal\node\NodeInterface;
12 use Drupal\user\Entity\User;
13 use Drupal\comment\Entity\Comment;
16 * Tests for the base handlers provided by Entity Reference.
18 * @group entity_reference
20 class EntityReferenceSelectionAccessTest extends BrowserTestBase {
29 public static $modules = ['node', 'comment'];
31 protected function setUp() {
34 // Create an Article node type.
35 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
39 * Checks that a selection plugin returns the expected results.
41 * @param array $selection_options
42 * An array of options as required by entity reference selection plugins.
44 * An array of tests to run.
45 * @param string $handler_name
46 * The name of the entity type selection handler being tested.
48 protected function assertReferenceable(array $selection_options, $tests, $handler_name) {
49 $handler = \Drupal::service('plugin.manager.entity_reference_selection')->getInstance($selection_options);
51 foreach ($tests as $test) {
52 foreach ($test['arguments'] as $arguments) {
53 $result = call_user_func_array([$handler, 'getReferenceableEntities'], $arguments);
54 $this->assertEqual($result, $test['result'], format_string('Valid result set returned by @handler.', ['@handler' => $handler_name]));
56 $result = call_user_func_array([$handler, 'countReferenceableEntities'], $arguments);
57 if (!empty($test['result'])) {
58 $bundle = key($test['result']);
59 $count = count($test['result'][$bundle]);
65 $this->assertEqual($result, $count, format_string('Valid count returned by @handler.', ['@handler' => $handler_name]));
71 * Test the node-specific overrides of the entity handler.
73 public function testNodeHandler() {
74 $selection_options = [
75 'target_type' => 'node',
76 'handler' => 'default',
77 'target_bundles' => NULL,
80 // Build a set of test data.
81 // Titles contain HTML-special characters to test escaping.
85 'status' => NodeInterface::PUBLISHED,
86 'title' => 'Node published1 (<&>)',
91 'status' => NodeInterface::PUBLISHED,
92 'title' => 'Node published2 (<&>)',
97 'status' => NodeInterface::NOT_PUBLISHED,
98 'title' => 'Node unpublished (<&>)',
105 foreach ($node_values as $key => $values) {
106 $node = Node::create($values);
108 $nodes[$key] = $node;
109 $node_labels[$key] = Html::escape($node->label());
112 // Test as a non-admin.
113 $normal_user = $this->drupalCreateUser(['access content']);
114 \Drupal::currentUser()->setAccount($normal_user);
115 $referenceable_tests = [
122 $nodes['published1']->id() => $node_labels['published1'],
123 $nodes['published2']->id() => $node_labels['published2'],
129 ['published1', 'CONTAINS'],
130 ['Published1', 'CONTAINS'],
134 $nodes['published1']->id() => $node_labels['published1'],
140 ['published2', 'CONTAINS'],
141 ['Published2', 'CONTAINS'],
145 $nodes['published2']->id() => $node_labels['published2'],
151 ['invalid node', 'CONTAINS'],
157 ['Node unpublished', 'CONTAINS'],
162 $this->assertReferenceable($selection_options, $referenceable_tests, 'Node handler');
165 $admin_user = $this->drupalCreateUser(['access content', 'bypass node access']);
166 \Drupal::currentUser()->setAccount($admin_user);
167 $referenceable_tests = [
174 $nodes['published1']->id() => $node_labels['published1'],
175 $nodes['published2']->id() => $node_labels['published2'],
176 $nodes['unpublished']->id() => $node_labels['unpublished'],
182 ['Node unpublished', 'CONTAINS'],
186 $nodes['unpublished']->id() => $node_labels['unpublished'],
191 $this->assertReferenceable($selection_options, $referenceable_tests, 'Node handler (admin)');
195 * Test the user-specific overrides of the entity handler.
197 public function testUserHandler() {
198 $selection_options = [
199 'target_type' => 'user',
200 'handler' => 'default',
201 'target_bundles' => NULL,
202 'include_anonymous' => TRUE,
205 // Build a set of test data.
207 'anonymous' => User::load(0),
208 'admin' => User::load(1),
210 'name' => 'non_admin <&>',
211 'mail' => 'non_admin@example.com',
213 'pass' => user_password(),
217 'name' => 'blocked <&>',
218 'mail' => 'blocked@example.com',
220 'pass' => user_password(),
225 $user_values['anonymous']->name = $this->config('user.settings')->get('anonymous');
229 foreach ($user_values as $key => $values) {
230 if (is_array($values)) {
231 $account = User::create($values);
237 $users[$key] = $account;
238 $user_labels[$key] = Html::escape($account->getUsername());
241 // Test as a non-admin.
242 \Drupal::currentUser()->setAccount($users['non_admin']);
243 $referenceable_tests = [
250 $users['admin']->id() => $user_labels['admin'],
251 $users['non_admin']->id() => $user_labels['non_admin'],
257 ['non_admin', 'CONTAINS'],
258 ['NON_ADMIN', 'CONTAINS'],
262 $users['non_admin']->id() => $user_labels['non_admin'],
268 ['invalid user', 'CONTAINS'],
274 ['blocked', 'CONTAINS'],
279 $this->assertReferenceable($selection_options, $referenceable_tests, 'User handler');
281 \Drupal::currentUser()->setAccount($users['admin']);
282 $referenceable_tests = [
289 $users['anonymous']->id() => $user_labels['anonymous'],
290 $users['admin']->id() => $user_labels['admin'],
291 $users['non_admin']->id() => $user_labels['non_admin'],
292 $users['blocked']->id() => $user_labels['blocked'],
298 ['blocked', 'CONTAINS'],
302 $users['blocked']->id() => $user_labels['blocked'],
308 ['Anonymous', 'CONTAINS'],
309 ['anonymous', 'CONTAINS'],
313 $users['anonymous']->id() => $user_labels['anonymous'],
318 $this->assertReferenceable($selection_options, $referenceable_tests, 'User handler (admin)');
320 // Test the 'include_anonymous' option.
321 $selection_options['include_anonymous'] = FALSE;
322 $referenceable_tests = [
325 ['Anonymous', 'CONTAINS'],
326 ['anonymous', 'CONTAINS'],
331 $this->assertReferenceable($selection_options, $referenceable_tests, 'User handler (does not include anonymous)');
333 // Check that the Anonymous user is not included in the results when no
334 // label matching is done, for example when using the 'options_select'
336 $referenceable_tests = [
343 $users['admin']->id() => $user_labels['admin'],
344 $users['non_admin']->id() => $user_labels['non_admin'],
345 $users['blocked']->id() => $user_labels['blocked'],
350 $this->assertReferenceable($selection_options, $referenceable_tests, 'User handler (does not include anonymous)');
354 * Test the comment-specific overrides of the entity handler.
356 public function testCommentHandler() {
357 $selection_options = [
358 'target_type' => 'comment',
359 'handler' => 'default',
360 'target_bundles' => NULL,
363 // Build a set of test data.
368 'title' => 'Node published',
374 'title' => 'Node unpublished',
379 foreach ($node_values as $key => $values) {
380 $node = Node::create($values);
382 $nodes[$key] = $node;
385 // Create comment field on article.
386 $this->addDefaultCommentField('node', 'article');
389 'published_published' => [
390 'entity_id' => $nodes['published']->id(),
391 'entity_type' => 'node',
392 'field_name' => 'comment',
396 'status' => CommentInterface::PUBLISHED,
397 'subject' => 'Comment Published <&>',
398 'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
400 'published_unpublished' => [
401 'entity_id' => $nodes['published']->id(),
402 'entity_type' => 'node',
403 'field_name' => 'comment',
407 'status' => CommentInterface::NOT_PUBLISHED,
408 'subject' => 'Comment Unpublished <&>',
409 'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
411 'unpublished_published' => [
412 'entity_id' => $nodes['unpublished']->id(),
413 'entity_type' => 'node',
414 'field_name' => 'comment',
418 'status' => CommentInterface::NOT_PUBLISHED,
419 'subject' => 'Comment Published on Unpublished node <&>',
420 'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
425 $comment_labels = [];
426 foreach ($comment_values as $key => $values) {
427 $comment = Comment::create($values);
429 $comments[$key] = $comment;
430 $comment_labels[$key] = Html::escape($comment->label());
433 // Test as a non-admin.
434 $normal_user = $this->drupalCreateUser(['access content', 'access comments']);
435 \Drupal::currentUser()->setAccount($normal_user);
436 $referenceable_tests = [
443 $comments['published_published']->cid->value => $comment_labels['published_published'],
449 ['Published', 'CONTAINS'],
453 $comments['published_published']->cid->value => $comment_labels['published_published'],
459 ['invalid comment', 'CONTAINS'],
465 ['Comment Unpublished', 'CONTAINS'],
470 $this->assertReferenceable($selection_options, $referenceable_tests, 'Comment handler');
472 // Test as a comment admin.
473 $admin_user = $this->drupalCreateUser(['access content', 'access comments', 'administer comments']);
474 \Drupal::currentUser()->setAccount($admin_user);
475 $referenceable_tests = [
482 $comments['published_published']->cid->value => $comment_labels['published_published'],
483 $comments['published_unpublished']->cid->value => $comment_labels['published_unpublished'],
488 $this->assertReferenceable($selection_options, $referenceable_tests, 'Comment handler (comment admin)');
490 // Test as a node and comment admin.
491 $admin_user = $this->drupalCreateUser(['access content', 'access comments', 'administer comments', 'bypass node access']);
492 \Drupal::currentUser()->setAccount($admin_user);
493 $referenceable_tests = [
500 $comments['published_published']->cid->value => $comment_labels['published_published'],
501 $comments['published_unpublished']->cid->value => $comment_labels['published_unpublished'],
502 $comments['unpublished_published']->cid->value => $comment_labels['unpublished_published'],
507 $this->assertReferenceable($selection_options, $referenceable_tests, 'Comment handler (comment + node admin)');