View source
<?php
namespace Drupal\Tests\minisite\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\minisite\Exception\UrlBagException;
use Drupal\minisite\UrlBag;
use Drupal\Tests\minisite\Traits\MockHelperTrait;
class UrlBagTest extends KernelTestBase {
use MockHelperTrait;
public function testToLocal($base_url, $url, $expected, $expect_exception = FALSE) {
if ($expect_exception) {
$this
->expectException(UrlBagException::class);
}
$actual = $this
->callProtectedMethod(UrlBag::class, 'toLocal', [
$url,
$base_url,
]);
$this
->assertEquals($expected, $actual);
}
public function dataProviderToLocal() {
return [
[
'http://example.com',
'http://example.com/page1',
'/page1',
],
[
'http://example.com',
'http://example.com/sub/path/page1',
'/sub/path/page1',
],
[
'http://example.com',
'/page1',
'/page1',
],
[
'http://example.com',
'/sub/path/page1',
'/sub/path/page1',
],
[
'http://example.com',
'page1',
'/page1',
],
[
'http://example.com',
'subpath/page1',
'/subpath/page1',
],
[
'http://example.com',
'http://otherexample.com/page1',
'',
TRUE,
],
[
'http://example.com',
'http://otherexample.com',
'',
TRUE,
],
[
'http://example.com',
'http://example.com',
'',
TRUE,
],
[
'http://example.com',
'public://page1',
'public://page1',
],
[
'http://example.com/hostpath',
'public://page1',
'public://page1',
],
[
'http://example.com/subdir',
'/page1',
'/page1',
],
[
'http://example.com/subdir',
'page1',
'/page1',
],
[
'http://example.com/subdir',
'/pagesub/page1',
'/pagesub/page1',
],
[
'http://example.com/subdir',
'http://example.com/subdir/pagesub/page1',
'/pagesub/page1',
],
];
}
public function testToAbsolute($base_url, $url, $expected, $expect_exception = FALSE) {
if ($expect_exception) {
$this
->expectException(UrlBagException::class);
}
$actual = $this
->callProtectedMethod(UrlBag::class, 'toAbsolute', [
$url,
$base_url,
]);
$this
->assertEquals($expected, $actual);
}
public function dataProviderToAbsolute() {
return [
[
'http://example.com',
'http://example.com',
'http://example.com',
],
[
'http://example.com',
'http://example.com/page1.html',
'http://example.com/page1.html',
],
[
'http://example.com',
'http://otherdomain.com/page1.html',
'http://otherdomain.com/page1.html',
],
[
'http://example.com',
'page1.html',
'http://example.com/page1.html',
],
[
'http://example.com',
'subpath/page1.html',
'http://example.com/subpath/page1.html',
],
[
'http://example.com',
'/page1.html',
'http://example.com/page1.html',
],
[
'http://example.com',
'/subpath/page1.html',
'http://example.com/subpath/page1.html',
],
];
}
public function testGetUriPart($uri, $part_name, $expected, $expect_exception = FALSE) {
if ($expect_exception) {
$this
->expectException(UrlBagException::class);
}
$actual = $this
->callProtectedMethod(UrlBag::class, 'getUriPart', [
$uri,
$part_name,
]);
$this
->assertEquals($expected, $actual);
}
public function dataProviderGetUriPart() {
return [
[
'public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/subpath/file',
UrlBag::URI_PART_ASSET_DIR,
'public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5',
],
[
'public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5',
UrlBag::URI_PART_ASSET_DIR,
'public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5',
],
[
'public://minisite/static/24c22dd1-2cf1-47ae-ac8a',
UrlBag::URI_PART_ASSET_DIR,
NULL,
TRUE,
],
[
'public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/subpath/file',
UrlBag::URI_PART_ROOT_ARCHIVE_DIR,
'subpath',
],
[
'public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/subpath/file/',
UrlBag::URI_PART_ROOT_ARCHIVE_DIR,
'subpath',
],
[
'public://minisite/static/24c22dd1-2cf1-47ae-ac8a/subpath/file/',
UrlBag::URI_PART_ROOT_ARCHIVE_DIR,
NULL,
TRUE,
],
[
'public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/subpath/file',
UrlBag::URI_PART_PATH_IN_ARCHIVE,
'file',
],
[
'public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/subpath/second/file',
UrlBag::URI_PART_PATH_IN_ARCHIVE,
'second/file',
],
[
'public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/subpath/second/third/file',
UrlBag::URI_PART_PATH_IN_ARCHIVE,
'second/third/file',
],
[
'public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/subpath/file/',
UrlBag::URI_PART_PATH_IN_ARCHIVE,
'file',
],
[
'public://minisite/static/24c22dd1-2cf1-47ae-ac8a/subpath/file/',
UrlBag::URI_PART_PATH_IN_ARCHIVE,
NULL,
TRUE,
],
[
'public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/subpath/file.txt',
UrlBag::URI_PART_BASENAME,
'file.txt',
],
[
'public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/subpath/file/',
UrlBag::URI_PART_BASENAME,
'file',
],
[
'public://minisite/static/24c22dd1-2cf1-47ae-ac8a/subpath/file/',
UrlBag::URI_PART_BASENAME,
NULL,
TRUE,
],
];
}
public function testGetters() {
$bag = new UrlBag('public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/rootpath/subpath/file.txt', 'http://example.com');
$this
->assertEquals('http://example.com', $bag
->getBaseUrl());
$this
->assertEquals('public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/rootpath/subpath/file.txt', $bag
->getUri());
$this
->assertEquals('rootpath', $bag
->getRootDir());
$this
->assertEquals('subpath/file.txt', $bag
->getPathInArchive());
$this
->assertContains('files/minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/rootpath/subpath/file.txt', $bag
->getUrl());
$this
->assertContains('files/minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/rootpath/subpath/file.txt', $bag
->getUrlAbsolute());
$this
->assertNull($bag
->getAlias());
$this
->assertNull($bag
->getAliasAbsolute());
$this
->assertNull($bag
->getParentAlias());
$this
->assertNull($bag
->getParentAliasAbsolute());
$bag
->setParentAlias('/parent/alias');
$this
->assertEquals('/parent/alias/rootpath/subpath/file.txt', $bag
->getAlias());
$this
->assertContains('/parent/alias/rootpath/subpath/file.txt', $bag
->getAliasAbsolute());
$this
->assertEquals('/parent/alias', $bag
->getParentAlias());
$this
->assertContains('/parent/alias', $bag
->getParentAliasAbsolute());
$this
->assertNotContains('rootpath/subpath/file.txt', $bag
->getParentAlias());
$this
->assertNotContains('rootpath/subpath/file.txt', $bag
->getParentAliasAbsolute());
$bag = new UrlBag('public://minisite/static/24c22dd1-2cf1-47ae-ac8a-23a7ff8b86c5/rootpath/subpath/file.txt', 'http://example.com');
$bag
->setAlias('/parent/alias/rootpath/subpath/file.txt');
$this
->assertEquals('/parent/alias/rootpath/subpath/file.txt', $bag
->getAlias());
$this
->assertContains('/parent/alias/rootpath/subpath/file.txt', $bag
->getAliasAbsolute());
$this
->assertEquals('/parent/alias', $bag
->getParentAlias());
$this
->assertContains('/parent/alias', $bag
->getParentAliasAbsolute());
$this
->assertNotContains('rootpath/subpath/file.txt', $bag
->getParentAlias());
$this
->assertNotContains('rootpath/subpath/file.txt', $bag
->getParentAliasAbsolute());
}
}