function EntityReferenceSelectionAccessTest::testNodeHandler

Same name in other branches
  1. 8.9.x core/modules/system/tests/src/Functional/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php \Drupal\Tests\system\Functional\Entity\EntityReferenceSelection\EntityReferenceSelectionAccessTest::testNodeHandler()
  2. 10 core/modules/system/tests/src/Kernel/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php \Drupal\Tests\system\Kernel\Entity\EntityReferenceSelection\EntityReferenceSelectionAccessTest::testNodeHandler()
  3. 11.x core/modules/system/tests/src/Kernel/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php \Drupal\Tests\system\Kernel\Entity\EntityReferenceSelection\EntityReferenceSelectionAccessTest::testNodeHandler()

Tests the node-specific overrides of the entity handler.

File

core/modules/system/tests/src/Functional/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php, line 122

Class

EntityReferenceSelectionAccessTest
Tests for the base handlers provided by Entity Reference.

Namespace

Drupal\Tests\system\Functional\Entity\EntityReferenceSelection

Code

public function testNodeHandler() {
    $selection_options = [
        'target_type' => 'node',
        'handler' => 'default',
        'target_bundles' => NULL,
    ];
    // Build a set of test data.
    // Titles contain HTML-special characters to test escaping.
    $node_values = [
        'published1' => [
            'type' => 'article',
            'status' => NodeInterface::PUBLISHED,
            'title' => 'Node published1 (<&>)',
            'uid' => 1,
        ],
        'published2' => [
            'type' => 'article',
            'status' => NodeInterface::PUBLISHED,
            'title' => 'Node published2 (<&>)',
            'uid' => 1,
        ],
        'unpublished' => [
            'type' => 'article',
            'status' => NodeInterface::NOT_PUBLISHED,
            'title' => 'Node unpublished (<&>)',
            'uid' => 1,
        ],
    ];
    $nodes = [];
    $node_labels = [];
    foreach ($node_values as $key => $values) {
        $node = Node::create($values);
        $node->save();
        $nodes[$key] = $node;
        $node_labels[$key] = Html::escape($node->label());
    }
    // Test as a non-admin.
    $normal_user = $this->createUser([
        'access content',
    ]);
    $this->setCurrentUser($normal_user);
    $referenceable_tests = [
        [
            'arguments' => [
                [
                    NULL,
                    'CONTAINS',
                ],
            ],
            'result' => [
                'article' => [
                    $nodes['published1']->id() => $node_labels['published1'],
                    $nodes['published2']->id() => $node_labels['published2'],
                ],
            ],
        ],
        [
            'arguments' => [
                [
                    'published1',
                    'CONTAINS',
                ],
                [
                    'Published1',
                    'CONTAINS',
                ],
            ],
            'result' => [
                'article' => [
                    $nodes['published1']->id() => $node_labels['published1'],
                ],
            ],
        ],
        [
            'arguments' => [
                [
                    'published2',
                    'CONTAINS',
                ],
                [
                    'Published2',
                    'CONTAINS',
                ],
            ],
            'result' => [
                'article' => [
                    $nodes['published2']->id() => $node_labels['published2'],
                ],
            ],
        ],
        [
            'arguments' => [
                [
                    'invalid node',
                    'CONTAINS',
                ],
            ],
            'result' => [],
        ],
        [
            'arguments' => [
                [
                    'Node unpublished',
                    'CONTAINS',
                ],
            ],
            'result' => [],
        ],
    ];
    $this->assertReferenceable($selection_options, $referenceable_tests, 'Node handler');
    // Test as an admin.
    $content_admin = $this->createUser([
        'access content',
        'bypass node access',
    ]);
    $this->setCurrentUser($content_admin);
    $referenceable_tests = [
        [
            'arguments' => [
                [
                    NULL,
                    'CONTAINS',
                ],
            ],
            'result' => [
                'article' => [
                    $nodes['published1']->id() => $node_labels['published1'],
                    $nodes['published2']->id() => $node_labels['published2'],
                    $nodes['unpublished']->id() => $node_labels['unpublished'],
                ],
            ],
        ],
        [
            'arguments' => [
                [
                    'Node unpublished',
                    'CONTAINS',
                ],
            ],
            'result' => [
                'article' => [
                    $nodes['unpublished']->id() => $node_labels['unpublished'],
                ],
            ],
        ],
    ];
    $this->assertReferenceable($selection_options, $referenceable_tests, 'Node handler (admin)');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.