xref: /dokuwiki/lib/plugins/config/_test/WriterTest.php (revision 0772dde297e1ba8cd913370fcdf9a3f959574ee0)
198a151baSAndreas Gohr<?php
298a151baSAndreas Gohr
398a151baSAndreas Gohrnamespace dokuwiki\plugin\config\test;
498a151baSAndreas Gohruse dokuwiki\plugin\config\core\Setting\SettingString;
598a151baSAndreas Gohruse dokuwiki\plugin\config\core\Writer;
698a151baSAndreas Gohr
798a151baSAndreas Gohr/**
898a151baSAndreas Gohr * @group plugin_config
998a151baSAndreas Gohr * @group admin_plugins
1098a151baSAndreas Gohr * @group plugins
1198a151baSAndreas Gohr * @group bundled_plugins
1298a151baSAndreas Gohr */
1398a151baSAndreas Gohrclass WriterTest extends \DokuWikiTest {
1498a151baSAndreas Gohr
1598a151baSAndreas Gohr    public function testSave() {
1698a151baSAndreas Gohr        global $config_cascade;
1798a151baSAndreas Gohr        $config = end($config_cascade['main']['local']);
1898a151baSAndreas Gohr
1998a151baSAndreas Gohr        $set1 = new SettingString('test1');
2098a151baSAndreas Gohr        $set1->initialize('foo','bar', null);
2198a151baSAndreas Gohr        $set2 = new SettingString('test2');
2298a151baSAndreas Gohr        $set2->initialize('foo','foo', null);
2398a151baSAndreas Gohr        $settings = [$set1, $set2];
2498a151baSAndreas Gohr        $writer = new Writer();
2598a151baSAndreas Gohr
2698a151baSAndreas Gohr        // before running, no backup should exist
2798a151baSAndreas Gohr        $this->assertFileExists($config);
2849bcbaeeSAndreas Gohr        $this->assertFileNotExists("$config.bak.php");
2998a151baSAndreas Gohr        $old = filesize($config);
3098a151baSAndreas Gohr
3198a151baSAndreas Gohr        /** @noinspection PhpUnhandledExceptionInspection */
3298a151baSAndreas Gohr        $writer->save($settings);
3398a151baSAndreas Gohr
3498a151baSAndreas Gohr        // after running, both should exist
3598a151baSAndreas Gohr        $this->assertFileExists($config);
3649bcbaeeSAndreas Gohr        $this->assertFileExists("$config.bak.php");
3749bcbaeeSAndreas Gohr        $this->assertEquals($old, filesize("$config.bak.php"), 'backup should have size of old file');
3898a151baSAndreas Gohr
3998a151baSAndreas Gohr        // check contents
4098a151baSAndreas Gohr        $conf = [];
4198a151baSAndreas Gohr        include $config;
4298a151baSAndreas Gohr        $this->assertArrayHasKey('test1', $conf);
4398a151baSAndreas Gohr        $this->assertEquals('bar', $conf['test1']);
4498a151baSAndreas Gohr        $this->assertArrayNotHasKey('test2', $conf);
4598a151baSAndreas Gohr
4698a151baSAndreas Gohr        /** @noinspection PhpUnhandledExceptionInspection */
4798a151baSAndreas Gohr        $writer->save($settings);
4849bcbaeeSAndreas Gohr        $this->assertEquals(filesize($config), filesize("$config.bak.php"));
4998a151baSAndreas Gohr    }
5098a151baSAndreas Gohr
5198a151baSAndreas Gohr    public function testTouch() {
5298a151baSAndreas Gohr        global $config_cascade;
5398a151baSAndreas Gohr        $config = end($config_cascade['main']['local']);
5498a151baSAndreas Gohr        $writer = new Writer();
5598a151baSAndreas Gohr
5698a151baSAndreas Gohr        $old = filemtime($config);
5798a151baSAndreas Gohr        $this->waitForTick(true);
5898a151baSAndreas Gohr        /** @noinspection PhpUnhandledExceptionInspection */
5998a151baSAndreas Gohr        $writer->touch();
6098a151baSAndreas Gohr        clearstatcache($config);
6198a151baSAndreas Gohr        $this->assertGreaterThan($old, filemtime($config));
6298a151baSAndreas Gohr    }
63*0772dde2SAndreas Gohr
64*0772dde2SAndreas Gohr    public function testEmpty() {
65*0772dde2SAndreas Gohr        $writer = new Writer();
66*0772dde2SAndreas Gohr        $this->expectException(\Exception::class);
67*0772dde2SAndreas Gohr        $this->expectErrorMessage('empty config');
68*0772dde2SAndreas Gohr        $writer->save([]);
69*0772dde2SAndreas Gohr    }
7098a151baSAndreas Gohr}
71