-
-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathConfigurationTest.php
More file actions
384 lines (314 loc) · 12.3 KB
/
ConfigurationTest.php
File metadata and controls
384 lines (314 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
namespace phpMyFAQ;
use Monolog\Handler\TestHandler;
use Monolog\Level;
use Monolog\Logger;
use phpMyFAQ\Configuration\LdapConfiguration;
use phpMyFAQ\Database\DatabaseDriver;
use phpMyFAQ\Database\Sqlite3;
use phpMyFAQ\Plugin\PluginException;
use phpMyFAQ\Plugin\PluginManager;
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Session;
/**
* Class ConfigurationTest
*/
#[AllowMockObjectsWithoutExpectations]
class ConfigurationTest extends TestCase
{
/** @var Configuration */
private Configuration $configuration;
private string $databaseFile;
private ?Configuration $originalConfigurationInstance = null;
/**
* Prepares the environment before running a test.
*/
protected function setUp(): void
{
parent::setUp();
$this->originalConfigurationInstance = $this->getStaticConfigurationInstance();
$this->setStaticConfigurationInstance(null);
Strings::init();
$this->databaseFile = tempnam(sys_get_temp_dir(), 'phpmyfaq-config-test-');
copy(PMF_TEST_DIR . '/test.db', $this->databaseFile);
$dbHandle = new Sqlite3();
$dbHandle->connect($this->databaseFile, '', '');
$this->configuration = new Configuration($dbHandle);
// Initialize the configuration table structure if needed
$this->setupTestDatabase();
}
/**
* Setup test database with configuration table
*/
private function setupTestDatabase(): void
{
$db = $this->configuration->getDb();
// Create faqconfig table if it doesn't exist
$createTable = '
CREATE TABLE IF NOT EXISTS faqconfig (
config_name VARCHAR(255) NOT NULL PRIMARY KEY,
config_value TEXT
)
';
$db->query($createTable);
// Insert some default configuration values for testing
$defaultConfigs = [
'main.currentVersion' => System::getVersion(),
'main.language' => 'en',
'security.permLevel' => 'basic',
];
foreach ($defaultConfigs as $key => $value) {
$insertQuery = sprintf(
"INSERT OR REPLACE INTO faqconfig (config_name, config_value) VALUES ('%s', '%s')",
$key,
$value,
);
$db->query($insertQuery);
}
}
/**
* Cleans up the environment after running a test.
*/
protected function tearDown(): void
{
$this->setStaticConfigurationInstance($this->originalConfigurationInstance);
parent::tearDown();
if (isset($this->databaseFile) && file_exists($this->databaseFile)) {
@unlink($this->databaseFile);
}
}
public function testGetConfigurationInstance(): void
{
$instance = Configuration::getConfigurationInstance();
$this->assertInstanceOf(Configuration::class, $instance);
$this->assertSame($instance, Configuration::getConfigurationInstance());
}
/**
* @throws Exception
*/
public function testSetDatabase(): void
{
$database = $this->createStub(DatabaseDriver::class);
$config = new Configuration($database);
$config->setDatabase($database);
$this->assertSame($database, $config->get('core.database'));
}
public function testSet(): void
{
$key = 'upgrade.releaseEnvironment';
$value = 'test';
$result = $this->configuration->set($key, $value);
$this->assertTrue($result);
$this->assertEquals($value, $this->configuration->get($key));
}
public function testAdd(): void
{
$key = 'test.add.' . bin2hex(random_bytes(4));
$result = $this->configuration->add($key, 'foo');
$this->assertTrue($result);
$this->assertEquals('foo', $this->configuration->get($key));
}
public function testGetDb(): void
{
$db = $this->configuration->getDb();
$this->assertInstanceOf(DatabaseDriver::class, $db);
}
public function testSetLdapConfigWithSingleServer(): void
{
// Demo data from /content/core/config/ldap.php
file_put_contents(
PMF_TEST_DIR . '/content/core/config/ldap.php',
"<?php\n"
. "\$PMF_LDAP['ldap_server'] = 'localhost';\n"
. "\$PMF_LDAP['ldap_port'] = 389;\n"
. "\$PMF_LDAP['ldap_user'] = 'admin';\n"
. "\$PMF_LDAP['ldap_password'] = 'foobar';\n"
. "\$PMF_LDAP['ldap_base'] = 'DC=foo,DC=bar,DC=baz';",
LOCK_EX,
);
$this->configuration->set('ldap.ldap_use_multiple_servers', 'false');
$expected = [
0 => [
'ldap_server' => 'localhost',
'ldap_port' => 389,
'ldap_user' => 'admin',
'ldap_password' => 'foobar',
'ldap_base' => 'DC=foo,DC=bar,DC=baz',
],
];
$ldapConfig = new LdapConfiguration(PMF_TEST_DIR . '/content/core/config/ldap.php');
$this->configuration->setLdapConfig($ldapConfig);
$this->assertEquals($expected, $this->configuration->getLdapServer());
}
public function testSetLdapConfigWithMultipleServers(): void
{
// Demo data from /content/core/config/ldap.php
file_put_contents(
PMF_TEST_DIR . '/content/core/config/ldap.php',
"<?php\n"
. "\$PMF_LDAP['ldap_server'] = 'localhost';\n"
. "\$PMF_LDAP['ldap_port'] = '389';\n"
. "\$PMF_LDAP['ldap_user'] = 'admin';\n"
. "\$PMF_LDAP['ldap_password'] = 'foobar';\n"
. "\$PMF_LDAP['ldap_base'] = 'DC=foo,DC=bar,DC=baz';"
. "\$PMF_LDAP[1]['ldap_server'] = '::1';\n"
. "\$PMF_LDAP[1]['ldap_port'] = '389';\n"
. "\$PMF_LDAP[1]['ldap_user'] = 'root';\n"
. "\$PMF_LDAP[1]['ldap_password'] = '42';\n"
. "\$PMF_LDAP[1]['ldap_base'] = 'DC=foo,DC=bar,DC=baz';",
LOCK_EX,
);
$this->configuration->set('ldap.ldap_use_multiple_servers', 'true');
$expected = [
0 => [
'ldap_server' => 'localhost',
'ldap_port' => '389',
'ldap_user' => 'admin',
'ldap_password' => 'foobar',
'ldap_base' => 'DC=foo,DC=bar,DC=baz',
],
1 => [
'server' => '::1',
'port' => '389',
'user' => 'root',
'password' => '42',
'base' => 'DC=foo,DC=bar,DC=baz',
],
];
$ldapConfig = new LdapConfiguration(PMF_TEST_DIR . '/content/core/config/ldap.php');
$this->configuration->setLdapConfig($ldapConfig);
$this->assertEquals($expected, $this->configuration->getLdapServer());
}
public function testSetLdapConfigWithMultipleServersButDisabled(): void
{
// Demo data from /content/core/config/ldap.php
file_put_contents(
PMF_TEST_DIR . '/content/core/config/ldap.php',
"<?php\n"
. "\$PMF_LDAP['ldap_server'] = 'localhost';\n"
. "\$PMF_LDAP['ldap_port'] = '389';\n"
. "\$PMF_LDAP['ldap_user'] = 'admin';\n"
. "\$PMF_LDAP['ldap_password'] = 'foobar';\n"
. "\$PMF_LDAP['ldap_base'] = 'DC=foo,DC=bar,DC=baz';"
. "\$PMF_LDAP[1]['ldap_server'] = '::1';\n"
. "\$PMF_LDAP[1]['ldap_port'] = '389';\n"
. "\$PMF_LDAP[1]['ldap_user'] = 'root';\n"
. "\$PMF_LDAP[1]['ldap_password'] = '42';\n"
. "\$PMF_LDAP[1]['ldap_base'] = 'DC=foo,DC=bar,DC=baz';",
LOCK_EX,
);
$this->configuration->set('ldap.ldap_use_multiple_servers', 'false');
$expected = [
0 => [
'ldap_server' => 'localhost',
'ldap_port' => '389',
'ldap_user' => 'admin',
'ldap_password' => 'foobar',
'ldap_base' => 'DC=foo,DC=bar,DC=baz',
],
];
$ldapConfig = new LdapConfiguration(PMF_TEST_DIR . '/content/core/config/ldap.php');
$this->configuration->setLdapConfig($ldapConfig);
$this->assertEquals($expected, $this->configuration->getLdapServer());
}
/**
* Test database operations
*/
public function testDatabaseOperations(): void
{
$db = $this->configuration->getDb();
$this->assertInstanceOf(DatabaseDriver::class, $db);
// Test setting new database instance
$newDb = new Sqlite3();
$this->configuration->setDatabase($newDb);
$this->assertSame($newDb, $this->configuration->getDb());
}
/**
* Test language configuration
*/
public function testLanguageConfiguration(): void
{
$language = new Language($this->configuration, $this->createStub(Session::class));
$this->configuration->setLanguage($language);
$retrievedLanguage = $this->configuration->getLanguage();
$this->assertSame($language, $retrievedLanguage);
$this->assertInstanceOf(Language::class, $retrievedLanguage);
}
/**
* Test plugin manager integration
*/
public function testPluginManager(): void
{
$pluginManager = $this->configuration->getPluginManager();
$this->assertInstanceOf(PluginManager::class, $pluginManager);
}
/**
* Test error handling for invalid operations - corrected
*/
public function testErrorHandling(): void
{
// Test getting non-existent configuration
$this->assertNull($this->configuration->get(item: 'non.existent.key'));
// Test with empty key might still work in some implementations
$result = $this->configuration->set('', 'value');
$this->assertIsBool($result); // Just verify it returns a boolean
}
/**
* Test singleton pattern implementation
*/
public function testSingletonPattern(): void
{
$instance1 = Configuration::getConfigurationInstance();
$instance2 = Configuration::getConfigurationInstance();
$this->assertSame($instance1, $instance2);
$this->assertInstanceOf(Configuration::class, $instance1);
}
public function testConstructorLogsPluginManagerException(): void
{
$testHandler = new TestHandler(Level::Error);
$dbHandle = new Sqlite3();
$dbHandle->connect($this->databaseFile, '', '');
$configuration = new class($dbHandle, $testHandler) extends Configuration {
public function __construct(
DatabaseDriver $databaseDriver,
private readonly TestHandler $testHandler,
) {
parent::__construct($databaseDriver);
}
public function setLogger(): void
{
$logger = new Logger('phpmyfaq-test');
$logger->pushHandler($this->testHandler);
$reflection = new \ReflectionProperty(Configuration::class, 'logger');
$reflection->setValue($this, $logger);
}
public function setPluginManager(): Configuration
{
throw new PluginException('Plugin setup failed');
}
};
$this->assertInstanceOf(Configuration::class, $configuration);
$this->assertTrue($testHandler->hasErrorThatContains('Plugin setup failed'));
}
public function testConstructorDoesNotOverwriteExistingSingletonInstance(): void
{
$firstInstance = $this->configuration;
$dbHandle = new Sqlite3();
$dbHandle->connect($this->databaseFile, '', '');
$secondInstance = new Configuration($dbHandle);
$this->assertNotSame($firstInstance, $secondInstance);
$this->assertSame($firstInstance, Configuration::getConfigurationInstance());
}
private function getStaticConfigurationInstance(): ?Configuration
{
$reflection = new \ReflectionProperty(Configuration::class, 'configuration');
return $reflection->getValue(null);
}
private function setStaticConfigurationInstance(?Configuration $configuration): void
{
$reflection = new \ReflectionProperty(Configuration::class, 'configuration');
$reflection->setValue(null, $configuration);
}
}