You are here

class OEmbedResourceUrlAlterEventTest 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

Class OEmbedResourceUrlAlterEventTest.

@group media_event_dispatcher

Hierarchy

Expanded class hierarchy of OEmbedResourceUrlAlterEventTest

File

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

Namespace

Drupal\Tests\media_event_dispatcher\Unit\Media
View source
class OEmbedResourceUrlAlterEventTest extends TestCase {

  /**
   * The manager.
   *
   * @var \Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy
   */
  private $manager;

  /**
   * Sets up the test.
   */
  public function setUp() : void {
    $builder = new ContainerBuilder();
    $this->manager = new HookEventDispatcherManagerSpy();
    $builder
      ->set('hook_event_dispatcher.manager', $this->manager);
    $builder
      ->compile();
    Drupal::setContainer($builder);
  }

  /**
   * Get a mock oEmbed provider.
   *
   * @return \Drupal\media\OEmbed\Provider
   *   A mock Vimeo oEmbed provider.
   */
  private function getMockProvider() : Provider {
    return new Provider('Vimeo', 'https://vimeo.com/', [
      [
        'url' => 'https://vimeo.com/api/oembed.json',
        'schemes' => [
          'https://vimeo.com/*',
        ],
      ],
    ]);
  }

  /**
   * OEmbedResourceUrlAlterEvent parsed URL alter test.
   *
   * This tests altering the parsed URL array for an oEmbed data request.
   */
  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());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OEmbedResourceUrlAlterEventTest::$manager private property The manager.
OEmbedResourceUrlAlterEventTest::getMockProvider private function Get a mock oEmbed provider.
OEmbedResourceUrlAlterEventTest::setUp public function Sets up the test.
OEmbedResourceUrlAlterEventTest::testOembedResourceParsedUrlAlter public function OEmbedResourceUrlAlterEvent parsed URL alter test.