2017-09-21 07:54:06 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Control\Tests;
|
|
|
|
|
|
|
|
use SilverStripe\Control\Director;
|
|
|
|
use SilverStripe\Control\SimpleResourceURLGenerator;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Core\Manifest\Module;
|
|
|
|
use SilverStripe\Core\Manifest\ResourceURLGenerator;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
|
|
|
class SimpleResourceURLGeneratorTest extends SapphireTest
|
|
|
|
{
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
Director::config()->set(
|
|
|
|
'alternate_base_folder',
|
2018-01-16 19:39:30 +01:00
|
|
|
__DIR__ . '/SimpleResourceURLGeneratorTest/_fakewebroot'
|
2017-09-21 07:54:06 +02:00
|
|
|
);
|
|
|
|
Director::config()->set(
|
|
|
|
'alternate_base_url',
|
|
|
|
'http://www.mysite.com/'
|
|
|
|
);
|
2018-01-12 04:25:02 +01:00
|
|
|
Director::config()->set(
|
|
|
|
'alternate_public_dir',
|
|
|
|
'public'
|
|
|
|
);
|
2017-09-21 07:54:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddMTime()
|
|
|
|
{
|
|
|
|
/** @var SimpleResourceURLGenerator $generator */
|
|
|
|
$generator = Injector::inst()->get(ResourceURLGenerator::class);
|
2018-09-27 00:43:56 +02:00
|
|
|
$mtime = filemtime(
|
|
|
|
__DIR__ . '/SimpleResourceURLGeneratorTest/_fakewebroot/basemodule/client/file.js'
|
|
|
|
);
|
2017-09-21 07:54:06 +02:00
|
|
|
$this->assertEquals(
|
2019-03-19 23:46:35 +01:00
|
|
|
'/' . RESOURCES_DIR . '/basemodule/client/file.js?m=' . $mtime,
|
2017-09-21 07:54:06 +02:00
|
|
|
$generator->urlForResource('basemodule/client/file.js')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-12 08:10:18 +02:00
|
|
|
public function testAddSha1()
|
|
|
|
{
|
|
|
|
/** @var SimpleResourceURLGenerator $generator */
|
|
|
|
$generator = Injector::inst()->get(ResourceURLGenerator::class);
|
|
|
|
$generator->setNonceStyle('sha1');
|
|
|
|
$hash = sha1_file(
|
|
|
|
__DIR__ . '/SimpleResourceURLGeneratorTest/_fakewebroot/basemodule/client/file.js'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'/' . RESOURCES_DIR . '/basemodule/client/file.js?m=' . $hash,
|
|
|
|
$generator->urlForResource('basemodule/client/file.js')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddMd5()
|
|
|
|
{
|
|
|
|
/** @var SimpleResourceURLGenerator $generator */
|
|
|
|
$generator = Injector::inst()->get(ResourceURLGenerator::class);
|
|
|
|
$generator->setNonceStyle('md5');
|
|
|
|
$hash = md5_file(
|
|
|
|
__DIR__ . '/SimpleResourceURLGeneratorTest/_fakewebroot/basemodule/client/file.js'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'/' . RESOURCES_DIR . '/basemodule/client/file.js?m=' . $hash,
|
|
|
|
$generator->urlForResource('basemodule/client/file.js')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-09-21 07:54:06 +02:00
|
|
|
public function testVendorResource()
|
|
|
|
{
|
|
|
|
/** @var SimpleResourceURLGenerator $generator */
|
|
|
|
$generator = Injector::inst()->get(ResourceURLGenerator::class);
|
|
|
|
$mtime = filemtime(
|
2018-01-16 19:39:30 +01:00
|
|
|
__DIR__ . '/SimpleResourceURLGeneratorTest/_fakewebroot/vendor/silverstripe/mymodule/client/style.css'
|
2017-09-21 07:54:06 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2019-03-19 23:46:35 +01:00
|
|
|
'/' . RESOURCES_DIR . '/vendor/silverstripe/mymodule/client/style.css?m=' . $mtime,
|
2017-09-21 07:54:06 +02:00
|
|
|
$generator->urlForResource('vendor/silverstripe/mymodule/client/style.css')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-12 04:25:02 +01:00
|
|
|
public function testPublicDirResource()
|
|
|
|
{
|
|
|
|
/** @var SimpleResourceURLGenerator $generator */
|
|
|
|
$generator = Injector::inst()->get(ResourceURLGenerator::class);
|
|
|
|
$mtime = filemtime(
|
2018-01-22 02:57:05 +01:00
|
|
|
__DIR__ . '/SimpleResourceURLGeneratorTest/_fakewebroot/public/basemodule/css/style.css'
|
2018-01-12 04:25:02 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2018-01-22 02:57:05 +01:00
|
|
|
'/basemodule/css/style.css?m=' . $mtime,
|
2018-01-12 04:25:02 +01:00
|
|
|
$generator->urlForResource('public/basemodule/css/style.css')
|
|
|
|
);
|
|
|
|
|
|
|
|
$mtime = filemtime(
|
2018-01-22 02:57:05 +01:00
|
|
|
__DIR__ . '/SimpleResourceURLGeneratorTest/_fakewebroot/basemodule/client/file.js'
|
2018-01-12 04:25:02 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2019-03-19 23:46:35 +01:00
|
|
|
'/' . RESOURCES_DIR . '/basemodule/client/file.js?m=' . $mtime,
|
2018-01-12 04:25:02 +01:00
|
|
|
$generator->urlForResource('basemodule/client/file.js')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-09-21 07:54:06 +02:00
|
|
|
public function testModuleResource()
|
|
|
|
{
|
|
|
|
/** @var SimpleResourceURLGenerator $generator */
|
|
|
|
$generator = Injector::inst()->get(ResourceURLGenerator::class);
|
|
|
|
$module = new Module(
|
2018-01-16 19:39:30 +01:00
|
|
|
__DIR__ . '/SimpleResourceURLGeneratorTest/_fakewebroot/vendor/silverstripe/mymodule/',
|
|
|
|
__DIR__ . '/SimpleResourceURLGeneratorTest/_fakewebroot/'
|
2017-09-21 07:54:06 +02:00
|
|
|
);
|
|
|
|
$mtime = filemtime(
|
2018-01-16 19:39:30 +01:00
|
|
|
__DIR__ . '/SimpleResourceURLGeneratorTest/_fakewebroot/vendor/silverstripe/mymodule/client/style.css'
|
2017-09-21 07:54:06 +02:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2019-03-19 23:46:35 +01:00
|
|
|
'/' . RESOURCES_DIR . '/vendor/silverstripe/mymodule/client/style.css?m=' . $mtime,
|
2017-09-21 07:54:06 +02:00
|
|
|
$generator->urlForResource($module->getResource('client/style.css'))
|
|
|
|
);
|
|
|
|
}
|
2017-10-16 05:11:42 +02:00
|
|
|
|
|
|
|
public function testURLForResourceFailsGracefully()
|
|
|
|
{
|
|
|
|
/** @var SimpleResourceURLGenerator $generator */
|
|
|
|
$generator = Injector::inst()->get(ResourceURLGenerator::class);
|
|
|
|
$url = @$generator->urlForResource('doesnotexist.jpg');
|
|
|
|
$this->assertEquals('/doesnotexist.jpg', $url);
|
|
|
|
}
|
2018-01-25 00:53:15 +01:00
|
|
|
|
2018-01-23 05:43:01 +01:00
|
|
|
public function testAbsoluteResource()
|
|
|
|
{
|
|
|
|
/** @var SimpleResourceURLGenerator $generator */
|
|
|
|
$generator = Injector::inst()->get(ResourceURLGenerator::class);
|
|
|
|
$fakeExternalAsset = 'https://cdn.example.com/some_library.css';
|
|
|
|
$this->assertEquals($fakeExternalAsset, $generator->urlForResource($fakeExternalAsset));
|
|
|
|
}
|
2017-09-21 07:54:06 +02:00
|
|
|
}
|