Skip to content

Commit

Permalink
Add method to ArrayHelper, add TestHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
derUli committed Apr 5, 2020
1 parent 5e6591d commit dba8bee
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 25 deletions.
Binary file modified doc-src/Neue Features in UliCMS 2020.2.odt
Binary file not shown.
Binary file modified doc/Neue Features in UliCMS 2020.2.pdf
Binary file not shown.
1 change: 1 addition & 0 deletions doc/changelog.txt
Expand Up @@ -17,6 +17,7 @@ Neue API-Methoden:
* User->getGroupCollection(): GroupCollection
* User->isOnline(): bool
* User->isCurrent(): bool
* ArrayHelper::getValueOrDefault(?array $input, $key, $default)

Entfernte API-Methoden:
* BackendHelper::formatDatasetCount(int $count): void
Expand Down
4 changes: 2 additions & 2 deletions ulicms/admin/inc/registerform.php
@@ -1,5 +1,5 @@
<?php
if (Settings::get("visitors_can_register") == "off" or!Settings::get("visitors_can_register")) {
if (Settings::get("visitors_can_register") == "off" or !Settings::get("visitors_can_register")) {
translate("FUNCTION_IS_DISABLED");
} else {
// TODO: Move logic to RegistrationController
Expand All @@ -8,7 +8,7 @@

$errors = false;
if (isset($_POST["register_user"])) {
if ($checkbox->isEnabled() and!$checkbox->isChecked()) {
if ($checkbox->isEnabled() and !$checkbox->isChecked()) {
echo "<p style='color:red;'>" . get_translation("please_accept_privacy_conditions") . "</p>";
} else if (empty($_POST["username"]) or empty($_POST["password"]) or empty($_POST["firstname"]) or empty($_POST["lastname"])) {
echo "<p style='color:red;'>" . get_translation("FILL_ALL_FIELDS") . "</p>";
Expand Down
14 changes: 11 additions & 3 deletions ulicms/classes/helpers/ArrayHelper.php
Expand Up @@ -26,7 +26,7 @@ public static function insertBefore(
break;
}
$tmpArray[$key] = $value;
$originalIndex ++;
$originalIndex++;
}
array_splice($input, 0, $originalIndex, $tmpArray);
return $input;
Expand All @@ -45,7 +45,7 @@ public static function insertAfter(
$originalIndex = 0;
foreach ($input as $key => $value) {
$tmpArray[$key] = $value;
$originalIndex ++;
$originalIndex++;
if ($key === $index) {
$tmpArray[] = $element;
break;
Expand Down Expand Up @@ -80,7 +80,7 @@ public static function getSingle(array $input) {
}

// flatten a nested array structure to one layer
public static function flatten($input) {
public static function flatten($input): array {
if (!is_array($input)) {
// nothing to do if it's not an array
return [$input];
Expand All @@ -95,4 +95,12 @@ public static function flatten($input) {
return $result;
}

public static function getValueOrDefault(?array $input, $key, $default) {
if (!$input) {
return $default;
}

return isset($input[$key]) ? $input[$key] : $default;
}

}
12 changes: 12 additions & 0 deletions ulicms/classes/helpers/TestHelper.php
@@ -0,0 +1,12 @@
<?php

namespace UliCMS\Helpers;

class TestHelper extends \Helper {

// Check if PHP is running in context of a Unit test
public static function isRunningPHPUnit(): bool {
return defined('PHPUNIT_COMPOSER_INSTALL') or defined('__PHPUNIT_PHAR__');
}

}
3 changes: 2 additions & 1 deletion ulicms/classes/helpers/load.php
Expand Up @@ -8,7 +8,8 @@
"NumberFormatHelper",
"StringHelper",
"UrlHelper",
"DataTablesHelper"
"DataTablesHelper",
"TestHelper"
);
foreach ($classes as $class) {
require_once dirname(__file__) . "/$class.php";
Expand Down
2 changes: 1 addition & 1 deletion ulicms/classes/objects/users/User.php
Expand Up @@ -311,7 +311,7 @@ public function resetPassword(): void {
}

public function getFullName(): string {
return (!empty($this->firstname) and!empty($this->lastname)) ?
return (!empty($this->firstname) and !empty($this->lastname)) ?
"{$this->firstname} {$this->lastname}" : "";
}

Expand Down
Expand Up @@ -577,7 +577,7 @@ public function getLanguageSelection(): array {
$language,
getLanguageNameByCode($language)
);
if (count($userLanguages) and!in_array($language, $userLanguages)) {
if (count($userLanguages) and !in_array($language, $userLanguages)) {
continue;
}

Expand Down
6 changes: 3 additions & 3 deletions ulicms/content/modules/core_content/templates/categories.php
Expand Up @@ -23,7 +23,7 @@
$categories = Categories::getAllCategories($order);
?>
<?php
if (!isset($_GET["add"]) and!isset($_GET["edit"])
if (!isset($_GET["add"]) and !isset($_GET["edit"])
and $permissionChecker->hasPermission("categories_create")) {
?>
<?php
Expand All @@ -48,8 +48,8 @@ class="fa fa-plus"></i>
}
?>
<?php
if (count($categories) > 0 and!isset($_GET["add"])
and!isset($_GET["edit"])) {
if (count($categories) > 0 and !isset($_GET["add"])
and !isset($_GET["edit"])) {
?>
<div class="scroll">
<table class="tablesorter">
Expand Down
4 changes: 2 additions & 2 deletions ulicms/content/modules/core_content/templates/pages/edit.php
Expand Up @@ -64,7 +64,7 @@
$can_edit_this = $checker->canWrite($row->id);

$languageAssignment = getAllLanguages(true);
if (count($languageAssignment) > 0 and!in_array($row->language, $languageAssignment)) {
if (count($languageAssignment) > 0 and !in_array($row->language, $languageAssignment)) {
$can_edit_this = false;
}

Expand Down Expand Up @@ -1036,7 +1036,7 @@ class="btn btn-default" target="_blank"><i class="fa fa-comments"></i> <?php tra
echo $th;
?>"
<?php
if (!is_null($row->theme) and!empty($row->theme) and $row->theme == $th)
if (!is_null($row->theme) and !empty($row->theme) and $row->theme == $th)
echo "selected";
?>>
<?php
Expand Down
Expand Up @@ -146,7 +146,7 @@ public function getData(
}

protected function buildFilterSQL($where, $filters): string {
if (isset($filters["type"]) and!empty($filters["type"])) {
if (isset($filters["type"]) and !empty($filters["type"])) {
$where .= " and type ='" .
Database::escapeValue($filters["type"]) .
"'";
Expand Down
2 changes: 1 addition & 1 deletion ulicms/content/modules/core_forms/templates/edit.php
Expand Up @@ -3,7 +3,7 @@
use UliCMS\Models\Content\Categories;

$permissionChecker = new ACL();
if (!$permissionChecker->hasPermission("forms") or!$permissionChecker->hasPermission("forms_edit")) {
if (!$permissionChecker->hasPermission("forms") or !$permissionChecker->hasPermission("forms_edit")) {
noPerms();
} else {
$forms = Forms::getAllForms();
Expand Down
Expand Up @@ -6,7 +6,7 @@
if ($permissionChecker->hasPermission("open_graph")) {
$og_image = Settings::get("og_image");
$og_url = "";
if (!empty($og_image) and!startsWith($og_image, "http")) {
if (!empty($og_image) and !startsWith($og_image, "http")) {
$og_url = get_protocol_and_domain() . $og_image;
}
?>
Expand Down
9 changes: 0 additions & 9 deletions ulicms/tests/environment/DependenciesTest.php

This file was deleted.

9 changes: 9 additions & 0 deletions ulicms/tests/environment/SystemRequirementsTest.php
@@ -1,5 +1,7 @@
<?php

use UliCMS\Helpers\TestHelper;

class SystemRequirementsTest extends \PHPUnit\Framework\TestCase {

public function testPhpVersion() {
Expand Down Expand Up @@ -49,5 +51,12 @@ public function testPhpModuleXml() {
public function testConnectToUliCMSServices() {
$this->assertNotNull(file_get_contents_wrapper("https://www.ulicms.de/", true));
}

public function testPhpIcoInstalled() {
$this->assertTrue(class_exists("PHP_ICO"));
}

public function testIsRunningPHPUnit(){
$this->assertTrue(TestHelper::isRunningPHPUnit());
}
}
19 changes: 19 additions & 0 deletions ulicms/tests/helpers/ArrayHelperTest.php
Expand Up @@ -181,4 +181,23 @@ public function testFlattenWithString() {
ArrayHelper::flatten("ulicms"));
}

public function testGetValueOrDefaultWithNullReturnsDefault() {
$this->assertEquals("foobar",
ArrayHelper::getValueOrDefault(null, "hello", "foobar"));
}

public function testGetValueOrDefaultWithArrayReturnsDefault() {
$this->assertEquals("foobar",
ArrayHelper::getValueOrDefault(
["gibts" => "not"], "hello", "foobar")
);
}

public function testGetValueOrDefaultWithArrayReturnsValue() {
$this->assertEquals("world",
ArrayHelper::getValueOrDefault(
["hello" => "world"], "hello", "foobar")
);
}

}

0 comments on commit dba8bee

Please sign in to comment.