Skip to content

Commit

Permalink
refactor: transform installer helper utils into class.
Browse files Browse the repository at this point in the history
  • Loading branch information
edmondas committed Dec 2, 2023
1 parent 3455ba2 commit 9427fdb
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 34 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
},
"autoload": {
"psr-4": {
"Poweradmin\\": "lib/"
"Poweradmin\\": "lib/",
"PoweradminInstall\\": "install/helpers"
}
},
"require-dev": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,50 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace PoweradminInstall;

use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Component\Translation\Loader\PoFileLoader;
use Symfony\Component\Translation\Translator;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

function initializeTwigEnvironment($language): Environment
class TemplateUtils
{
$loader = new FilesystemLoader('templates');
$twig = new Environment($loader);

$translator = new Translator($language);
$translator->addLoader('po', new PoFileLoader());
$translator->addResource('po', getLocaleFile($language), $language);
public static function initializeTwigEnvironment($language): Environment
{
$loader = new FilesystemLoader('templates');
$twig = new Environment($loader);

$twig->addExtension(new TranslationExtension($translator));
$translator = new Translator($language);
$translator->addLoader('po', new PoFileLoader());
$translator->addResource('po', getLocaleFile($language), $language);

return $twig;
}
$twig->addExtension(new TranslationExtension($translator));

function getCurrentStep(): int
{
if (isset($_POST['step']) && is_numeric($_POST['step'])) {
return $_POST['step'];
return $twig;
}

return 1;
}
public static function getCurrentStep(): int
{
if (isset($_POST['step']) && is_numeric($_POST['step'])) {
return $_POST['step'];
}

function renderHeader($twig, $current_step): void
{
echo $twig->render('header.html', array(
'current_step' => htmlspecialchars($current_step),
'file_version' => time()
));
}
return 1;
}

function renderFooter($twig): void
{
echo $twig->render('footer.html');
}
public static function renderHeader($twig, $current_step): void
{
echo $twig->render('header.html', array(
'current_step' => htmlspecialchars($current_step),
'file_version' => time()
));
}

public static function renderFooter($twig): void
{
echo $twig->render('footer.html');
}
}
11 changes: 6 additions & 5 deletions install/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

use PoweradminInstall\TemplateUtils;

require dirname(__DIR__) . '/vendor/autoload.php';

require_once 'helpers/locale_handler.php';
require_once 'helpers/database_structure.php';
require_once 'helpers/template_utils.php';
require_once 'helpers/install_helpers.php';

$local_config_file = dirname(__DIR__) . '/inc/config.inc.php';
Expand All @@ -34,9 +35,9 @@
$language = getLanguageFromRequest();
setLanguage($language);

$twig = initializeTwigEnvironment($language);
$current_step = getCurrentStep();
renderHeader($twig, $current_step);
$twig = TemplateUtils::initializeTwigEnvironment($language);
$current_step = TemplateUtils::getCurrentStep();
TemplateUtils::renderHeader($twig, $current_step);
checkConfigFile($current_step, $local_config_file, $twig);

switch ($current_step) {
Expand Down Expand Up @@ -73,4 +74,4 @@
break;
}

renderFooter($twig);
TemplateUtils::renderFooter($twig);
85 changes: 85 additions & 0 deletions tests/TemplateUtilsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

use PHPUnit\Framework\TestCase;
use PoweradminInstall\TemplateUtils;

