ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
StartFileFallbackTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use PHPUnit\Framework\Attributes\DataProvider;
22use PHPUnit\Framework\TestCase;
23
24class StartFileFallbackTest extends TestCase
25{
26 private string $dir;
27
28 protected function setUp(): void
29 {
30 $this->dir = sys_get_temp_dir() . '/htlm_start_file_' . bin2hex(random_bytes(4));
31 mkdir($this->dir);
32 }
33
34 protected function tearDown(): void
35 {
36 $it = new RecursiveIteratorIterator(
37 new RecursiveDirectoryIterator($this->dir, FilesystemIterator::SKIP_DOTS),
38 RecursiveIteratorIterator::CHILD_FIRST
39 );
40 foreach ($it as $file) {
41 $file->isDir() ? rmdir($file->getPathname()) : unlink($file->getPathname());
42 }
43 rmdir($this->dir);
44 }
45
49 private function archive(array $files): ZipArchive
50 {
51 $path = $this->dir . '/lm.zip';
52 $zip = new ZipArchive();
53 $zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE);
54 foreach ($files as $file) {
55 $zip->addFromString($file, 'content');
56 }
57 $zip->close();
58
59 $zip = new ZipArchive();
60 $zip->open($path, ZipArchive::RDONLY);
61 return $zip;
62 }
63
64 public static function archiveProvider(): array
65 {
66 return [
67 'stored start file wins' => [['index.html', 'sub/start.html'], 'sub/start.html', 'sub/start.html'],
68 'empty start file falls back to index.html' => [['index.html', 'page.html'], '', 'index.html'],
69 'empty start file falls back to index.htm' => [['index.htm', 'page.html'], '', 'index.htm'],
70 'index.html is preferred over index.htm' => [['index.htm', 'index.html'], '', 'index.html'],
71 'stored start file missing falls back' => [['index.html'], 'gone.html', 'index.html'],
72 'nested index is not used' => [['sub/index.html'], '', ''],
73 'nothing found' => [['page.html'], '', ''],
74 ];
75 }
76
80 #[DataProvider('archiveProvider')]
81 public function testLocateStartFileInArchive(array $files, string $stored, string $expected): void
82 {
83 $zip = $this->archive($files);
84 $this->assertSame($expected, ilObjFileBasedLMAccess::locateStartFileInArchive($zip, $stored));
85 $zip->close();
86 }
87
88 public static function directoryProvider(): array
89 {
90 return [
91 'stored start file is kept' => [['index.html'], 'start.html', 'start.html'],
92 'index.html' => [['index.html', 'index.htm'], '', 'index.html'],
93 'index.htm' => [['index.htm'], '', 'index.htm'],
94 'nothing found' => [['page.html'], '', ''],
95 ];
96 }
97
101 #[DataProvider('directoryProvider')]
102 public function testMigrationDeterminesStartFile(array $files, string $stored, string $expected): void
103 {
104 foreach ($files as $file) {
105 touch($this->dir . '/' . $file);
106 }
107 $migration = new class () extends ilHTLMMigration {
108 public function determine(string $lm_path, string $start_file): string
109 {
110 return $this->determineStartFile($lm_path, $start_file);
111 }
112 };
113 $this->assertSame($expected, $migration->determine($this->dir, $stored));
114 }
115}
testMigrationDeterminesStartFile(array $files, string $stored, string $expected)
testLocateStartFileInArchive(array $files, string $stored, string $expected)
static locateStartFileInArchive(\ZipArchive $archive, string $start_file)
Returns the stored start file if the archive contains it, otherwise falls back to index....