1<?php
2
3use ComboStrap\TplUtility;
4use dokuwiki\plugin\config\core\ConfigParser;
5use dokuwiki\plugin\config\core\Loader;
6
7require_once(__DIR__ . '/../class/TplUtility.php');
8
9
10/**
11 * Test the settings.php file
12 *
13 * @group template_strap
14 * @group templates
15 */
16class configTest extends DokuWikiTest
17{
18
19    const CONF_WITHOUT_DEFAULT = [TplUtility::CONF_FOOTER_SLOT_PAGE_NAME,
20        TplUtility::CONF_HEADER_SLOT_PAGE_NAME,
21        TplUtility::CONF_SIDEKICK_SLOT_PAGE_NAME,
22        TplUtility::CONF_REM_SIZE
23        ];
24
25    public function setUp()
26    {
27
28        global $conf;
29        parent::setUp();
30        $conf['template'] = 'strap';
31        /**
32         * static variable bug in the {@link tpl_getConf()}
33         * that does not load the configuration twice
34         */
35        TplUtility::reloadConf();
36
37
38    }
39
40
41    /**
42     *
43     * Test if we don't have any problem
44     * in the file settings.php
45     *
46     * If there is, we got an error in the admin config page
47     */
48    public function test_base()
49    {
50
51        $request = new TestRequest();
52        global $conf;
53        $conf['useacl'] = 1;
54        $user = 'admin';
55        $conf['superuser'] = $user;
56
57        // $_SERVER[] = $user;
58        $request->setServer('REMOTE_USER', $user);
59
60        $response = $request->get(array('do' => 'admin', 'page' => "config"), '/doku.php');
61
62        // Simple
63        /**
64         * The conf identifier used as id in the pae configuration
65         * and in array
66         */
67        $htmlId = "tpl____" . TplUtility::TEMPLATE_NAME . "____tpl_settings_name";
68        $countListContainer = $response->queryHTML("#" . $htmlId)->count();
69        $this->assertEquals(1, $countListContainer, "There should an element");
70
71    }
72
73    /**
74     * Test to ensure that every conf['...'] entry
75     * in conf/default.php has a corresponding meta['...'] entry in conf/metadata.php.
76     */
77    public function test_plugin_default()
78    {
79        $conf = array();
80        $conf_file = __DIR__ . '/../conf/default.php';
81        if (file_exists($conf_file)) {
82            include($conf_file);
83        }
84
85        $meta = array();
86        $meta_file = __DIR__ . '/../conf/metadata.php';
87        if (file_exists($meta_file)) {
88            include($meta_file);
89        }
90
91
92        $this->assertEquals(
93            gettype($conf),
94            gettype($meta),
95            'Both ' . DOKU_PLUGIN . 'syntax/conf/default.php and ' . DOKU_PLUGIN . 'syntax/conf/metadata.php have to exist and contain the same keys.'
96        );
97
98        if (gettype($conf) != 'NULL' && gettype($meta) != 'NULL') {
99            foreach ($conf as $key => $value) {
100                $this->assertArrayHasKey(
101                    $key,
102                    $meta,
103                    'Key $meta[\'' . $key . '\'] missing in ' . DOKU_PLUGIN . 'syntax/conf/metadata.php'
104                );
105            }
106
107            foreach ($meta as $key => $value) {
108
109                /**
110                 * Known configuration without default
111                 */
112                if (in_array($key,
113                    self::CONF_WITHOUT_DEFAULT
114                )) {
115                    continue;
116                }
117                $this->assertArrayHasKey(
118                    $key,
119                    $conf,
120                    'Key $conf[\'' . $key . '\'] missing in ' . DOKU_PLUGIN . 'syntax/conf/default.php'
121                );
122            }
123        }
124
125        /**
126         * English language
127         */
128        $lang = array();
129        $settings_file = __DIR__ . '/../lang/en/settings.php';
130        if (file_exists($settings_file)) {
131            /** @noinspection PhpIncludeInspection */
132            include($settings_file);
133        }
134
135
136        $this->assertEquals(
137            gettype($conf),
138            gettype($lang),
139            'Both ' . DOKU_PLUGIN . 'syntax/conf/metadata.php and ' . DOKU_PLUGIN . 'syntax/lang/en/settings.php have to exist and contain the same keys.'
140        );
141
142        if (gettype($conf) != 'NULL' && gettype($lang) != 'NULL') {
143            foreach ($conf as $key => $value) {
144                $this->assertArrayHasKey(
145                    $key,
146                    $conf,
147                    'Key $meta[\'' . $key . '\'] missing in ' . DOKU_PLUGIN . 'syntax/conf/metadata.php'
148                );
149            }
150
151            foreach ($lang as $key => $value) {
152                $this->assertArrayHasKey(
153                    $key,
154                    $lang,
155                    'Key $lang[\'' . $key . '\'] missing in ' . DOKU_PLUGIN . 'syntax/lang/en/settings.php'
156                );
157            }
158        }
159
160
161        /**
162         * The default value are read through parsing
163         * by the config plugin.
164         * You can't use variable in them.
165         *
166         * Yes that's fuck up but yeah
167         * This test check that we can read them
168         */
169        $parser = new ConfigParser();
170        $loader = new Loader($parser);
171        $defaultConf = $loader->loadDefaults();
172        $keyPrefix = 'tpl____strap____';
173        $this->assertTrue(is_array($defaultConf));
174
175        // plugin defaults
176        foreach ($meta as $key => $value) {
177            /**
178             * Known configuration without default
179             */
180            if (in_array($key,
181                self::CONF_WITHOUT_DEFAULT
182            )) {
183                continue;
184            }
185            $this->assertArrayHasKey(
186                $keyPrefix . $key,
187                $defaultConf,
188                'Key $conf[\'' . $key . '\'] could not be parsed in ' . DOKU_PLUGIN . 'syntax/conf/default.php. Be sure to give only values and not variable.'
189            );
190        }
191
192
193    }
194
195
196}
197