ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
IniFileWriteRepositoryTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22use ILIAS\Environment\Configuration\Ini\IniFileWriteRepository;
23use PHPUnit\Framework\Attributes\DataProvider;
24use PHPUnit\Framework\TestCase;
25
26final class IniFileWriteRepositoryTest extends TestCase
27{
28 private string $file;
29
30 protected function setUp(): void
31 {
32 $this->file = tempnam(sys_get_temp_dir(), 'ini_write_');
33 }
34
35 protected function tearDown(): void
36 {
37 if (is_file($this->file)) {
38 unlink($this->file);
39 }
40 }
41
42 public function testSetThenGet(): void
43 {
44 $repo = new IniFileWriteRepository($this->file);
45 $repo->set('server', 'absolute_path', '/var/www');
46 self::assertSame('/var/www', $repo->get('server', 'absolute_path'));
47 }
48
49 public function testAddAndRemoveSection(): void
50 {
51 $repo = new IniFileWriteRepository($this->file);
52 $repo->addSection('tools');
53 self::assertTrue($repo->hasSection('tools'));
54 $repo->removeSection('tools');
55 self::assertFalse($repo->hasSection('tools'));
56 }
57
58 public function testRemoveKey(): void
59 {
60 $repo = new IniFileWriteRepository($this->file);
61 $repo->set('section', 'key', 'value');
62 $repo->remove('section', 'key');
63 self::assertFalse($repo->has('section', 'key'));
64 }
65
66 public function testPersistRoundTrip(): void
67 {
68 $repo = new IniFileWriteRepository($this->file);
69 $repo->set('server', 'absolute_path', '/var/www/ilias');
70 $repo->set('clients', 'default', 'default');
71 $repo->persist();
72
73 $reloaded = new IniFileReadRepository($this->file);
74 self::assertSame('/var/www/ilias', $reloaded->get('server', 'absolute_path'));
75 self::assertSame('default', $reloaded->get('clients', 'default'));
76 }
77
78 #[DataProvider('specialValues')]
79 public function testPersistRoundTripsSpecialCharacters(string $value): void
80 {
81 $repo = new IniFileWriteRepository($this->file);
82 $repo->set('section', 'key', $value);
83 $repo->persist();
84
85 $reloaded = new IniFileReadRepository($this->file);
86 self::assertSame($value, $reloaded->get('section', 'key'));
87 }
88
92 public static function specialValues(): array
93 {
94 return [
95 'double quote' => ['pa"ss"word'],
96 'backslash' => ['C:\\path\\to\\dir'],
97 'quote after backslash' => ['a\\"b'],
98 'trailing slash' => ['/var/www/'],
99 'equals sign' => ['key=value'],
100 'semicolon' => ['value;with;semicolons'],
101 'leading and trailing spaces' => [' spaced '],
102 'empty' => [''],
103 ];
104 }
105
106 public function testSetRejectsLineFeed(): void
107 {
108 $repo = new IniFileWriteRepository($this->file);
109 $this->expectException(InvalidArgumentException::class);
110 $repo->set('section', 'key', "line1\nline2");
111 }
112
113 public function testSetRejectsCarriageReturn(): void
114 {
115 $repo = new IniFileWriteRepository($this->file);
116 $this->expectException(InvalidArgumentException::class);
117 $repo->set('section', 'key', "a\rb");
118 }
119
120 public function testSetRejectsNulByte(): void
121 {
122 $repo = new IniFileWriteRepository($this->file);
123 $this->expectException(InvalidArgumentException::class);
124 $repo->set('section', 'key', "a\x00b");
125 }
126
128 {
129 $repo = new IniFileWriteRepository($this->file);
130 $this->expectException(InvalidArgumentException::class);
131 $repo->set('section', 'k=bad', 'value');
132 }
133
134 public function testSetRejectsInvalidSection(): void
135 {
136 $repo = new IniFileWriteRepository($this->file);
137 $this->expectException(InvalidArgumentException::class);
138 $repo->set('bad]section', 'key', 'value');
139 }
140}