1<?php
2
3namespace dokuwiki\plugin\config\test;
4use dokuwiki\plugin\config\core\Setting\SettingString;
5use dokuwiki\plugin\config\core\Writer;
6
7/**
8 * @group plugin_config
9 * @group admin_plugins
10 * @group plugins
11 * @group bundled_plugins
12 */
13class WriterTest extends \DokuWikiTest {
14
15    public function testSave() {
16        global $config_cascade;
17        $config = end($config_cascade['main']['local']);
18
19        $set1 = new SettingString('test1');
20        $set1->initialize('foo','bar', null);
21        $set2 = new SettingString('test2');
22        $set2->initialize('foo','foo', null);
23        $settings = [$set1, $set2];
24        $writer = new Writer();
25
26        // before running, no backup should exist
27        $this->assertFileExists($config);
28        $this->assertFileDoesNotExist("$config.bak.php");
29        $old = filesize($config);
30
31        /** @noinspection PhpUnhandledExceptionInspection */
32        $writer->save($settings);
33
34        // after running, both should exist
35        $this->assertFileExists($config);
36        $this->assertFileExists("$config.bak.php");
37        $this->assertEquals($old, filesize("$config.bak.php"), 'backup should have size of old file');
38
39        // check contents
40        $conf = [];
41        include $config;
42        $this->assertArrayHasKey('test1', $conf);
43        $this->assertEquals('bar', $conf['test1']);
44        $this->assertArrayNotHasKey('test2', $conf);
45
46        /** @noinspection PhpUnhandledExceptionInspection */
47        $writer->save($settings);
48        $this->assertEquals(filesize($config), filesize("$config.bak.php"));
49    }
50
51    public function testTouch() {
52        global $config_cascade;
53        $config = end($config_cascade['main']['local']);
54        $writer = new Writer();
55
56        $old = filemtime($config);
57        $this->waitForTick(true);
58        /** @noinspection PhpUnhandledExceptionInspection */
59        $writer->touch();
60        clearstatcache($config);
61        $this->assertGreaterThan($old, filemtime($config));
62    }
63
64    public function testEmpty() {
65        $writer = new Writer();
66        $this->expectException(\Exception::class);
67        $this->expectExceptionMessage('empty config');
68        $writer->save([]);
69    }
70}
71