123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598 |
- <?php
- use Codeception\Util\Fixtures;
- use Grav\Common\Grav;
- use Grav\Common\Assets;
- /**
- * Class AssetsTest
- */
- class AssetsTest extends \Codeception\TestCase\Test
- {
- /** @var Grav $grav */
- protected $grav;
- /** @var Assets $assets */
- protected $assets;
- protected function _before()
- {
- $grav = Fixtures::get('grav');
- $this->grav = $grav();
- $this->assets = $this->grav['assets'];
- }
- protected function _after()
- {
- }
- public function testAddingAssets()
- {
- //test add()
- $this->assets->add('test.css');
- $css = $this->assets->css();
- $this->assertSame('<link href="/test.css" type="text/css" rel="stylesheet" />' . PHP_EOL, $css);
- $array = $this->assets->getCss();
- $this->assertSame([
- 'asset' => '/test.css',
- 'remote' => false,
- 'priority' => 10,
- 'order' => 0,
- 'pipeline' => true,
- 'loading' => '',
- 'group' => 'head',
- 'modified' => false,
- 'query' => ''
- ], reset($array));
- $this->assets->add('test.js');
- $js = $this->assets->js();
- $this->assertSame('<script src="/test.js" ></script>' . PHP_EOL, $js);
- $array = $this->assets->getCss();
- $this->assertSame([
- 'asset' => '/test.css',
- 'remote' => false,
- 'priority' => 10,
- 'order' => 0,
- 'pipeline' => true,
- 'loading' => '',
- 'group' => 'head',
- 'modified' => false,
- 'query' => ''
- ], reset($array));
- //test addCss(). Test adding asset to a separate group
- $this->assets->reset();
- $this->assets->addCSS('test.css');
- $css = $this->assets->css();
- $this->assertSame('<link href="/test.css" type="text/css" rel="stylesheet" />' . PHP_EOL, $css);
- $array = $this->assets->getCss();
- $this->assertSame([
- 'asset' => '/test.css',
- 'remote' => false,
- 'priority' => 10,
- 'order' => 0,
- 'pipeline' => true,
- 'loading' => '',
- 'group' => 'head',
- 'modified' => false,
- 'query' => ''
- ], reset($array));
- //test addCss(). Testing with remote URL
- $this->assets->reset();
- $this->assets->addCSS('http://www.somesite.com/test.css');
- $css = $this->assets->css();
- $this->assertSame('<link href="http://www.somesite.com/test.css" type="text/css" rel="stylesheet" />' . PHP_EOL, $css);
- $array = $this->assets->getCss();
- $this->assertSame([
- 'asset' => 'http://www.somesite.com/test.css',
- 'remote' => true,
- 'priority' => 10,
- 'order' => 0,
- 'pipeline' => true,
- 'loading' => '',
- 'group' => 'head',
- 'modified' => false,
- 'query' => ''
- ], reset($array));
- //test addCss() adding asset to a separate group, and with an alternate rel attribute
- $this->assets->reset();
- $this->assets->addCSS('test.css', ['group' => 'alternate']);
- $css = $this->assets->css('alternate', ['rel' => 'alternate']);
- $this->assertSame('<link href="/test.css" type="text/css" rel="alternate" />' . PHP_EOL, $css);
- //test addJs()
- $this->assets->reset();
- $this->assets->addJs('test.js');
- $js = $this->assets->js();
- $this->assertSame('<script src="/test.js" ></script>' . PHP_EOL, $js);
- $array = $this->assets->getJs();
- $this->assertSame([
- 'asset' => '/test.js',
- 'remote' => false,
- 'priority' => 10,
- 'order' => 0,
- 'pipeline' => true,
- 'loading' => '',
- 'group' => 'head',
- 'modified' => false,
- 'query' => ''
- ], reset($array));
- //Test CSS Groups
- $this->assets->reset();
- $this->assets->addCSS('test.css', null, true, 'footer');
- $css = $this->assets->css();
- $this->assertEmpty($css);
- $css = $this->assets->css('footer');
- $this->assertSame('<link href="/test.css" type="text/css" rel="stylesheet" />' . PHP_EOL, $css);
- $array = $this->assets->getCss();
- $this->assertSame([
- 'asset' => '/test.css',
- 'remote' => false,
- 'priority' => 10,
- 'order' => 0,
- 'pipeline' => true,
- 'loading' => '',
- 'group' => 'footer',
- 'modified' => false,
- 'query' => ''
- ], reset($array));
- //Test JS Groups
- $this->assets->reset();
- $this->assets->addJs('test.js', null, true, null, 'footer');
- $js = $this->assets->js();
- $this->assertEmpty($js);
- $js = $this->assets->js('footer');
- $this->assertSame('<script src="/test.js" ></script>' . PHP_EOL, $js);
- $array = $this->assets->getJs();
- $this->assertSame([
- 'asset' => '/test.js',
- 'remote' => false,
- 'priority' => 10,
- 'order' => 0,
- 'pipeline' => true,
- 'loading' => '',
- 'group' => 'footer',
- 'modified' => false,
- 'query' => ''
- ], reset($array));
- //Test async / defer
- $this->assets->reset();
- $this->assets->addJs('test.js', null, true, 'async', null);
- $js = $this->assets->js();
- $this->assertSame('<script src="/test.js" async></script>' . PHP_EOL, $js);
- $array = $this->assets->getJs();
- $this->assertSame([
- 'asset' => '/test.js',
- 'remote' => false,
- 'priority' => 10,
- 'order' => 0,
- 'pipeline' => true,
- 'loading' => 'async',
- 'group' => 'head',
- 'modified' => false,
- 'query' => ''
- ], reset($array));
- $this->assets->reset();
- $this->assets->addJs('test.js', null, true, 'defer', null);
- $js = $this->assets->js();
- $this->assertSame('<script src="/test.js" defer></script>' . PHP_EOL, $js);
- $array = $this->assets->getJs();
- $this->assertSame([
- 'asset' => '/test.js',
- 'remote' => false,
- 'priority' => 10,
- 'order' => 0,
- 'pipeline' => true,
- 'loading' => 'defer',
- 'group' => 'head',
- 'modified' => false,
- 'query' => ''
- ], reset($array));
- //Test inline
- $this->assets->reset();
- $this->assets->addJs('/system/assets/jquery/jquery-2.x.min.js', null, true, 'inline', null);
- $js = $this->assets->js();
- $this->assertContains('jQuery Foundation', $js);
- $this->assets->reset();
- $this->assets->addCss('/system/assets/debugger.css', null, true, null, 'inline');
- $css = $this->assets->css();
- $this->assertContains('div.phpdebugbar', $css);
- $this->assets->reset();
- $this->assets->addCss('https://fonts.googleapis.com/css?family=Roboto', null, true, null, 'inline');
- $css = $this->assets->css();
- $this->assertContains('font-family: \'Roboto\';', $css);
- //Test adding media queries
- $this->assets->reset();
- $this->assets->add('test.css', ['media' => 'only screen and (min-width: 640px)']);
- $css = $this->assets->css();
- $this->assertSame('<link href="/test.css" type="text/css" rel="stylesheet" media="only screen and (min-width: 640px)" />' . PHP_EOL, $css);
- }
- public function testAddingAssetPropertiesWithArray()
- {
- //Test adding assets with object to define properties
- $this->assets->reset();
- $this->assets->addJs('test.js', ['loading' => 'async']);
- $js = $this->assets->js();
- $this->assertSame('<script src="/test.js" async></script>' . PHP_EOL, $js);
- $this->assets->reset();
- }
- public function testAddingJSAssetPropertiesWithArrayFromCollection()
- {
- //Test adding properties with array
- $this->assets->reset();
- $this->assets->addJs('jquery', ['loading' => 'async']);
- $js = $this->assets->js();
- $this->assertSame('<script src="/system/assets/jquery/jquery-2.x.min.js" async></script>' . PHP_EOL, $js);
- //Test priority too
- $this->assets->reset();
- $this->assets->addJs('jquery', ['loading' => 'async', 'priority' => 1]);
- $this->assets->addJs('test.js', ['loading' => 'async', 'priority' => 2]);
- $js = $this->assets->js();
- $this->assertSame('<script src="/test.js" async></script>' . PHP_EOL .
- '<script src="/system/assets/jquery/jquery-2.x.min.js" async></script>' . PHP_EOL, $js);
- //Test multiple groups
- $this->assets->reset();
- $this->assets->addJs('jquery', ['loading' => 'async', 'priority' => 1, 'group' => 'footer']);
- $this->assets->addJs('test.js', ['loading' => 'async', 'priority' => 2]);
- $js = $this->assets->js();
- $this->assertSame('<script src="/test.js" async></script>' . PHP_EOL, $js);
- $js = $this->assets->js('footer');
- $this->assertSame('<script src="/system/assets/jquery/jquery-2.x.min.js" async></script>' . PHP_EOL, $js);
- //Test adding array of assets
- //Test priority too
- $this->assets->reset();
- $this->assets->addJs(['jquery', 'test.js'], ['loading' => 'async']);
- $js = $this->assets->js();
- $this->assertSame('<script src="/system/assets/jquery/jquery-2.x.min.js" async></script>' . PHP_EOL .
- '<script src="/test.js" async></script>' . PHP_EOL, $js);
- }
- public function testAddingCSSAssetPropertiesWithArrayFromCollection()
- {
- $this->assets->registerCollection('test', ['/system/assets/whoops.css']);
- //Test priority too
- $this->assets->reset();
- $this->assets->addCss('test', ['priority' => 1]);
- $this->assets->addCss('test.css', ['priority' => 2]);
- $css = $this->assets->css();
- $this->assertSame('<link href="/test.css" type="text/css" rel="stylesheet" />' . PHP_EOL .
- '<link href="/system/assets/whoops.css" type="text/css" rel="stylesheet" />' . PHP_EOL, $css);
- //Test multiple groups
- $this->assets->reset();
- $this->assets->addCss('test', ['priority' => 1, 'group' => 'footer']);
- $this->assets->addCss('test.css', ['priority' => 2]);
- $css = $this->assets->css();
- $this->assertSame('<link href="/test.css" type="text/css" rel="stylesheet" />' . PHP_EOL, $css);
- $css = $this->assets->css('footer');
- $this->assertSame('<link href="/system/assets/whoops.css" type="text/css" rel="stylesheet" />' . PHP_EOL, $css);
- //Test adding array of assets
- //Test priority too
- $this->assets->reset();
- $this->assets->addCss(['test', 'test.css'], ['loading' => 'async']);
- $css = $this->assets->css();
- $this->assertSame('<link href="/system/assets/whoops.css" type="text/css" rel="stylesheet" />' . PHP_EOL .
- '<link href="/test.css" type="text/css" rel="stylesheet" />' . PHP_EOL, $css);
- }
- public function testPriorityOfAssets()
- {
- $this->assets->reset();
- $this->assets->add('test.css');
- $this->assets->add('test-after.css');
- $css = $this->assets->css();
- $this->assertSame('<link href="/test.css" type="text/css" rel="stylesheet" />' . PHP_EOL .
- '<link href="/test-after.css" type="text/css" rel="stylesheet" />' . PHP_EOL, $css);
- //----------------
- $this->assets->reset();
- $this->assets->add('test-after.css', 1);
- $this->assets->add('test.css', 2);
- $css = $this->assets->css();
- $this->assertSame('<link href="/test.css" type="text/css" rel="stylesheet" />' . PHP_EOL .
- '<link href="/test-after.css" type="text/css" rel="stylesheet" />' . PHP_EOL, $css);
- //----------------
- $this->assets->reset();
- $this->assets->add('test-after.css', 1);
- $this->assets->add('test.css', 2);
- $this->assets->add('test-before.css', 3);
- $css = $this->assets->css();
- $this->assertSame('<link href="/test-before.css" type="text/css" rel="stylesheet" />' . PHP_EOL .
- '<link href="/test.css" type="text/css" rel="stylesheet" />' . PHP_EOL .
- '<link href="/test-after.css" type="text/css" rel="stylesheet" />' . PHP_EOL, $css);
- }
- public function testPipeline()
- {
- $this->assets->reset();
- //File not existing. Pipeline searches for that file without reaching it. Output is empty.
- $this->assets->add('test.css', null, true);
- $this->assets->setCssPipeline(true);
- $css = $this->assets->css();
- $this->assertSame('', $css);
- //Add a core Grav CSS file, which is found. Pipeline will now return a file
- $this->assets->add('/system/assets/debugger.css', null, true);
- $css = $this->assets->css();
- $this->assertContains('<link href=', $css);
- $this->assertContains('type="text/css" rel="stylesheet" />', $css);
- }
- public function testPipelineWithTimestamp()
- {
- $this->assets->reset();
- $this->assets->setTimestamp('foo');
- //Add a core Grav CSS file, which is found. Pipeline will now return a file
- $this->assets->add('/system/assets/debugger.css', null, true);
- $css = $this->assets->css();
- $this->assertContains('<link href=', $css);
- $this->assertContains('type="text/css" rel="stylesheet" />', $css);
- $this->assertContains($this->assets->getTimestamp(), $css);
- }
- public function testInlinePipeline()
- {
- $this->assets->reset();
- //File not existing. Pipeline searches for that file without reaching it. Output is empty.
- $this->assets->add('test.css', null, true);
- $this->assets->setCssPipeline(true);
- $css = $this->assets->css('head', ['loading' => 'inline']);
- $this->assertSame('', $css);
- //Add a core Grav CSS file, which is found. Pipeline will now return its content.
- $this->assets->addCss('https://fonts.googleapis.com/css?family=Roboto', null, true);
- $this->assets->add('/system/assets/debugger.css', null, true);
- $css = $this->assets->css('head', ['loading' => 'inline']);
- $this->assertContains('font-family:\'Roboto\';', $css);
- $this->assertContains('div.phpdebugbar', $css);
- }
- public function testAddAsyncJs()
- {
- $this->assets->reset();
- $this->assets->addAsyncJs('jquery');
- $js = $this->assets->js();
- $this->assertSame('<script src="/system/assets/jquery/jquery-2.x.min.js" async></script>' . PHP_EOL, $js);
- }
- public function testAddDeferJs()
- {
- $this->assets->reset();
- $this->assets->addDeferJs('jquery');
- $js = $this->assets->js();
- $this->assertSame('<script src="/system/assets/jquery/jquery-2.x.min.js" defer></script>' . PHP_EOL, $js);
- }
- public function testTimestamps()
- {
- // local CSS nothing extra
- $this->assets->reset();
- $this->assets->setTimestamp('foo');
- $this->assets->addCSS('test.css');
- $css = $this->assets->css();
- $this->assertSame('<link href="/test.css?foo" type="text/css" rel="stylesheet" />' . PHP_EOL, $css);
- // local CSS already with param
- $this->assets->reset();
- $this->assets->setTimestamp('foo');
- $this->assets->addCSS('test.css?bar');
- $css = $this->assets->css();
- $this->assertSame('<link href="/test.css?bar&foo" type="text/css" rel="stylesheet" />' . PHP_EOL, $css);
- // external CSS already
- $this->assets->reset();
- $this->assets->setTimestamp('foo');
- $this->assets->addCSS('http://somesite.com/test.css');
- $css = $this->assets->css();
- $this->assertSame('<link href="http://somesite.com/test.css?foo" type="text/css" rel="stylesheet" />' . PHP_EOL, $css);
- // external CSS already with param
- $this->assets->reset();
- $this->assets->setTimestamp('foo');
- $this->assets->addCSS('http://somesite.com/test.css?bar');
- $css = $this->assets->css();
- $this->assertSame('<link href="http://somesite.com/test.css?bar&foo" type="text/css" rel="stylesheet" />' . PHP_EOL, $css);
- // local JS nothing extra
- $this->assets->reset();
- $this->assets->setTimestamp('foo');
- $this->assets->addJs('test.js');
- $css = $this->assets->js();
- $this->assertSame('<script src="/test.js?foo" ></script>' . PHP_EOL, $css);
- // local JS already with param
- $this->assets->reset();
- $this->assets->setTimestamp('foo');
- $this->assets->addJs('test.js?bar');
- $css = $this->assets->js();
- $this->assertSame('<script src="/test.js?bar&foo" ></script>' . PHP_EOL, $css);
- // external JS already
- $this->assets->reset();
- $this->assets->setTimestamp('foo');
- $this->assets->addJs('http://somesite.com/test.js');
- $css = $this->assets->js();
- $this->assertSame('<script src="http://somesite.com/test.js?foo" ></script>' . PHP_EOL, $css);
- // external JS already with param
- $this->assets->reset();
- $this->assets->setTimestamp('foo');
- $this->assets->addJs('http://somesite.com/test.js?bar');
- $css = $this->assets->js();
- $this->assertSame('<script src="http://somesite.com/test.js?bar&foo" ></script>' . PHP_EOL, $css);
- }
- public function testAddInlineCss()
- {
- $this->assets->reset();
- $this->assets->addInlineCss('body { color: black }');
- $css = $this->assets->css();
- $this->assertSame(PHP_EOL . '<style>' . PHP_EOL . 'body { color: black }' . PHP_EOL . PHP_EOL . '</style>' . PHP_EOL, $css);
- }
- public function testAddInlineJs()
- {
- $this->assets->reset();
- $this->assets->addInlineJs('alert("test")');
- $js = $this->assets->js();
- $this->assertSame(PHP_EOL . '<script>' . PHP_EOL . 'alert("test")' . PHP_EOL . PHP_EOL . '</script>' . PHP_EOL, $js);
- }
- public function testGetCollections()
- {
- $this->assertInternalType('array', $this->assets->getCollections());
- $this->assertContains('jquery', array_keys($this->assets->getCollections()));
- $this->assertContains('system://assets/jquery/jquery-2.x.min.js', $this->assets->getCollections());
- }
- public function testExists()
- {
- $this->assertTrue($this->assets->exists('jquery'));
- $this->assertFalse($this->assets->exists('another-unexisting-library'));
- }
- public function testRegisterCollection()
- {
- $this->assets->registerCollection('debugger', ['/system/assets/debugger.css']);
- $this->assertTrue($this->assets->exists('debugger'));
- $this->assertContains('debugger', array_keys($this->assets->getCollections()));
- }
- public function testReset()
- {
- $this->assets->addInlineJs('alert("test")');
- $this->assets->reset();
- $this->assertCount(0, (array) $this->assets->js());
- $this->assets->addAsyncJs('jquery');
- $this->assets->reset();
- $this->assertCount(0, (array) $this->assets->js());
- $this->assets->addInlineCss('body { color: black }');
- $this->assets->reset();
- $this->assertCount(0, (array) $this->assets->css());
- $this->assets->add('/system/assets/debugger.css', null, true);
- $this->assets->reset();
- $this->assertCount(0, (array) $this->assets->css());
- }
- public function testResetJs()
- {
- $this->assets->addInlineJs('alert("test")');
- $this->assets->resetJs();
- $this->assertCount(0, (array) $this->assets->js());
- $this->assets->addAsyncJs('jquery');
- $this->assets->resetJs();
- $this->assertCount(0, (array) $this->assets->js());
- }
- public function testResetCss()
- {
- $this->assertCount(0, (array) $this->assets->js());
- $this->assets->addInlineCss('body { color: black }');
- $this->assets->resetCss();
- $this->assertCount(0, (array) $this->assets->css());
- $this->assets->add('/system/assets/debugger.css', null, true);
- $this->assets->resetCss();
- $this->assertCount(0, (array) $this->assets->css());
- }
- public function testAddDirCss()
- {
- $this->assets->addDirCss('/system');
- $this->assertInternalType('array', $this->assets->getCss());
- $this->assertGreaterThan(0, (array) $this->assets->getCss());
- $this->assertInternalType('array', $this->assets->getJs());
- $this->assertCount(0, (array) $this->assets->getJs());
- $this->assets->reset();
- $this->assets->addDirCss('/system/assets');
- $this->assertInternalType('array', $this->assets->getCss());
- $this->assertGreaterThan(0, (array) $this->assets->getCss());
- $this->assertInternalType('array', $this->assets->getJs());
- $this->assertCount(0, (array) $this->assets->getJs());
- $this->assets->reset();
- $this->assets->addDirJs('/system');
- $this->assertInternalType('array', $this->assets->getCss());
- $this->assertCount(0, (array) $this->assets->getCss());
- $this->assertInternalType('array', $this->assets->getJs());
- $this->assertGreaterThan(0, (array) $this->assets->getJs());
- $this->assets->reset();
- $this->assets->addDirJs('/system/assets');
- $this->assertInternalType('array', $this->assets->getCss());
- $this->assertCount(0, (array) $this->assets->getCss());
- $this->assertInternalType('array', $this->assets->getJs());
- $this->assertGreaterThan(0, (array) $this->assets->getJs());
- $this->assets->reset();
- $this->assets->addDir('/system/assets');
- $this->assertInternalType('array', $this->assets->getCss());
- $this->assertGreaterThan(0, (array) $this->assets->getCss());
- $this->assertInternalType('array', $this->assets->getJs());
- $this->assertGreaterThan(0, (array) $this->assets->getJs());
- //Use streams
- $this->assets->reset();
- $this->assets->addDir('system://assets');
- $this->assertInternalType('array', $this->assets->getCss());
- $this->assertGreaterThan(0, (array) $this->assets->getCss());
- $this->assertInternalType('array', $this->assets->getJs());
- $this->assertGreaterThan(0, (array) $this->assets->getJs());
- }
- }
|