aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/lib/plugins/config/_test/WriterTest.php
blob: 973bd5e52ffb44bb159f916996b1d9467752ed5d (plain) (blame)
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
<?php

namespace dokuwiki\plugin\config\test;
use dokuwiki\plugin\config\core\Setting\SettingString;
use dokuwiki\plugin\config\core\Writer;

/**
 * @group plugin_config
 * @group admin_plugins
 * @group plugins
 * @group bundled_plugins
 */
class WriterTest extends \DokuWikiTest {

    public function testSave() {
        global $config_cascade;
        $config = end($config_cascade['main']['local']);

        $set1 = new SettingString('test1');
        $set1->initialize('foo','bar', null);
        $set2 = new SettingString('test2');
        $set2->initialize('foo','foo', null);
        $settings = [$set1, $set2];
        $writer = new Writer();

        // before running, no backup should exist
        $this->assertFileExists($config);
        $this->assertFileDoesNotExist("$config.bak.php");
        $old = filesize($config);

        /** @noinspection PhpUnhandledExceptionInspection */
        $writer->save($settings);

        // after running, both should exist
        $this->assertFileExists($config);
        $this->assertFileExists("$config.bak.php");
        $this->assertEquals($old, filesize("$config.bak.php"), 'backup should have size of old file');

        // check contents
        $conf = [];
        include $config;
        $this->assertArrayHasKey('test1', $conf);
        $this->assertEquals('bar', $conf['test1']);
        $this->assertArrayNotHasKey('test2', $conf);

        /** @noinspection PhpUnhandledExceptionInspection */
        $writer->save($settings);
        $this->assertEquals(filesize($config), filesize("$config.bak.php"));
    }

    public function testTouch() {
        global $config_cascade;
        $config = end($config_cascade['main']['local']);
        $writer = new Writer();

        $old = filemtime($config);
        $this->waitForTick(true);
        /** @noinspection PhpUnhandledExceptionInspection */
        $writer->touch();
        clearstatcache($config);
        $this->assertGreaterThan($old, filemtime($config));
    }

    public function testEmpty() {
        $writer = new Writer();
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('empty config');
        $writer->save([]);
    }
}