|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @package Joomla.UnitTest |
| 5 | + * @subpackage WebAsset |
| 6 | + * |
| 7 | + * @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org> |
| 8 | + * @license GNU General Public License version 2 or later; see LICENSE.txt |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Joomla\Tests\Unit\Libraries\Cms\WebAsset; |
| 12 | + |
| 13 | +use Joomla\CMS\WebAsset\Exception\InvalidActionException; |
| 14 | +use Joomla\CMS\WebAsset\Exception\UnknownAssetException; |
| 15 | +use Joomla\CMS\WebAsset\Exception\UnsatisfiedDependencyException; |
| 16 | +use Joomla\CMS\WebAsset\WebAssetManager; |
| 17 | +use Joomla\CMS\WebAsset\WebAssetRegistry; |
| 18 | +use Joomla\Tests\Unit\UnitTestCase; |
| 19 | + |
| 20 | +/** |
| 21 | + * Test class for WebAssetManager. |
| 22 | + * |
| 23 | + * @package Joomla.UnitTest |
| 24 | + * @subpackage WebAsset |
| 25 | + * @since __DEPLOY_VERSION__ |
| 26 | + */ |
| 27 | +class WebAssetManagerTest extends UnitTestCase |
| 28 | +{ |
| 29 | + /** |
| 30 | + * The WebAsset Registry instance |
| 31 | + * |
| 32 | + * @var WebAssetRegistry |
| 33 | + * |
| 34 | + * @since __DEPLOY_VERSION__ |
| 35 | + */ |
| 36 | + protected $registry; |
| 37 | + |
| 38 | + /** |
| 39 | + * Sets up the fixture. |
| 40 | + * |
| 41 | + * @return void |
| 42 | + * |
| 43 | + * @since __DEPLOY_VERSION__ |
| 44 | + */ |
| 45 | + protected function setUp(): void |
| 46 | + { |
| 47 | + $this->registry = new WebAssetRegistry(); |
| 48 | + $this->registry->addRegistryFile('tests/Unit/Libraries/Cms/WebAsset/asset.registry.json'); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Tears down the fixture. |
| 53 | + * |
| 54 | + * @return void |
| 55 | + * |
| 56 | + * @since __DEPLOY_VERSION__ |
| 57 | + */ |
| 58 | + protected function tearDown(): void |
| 59 | + { |
| 60 | + $this->registry = null; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Test useAsset, isAssetActive |
| 65 | + * |
| 66 | + * @return void |
| 67 | + * |
| 68 | + * @since __DEPLOY_VERSION__ |
| 69 | + */ |
| 70 | + public function testUseAsset(): void |
| 71 | + { |
| 72 | + $wa = new WebAssetManager($this->registry); |
| 73 | + |
| 74 | + $this->assertFalse($wa->isAssetActive('script', 'test1')); |
| 75 | + $wa->useAsset('script', 'test1'); |
| 76 | + $this->assertTrue($wa->isAssetActive('script', 'test1')); |
| 77 | + |
| 78 | + // Test asset with dependencies |
| 79 | + $wa->useAsset('script', 'test2'); |
| 80 | + $this->assertTrue($wa->isAssetActive('script', 'test2'), 'Dependency should be active also'); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Test useAsset with unknown asset |
| 85 | + * |
| 86 | + * @return void |
| 87 | + * |
| 88 | + * @since __DEPLOY_VERSION__ |
| 89 | + */ |
| 90 | + public function testUseAssetUnknownAsset(): void |
| 91 | + { |
| 92 | + $wa = new WebAssetManager($this->registry); |
| 93 | + $this->expectException(UnknownAssetException::class); |
| 94 | + $wa->useAsset('script', 'test.nope'); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Test isAssetActive with unknown asset |
| 99 | + * |
| 100 | + * @return void |
| 101 | + * |
| 102 | + * @since __DEPLOY_VERSION__ |
| 103 | + */ |
| 104 | + public function testIsAssetActiveUnknownAsset(): void |
| 105 | + { |
| 106 | + $wa = new WebAssetManager($this->registry); |
| 107 | + $this->expectException(UnknownAssetException::class); |
| 108 | + $wa->isAssetActive('script', 'test.nope'); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Test useAsset, when WA is locked |
| 113 | + * |
| 114 | + * @return void |
| 115 | + * |
| 116 | + * @since __DEPLOY_VERSION__ |
| 117 | + */ |
| 118 | + public function testUseAssetWALocked(): void |
| 119 | + { |
| 120 | + $wa = new WebAssetManager($this->registry); |
| 121 | + $wa->lock(); |
| 122 | + |
| 123 | + $this->expectException(InvalidActionException::class); |
| 124 | + $wa->useAsset('script', 'test1'); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Test disableAsset |
| 129 | + * |
| 130 | + * @return void |
| 131 | + * |
| 132 | + * @since __DEPLOY_VERSION__ |
| 133 | + */ |
| 134 | + public function testDisableAsset(): void |
| 135 | + { |
| 136 | + $wa = new WebAssetManager($this->registry); |
| 137 | + $wa->useAsset('script', 'test1'); |
| 138 | + |
| 139 | + $this->assertTrue($wa->isAssetActive('script', 'test1')); |
| 140 | + |
| 141 | + $wa->disableAsset('script', 'test1'); |
| 142 | + |
| 143 | + $this->assertFalse($wa->isAssetActive('script', 'test1')); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Test disableAsset with unknown asset |
| 148 | + * |
| 149 | + * @return void |
| 150 | + * |
| 151 | + * @since __DEPLOY_VERSION__ |
| 152 | + */ |
| 153 | + public function testDisableAssetUnknownAsset(): void |
| 154 | + { |
| 155 | + $wa = new WebAssetManager($this->registry); |
| 156 | + $this->expectException(UnknownAssetException::class); |
| 157 | + $wa->disableAsset('script', 'test.nope'); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Test disableAsset, when WA is locked |
| 162 | + * |
| 163 | + * @return void |
| 164 | + * |
| 165 | + * @since __DEPLOY_VERSION__ |
| 166 | + */ |
| 167 | + public function testDisableAssetWALocked(): void |
| 168 | + { |
| 169 | + $wa = new WebAssetManager($this->registry); |
| 170 | + $wa->lock(); |
| 171 | + |
| 172 | + $this->expectException(InvalidActionException::class); |
| 173 | + $wa->disableAsset('script', 'test1'); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Test getAssets |
| 178 | + * |
| 179 | + * @return void |
| 180 | + * |
| 181 | + * @since __DEPLOY_VERSION__ |
| 182 | + */ |
| 183 | + public function testGetAssets(): void |
| 184 | + { |
| 185 | + $wa = new WebAssetManager($this->registry); |
| 186 | + |
| 187 | + $wa->useAsset('script', 'test1'); |
| 188 | + $wa->useAsset('script', 'test2'); |
| 189 | + $wa->useAsset('script', 'test4'); |
| 190 | + $wa->useAsset('script', 'test5'); |
| 191 | + |
| 192 | + $assets = $wa->getAssets('script'); |
| 193 | + $this->assertEquals( |
| 194 | + ['test3', 'test1', 'test2', 'test4', 'test5'], |
| 195 | + array_keys($assets), |
| 196 | + 'Should return all active assets in FIFO order, with automatically enabled dependencies "at top"' |
| 197 | + ); |
| 198 | + |
| 199 | + $assets2 = $wa->getAssets('script', true); |
| 200 | + $this->assertEquals( |
| 201 | + ['test3', 'test1', 'test2', 'test5', 'test4'], |
| 202 | + array_keys($assets2), |
| 203 | + 'Should return all active assets in a Graph order' |
| 204 | + ); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Test getAssets with broken dependency |
| 209 | + * |
| 210 | + * @return void |
| 211 | + * |
| 212 | + * @since __DEPLOY_VERSION__ |
| 213 | + */ |
| 214 | + public function testGetAssetsUnknownDep(): void |
| 215 | + { |
| 216 | + $wa = new WebAssetManager($this->registry); |
| 217 | + $wa->useAsset('script', 'test.bad-dep'); |
| 218 | + |
| 219 | + $this->expectException(UnsatisfiedDependencyException::class); |
| 220 | + $wa->getAssets('script'); |
| 221 | + } |
| 222 | +} |
0 commit comments