ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
PublicAssetManagerTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24use PHPUnit\Framework\TestCase;
26use PHPUnit\Framework\Attributes\DataProvider;
27
28class PublicAssetManagerTest extends TestCase
29{
31
32 protected function newPublicAsset($source, $target): PublicAsset
33 {
34 return new class ($source, $target) implements R\PublicAsset {
35 public function __construct(
36 protected string $source,
37 protected string $target,
38 ) {
39 }
40
41 public function getSource(): string
42 {
43 return $this->source;
44 }
45
46 public function getTarget(): string
47 {
48 return $this->target;
49 }
50 };
51 }
52
53 public function setUp(): void
54 {
55 $this->manager = new class () extends R\PublicAssetManager {
56 public $copied = [];
57 public $purged = [];
58 public $madeDir = [];
59
60 protected function copy(string $source, $target): void
61 {
62 $this->copied[] = [$source, $target];
63 }
64
65 protected function purge(string $path, array $dont_purge): bool
66 {
67 $this->purged[] = $path;
68 return true;
69 }
70
71 protected function makeDir(string $path): void
72 {
73 $this->madeDir[] = $path;
74 }
75 };
76 }
77
78 #[DataProvider('provideValidBasePathData')]
79 public function testDifferentBasePaths(string $base_path): void
80 {
81 $this->assertIsString($base_path);
82 $this->manager->buildPublicFolder($base_path, '/target');
83 }
84
85 public static function provideValidBasePathData(): \Iterator
86 {
87 yield ['/var/www/ilias'];
88 yield ['/var/www/html'];
89 yield ['/var/www/html/ilias'];
90 yield ['/srv/ilias'];
91 yield ['/srv/ilias10'];
92 yield ['/srv/ilias-10'];
93 yield ['/srv/ilias_10'];
94 yield ['/srv/ilias.10'];
95 yield ['/base'];
96 yield ['/base/path'];
97 yield ['/base-path'];
98 yield ['/base.path'];
99 yield ['/var/www/vhosts/bl-a.blubs/httpdoc'];
100 yield ['/var/www/v_hosts/bl-a.blubs/httpdoc'];
101 yield ['/var/www/v_hosts/bl-a.blubs_one/httpdoc'];
102 yield ['/srv/ilias-build/work/ilias-10.4.0'];
103 }
104
105 public function testTargetCanOnlyBeUsedOnce(): void
106 {
107 $this->expectException(\LogicException::class);
108
109 $asset1 = $this->newPublicAsset("some/source", "target");
110 $asset2 = $this->newPublicAsset("some/other/source", "target");
111
112 $this->manager->addAssets($asset1, $asset2);
113 }
114
116 {
117 $this->expectException(\LogicException::class);
118
119 $asset1 = $this->newPublicAsset("some/source", "target");
120 $asset2 = $this->newPublicAsset("some/other/source", "target/sub");
121
122 $this->manager->addAssets($asset1, $asset2);
123 }
124
126 {
127 $this->expectException(\LogicException::class);
128
129 $asset1 = $this->newPublicAsset("some/source", "target/sub");
130 $asset2 = $this->newPublicAsset("some/other/source", "target");
131
132 $this->manager->addAssets($asset1, $asset2);
133 }
134
135 public function testBuildAssetFolderEmpty(): void
136 {
137 $this->manager->buildPublicFolder("/base", "/target");
138 $this->assertEquals([], $this->manager->copied);
139 $this->assertEquals(["/target"], $this->manager->purged);
140 $this->assertEquals(["/target"], $this->manager->madeDir);
141 }
142
143 public function testBuildAssetFolder(): void
144 {
145 $this->manager->addAssets(
146 $this->newPublicAsset("source1", "target1"),
147 $this->newPublicAsset("source2", "second/target")
148 );
149
150 $this->manager->buildPublicFolder("/base", "/public");
151
152 $this->assertEquals(["/public"], $this->manager->purged);
153 $this->assertEquals(["/public", "/public/second"], $this->manager->madeDir);
154 $this->assertEquals(
155 [["/base/source1", "/public/target1"], ["/base/source2", "/public/second/target"]],
156 $this->manager->copied
157 );
158 }
159
160 public function testValidFolderPaths(): void
161 {
162 $this->expectNotToPerformAssertions();
163
164 $this->manager->buildPublicFolder("/base", "/public");
165 $this->manager->buildPublicFolder("/srv/demo10-ilias", "/public");
166 $this->manager->buildPublicFolder("/srv/demo10.ilias.de", "/public");
167 }
168
169 #[DataProvider('provideInvalidFolderPathData')]
170 public function testInvalidFolderPaths(string $ilias_base, string $target): void
171 {
172 $this->expectException(\InvalidArgumentException::class);
173 $this->manager->buildPublicFolder($ilias_base, $target);
174 }
175
176 public static function provideInvalidFolderPathData(): \Iterator
177 {
178 yield 'base - missing leading slash' => ['base', '/public'];
179 yield 'base - extra trailing slash' => ['/base/', '/public'];
180 yield 'base - dot only' => ['.', '/public'];
181 yield 'base - dash only' => ['-', '/public'];
182 yield 'base - invalid trailing dash' => ['/base/demo-', '/public'];
183 yield 'base - invalid trailing dot' => ['/base/demo.', '/public'];
184 yield 'base - invalid leading dash' => ['/base/.demo', '/public'];
185 yield 'base - invalid leading dot' => ['/base/-demo', '/public'];
186 yield 'base - invalid dot sub directory' => ['/./test', '/public'];
187 yield 'base - invalid dash sub directory' => ['/-/test', '/public'];
188 yield 'target - missing leading slash' => ['/base', 'public'];
189 yield 'target - extra trailing slash' => ['/base', '/public/'];
190 yield 'target - dot only' => ['/base', '.'];
191 yield 'target - dash only' => ['/base', '-'];
192 yield 'target - invalid trailing dash' => ['/base', '/public.'];
193 yield 'target - invalid trailing dot' => ['/base', '/public-'];
194 yield 'target - invalid leading dash' => ['/base', '/.public'];
195 yield 'target - invalid leading dot' => ['/base', '/-public'];
196 yield 'target - invalid dot sub directory' => ['/base', '/./public'];
197 yield 'target - invalid dash sub directory' => ['/base', '/-/public'];
198 }
199}
Will take care of the public assets, just like a good manager does.
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
An public asset is a file or folder that should be served via the web.
Definition: PublicAsset.php:27
$path
Definition: ltiservices.php:30