ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
PublicAssetManagerTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
26
27class PublicAssetManagerTest extends TestCase
28{
30
31 protected function newPublicAsset($source, $target)
32 {
33 return new class ($source, $target) implements R\PublicAsset {
34 public function __construct(
35 protected string $source,
36 protected string $target,
37 ) {
38 }
39
40 public function getSource(): string
41 {
42 return $this->source;
43 }
44
45 public function getTarget(): string
46 {
47 return $this->target;
48 }
49 };
50 }
51
52 public function setUp(): void
53 {
54 $this->manager = new class () extends R\PublicAssetManager {
55 public $copied = [];
56 public $purged = [];
57 public $madeDir = [];
58
59 protected function copy(string $source, $target): void
60 {
61 $this->copied[] = [$source, $target];
62 }
63
64 protected function purge(string $path, array $dont_purge): bool
65 {
66 $this->purged[] = $path;
67 return true;
68 }
69
70 protected function makeDir(string $path): void
71 {
72 $this->madeDir[] = $path;
73 }
74 };
75 }
76
78 {
79 $this->expectException(\LogicException::class);
80
81 $asset1 = $this->newPublicAsset("some/source", "target");
82 $asset2 = $this->newPublicAsset("some/other/source", "target");
83
84 $this->manager->addAssets($asset1, $asset2);
85 }
86
88 {
89 $this->expectException(\LogicException::class);
90
91 $asset1 = $this->newPublicAsset("some/source", "target");
92 $asset2 = $this->newPublicAsset("some/other/source", "target/sub");
93
94 $this->manager->addAssets($asset1, $asset2);
95 }
96
98 {
99 $this->expectException(\LogicException::class);
100
101 $asset1 = $this->newPublicAsset("some/source", "target/sub");
102 $asset2 = $this->newPublicAsset("some/other/source", "target");
103
104 $this->manager->addAssets($asset1, $asset2);
105 }
106
108 {
109 $this->manager->buildPublicFolder("/base", "/target");
110 $this->assertEquals([], $this->manager->copied);
111 $this->assertEquals(["/target"], $this->manager->purged);
112 $this->assertEquals(["/target"], $this->manager->madeDir);
113 }
114
115 public function testBuildAssetFolder()
116 {
117 $this->manager->addAssets(
118 $this->newPublicAsset("source1", "target1"),
119 $this->newPublicAsset("source2", "second/target")
120 );
121
122 $this->manager->buildPublicFolder("/base", "/public");
123
124 $this->assertEquals(["/public"], $this->manager->purged);
125 $this->assertEquals(["/public", "/public/second"], $this->manager->madeDir);
126 $this->assertEquals([["/base/source1", "/public/target1"], ["/base/source2", "/public/second/target"]], $this->manager->copied);
127 }
128
129 public function testValidFolderPaths(): void
130 {
131 $this->expectNotToPerformAssertions();
132
133 $this->manager->buildPublicFolder("/base", "/public");
134 $this->manager->buildPublicFolder("/srv/demo10-ilias", "/public");
135 $this->manager->buildPublicFolder("/srv/demo10.ilias.de", "/public");
136 }
137
138 #[\PHPUnit\Framework\Attributes\DataProvider('provideInvalidFolderPathData')]
139 public function testInvalidFolderPaths(string $ilias_base, string $target): void
140 {
141 $this->expectException(\InvalidArgumentException::class);
142 $this->manager->buildPublicFolder($ilias_base, $target);
143 }
144
145 public static function provideInvalidFolderPathData(): array
146 {
147 return [
148 'base - missing leading slash' => ['base', '/public'],
149 'base - extra trailing slash' => ['/base/', '/public'],
150 'base - dot only' => ['.', '/public'],
151 'base - dash only' => ['-', '/public'],
152 'base - invalid trailing dash' => ['/base/demo-', '/public'],
153 'base - invalid trailing dot' => ['/base/demo.', '/public'],
154 'base - invalid leading dash' => ['/base/.demo', '/public'],
155 'base - invalid leading dot' => ['/base/-demo', '/public'],
156 'base - invalid dot sub directory' => ['/./test', '/public'],
157 'base - invalid dash sub directory' => ['/-/test', '/public'],
158 'target - missing leading slash' => ['/base', 'public'],
159 'target - extra trailing slash' => ['/base', '/public/'],
160 'target - dot only' => ['/base', '.'],
161 'target - dash only' => ['/base', '-'],
162 'target - invalid trailing dash' => ['/base', '/public.'],
163 'target - invalid trailing dot' => ['/base', '/public-'],
164 'target - invalid leading dash' => ['/base', '/.public'],
165 'target - invalid leading dot' => ['/base', '/-public'],
166 'target - invalid dot sub directory' => ['/base', '/./public'],
167 'target - invalid dash sub directory' => ['/base', '/-/public'],
168 ];
169 }
170}
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