You are here

public function UrlExtractorTest::testUrlExtractor in Twig Tweak 3.1.x

Same name and namespace in other branches
  1. 3.x tests/src/Kernel/UrlExtractorTest.php \Drupal\Tests\twig_tweak\Kernel\UrlExtractorTest::testUrlExtractor()

Test callback.

File

tests/src/Kernel/UrlExtractorTest.php, line 15

Class

UrlExtractorTest
A test for URL Extractor service.

Namespace

Drupal\Tests\twig_tweak\Kernel

Code

public function testUrlExtractor() : void {
  $extractor = $this->container
    ->get('twig_tweak.url_extractor');
  $base_url = file_create_url('');
  $request = \Drupal::request();
  $absolute_url = "{$request->getScheme()}://{$request->getHost()}/foo/bar.txt";
  $url = $extractor
    ->extractUrl($absolute_url);
  self::assertSame('/foo/bar.txt', $url);
  $url = $extractor
    ->extractUrl($absolute_url, FALSE);
  self::assertSame($base_url . 'foo/bar.txt', $url);
  $url = $extractor
    ->extractUrl('foo/bar.jpg');
  self::assertSame('/foo/bar.jpg', $url);
  $url = $extractor
    ->extractUrl('foo/bar.jpg', FALSE);
  self::assertSame($base_url . 'foo/bar.jpg', $url);
  $url = $extractor
    ->extractUrl('');
  self::assertSame('/', $url);
  $url = $extractor
    ->extractUrl('', FALSE);
  self::assertSame($base_url, $url);
  $url = $extractor
    ->extractUrl(NULL);
  self::assertNull($url);
  $url = $extractor
    ->extractUrl($this->node);
  self::assertNull($url);
  $url = $extractor
    ->extractUrl($this->node
    ->get('title'));
  self::assertNull($url);
  $url = $extractor
    ->extractUrl($this->node
    ->get('field_image')[0]);
  self::assertStringEndsWith('/files/image-test.png', $url);
  self::assertStringNotContainsString($base_url, $url);
  $url = $extractor
    ->extractUrl($this->node
    ->get('field_image')[0], FALSE);
  self::assertStringStartsWith($base_url, $url);
  self::assertStringEndsWith('/files/image-test.png', $url);
  $url = $extractor
    ->extractUrl($this->node
    ->get('field_image')[1]);
  self::assertNull($url);
  $url = $extractor
    ->extractUrl($this->node
    ->get('field_image'));
  self::assertStringEndsWith('/files/image-test.png', $url);
  $url = $extractor
    ->extractUrl($this->node
    ->get('field_image')->entity);
  self::assertStringEndsWith('/files/image-test.png', $url);
  $this->node
    ->get('field_image')
    ->removeItem(0);
  $url = $extractor
    ->extractUrl($this->node
    ->get('field_image'));
  self::assertNull($url);
  $url = $extractor
    ->extractUrl($this->node
    ->get('field_media')[0]);
  self::assertStringEndsWith('/files/image-test.gif', $url);
  $url = $extractor
    ->extractUrl($this->node
    ->get('field_media')[1]);
  self::assertNull($url);
  $url = $extractor
    ->extractUrl($this->node
    ->get('field_media'));
  self::assertStringEndsWith('/files/image-test.gif', $url);
  $url = $extractor
    ->extractUrl($this->node
    ->get('field_media')->entity);
  self::assertStringEndsWith('/files/image-test.gif', $url);
  $this->node
    ->get('field_media')
    ->removeItem(0);
  $url = $extractor
    ->extractUrl($this->node
    ->get('field_media'));
  self::assertNull($url);
}