class TemplateUtilsTest extends TestCase
{

public function testCanGetStepFromPostRequest(): void
{
$_POST['step'] = 3;
$this->assertEquals(3, TemplateUtils::getCurrentStep());

unset($_POST['step']);
}

public function testCanReturnDefaultStepWhenNotInPostRequest(): void
{
$this->assertEquals(1, TemplateUtils::getCurrentStep());
}

public function testCanHandleNonNumericStepInPostRequest(): void
{
$_POST['step'] = 'non-numeric';
$this->assertEquals(1, TemplateUtils::getCurrentStep());

unset($_POST['step']);
}

public function testGetCurrentStepWithVeryLargeNumber()
{
$_POST['step'] = '999999999999999999999999'; // An extremely large number
$result = TemplateUtils::getCurrentStep();
$this->assertEquals(999999999999999999999999, $result);
}

public function testGetCurrentStepWithNegativeNumber()
{
$_POST['step'] = '-5'; // A negative number
$result = TemplateUtils::getCurrentStep();
$this->assertEquals(-5, $result);
}

public function testGetCurrentStepWithZero()
{
$_POST['step'] = '0';
$result = TemplateUtils::getCurrentStep();
$this->assertEquals(0, $result);
}

public function testGetCurrentStepWithFloat()
{
$_POST['step'] = '3.5';
$result = TemplateUtils::getCurrentStep();
$this->assertEquals(3.5, $result);
}

public function testGetCurrentStepWithStringNumber()
{
$_POST['step'] = '5';
$result = TemplateUtils::getCurrentStep();
$this->assertEquals(5, $result);
}

public function testGetCurrentStepWithNonAsciiNumbers()
{
$_POST['step'] = '٣';
$result = TemplateUtils::getCurrentStep();
$this->assertEquals(1, $result);
}

public function testGetCurrentStepWithInjection()
{
$_POST['step'] = '<script>alert("test")</script>';
$result = TemplateUtils::getCurrentStep();
$this->assertEquals(1, $result);
}

public function testGetCurrentStepWithArray()
{
$_POST['step'] = ['1', '2'];
$result = TemplateUtils::getCurrentStep();
$this->assertEquals(1, $result);
}
}
1 change: 1 addition & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'Attribute' => $vendorDir . '/symfony/polyfill-php80/Resources/stubs/Attribute.php',
'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php',
'PhpToken' => $vendorDir . '/symfony/polyfill-php80/Resources/stubs/PhpToken.php',
'PoweradminInstall\\TemplateUtils' => $baseDir . '/install/helpers/TemplateUtils.php',
'Poweradmin\\AppFactory' => $baseDir . '/lib/AppFactory.php',
'Poweradmin\\Application' => $baseDir . '/lib/Application.php',
'Poweradmin\\Application\\Dnssec\\DnssecProviderFactory' => $baseDir . '/lib/Application/Dnssec/DnssecProviderFactory.php',
Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_psr4.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
'Symfony\\Component\\Translation\\' => array($vendorDir . '/symfony/translation'),
'Symfony\\Bridge\\Twig\\' => array($vendorDir . '/symfony/twig-bridge'),
'Poweradmin\\' => array($baseDir . '/lib'),
'PoweradminInstall\\' => array($baseDir . '/install/helpers'),
);
6 changes: 6 additions & 0 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ComposerStaticInit02fd231869a48df50cb631d6580008da
'P' =>
array (
'Poweradmin\\' => 11,
'PoweradminInstall\\' => 18,
),
);

Expand Down Expand Up @@ -75,12 +76,17 @@ class ComposerStaticInit02fd231869a48df50cb631d6580008da
array (
0 => __DIR__ . '/../..' . '/lib',
),
'PoweradminInstall\\' =>
array (
0 => __DIR__ . '/../..' . '/install/helpers',
),
);

public static $classMap = array (
'Attribute' => __DIR__ . '/..' . '/symfony/polyfill-php80/Resources/stubs/Attribute.php',
'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php',
'PhpToken' => __DIR__ . '/..' . '/symfony/polyfill-php80/Resources/stubs/PhpToken.php',
'PoweradminInstall\\TemplateUtils' => __DIR__ . '/../..' . '/install/helpers/TemplateUtils.php',
'Poweradmin\\AppFactory' => __DIR__ . '/../..' . '/lib/AppFactory.php',
'Poweradmin\\Application' => __DIR__ . '/../..' . '/lib/Application.php',
'Poweradmin\\Application\\Dnssec\\DnssecProviderFactory' => __DIR__ . '/../..' . '/lib/Application/Dnssec/DnssecProviderFactory.php',
Expand Down
4 changes: 2 additions & 2 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'name' => 'poweradmin/poweradmin',
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'a0694cfd41c765c0e910ae666b382a06ba2fdda3',
'reference' => '3455ba2a4340faf3b2675dfac0ba1e9de69e92df',
'type' => 'project',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand All @@ -13,7 +13,7 @@
'poweradmin/poweradmin' => array(
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'a0694cfd41c765c0e910ae666b382a06ba2fdda3',
'reference' => '3455ba2a4340faf3b2675dfac0ba1e9de69e92df',
'type' => 'project',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
Expand Down

0 comments on commit 9427fdb

Please sign in to comment.