MediaSourceInfoAlterEventTest.php in Hook Event Dispatcher 8.2
File
modules/media_event_dispatcher/tests/src/Unit/Media/MediaSourceInfoAlterEventTest.php
View source
<?php
namespace Drupal\Tests\media_event_dispatcher\Unit\Media;
use Drupal;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\media_event_dispatcher\Event\Media\MediaSourceInfoAlterEvent;
use Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy;
use PHPUnit\Framework\TestCase;
use function media_event_dispatcher_media_source_info_alter;
class MediaSourceInfoAlterEventTest extends TestCase {
private $manager;
public function setUp() : void {
$builder = new ContainerBuilder();
$this->manager = new HookEventDispatcherManagerSpy();
$builder
->set('hook_event_dispatcher.manager', $this->manager);
$builder
->compile();
Drupal::setContainer($builder);
}
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';
},
]);
media_event_dispatcher_media_source_info_alter($sources);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::MEDIA_SOURCE_INFO_ALTER);
self::assertSame($sources, $event
->getSources());
$expectedSources['image']['allowed_field_types'][] = 'test';
self::assertSame($expectedSources, $sources);
}
}