Skip to content

Commit

Permalink
bug #6700 Add tests for web assets (javiereguiluz)
Browse files Browse the repository at this point in the history
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
javiereguiluz committed Jan 11, 2025
2 parents 623f063 + c6dcace commit b76eca4
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 4 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"symfony/dom-crawler": "^5.4|^6.0|^7.0",
"symfony/expression-language": "^5.4|^6.0|^7.0",
"symfony/phpunit-bridge": "^6.1|^7.0",
"symfony/process": "^5.4|^6.0|^7.0"
"symfony/process": "^5.4|^6.0|^7.0",
"symfony/web-link": "^5.4|^6.0|^7.0"
},
"config": {
"sort-packages": true,
Expand Down
2 changes: 1 addition & 1 deletion templates/includes/_css_assets.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
{% for css_asset in assets %}
{% set href = asset(css_asset.value, css_asset.packageName) %}
<link rel="stylesheet" href="{{ (css_asset.preload ? ea_call_function_if_exists('preload', href, { as: 'style', nopush: css_asset.nopush }))|default(href) }}"
{% for attr, value in css_asset.htmlAttributes %}{{ attr }}="{{ value|e('html_attr') }}" {% endfor %}>
{%- for attr, value in css_asset.htmlAttributes %} {{ attr }}="{{ value|e('html_attr') }}"{% endfor %}>
{% endfor %}
4 changes: 2 additions & 2 deletions templates/includes/_js_assets.html.twig
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 %}
71 changes: 71 additions & 0 deletions tests/Controller/AssetsControllerTest.php
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);
}
}
59 changes: 59 additions & 0 deletions tests/TestApplication/src/Controller/AssetsController.php
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>')
;
}
}

0 comments on commit b76eca4

Please sign in to comment.