function EntityReferenceSelectionAccessTest::testTermHandler

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

Tests the term-specific overrides of the selection handler.

File

core/modules/system/tests/src/Kernel/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php, line 564

Class

EntityReferenceSelectionAccessTest
Tests for the base handlers provided by Entity Reference.

Namespace

Drupal\Tests\system\Kernel\Entity\EntityReferenceSelection

Code

public function testTermHandler() : void {
    // 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)');
}

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