You are here

public function MediaSourceTest::testDefaultName in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media/tests/src/Kernel/MediaSourceTest.php \Drupal\Tests\media\Kernel\MediaSourceTest::testDefaultName()
  2. 10 core/modules/media/tests/src/Kernel/MediaSourceTest.php \Drupal\Tests\media\Kernel\MediaSourceTest::testDefaultName()

Tests default media name functionality.

File

core/modules/media/tests/src/Kernel/MediaSourceTest.php, line 87

Class

MediaSourceTest
Tests media source plugins related logic.

Namespace

Drupal\Tests\media\Kernel

Code

public function testDefaultName() {

  // Make sure that the default name is set if not provided by the user.

  /** @var \Drupal\media\MediaInterface $media */
  $media = Media::create([
    'bundle' => $this->testMediaType
      ->id(),
  ]);
  $media_source = $media
    ->getSource();
  $this
    ->assertSame('default_name', $media_source
    ->getPluginDefinition()['default_name_metadata_attribute'], 'Default metadata attribute is not used for the default name.');
  $this
    ->assertSame('media:' . $media
    ->bundle() . ':' . $media
    ->uuid(), $media_source
    ->getMetadata($media, 'default_name'), 'Value of the default name metadata attribute does not look correct.');
  $this
    ->assertSame('media:' . $media
    ->bundle() . ':' . $media
    ->uuid(), $media
    ->getName(), 'Default name was not used correctly by getName().');
  $this
    ->assertSame($media
    ->getName(), $media
    ->label(), 'Default name and label are not the same.');
  $media
    ->save();
  $this
    ->assertSame('media:' . $media
    ->bundle() . ':' . $media
    ->uuid(), $media
    ->getName(), 'Default name was not saved correctly.');
  $this
    ->assertSame($media
    ->getName(), $media
    ->label(), 'The label changed during save.');

  // Make sure that the user-supplied name is used.

  /** @var \Drupal\media\MediaInterface $media */
  $name = 'User-supplied name';
  $media = Media::create([
    'bundle' => $this->testMediaType
      ->id(),
    'name' => $name,
  ]);
  $media_source = $media
    ->getSource();
  $this
    ->assertSame('default_name', $media_source
    ->getPluginDefinition()['default_name_metadata_attribute'], 'Default metadata attribute is not used for the default name.');
  $this
    ->assertSame('media:' . $media
    ->bundle() . ':' . $media
    ->uuid(), $media_source
    ->getMetadata($media, 'default_name'), 'Value of the default name metadata attribute does not look correct.');
  $media
    ->save();
  $this
    ->assertSame($name, $media
    ->getName(), 'User-supplied name was not set correctly.');
  $this
    ->assertSame($media
    ->getName(), $media
    ->label(), 'The user-supplied name does not match the label.');

  // Change the default name attribute and see if it is used to set the name.
  $name = 'Old Major';
  \Drupal::state()
    ->set('media_source_test_attributes', [
    'alternative_name' => [
      'title' => 'Alternative name',
      'value' => $name,
    ],
  ]);
  \Drupal::state()
    ->set('media_source_test_definition', [
    'default_name_metadata_attribute' => 'alternative_name',
  ]);

  /** @var \Drupal\media\MediaInterface $media */
  $media = Media::create([
    'bundle' => $this->testMediaType
      ->id(),
  ]);
  $media_source = $media
    ->getSource();
  $this
    ->assertSame('alternative_name', $media_source
    ->getPluginDefinition()['default_name_metadata_attribute'], 'Correct metadata attribute is not used for the default name.');
  $this
    ->assertSame($name, $media_source
    ->getMetadata($media, 'alternative_name'), 'Value of the default name metadata attribute does not look correct.');
  $media
    ->save();
  $this
    ->assertSame($name, $media
    ->getName(), 'Default name was not set correctly.');
  $this
    ->assertSame($media
    ->getName(), $media
    ->label(), 'The default name does not match the label.');
}