ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
MediaTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
38
39use PHPUnit\Framework\TestCase;
47
48require_once('./vendor/composer/vendor/autoload.php');
49
55final class MediaTest extends TestCase
56{
58 public string $version;
60
61 protected function setUp(): void
62 {
63 parent::setUp();
64 $this->version = '1.2.3.4.5.6.7.8.9';
65 $this->meta_content = new MetaContent(
66 $this->version,
67 true,
68 false,
69 true,
70 true
71 );
72 }
73
74 public function testAddCssFile(): void
75 {
76 $path = '/path/to/file.css';
77 $this->meta_content->addCss($path);
78 $collection = $this->meta_content->getCss();
79
80 $iterator_to_array = iterator_to_array($collection->getItems());
81 $first_item = array_shift($iterator_to_array);
82 $this->assertInstanceOf(Css::class, $first_item);
83 $this->assertSame($path . '?' . self::VERSION . '=' . $this->version, $first_item->getContent());
84 $this->assertSame(MetaContent::MEDIA_SCREEN, $first_item->getMedia());
85 }
86
87 public function testAddCssFileWithQuery(): void
88 {
89 $path = '/path/to/file.css?my=query';
90 $this->meta_content->addCss($path);
91 $collection = $this->meta_content->getCss();
92
93 $iterator_to_array = iterator_to_array($collection->getItems());
94 $first_item = array_shift($iterator_to_array);
95 $this->assertInstanceOf(Css::class, $first_item);
96 $this->assertSame($path . '&' . self::VERSION . '=' . $this->version, $first_item->getContent());
97 $this->assertSame(MetaContent::MEDIA_SCREEN, $first_item->getMedia());
98 }
99
100 public function testAddInlineCss(): void
101 {
102 $css = 'body {background-color:red;}';
103 $this->meta_content->addInlineCss($css);
104 $collection = $this->meta_content->getInlineCss();
105
106 $first_item = iterator_to_array($collection->getItems())[0];
107 $this->assertInstanceOf(InlineCss::class, $first_item);
108 $this->assertSame($css, $first_item->getContent());
109 $this->assertSame(MetaContent::MEDIA_SCREEN, $first_item->getMedia());
110 }
111
112 public function testAddJsFile(): void
113 {
114 $path = '/path/to/file.js';
115 $this->meta_content->addJs($path);
116 $collection = $this->meta_content->getJs();
117
118 $iterator_to_array = iterator_to_array($collection->getItems());
119 $first_item = $iterator_to_array[$path];
120 $this->assertInstanceOf(Js::class, $first_item);
121 $this->assertSame($path . '?' . self::VERSION . '=' . $this->version, $first_item->getContent());
122 $this->assertSame(2, $first_item->getBatch());
123 }
124
125 public function testAddJsFileWithQuery(): void
126 {
127 $path = '/path/to/file.js';
128 $path_with_query = $path . '?my=query';
129 $this->meta_content->addJs($path_with_query);
130 $collection = $this->meta_content->getJs();
131
132 $first_item = iterator_to_array($collection->getItems())[$path];
133 $this->assertInstanceOf(Js::class, $first_item);
134 $this->assertSame($path_with_query . '&' . self::VERSION . '=' . $this->version, $first_item->getContent());
135 $this->assertSame(2, $first_item->getBatch());
136 }
137
139 {
140 $this->meta_content->addVersionParameterFilter(new DeliverPhpFilter());
141 $path = '/ilias/public/deliver.php/HczLCgMhDIXhd8m6NDGjUYdhnsRNqk43vVGlUErfvXY2/-/style.css';
142 $this->meta_content->addCss($path);
143 $collection = $this->meta_content->getCss();
144
145 $items = iterator_to_array($collection->getItems());
146 $first_item = array_shift($items);
147 $this->assertInstanceOf(Css::class, $first_item);
148 $this->assertSame($path, $first_item->getContent());
149 }
150
152 {
153 $this->meta_content->addVersionParameterFilter(new DeliverPhpFilter());
154 $path = '/ilias/public/deliver.php/HczLCgMhDIXhd8m6NDGjUYdhnsRNqk43vVGlUErfvXY2/-/script.js';
155 $this->meta_content->addJs($path);
156 $collection = $this->meta_content->getJs();
157
158 $first_item = iterator_to_array($collection->getItems())[$path];
159 $this->assertInstanceOf(Js::class, $first_item);
160 $this->assertSame($path, $first_item->getContent());
161 }
162
164 {
165 $this->meta_content->addVersionParameterFilter(new DeliverPhpFilter());
166 $path = '/path/to/file.css';
167 $this->meta_content->addCss($path);
168 $collection = $this->meta_content->getCss();
169
170 $items = iterator_to_array($collection->getItems());
171 $first_item = array_shift($items);
172 $this->assertInstanceOf(Css::class, $first_item);
173 $this->assertSame($path . '?' . self::VERSION . '=' . $this->version, $first_item->getContent());
174 }
175
176 public function testCustomVersionParameterFilter(): void
177 {
178 $custom_filter = new class () implements VersionParameterFilter {
179 public function shouldAppend(string $content): bool
180 {
181 return !str_contains($content, '/no-version/');
182 }
183 };
184 $this->meta_content->addVersionParameterFilter($custom_filter);
185
186 $excluded = '/no-version/file.js';
187 $included = '/path/to/file.js';
188 $this->meta_content->addJs($excluded);
189 $this->meta_content->addJs($included);
190
191 $items = iterator_to_array($this->meta_content->getJs()->getItems());
192 $this->assertEquals($excluded, $items[$excluded]->getContent());
193 $this->assertEquals(
194 $included . '?' . self::VERSION . '=' . $this->version,
195 $items[$included]->getContent()
196 );
197 }
198
200 {
202 $this->version,
203 true,
204 false,
205 true,
206 true,
207 [new DeliverPhpFilter()]
208 );
209 $path = '/ilias/public/deliver.php/SIGNED/-/style.css';
211
212 $items = iterator_to_array($meta_content->getCss()->getItems());
213 $first_item = array_shift($items);
214 $this->assertEquals($path, $first_item->getContent());
215 }
216}
Excludes URLs delivered by deliver.php from the resource version parameter.
addCss(string $path, string $media=self::MEDIA_SCREEN)
$path
Definition: ltiservices.php:30
@noinspection PhpIncompatibleReturnTypeInspection