-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #6700 Add tests for web assets (javiereguiluz)
This PR was squashed before being merged into the 4.x branch. Discussion ---------- Add tests for web assets The #6697 PR had a minor bug in JS asset handling. This PR fixes it and adds some tests. Commits ------- c6dcace Add tests for web assets
- Loading branch information
Showing
5 changed files
with
135 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{# @var assets \EasyCorp\Bundle\EasyAdminBundle\Dto\AssetDto[] #} | ||
{% for js_asset in assets %} | ||
{% set src = asset(js_asset.value, js_asset.packageName) %} | ||
<script src="{{ js_asset.preload ? ea_call_function_if_exists('preload', src, { as: 'script', nopush: js_asset.nopush })|default(src) }}" {{ js_asset.async ? 'async' }} {{ js_asset.defer ? 'defer' }} | ||
{% for attr, value in js_asset.htmlAttributes %}{{ attr }}="{{ value|e('html_attr') }}" {% endfor %}></script> | ||
<script src="{{ (js_asset.preload ? ea_call_function_if_exists('preload', src, { as: 'script', nopush: js_asset.nopush }))|default(src) }}" {{ js_asset.async ? 'async' }} {{ js_asset.defer ? 'defer' }} | ||
{%- for attr, value in js_asset.htmlAttributes %} {{ attr }}="{{ value|e('html_attr') }}"{% endfor %}></script> | ||
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Controller; | ||
|
||
use EasyCorp\Bundle\EasyAdminBundle\Test\AbstractCrudTestCase; | ||
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\AssetsController; | ||
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\DashboardController; | ||
|
||
class AssetsControllerTest extends AbstractCrudTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->client->followRedirects(); | ||
} | ||
|
||
protected function getControllerFqcn(): string | ||
{ | ||
return AssetsController::class; | ||
} | ||
|
||
protected function getDashboardFqcn(): string | ||
{ | ||
return DashboardController::class; | ||
} | ||
|
||
public function testCssAssets() | ||
{ | ||
$crawler = $this->client->request('GET', $this->generateIndexUrl()); | ||
$headHtmlContents = $crawler->filter('head')->html(); | ||
$bodyHtmlContents = $crawler->filter('body')->html(); | ||
$linkResponseHeaderContents = $this->client->getResponse()->headers->get('Link'); | ||
|
||
static::assertStringContainsString('<link rel="stylesheet" href="https://cdn.example.com/css1.css">', $headHtmlContents); | ||
static::assertStringContainsString('<link rel="stylesheet" href="/css2.css">', $headHtmlContents); | ||
static::assertStringContainsString('<link rel="stylesheet" href="/css3.css">', $headHtmlContents); | ||
static::assertStringContainsString('<link rel="stylesheet" href="/foo/bar/css4.css">', $headHtmlContents); | ||
static::assertStringContainsString('<link rel="stylesheet" href="/css5.css">', $headHtmlContents); | ||
static::assertStringContainsString('<link rel="stylesheet" href="/css6.css">', $headHtmlContents); | ||
static::assertStringContainsString('</css6.css>; rel="preload"; as="style",', $linkResponseHeaderContents); | ||
static::assertStringContainsString('<link rel="stylesheet" href="/css7.css">', $headHtmlContents); | ||
static::assertStringContainsString('</css7.css>; rel="preload"; as="style"; nopush,', $linkResponseHeaderContents); | ||
static::assertStringContainsString('<link rel="stylesheet" href="/css8.css" media="print">', $headHtmlContents); | ||
static::assertStringContainsString('<link rel="stylesheet" href="/css9.css" media="print" title="foo">', $headHtmlContents); | ||
static::assertStringContainsString('<link rel="stylesheet" href="/css10.css">', $headHtmlContents); | ||
static::assertStringContainsString('<link rel="stylesheet" href="/css11.css">', $headHtmlContents); | ||
static::assertStringContainsString('<link rel="stylesheet" href="/css12.css">', $headHtmlContents); | ||
|
||
static::assertStringContainsString('<script src="https://cdn.example.com/js1.js"></script>', $headHtmlContents); | ||
static::assertStringContainsString('<script src="/js2.js"></script>', $headHtmlContents); | ||
static::assertStringContainsString('<script src="/js3.js"></script>', $headHtmlContents); | ||
static::assertStringContainsString('<script src="/foo/bar/js4.js"></script>', $headHtmlContents); | ||
static::assertStringContainsString('<script src="/js5.js"></script>', $headHtmlContents); | ||
static::assertStringContainsString('<script src="/js6.js"></script>', $headHtmlContents); | ||
static::assertStringContainsString('</js6.js>; rel="preload"; as="script",', $linkResponseHeaderContents); | ||
static::assertStringContainsString('<script src="/js7.js"></script>', $headHtmlContents); | ||
static::assertStringContainsString('</js7.js>; rel="preload"; as="script"; nopush,', $linkResponseHeaderContents); | ||
static::assertStringContainsString('<script src="/js8.js" defer></script>', $headHtmlContents); | ||
static::assertStringContainsString('<script src="/js9.js" async></script>', $headHtmlContents); | ||
static::assertStringContainsString('<script src="/js10.js" async defer></script>', $headHtmlContents); | ||
static::assertStringContainsString('<script src="/js11.js" async defer></script>', $headHtmlContents); | ||
static::assertStringContainsString('</js11.js>; rel="preload"; as="script"; nopush', $linkResponseHeaderContents); | ||
static::assertStringContainsString('<script src="/js12.js" foo="bar"></script>', $headHtmlContents); | ||
static::assertStringContainsString('<script src="/js13.js" foo="bar" baz="qux"></script>', $headHtmlContents); | ||
|
||
static::assertResponseHeaderSame('Link', '</css6.css>; rel="preload"; as="style",</css7.css>; rel="preload"; as="style"; nopush,</js6.js>; rel="preload"; as="script",</js7.js>; rel="preload"; as="script"; nopush,</js11.js>; rel="preload"; as="script"; nopush'); | ||
|
||
static::assertStringContainsString('<link rel="stylesheet" href="https://cdn.example.com/css11.css">', $headHtmlContents); | ||
static::assertStringContainsString('<script src="https://cdn.example.com/js14.js"></script>', $bodyHtmlContents); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller; | ||
|
||
use EasyCorp\Bundle\EasyAdminBundle\Config\Asset; | ||
use EasyCorp\Bundle\EasyAdminBundle\Config\Assets; | ||
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController; | ||
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Entity\BlogPost; | ||
|
||
/** | ||
* Tests all the different ways of configuring asn customizing the assets. | ||
*/ | ||
class AssetsController extends AbstractCrudController | ||
{ | ||
public static function getEntityFqcn(): string | ||
{ | ||
return BlogPost::class; | ||
} | ||
|
||
public function configureFields(string $pageName): iterable | ||
{ | ||
return ['title', 'content', 'publishedAt']; | ||
} | ||
|
||
public function configureAssets(Assets $assets): Assets | ||
{ | ||
return $assets | ||
->addCssFile('https://cdn.example.com/css1.css') | ||
->addCssFile('css2.css') | ||
->addCssFile('/css3.css') | ||
->addCssFile('/foo/bar/css4.css') | ||
->addCssFile(Asset::new('css5.css')) | ||
->addCssFile(Asset::new('css6.css')->preload()) | ||
->addCssFile(Asset::new('css7.css')->preload()->nopush()) | ||
->addCssFile(Asset::new('css8.css')->htmlAttr('media', 'print')) | ||
->addCssFile(Asset::new('css9.css')->htmlAttrs(['media' => 'print', 'title' => 'foo'])) | ||
->addCssFile(Asset::new('css10.css')->webpackPackageName('foo')) | ||
->addCssFile(Asset::new('css11.css')->webpackEntrypointName('foo')) | ||
->addCssFile(Asset::new('css12.css')->webpackEntrypointName('foo')->webpackPackageName('bar')) | ||
|
||
->addJsFile('https://cdn.example.com/js1.js') | ||
->addJsFile('js2.js') | ||
->addJsFile('/js3.js') | ||
->addJsFile('/foo/bar/js4.js') | ||
->addJsFile(Asset::new('js5.js')) | ||
->addJsFile(Asset::new('js6.js')->preload()) | ||
->addJsFile(Asset::new('js7.js')->preload()->nopush()) | ||
->addJsFile(Asset::new('js8.js')->defer()) | ||
->addJsFile(Asset::new('js9.js')->async()) | ||
->addJsFile(Asset::new('js10.js')->defer()->async()) | ||
->addJsFile(Asset::new('js11.js')->preload()->nopush()->defer()->async()) | ||
->addJsFile(Asset::new('js12.js')->htmlAttr('foo', 'bar')) | ||
->addJsFile(Asset::new('js13.js')->htmlAttrs(['foo' => 'bar', 'baz' => 'qux'])) | ||
|
||
->addHtmlContentToHead('<link rel="stylesheet" href="https://cdn.example.com/css11.css">') | ||
->addHtmlContentToBody('<script src="https://cdn.example.com/js14.js"></script>') | ||
; | ||
} | ||
} |