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 'handler_settings' => [
78 'target_bundles' => NULL,
82 // Build a set of test data.
83 // Titles contain HTML-special characters to test escaping.
87 'status' => NodeInterface::PUBLISHED,
88 'title' => 'Node published1 (<&>)',
93 'status' => NodeInterface::PUBLISHED,
94 'title' => 'Node published2 (<&>)',
99 'status' => NodeInterface::NOT_PUBLISHED,
100 'title' => 'Node unpublished (<&>)',
107 foreach ($node_values as $key => $values) {
108 $node = Node::create($values);
110 $nodes[$key] = $node;
111 $node_labels[$key] = Html::escape($node->label());
114 // Test as a non-admin.
115 $normal_user = $this->drupalCreateUser(['access content']);
116 \Drupal::currentUser()->setAccount($normal_user);
117 $referenceable_tests = [
124 $nodes['published1']->id() => $node_labels['published1'],
125 $nodes['published2']->id() => $node_labels['published2'],
131 ['published1', 'CONTAINS'],
132 ['Published1', 'CONTAINS'],
136 $nodes['published1']->id() => $node_labels['published1'],
142 ['published2', 'CONTAINS'],
143 ['Published2', 'CONTAINS'],
147 $nodes['published2']->id() => $node_labels['published2'],
153 ['invalid node', 'CONTAINS'],
159 ['Node unpublished', 'CONTAINS'],
164 $this->assertReferenceable($selection_options, $referenceable_tests, 'Node handler');
167 $admin_user = $this->drupalCreateUser(['access content', 'bypass node access']);
168 \Drupal::currentUser()->setAccount($admin_user);
169 $referenceable_tests = [
176 $nodes['published1']->id() => $node_labels['published1'],
177 $nodes['published2']->id() => $node_labels['published2'],
178 $nodes['unpublished']->id() => $node_labels['unpublished'],
184 ['Node unpublished', 'CONTAINS'],
188 $nodes['unpublished']->id() => $node_labels['unpublished'],
193 $this->assertReferenceable($selection_options, $referenceable_tests, 'Node handler (admin)');
197 * Test the user-specific overrides of the entity handler.
199 public function testUserHandler() {
200 $selection_options = [
201 'target_type' => 'user',
202 'handler' => 'default',
203 'handler_settings' => [
204 'target_bundles' => NULL,
205 'include_anonymous' => TRUE,
209 // Build a set of test data.
211 'anonymous' => User::load(0),
212 'admin' => User::load(1),
214 'name' => 'non_admin <&>',
215 'mail' => 'non_admin@example.com',
217 'pass' => user_password(),
221 'name' => 'blocked <&>',
222 'mail' => 'blocked@example.com',
224 'pass' => user_password(),
229 $user_values['anonymous']->name = $this->config('user.settings')->get('anonymous');
233 foreach ($user_values as $key => $values) {
234 if (is_array($values)) {
235 $account = User::create($values);
241 $users[$key] = $account;
242 $user_labels[$key] = Html::escape($account->getUsername());
245 // Test as a non-admin.
246 \Drupal::currentUser()->setAccount($users['non_admin']);
247 $referenceable_tests = [
254 $users['admin']->id() => $user_labels['admin'],
255 $users['non_admin']->id() => $user_labels['non_admin'],
261 ['non_admin', 'CONTAINS'],
262 ['NON_ADMIN', 'CONTAINS'],
266 $users['non_admin']->id() => $user_labels['non_admin'],
272 ['invalid user', 'CONTAINS'],
278 ['blocked', 'CONTAINS'],
283 $this->assertReferenceable($selection_options, $referenceable_tests, 'User handler');
285 \Drupal::currentUser()->setAccount($users['admin']);
286 $referenceable_tests = [
293 $users['anonymous']->id() => $user_labels['anonymous'],
294 $users['admin']->id() => $user_labels['admin'],
295 $users['non_admin']->id() => $user_labels['non_admin'],
296 $users['blocked']->id() => $user_labels['blocked'],
302 ['blocked', 'CONTAINS'],
306 $users['blocked']->id() => $user_labels['blocked'],
312 ['Anonymous', 'CONTAINS'],
313 ['anonymous', 'CONTAINS'],
317 $users['anonymous']->id() => $user_labels['anonymous'],
322 $this->assertReferenceable($selection_options, $referenceable_tests, 'User handler (admin)');
324 // Test the 'include_anonymous' option.
325 $selection_options['handler_settings']['include_anonymous'] = FALSE;
326 $referenceable_tests = [
329 ['Anonymous', 'CONTAINS'],
330 ['anonymous', 'CONTAINS'],
335 $this->assertReferenceable($selection_options, $referenceable_tests, 'User handler (does not include anonymous)');
337 // Check that the Anonymous user is not included in the results when no
338 // label matching is done, for example when using the 'options_select'
340 $referenceable_tests = [
347 $users['admin']->id() => $user_labels['admin'],
348 $users['non_admin']->id() => $user_labels['non_admin'],
349 $users['blocked']->id() => $user_labels['blocked'],
354 $this->assertReferenceable($selection_options, $referenceable_tests, 'User handler (does not include anonymous)');
358 * Test the comment-specific overrides of the entity handler.
360 public function testCommentHandler() {
361 $selection_options = [
362 'target_type' => 'comment',
363 'handler' => 'default',
364 'handler_settings' => [
365 'target_bundles' => NULL,
369 // Build a set of test data.
374 'title' => 'Node published',
380 'title' => 'Node unpublished',
385 foreach ($node_values as $key => $values) {
386 $node = Node::create($values);
388 $nodes[$key] = $node;
391 // Create comment field on article.
392 $this->addDefaultCommentField('node', 'article');
395 'published_published' => [
396 'entity_id' => $nodes['published']->id(),
397 'entity_type' => 'node',
398 'field_name' => 'comment',
402 'status' => CommentInterface::PUBLISHED,
403 'subject' => 'Comment Published <&>',
404 'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
406 'published_unpublished' => [
407 'entity_id' => $nodes['published']->id(),
408 'entity_type' => 'node',
409 'field_name' => 'comment',
413 'status' => CommentInterface::NOT_PUBLISHED,
414 'subject' => 'Comment Unpublished <&>',
415 'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
417 'unpublished_published' => [
418 'entity_id' => $nodes['unpublished']->id(),
419 'entity_type' => 'node',
420 'field_name' => 'comment',
424 'status' => CommentInterface::NOT_PUBLISHED,
425 'subject' => 'Comment Published on Unpublished node <&>',
426 'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
431 $comment_labels = [];
432 foreach ($comment_values as $key => $values) {
433 $comment = Comment::create($values);
435 $comments[$key] = $comment;
436 $comment_labels[$key] = Html::escape($comment->label());
439 // Test as a non-admin.
440 $normal_user = $this->drupalCreateUser(['access content', 'access comments']);
441 \Drupal::currentUser()->setAccount($normal_user);
442 $referenceable_tests = [
449 $comments['published_published']->cid->value => $comment_labels['published_published'],
455 ['Published', 'CONTAINS'],
459 $comments['published_published']->cid->value => $comment_labels['published_published'],
465 ['invalid comment', 'CONTAINS'],
471 ['Comment Unpublished', 'CONTAINS'],
476 $this->assertReferenceable($selection_options, $referenceable_tests, 'Comment handler');
478 // Test as a comment admin.
479 $admin_user = $this->drupalCreateUser(['access content', 'access comments', 'administer comments']);
480 \Drupal::currentUser()->setAccount($admin_user);
481 $referenceable_tests = [
488 $comments['published_published']->cid->value => $comment_labels['published_published'],
489 $comments['published_unpublished']->cid->value => $comment_labels['published_unpublished'],
494 $this->assertReferenceable($selection_options, $referenceable_tests, 'Comment handler (comment admin)');
496 // Test as a node and comment admin.
497 $admin_user = $this->drupalCreateUser(['access content', 'access comments', 'administer comments', 'bypass node access']);
498 \Drupal::currentUser()->setAccount($admin_user);
499 $referenceable_tests = [
506 $comments['published_published']->cid->value => $comment_labels['published_published'],
507 $comments['published_unpublished']->cid->value => $comment_labels['published_unpublished'],
508 $comments['unpublished_published']->cid->value => $comment_labels['unpublished_published'],
513 $this->assertReferenceable($selection_options, $referenceable_tests, 'Comment handler (comment + node admin)');