1<?php
2
3use ComboStrap\TplUtility;
4use dokuwiki\plugin\config\core\Configuration;
5
6require_once(__DIR__ . '/../class/TplUtility.php');
7
8/**
9 *
10 *
11 * @group template_strap
12 * @group templates
13 */
14class tplUtilityTest extends DokuWikiTest
15{
16
17    public function setUp()
18    {
19
20        global $conf;
21        parent::setUp();
22        $conf ['template'] = 'strap';
23
24        /**
25         * static variable bug in the {@link tpl_getConf()}
26         * that does not load the configuration twice
27         */
28        TplUtility::reloadConf();
29
30    }
31
32    /**
33     * Test the {@link \Combostrap\TplUtility::getStylesheetsForMetadataConfiguration()} function
34     */
35    public function testGetStylesheetsForMetadataConfiguration()
36    {
37
38        // Local file created by the users with their own stylesheet
39        $destination = __DIR__ . '/../bootstrap/bootstrapLocal.json';
40        // If we debug, it may not be deleted
41        if (file_exists($destination)) {
42            unlink($destination);
43        }
44
45        // Default
46        $configurationList = TplUtility::getStylesheetsForMetadataConfiguration();
47        $distributionStylesheet = 51;
48        $this->assertEquals($distributionStylesheet, sizeof($configurationList), "Number of stylesheet");
49
50
51        copy(__DIR__ . '/resources/bootstrapLocal.json', $destination);
52        $configurationList = TplUtility::getStylesheetsForMetadataConfiguration();
53        $styleSheetWithCustom = $distributionStylesheet + 1;
54        $this->assertEquals($styleSheetWithCustom, sizeof($configurationList), "There is one stylesheet more");
55        unlink($destination);
56
57
58    }
59
60    public function testGetStyleSheetAndBootstrapVersionConf()
61    {
62        $stylesheet = "bootstrap.16col";
63        $boostrapVersion = "4.5.0";
64        TplUtility::setConf(TplUtility::CONF_BOOTSTRAP_VERSION_STYLESHEET, $boostrapVersion . TplUtility::BOOTSTRAP_VERSION_STYLESHEET_SEPARATOR . $stylesheet);
65        $actualStyleSheet = TplUtility::getStyleSheetConf();
66        $this->assertEquals($stylesheet, $actualStyleSheet);
67        $actualBootStrapVersion = TplUtility::getBootStrapVersion();
68        $this->assertEquals($boostrapVersion, $actualBootStrapVersion);
69    }
70
71
72    /**
73     * Test the {@link \Combostrap\TplUtility::buildBootstrapMetas()} function
74     * that returns the needed bootstrap resources
75     * @throws Exception
76     */
77    public function test_buildBootstrapMetas()
78    {
79        $boostrapVersion = "4.5.0";
80        $metas = TplUtility::buildBootstrapMetas($boostrapVersion);
81        $this->assertEquals(4, sizeof($metas));
82        $this->assertEquals("bootstrap.min.css", $metas["css"]["file"]);
83
84        TplUtility::setConf(TplUtility::CONF_BOOTSTRAP_VERSION_STYLESHEET, $boostrapVersion . TplUtility::BOOTSTRAP_VERSION_STYLESHEET_SEPARATOR . "16col");
85        $metas = TplUtility::buildBootstrapMetas($boostrapVersion);
86        $this->assertEquals(4, sizeof($metas));
87        $this->assertEquals("bootstrap.16col.min.css", $metas["css"]["file"]);
88
89        TplUtility::setConf(TplUtility::CONF_BOOTSTRAP_VERSION_STYLESHEET, $boostrapVersion . TplUtility::BOOTSTRAP_VERSION_STYLESHEET_SEPARATOR . "simplex");
90        $metas = TplUtility::buildBootstrapMetas($boostrapVersion);
91        $this->assertEquals(4, sizeof($metas));
92        $this->assertEquals("bootstrap.simplex.min.css", $metas["css"]["file"]);
93        $this->assertEquals("https://cdn.jsdelivr.net/npm/bootswatch@4.5.0/dist/simplex/bootstrap.min.css", $metas["css"]["url"]);
94
95    }
96
97    /**
98     * Rtl supports
99     */
100    public function test_buildBootstrapMetasWithRtl()
101    {
102        global $lang;
103        $lang["direction"] = "rtl";
104
105        $boostrapVersion = "5.0.1";
106        $metas = TplUtility::buildBootstrapMetas($boostrapVersion);
107        $this->assertEquals(3, sizeof($metas));
108        $this->assertEquals("bootstrap.rtl.min.css", $metas["css"]["file"]);
109
110
111        TplUtility::setConf(TplUtility::CONF_BOOTSTRAP_VERSION_STYLESHEET, $boostrapVersion . TplUtility::BOOTSTRAP_VERSION_STYLESHEET_SEPARATOR . "simplex");
112        $metas = TplUtility::buildBootstrapMetas($boostrapVersion);
113        $this->assertEquals(3, sizeof($metas));
114        $this->assertEquals("bootstrap.simplex.min.css", $metas["css"]["file"]);
115        $this->assertEquals("https://cdn.jsdelivr.net/npm/bootswatch@5.0.1/dist/simplex/bootstrap.min.css", $metas["css"]["url"]);
116
117    }
118
119
120    /**
121     * Testing the {@link TplUtility::renderSlot()}
122     */
123    public function testBarCache()
124    {
125
126        $sidebarName = "sidebar";
127        $sidebarId = ":" . $sidebarName;
128        saveWikiText($sidebarId, "=== title ===", "");
129        $metadata = p_read_metadata($sidebarId);
130        p_save_metadata($sidebarName, $metadata);
131        global $ID;
132        $ID = ":namespace:whatever";
133        $data = TplUtility::renderSlot($sidebarName);
134        $this->assertNotEmpty($data);
135        /**
136         * TODO:  We should test that the file are not the same with bar plugin that shows the files of a namespace
137         * The test was done manually
138         */
139
140    }
141
142    /**
143     * Test that a wiki with an old header configuration
144     * is saved to the old value
145     *
146     * The functionality scan for children page
147     * with the same name and if found set the new configuration
148     * when we try to get the value
149     */
150    public function testUpdateConfigurationWithOldValue()
151    {
152
153        /**
154         * A switch to update the configuration
155         * (Not done normally due to the hard coded constant DOKU_DATA. See more at {@link TplUtility::updateConfiguration()}
156         */
157        global $_REQUEST;
158        $_REQUEST[TplUtility::COMBO_TEST_UPDATE] = true;
159
160        /**
161         * Creating a page in a children directory
162         * with the old configuration
163         */
164        $oldConf = TplUtility::CONF_HEADER_OLD;
165        $expectedValue = TplUtility::CONF_HEADER_OLD_VALUE;
166        saveWikiText("ns:" . $oldConf, "Header page with the old", 'Script Test base');
167
168        $strapName = "strap";
169        $strapKey = TplUtility::CONF_HEADER_SLOT_PAGE_NAME;
170
171        $value = TplUtility::getHeaderSlotPageName();
172        $this->assertEquals($expectedValue, $value);
173
174        $configuration = new Configuration();
175        $settings = $configuration->getSettings();
176        $key = "tpl____${strapName}____" . $strapKey;
177
178        $setting = $settings[$key];
179        $this->assertEquals(true, isset($setting));
180
181        $formsOutput = $setting->out("conf");
182        $formsOutputExpected = <<<EOF
183\$conf['tpl']['$strapName']['$strapKey'] = '$expectedValue';
184
185EOF;
186
187        $this->assertEquals($formsOutputExpected, $formsOutput);
188
189
190        global $config_cascade;
191        $config = end($config_cascade['main']['local']);
192        $conf = [];
193        /** @noinspection PhpIncludeInspection */
194        include $config;
195        $this->assertEquals($expectedValue, $conf["tpl"]["strap"][$strapKey], "Good value in config");
196
197        /**
198         * The conf has been messed up
199         * See {@link TplUtility::updateConfiguration()} for information
200         */
201        unset($_REQUEST[TplUtility::COMBO_TEST_UPDATE]);
202        self::setUpBeforeClass();
203
204    }
205
206    public function testUpdateConfigurationForANewInstallation()
207    {
208
209        /**
210         * A switch to update the configuration
211         * (Not done normally due to the hard coded constant DOKU_DATA. See more at {@link TplUtility::updateConfiguration()}
212         */
213        global $_REQUEST;
214        $_REQUEST[TplUtility::COMBO_TEST_UPDATE] = true;
215
216        $expectedValue = "slot_header";
217        $strapName = "strap";
218        $strapKey = TplUtility::CONF_HEADER_SLOT_PAGE_NAME;
219
220        $value = TplUtility::getHeaderSlotPageName();
221        $this->assertEquals($expectedValue, $value);
222
223        $configuration = new Configuration();
224        $settings = $configuration->getSettings();
225        $key = "tpl____${strapName}____" . $strapKey;
226
227        $setting = $settings[$key];
228        $this->assertEquals(true, isset($setting));
229
230        $formsOutput = $setting->out("conf");
231        $formsOutputExpected = <<<EOF
232\$conf['tpl']['$strapName']['$strapKey'] = '$expectedValue';
233
234EOF;
235
236        $this->assertEquals($formsOutputExpected, $formsOutput);
237
238        global $config_cascade;
239        $config = end($config_cascade['main']['local']);
240        $conf = [];
241        /** @noinspection PhpIncludeInspection */
242        include $config;
243        $this->assertEquals($expectedValue, $conf["tpl"]["strap"][$strapKey], "Good value in config");
244
245        /**
246         * The conf has been messed up
247         * See {@link TplUtility::updateConfiguration()} for information
248         */
249        unset($_REQUEST[TplUtility::COMBO_TEST_UPDATE]);
250        self::setUpBeforeClass();
251
252    }
253
254
255}
256