You are here

public function MediaSourceInfoAlterEventTest::testMediaSourceInfoAlter in Hook Event Dispatcher 3.x

Same name and namespace in other branches
  1. 8.2 modules/media_event_dispatcher/tests/src/Unit/Media/MediaSourceInfoAlterEventTest.php \Drupal\Tests\media_event_dispatcher\Unit\Media\MediaSourceInfoAlterEventTest::testMediaSourceInfoAlter()

MediaSourceInfoAlterEvent sources array alter test.

This tests altering the media source plugin definitions array.

File

modules/media_event_dispatcher/tests/src/Unit/Media/MediaSourceInfoAlterEventTest.php, line 44

Class

MediaSourceInfoAlterEventTest
Class MediaSourceInfoAlterEventTest.

Namespace

Drupal\Tests\media_event_dispatcher\Unit\Media

Code

public function testMediaSourceInfoAlter() {
  $sources = $expectedSources = [
    'image' => [
      'id' => 'image',
      'label' => new TranslatableMarkup('Image'),
      'description' => new TranslatableMarkup('Use local images for reusable media.'),
      'class' => 'Drupal\\media\\Plugin\\media\\Source\\Image',
      'allowed_field_types' => [
        'image',
      ],
    ],
  ];
  $this->manager
    ->setEventCallbacks([
    HookEventDispatcherInterface::MEDIA_SOURCE_INFO_ALTER => static function (MediaSourceInfoAlterEvent $event) {
      $sources =& $event
        ->getSources();
      $sources['image']['allowed_field_types'][] = 'test';
    },
  ]);

  // Run the procedural hook which should trigger the above handler.
  media_event_dispatcher_media_source_info_alter($sources);

  /** @var \Drupal\media_event_dispatcher\Event\Media\MediaSourceInfoAlterEvent $event */
  $event = $this->manager
    ->getRegisteredEvent(HookEventDispatcherInterface::MEDIA_SOURCE_INFO_ALTER);
  self::assertSame($sources, $event
    ->getSources());
  $expectedSources['image']['allowed_field_types'][] = 'test';
  self::assertSame($expectedSources, $sources);
}