function EntityReferenceSelectionAccessTest::testMediaHandler

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

Tests the selection handler for the media entity type.

File

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

Class

EntityReferenceSelectionAccessTest
Tests for the base handlers provided by Entity Reference.

Namespace

Drupal\Tests\system\Kernel\Entity\EntityReferenceSelection

Code

public function testMediaHandler() : void {
    $selection_options = [
        'target_type' => 'media',
        'handler' => 'default',
        'target_bundles' => NULL,
    ];
    // Build a set of test data.
    $media_type = $this->createMediaType('file');
    $media_values = [
        'published' => [
            'bundle' => $media_type->id(),
            'status' => 1,
            'name' => 'Media published',
            'uid' => 1,
        ],
        'unpublished' => [
            'bundle' => $media_type->id(),
            'status' => 0,
            'name' => 'Media unpublished',
            'uid' => 1,
        ],
    ];
    $media_entities = [];
    $media_labels = [];
    foreach ($media_values as $key => $values) {
        $media = Media::create($values);
        $media->save();
        $media_entities[$key] = $media;
        $media_labels[$key] = Html::escape($media->label());
    }
    // Test as a non-admin.
    $normal_user = $this->createUser([
        'view media',
    ]);
    $this->setCurrentUser($normal_user);
    $referenceable_tests = [
        [
            'arguments' => [
                [
                    NULL,
                    'CONTAINS',
                ],
            ],
            'result' => [
                $media_type->id() => [
                    $media_entities['published']->id() => $media_labels['published'],
                ],
            ],
        ],
        [
            'arguments' => [
                [
                    'Media unpublished',
                    'CONTAINS',
                ],
            ],
            'result' => [],
        ],
    ];
    $this->assertReferenceable($selection_options, $referenceable_tests, 'Media handler');
    // Test as an admin.
    $admin_user = $this->createUser([
        'view media',
        'administer media',
    ]);
    $this->setCurrentUser($admin_user);
    $referenceable_tests = [
        [
            'arguments' => [
                [
                    NULL,
                    'CONTAINS',
                ],
            ],
            'result' => [
                $media_type->id() => [
                    $media_entities['published']->id() => $media_labels['published'],
                    $media_entities['unpublished']->id() => $media_labels['unpublished'],
                ],
            ],
        ],
        [
            'arguments' => [
                [
                    'Media unpublished',
                    'CONTAINS',
                ],
            ],
            'result' => [
                $media_type->id() => [
                    $media_entities['unpublished']->id() => $media_labels['unpublished'],
                ],
            ],
        ],
    ];
    $this->assertReferenceable($selection_options, $referenceable_tests, 'Media handler (admin)');
}

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