Use the traditional "template" config key, update unit-tests

This commit is contained in:
Ribas160 2025-02-10 18:19:36 +02:00
parent bce449de10
commit 5b54f2cdb0
5 changed files with 28 additions and 12 deletions

View File

@ -53,7 +53,7 @@ templateselection = false
; which can be combined with "-dark" and "-compact" for "bootstrap-dark-page", ; which can be combined with "-dark" and "-compact" for "bootstrap-dark-page",
; "bootstrap-compact-page" and finally "bootstrap5" (tpl/bootstrap5.php) - previews at: ; "bootstrap-compact-page" and finally "bootstrap5" (tpl/bootstrap5.php) - previews at:
; https://privatebin.info/screenshots.html ; https://privatebin.info/screenshots.html
; templatedefault = "bootstrap" ; template = "bootstrap"
; (optional) info text to display ; (optional) info text to display
; use single, instead of double quotes for HTML attributes ; use single, instead of double quotes for HTML attributes

View File

@ -46,7 +46,7 @@ class Configuration
'syntaxhighlightingtheme' => '', 'syntaxhighlightingtheme' => '',
'sizelimit' => 10485760, 'sizelimit' => 10485760,
'templateselection' => false, 'templateselection' => false,
'templatedefault' => '', 'template' => 'bootstrap',
'info' => 'More information on the <a href=\'https://privatebin.info/\'>project page</a>.', 'info' => 'More information on the <a href=\'https://privatebin.info/\'>project page</a>.',
'notice' => '', 'notice' => '',
'languageselection' => false, 'languageselection' => false,
@ -79,13 +79,13 @@ class Configuration
'markdown' => 'Markdown', 'markdown' => 'Markdown',
), ),
'available_templates' => array( 'available_templates' => array(
'bootstrap5',
'bootstrap', 'bootstrap',
'bootstrap-page', 'bootstrap-page',
'bootstrap-dark', 'bootstrap-dark',
'bootstrap-dark-page', 'bootstrap-dark-page',
'bootstrap-compact', 'bootstrap-compact',
'bootstrap-compact-page', 'bootstrap-compact-page',
'bootstrap5',
'page', 'page',
), ),
'traffic' => array( 'traffic' => array(

View File

@ -213,9 +213,10 @@ class Controller
$this->_conf = new Configuration; $this->_conf = new Configuration;
$templates = $this->_conf->getSection('available_templates'); $templates = $this->_conf->getSection('available_templates');
$template = $this->_conf->getKey('templatedefault'); $template = $this->_conf->getKey('template');
TemplateSwitcher::setAvailableTemplates($templates); TemplateSwitcher::setAvailableTemplates($templates);
TemplateSwitcher::setTemplateFallback($template); TemplateSwitcher::setTemplateFallback($template);
// force default template, if template selection is disabled and a default is set // force default template, if template selection is disabled and a default is set
if (!$this->_conf->getKey('templateselection') && !empty($template)) { if (!$this->_conf->getKey('templateselection') && !empty($template)) {
$_COOKIE['template'] = $template; $_COOKIE['template'] = $template;

View File

@ -26,7 +26,7 @@ class TemplateSwitcher {
* @static * @static
* @var string * @var string
*/ */
protected static $_templateFallback = "bootstrap"; protected static $_templateFallback;
/** /**

View File

@ -1,6 +1,7 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use PrivateBin\Configuration;
use PrivateBin\TemplateSwitcher; use PrivateBin\TemplateSwitcher;
@ -9,17 +10,27 @@ class TemplateSwitcherTest extends TestCase
public function testSetTemplateFallback() public function testSetTemplateFallback()
{ {
$defaultTemplateFallback = "bootstrap"; $conf = new Configuration;
$customTemplateFallback = "bootstrap-dark";
$existingTemplateFallback = "bootstrap-dark";
$wrongTemplateFallback = "bootstrap-wrong"; $wrongTemplateFallback = "bootstrap-wrong";
$this->assertEquals($defaultTemplateFallback, TemplateSwitcher::getTemplate(), "Default template fallback"); TemplateSwitcher::setAvailableTemplates($conf->getSection('available_templates'));
TemplateSwitcher::setTemplateFallback($existingTemplateFallback);
$this->assertEquals($existingTemplateFallback, TemplateSwitcher::getTemplate(), "Correct template fallback");
TemplateSwitcher::setTemplateFallback($wrongTemplateFallback); TemplateSwitcher::setTemplateFallback($wrongTemplateFallback);
$this->assertEquals($defaultTemplateFallback, TemplateSwitcher::getTemplate(), "Wrong template fallback"); $this->assertEquals($existingTemplateFallback, TemplateSwitcher::getTemplate(), "Wrong template fallback");
}
TemplateSwitcher::setTemplateFallback($customTemplateFallback);
$this->assertEquals($customTemplateFallback, TemplateSwitcher::getTemplate(), "Custom template fallback"); public function testSetAvailableTemplates()
{
$conf = new Configuration;
$availableTemplates = $conf->getSection('available_templates');
TemplateSwitcher::setAvailableTemplates($availableTemplates);
$this->assertEquals($availableTemplates, TemplateSwitcher::getAvailableTemplates(), "Set available templates");
} }
@ -41,15 +52,19 @@ class TemplateSwitcherTest extends TestCase
public function testGetAvailableTemplates() public function testGetAvailableTemplates()
{ {
$this->assertNotEmpty(TemplateSwitcher::getAvailableTemplates(), "Available templates"); $this->assertNotEmpty(TemplateSwitcher::getAvailableTemplates(), "Get available templates");
} }
public function testIsTemplateAvailable() public function testIsTemplateAvailable()
{ {
$conf = new Configuration;
$existingTemplate = "bootstrap"; $existingTemplate = "bootstrap";
$nonExistentTemplate = "bootstrap-wrong"; $nonExistentTemplate = "bootstrap-wrong";
TemplateSwitcher::setAvailableTemplates($conf->getSection('available_templates'));
$this->assertTrue(TemplateSwitcher::isTemplateAvailable($existingTemplate), "Existing template"); $this->assertTrue(TemplateSwitcher::isTemplateAvailable($existingTemplate), "Existing template");
$this->assertFalse(TemplateSwitcher::isTemplateAvailable($nonExistentTemplate), "Non-existent template"); $this->assertFalse(TemplateSwitcher::isTemplateAvailable($nonExistentTemplate), "Non-existent template");
} }