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(): void 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 // Perf issue with configuration update 151// public function testUpdateConfigurationWithOldValue() 152// { 153// 154// /** 155// * A switch to update the configuration 156// * (Not done normally due to the hard coded constant DOKU_DATA. See more at {@link TplUtility::updateConfiguration()} 157// */ 158// global $_REQUEST; 159// $_REQUEST[TplUtility::COMBO_TEST_UPDATE] = true; 160// 161// /** 162// * Creating a page in a children directory 163// * with the old configuration 164// */ 165// $oldConf = TplUtility::CONF_HEADER_OLD; 166// $expectedValue = TplUtility::CONF_HEADER_OLD_VALUE; 167// saveWikiText("ns:" . $oldConf, "Header page with the old", 'Script Test base'); 168// 169// $strapName = "strap"; 170// $strapKey = TplUtility::CONF_HEADER_SLOT_PAGE_NAME; 171// 172// $value = TplUtility::getHeaderSlotPageName(); 173// $this->assertEquals($expectedValue, $value); 174// 175// $configuration = new Configuration(); 176// $settings = $configuration->getSettings(); 177// $key = "tpl____${strapName}____" . $strapKey; 178// 179// $setting = $settings[$key]; 180// $this->assertEquals(true, isset($setting)); 181// 182// $formsOutput = $setting->out("conf"); 183// $formsOutputExpected = <<<EOF 184//\$conf['tpl']['$strapName']['$strapKey'] = '$expectedValue'; 185// 186//EOF; 187// 188// $this->assertEquals($formsOutputExpected, $formsOutput); 189// 190// 191// global $config_cascade; 192// $config = end($config_cascade['main']['local']); 193// $conf = []; 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 // Perf issue with configuration update 207// public function testUpdateConfigurationForANewInstallation() 208// { 209// 210// /** 211// * A switch to update the configuration 212// * (Not done normally due to the hard coded constant DOKU_DATA. See more at {@link TplUtility::updateConfiguration()} 213// */ 214// global $_REQUEST; 215// $_REQUEST[TplUtility::COMBO_TEST_UPDATE] = true; 216// 217// $expectedValue = "slot_header"; 218// $strapName = "strap"; 219// $strapKey = TplUtility::CONF_HEADER_SLOT_PAGE_NAME; 220// 221// $value = TplUtility::getHeaderSlotPageName(); 222// $this->assertEquals($expectedValue, $value); 223// 224// $configuration = new Configuration(); 225// $settings = $configuration->getSettings(); 226// $key = "tpl____${strapName}____" . $strapKey; 227// 228// $setting = $settings[$key]; 229// $this->assertEquals(true, isset($setting)); 230// 231// $formsOutput = $setting->out("conf"); 232// $formsOutputExpected = <<<EOF 233//\$conf['tpl']['$strapName']['$strapKey'] = '$expectedValue'; 234// 235//EOF; 236// 237// $this->assertEquals($formsOutputExpected, $formsOutput); 238// 239// global $config_cascade; 240// $config = end($config_cascade['main']['local']); 241// $conf = []; 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