You are here

public function EntityReferenceSelectionAccessTest::testMediaHandler in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Functional/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php \Drupal\Tests\system\Functional\Entity\EntityReferenceSelection\EntityReferenceSelectionAccessTest::testMediaHandler()
  2. 9 core/modules/system/tests/src/Functional/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php \Drupal\Tests\system\Functional\Entity\EntityReferenceSelection\EntityReferenceSelectionAccessTest::testMediaHandler()

Tests the selection handler for the media entity type.

File

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

Class

EntityReferenceSelectionAccessTest
Tests for the base handlers provided by Entity Reference.

Namespace

Drupal\Tests\system\Functional\Entity\EntityReferenceSelection

Code

public function testMediaHandler() {
  $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)');
}