class UrlResolverTest

Same name in this branch
  1. 11.x core/modules/media/tests/src/Functional/UrlResolverTest.php \Drupal\Tests\media\Functional\UrlResolverTest
Same name and namespace in other branches
  1. 10 core/modules/media/tests/src/Functional/UrlResolverTest.php \Drupal\Tests\media\Functional\UrlResolverTest
  2. 9 core/modules/media/tests/src/Kernel/UrlResolverTest.php \Drupal\Tests\media\Kernel\UrlResolverTest
  3. 9 core/modules/media/tests/src/Functional/UrlResolverTest.php \Drupal\Tests\media\Functional\UrlResolverTest
  4. 8.9.x core/modules/media/tests/src/Functional/UrlResolverTest.php \Drupal\Tests\media\Functional\UrlResolverTest

Tests the oEmbed URL resolver.

Attributes

#[CoversClass(UrlResolver::class)] #[Group('media')]

Hierarchy

Expanded class hierarchy of UrlResolverTest

File

core/modules/media/tests/src/Unit/UrlResolverTest.php, line 23

Namespace

Drupal\Tests\media\Unit
View source
class UrlResolverTest extends UnitTestCase {
  
  /**
   * Creates a UrlResolver with exposed protected methods for testing.
   *
   * @param \GuzzleHttp\Client $client
   *   The HTTP client.
   *
   * @return \Drupal\media\OEmbed\UrlResolver
   *   A UrlResolver instance with a public discoverResourceUrl method.
   */
  protected function createTestableUrlResolver(Client $client) : UrlResolver {
    return new class ($this->createMock(ProviderRepositoryInterface::class), $this->createMock(ResourceFetcherInterface::class), $client, $this->createMock(ModuleHandlerInterface::class), new NullBackend('default')) extends UrlResolver {
      
      /**
       * {@inheritdoc}
       */
      public function discoverResourceUrl($url) : string|false {
        return parent::discoverResourceUrl($url);
      }

};
  }
  
  /**
   * Tests that discoverResourceUrl parses HTML responses.
   */
  public function testDiscoverResourceUrlParsesHtml() : void {
    $html_with_oembed = <<<HTML
    <!DOCTYPE html>
    <html>
    <head>
      <link rel="alternate" href="https://example.com/oembed?url=test" type="application/json+oembed">
    </head>
    <body></body>
    </html>
    HTML;
    $response = new Response(200, [
      'Content-Type' => 'text/html',
    ], $html_with_oembed);
    $mock_handler = new MockHandler([
      $response,
    ]);
    $client = new Client([
      'handler' => HandlerStack::create($mock_handler),
    ]);
    $url_resolver = $this->createTestableUrlResolver($client);
    $result = $url_resolver->discoverResourceUrl('https://example.com/some-page');
    $this->assertSame('https://example.com/oembed?url=test', $result);
  }
  
  /**
   * Tests that discoverResourceUrl skips non-HTML responses.
   */
  public function testDiscoverResourceUrlSkipsNonHtml() : void {
    $response = new Response(200, [
      'Content-Type' => 'application/json',
    ], '');
    $mock_handler = new MockHandler([
      $response,
    ]);
    $client = new Client([
      'handler' => HandlerStack::create($mock_handler),
    ]);
    $url_resolver = $this->createTestableUrlResolver($client);
    $result = $url_resolver->discoverResourceUrl('https://example.com/some-page');
    $this->assertFalse($result);
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
UnitTestCase::setUp protected function 370
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.
UrlResolverTest::createTestableUrlResolver protected function Creates a UrlResolver with exposed protected methods for testing.
UrlResolverTest::testDiscoverResourceUrlParsesHtml public function Tests that discoverResourceUrl parses HTML responses.
UrlResolverTest::testDiscoverResourceUrlSkipsNonHtml public function Tests that discoverResourceUrl skips non-HTML responses.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.