You are here

public function OEmbedResourceUrlAlterEventTest::testOembedResourceParsedUrlAlter in Hook Event Dispatcher 8.2

Same name and namespace in other branches
  1. 3.x modules/media_event_dispatcher/tests/src/Unit/Media/OEmbedResourceUrlAlterEventTest.php \Drupal\Tests\media_event_dispatcher\Unit\Media\OEmbedResourceUrlAlterEventTest::testOembedResourceParsedUrlAlter()

OEmbedResourceUrlAlterEvent parsed URL alter test.

This tests altering the parsed URL array for an oEmbed data request.

File

modules/media_event_dispatcher/tests/src/Unit/Media/OEmbedResourceUrlAlterEventTest.php, line 61

Class

OEmbedResourceUrlAlterEventTest
Class OEmbedResourceUrlAlterEventTest.

Namespace

Drupal\Tests\media_event_dispatcher\Unit\Media

Code

public function testOembedResourceParsedUrlAlter() {

  // A mock URL array as would be provided in the real hook.
  $url = $expected = [
    'path' => 'https://vimeo.com/api/oembed.json',
    'query' => [
      'url' => 'https://vimeo.com/118146193',
    ],
    'fragment' => '',
  ];

  /** @var \Drupal\media\OEmbed\Provider $provider */
  $provider = $this
    ->getMockProvider();
  $this->manager
    ->setEventCallbacks([
    HookEventDispatcherInterface::MEDIA_OEMBED_RESOURCE_DATA_ALTER => function (OEmbedResourceUrlAlterEvent $event) {
      $url =& $event
        ->getParsedURL();
      $url['query']['width'] = '1280';
    },
  ]);

  // Run the procedural hook which should trigger the above handler.
  media_event_dispatcher_oembed_resource_url_alter($url, $provider);
  $expected['query']['width'] = '1280';
  self::assertSame($expected, $url);

  /** @var \Drupal\media_event_dispatcher\Event\Media\OEmbedResourceUrlAlterEvent $event */
  $event = $this->manager
    ->getRegisteredEvent(HookEventDispatcherInterface::MEDIA_OEMBED_RESOURCE_DATA_ALTER);
  self::assertSame($url, $event
    ->getParsedURL());
}