Commit 9cbbe2e8 authored by Tino Goratsch's avatar Tino Goratsch

restructured the Unit tests

parent d2ab93dd
<?php
namespace ACP3\Core;
/**
* Class Functions
* @package ACP3\Core
*/
class Functions
{
/**
* Enkodiert alle HTML-Entitäten eines Strings
* zur Vermeidung von XSS
*
* @param string $var
* @param boolean $scriptTagOnly
*
* @return string
*/
public static function strEncode($var, $scriptTagOnly = false)
{
$var = preg_replace('=<script[^>]*>.*</script>=isU', '', $var);
return $scriptTagOnly === true ? $var : htmlentities($var, ENT_QUOTES, 'UTF-8');
}
}
......@@ -13,33 +13,29 @@ class Secure
* Generiert ein gesalzenes Passwort
*
* @param string $salt
* Das zu verwendende Salz
* @param string $plaintext
* Das Passwort in Klartextform, welches verschlüsselt werden soll
* @param string $password
* @param string $algorithm
* Der zu verwendende Hash-Algorithmus
*
* @return string
*/
public function generateSaltedPassword($salt, $plaintext, $algorithm = 'sha1')
public function generateSaltedPassword($salt, $password, $algorithm = 'sha1')
{
return hash($algorithm, $salt . hash($algorithm, $plaintext));
return hash($algorithm, $salt . hash($algorithm, $password));
}
/**
* Generiert einen Zufallsstring beliebiger Länge
*
* @param integer $strLength
* Länge des zufälligen Strings
* @param integer $length
*
* @return string
*/
public function salt($strLength)
public function salt($length)
{
$salt = '';
$chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$cChars = strlen($chars) - 1;
while (strlen($salt) < $strLength) {
while (strlen($salt) < $length) {
$char = $chars[mt_rand(0, $cChars)];
// Zeichen nur hinzufügen, wenn sich dieses nicht bereits im Salz befindet
if (strpos($salt, $char) === false) {
......@@ -50,8 +46,7 @@ class Secure
}
/**
* Enkodiert alle HTML-Entitäten eines Strings
* zur Vermeidung von XSS
* Enkodiert alle HTML-Entitäten eines Strings zur Vermeidung von XSS
*
* @param string $var
* @param boolean $scriptTagOnly
......
......@@ -3,6 +3,7 @@ namespace ACP3\Core;
use ACP3\Core\Controller\AreaEnum;
use ACP3\Core\Helpers\Forms;
use ACP3\Core\Helpers\Secure;
use ACP3\Core\Http\RequestInterface;
use ACP3\Core\I18n\Translator;
use ACP3\Core\Router\Aliases;
......@@ -31,6 +32,10 @@ class SEO
* @var \ACP3\Core\Helpers\Forms
*/
protected $formsHelper;
/**
* @var \ACP3\Core\Helpers\Secure
*/
protected $secureHelper;
/**
* @var \ACP3\Core\Config
*/
......@@ -70,6 +75,7 @@ class SEO
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\Router\Aliases $aliases
* @param \ACP3\Core\Helpers\Forms $formsHelper
* @param \ACP3\Core\Helpers\Secure $secureHelper
* @param \ACP3\Modules\ACP3\Seo\Cache $seoCache
* @param \ACP3\Core\Config $config
* @param \ACP3\Modules\ACP3\Seo\Model\SeoRepository $seoRepository
......@@ -79,6 +85,7 @@ class SEO
RequestInterface $request,
Aliases $aliases,
Forms $formsHelper,
Secure $secureHelper,
SeoCache $seoCache,
Config $config,
seoRepository $seoRepository)
......@@ -87,6 +94,7 @@ class SEO
$this->request = $request;
$this->aliases = $aliases;
$this->formsHelper = $formsHelper;
$this->secureHelper = $secureHelper;
$this->seoCache = $seoCache;
$this->config = $config;
$this->seoRepository = $seoRepository;
......@@ -365,8 +373,8 @@ class SEO
public function insertUriAlias($path, $alias, $keywords = '', $description = '', $robots = 0)
{
$path .= !preg_match('/\/$/', $path) ? '/' : '';
$keywords = Functions::strEncode($keywords);
$description = Functions::strEncode($description);
$keywords = $this->secureHelper->strEncode($keywords);
$description = $this->secureHelper->strEncode($description);
$values = [
'alias' => $alias,
'keywords' => $keywords,
......
<?php
namespace ACP3\Core\Test;
class AssetsTest extends PHPUnit_Framework_TestCase
use ACP3\Core\Assets;
use ACP3\Core\Environment\ApplicationMode;
use ACP3\Core\Environment\ApplicationPath;
class AssetsTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \ACP3\Core\Assets
* @var Assets
*/
private $assets;
protected function setUp()
{
$appPath = new \ACP3\Core\Environment\ApplicationPath(\ACP3\Core\Environment\ApplicationMode::DEVELOPMENT);
$appPath = new ApplicationPath(ApplicationMode::DEVELOPMENT);
$appPath->setDesignPathInternal(ACP3_ROOT_DIR . 'tests/designs/acp3/');
$this->assets = new \ACP3\Core\Assets($appPath);
$this->assets = new Assets($appPath);
}
public function testDefaultLibrariesEnabled()
......@@ -43,4 +49,4 @@ class AssetsTest extends PHPUnit_Framework_TestCase
$this->assertEquals(['additional-script.js'], $files);
}
}
\ No newline at end of file
}
......@@ -4,28 +4,34 @@
* details.
*/
namespace ACP3\Core\Test\Date;
use ACP3\Core\Date\DateTranslator;
use ACP3\Core\I18n\Translator;
/**
* Class DateTranslatorTest
* @package ACP3\Core\Test
*/
class DateTranslatorTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \ACP3\Installer\Core\I18n\Translator|PHPUnit_Framework_MockObject_MockObject
* @var \ACP3\Installer\Core\I18n\Translator|\PHPUnit_Framework_MockObject_MockObject
*/
private $translatorMock;
/**
* @var \ACP3\Core\Date\DateTranslator
* @var DateTranslator
*/
private $dateTranslator;
protected function setUp()
{
$this->translatorMock = $this->getMockBuilder(\ACP3\Core\I18n\Translator::class)
$this->translatorMock = $this->getMockBuilder(Translator::class)
->disableOriginalConstructor()
->setMethods(['t'])
->getMock();
$this->dateTranslator = new \ACP3\Core\Date\DateTranslator($this->translatorMock);
$this->dateTranslator = new DateTranslator($this->translatorMock);
}
public function testAbbreviatedDaysLocalization()
......@@ -123,4 +129,4 @@ class DateTranslatorTest extends \PHPUnit_Framework_TestCase
call_user_func_array([$invocationMocker, 'willReturnOnConsecutiveCalls'], $translations);
}
}
\ No newline at end of file
}
<?php
abstract class AbstractColumnRendererTest extends PHPUnit_Framework_TestCase
namespace ACP3\Core\Test\Helpers\ColumnRenderer;
abstract class AbstractColumnRendererTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \ACP3\Core\Helpers\DataGrid\ColumnRenderer\AbstractColumnRenderer
......@@ -163,4 +165,4 @@ abstract class AbstractColumnRendererTest extends PHPUnit_Framework_TestCase
$this->assertEquals($expected, $actual);
}
}
\ No newline at end of file
}
<?php
namespace ACP3\Core\Test\Helpers\ColumnRenderer;
use ACP3\Core\Config;
use ACP3\Core\Date;
use ACP3\Core\Date\DateTranslator;
use ACP3\Core\Helpers\DataGrid\ColumnRenderer\DateColumnRenderer;
use ACP3\Core\Helpers\Formatter\DateRange;
use ACP3\Core\I18n\Translator;
use ACP3\Core\User;
class DateColumnRendererTest extends AbstractColumnRendererTest
{
/**
* @var \ACP3\Core\I18n\Translator|PHPUnit_Framework_MockObject_MockObject
* @var Translator|\PHPUnit_Framework_MockObject_MockObject
*/
protected $langMock;
/**
* @var \ACP3\Core\User|PHPUnit_Framework_MockObject_MockObject
* @var User|\PHPUnit_Framework_MockObject_MockObject
*/
protected $userMock;
/**
* @var \ACP3\Core\Config|PHPUnit_Framework_MockObject_MockObject
* @var Config|\PHPUnit_Framework_MockObject_MockObject
*/
protected $configMock;
/**
* @var \ACP3\Core\Date\DateTranslator
* @var DateTranslator
*/
protected $dateTranslator;
/**
* @var \ACP3\Core\Date
* @var Date
*/
protected $date;
/**
* @var \ACP3\Core\Helpers\Formatter\DateRange
* @var DateRange
*/
protected $dateRange;
protected function setUp()
{
$this->langMock = $this->getMockBuilder(\ACP3\Core\I18n\Translator::class)
$this->langMock = $this->getMockBuilder(Translator::class)
->disableOriginalConstructor()
->setMethods(['t'])
->getMock();
$this->dateTranslator = new \ACP3\Core\Date\DateTranslator($this->langMock);
$this->userMock = $this->getMockBuilder(\ACP3\Core\User::class)
$this->dateTranslator = new DateTranslator($this->langMock);
$this->userMock = $this->getMockBuilder(User::class)
->disableOriginalConstructor()
->getMock();
$this->configMock = $this->getMockBuilder(\ACP3\Core\Config::class)
$this->configMock = $this->getMockBuilder(Config::class)
->disableOriginalConstructor()
->getMock();
......@@ -49,17 +59,17 @@ class DateColumnRendererTest extends AbstractColumnRendererTest
'time_zone' => 'Europe/Berlin',
]);
$this->date = new \ACP3\Core\Date(
$this->date = new Date(
$this->userMock,
$this->dateTranslator,
$this->configMock
);
$this->dateRange = new \ACP3\Core\Helpers\Formatter\DateRange(
$this->dateRange = new DateRange(
$this->date,
$this->langMock
);
$this->columnRenderer = new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\DateColumnRenderer(
$this->columnRenderer = new DateColumnRenderer(
$this->date,
$this->dateRange
);
......@@ -93,4 +103,4 @@ class DateColumnRendererTest extends AbstractColumnRendererTest
$expected = '<td data-order="1445372421"><time datetime="2015-10-20T22:20:21+02:00">2015-10-20 22:20</time>&ndash;<time datetime="2015-10-25T21:20:21+01:00">2015-10-25 21:20</time></td>';
$this->compareResults($expected);
}
}
\ No newline at end of file
}
<?php
namespace ACP3\Core\Test\Helpers\ColumnRenderer;
use ACP3\Core\Helpers\DataGrid\ColumnRenderer\IntegerColumnRenderer;
class IntegerColumnRendererTest extends AbstractColumnRendererTest
{
protected function setUp()
{
$this->columnRenderer = new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\IntegerColumnRenderer();
$this->columnRenderer = new IntegerColumnRenderer();
parent::setUp();
}
......@@ -34,4 +37,4 @@ class IntegerColumnRendererTest extends AbstractColumnRendererTest
$expected = '<td>0</td>';
$this->compareResults($expected);
}
}
\ No newline at end of file
}
<?php
namespace ACP3\Core\Test\Helpers\ColumnRenderer;
use ACP3\Core\Helpers\DataGrid\ColumnRenderer\MassActionColumnRenderer;
class MassActionColumnRendererTest extends AbstractColumnRendererTest
{
protected function setUp()
{
$this->columnRenderer = new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\MassActionColumnRenderer();
$this->columnRenderer = new MassActionColumnRenderer();
parent::setUp();
}
......@@ -27,4 +30,4 @@ class MassActionColumnRendererTest extends AbstractColumnRendererTest
}
}
\ No newline at end of file
}
<?php
namespace ACP3\Core\Test\Helpers\ColumnRenderer;
use ACP3\Core\Helpers\DataGrid\ColumnRenderer\Nl2pColumnRenderer;
use ACP3\Core\Helpers\StringFormatter;
class Nl2pColumnRendererTest extends AbstractColumnRendererTest
{
/**
* @var \ACP3\Core\Helpers\StringFormatter
* @var StringFormatter
*/
protected $stringFormatter;
protected function setUp()
{
$this->stringFormatter = new \ACP3\Core\Helpers\StringFormatter();
$this->stringFormatter = new StringFormatter();
$this->columnRenderer = new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\Nl2pColumnRenderer($this->stringFormatter);
$this->columnRenderer = new Nl2pColumnRenderer($this->stringFormatter);
parent::setUp();
}
......@@ -41,4 +45,4 @@ class Nl2pColumnRendererTest extends AbstractColumnRendererTest
$expected = "<td><p>Lorem Ipsum</p>\n<p>Dolor</p></td>";
$this->compareResults($expected);
}
}
\ No newline at end of file
}
<?php
namespace ACP3\Core\Test\Helpers\ColumnRenderer;
use ACP3\Core\Helpers\DataGrid\ColumnRenderer\PictureColumnRenderer;
use ACP3\Core\Router;
class PictureColumnRendererTest extends AbstractColumnRendererTest
{
/**
* @var \ACP3\Core\Router|PHPUnit_Framework_MockObject_MockObject
* @var Router|\PHPUnit_Framework_MockObject_MockObject
*/
protected $routerMock;
protected function setUp()
{
$this->routerMock = $this->getMockBuilder(\ACP3\Core\Router::class)
$this->routerMock = $this->getMockBuilder(Router::class)
->disableOriginalConstructor()
->setMethods(['route'])
->getMock();
$this->columnRenderer = new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\PictureColumnRenderer(
$this->columnRenderer = new PictureColumnRenderer(
$this->routerMock
);
......@@ -61,4 +65,4 @@ class PictureColumnRendererTest extends AbstractColumnRendererTest
$expected = '<td><img src="gallery/index/pic/id_1" alt=""></td>';
$this->compareResults($expected);
}
}
\ No newline at end of file
}
<?php
namespace ACP3\Core\Test\Helpers\ColumnRenderer;
use ACP3\Core\Helpers\DataGrid\ColumnRenderer\ReplaceValueColumnRenderer;
class ReplaceValueColumnRendererTest extends AbstractColumnRendererTest
{
protected function setUp()
{
$this->columnRenderer = new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\ReplaceValueColumnRenderer();
$this->columnRenderer = new ReplaceValueColumnRenderer();
$this->columnData = array_merge(
$this->getColumnDefaults(),
......@@ -70,4 +73,4 @@ class ReplaceValueColumnRendererTest extends AbstractColumnRendererTest
$this->compareResults($expected);
}
}
\ No newline at end of file
}
<?php
namespace ACP3\Core\Test\Helpers\ColumnRenderer;
use ACP3\Core\Helpers\DataGrid\ColumnRenderer\TextColumnRenderer;
class TextColumnRendererTest extends AbstractColumnRendererTest
{
protected function setUp()
{
$this->columnRenderer = new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\TextColumnRenderer();
$this->columnRenderer = new TextColumnRenderer();
parent::setUp();
}
}
\ No newline at end of file
}
<?php
namespace ACP3\Core\Test\Helpers\ColumnRenderer;
use ACP3\Core\Helpers\DataGrid\ColumnRenderer\TranslateColumnRenderer;
use ACP3\Core\I18n\Translator;
class TranslateColumnRendererTest extends AbstractColumnRendererTest
{
/**
* @var \ACP3\Core\I18n\Translator|PHPUnit_Framework_MockObject_MockObject
* @var Translator|\PHPUnit_Framework_MockObject_MockObject
*/
protected $langMock;
protected function setUp()
{
$this->langMock = $this->getMockBuilder(\ACP3\Core\I18n\Translator::class)
$this->langMock = $this->getMockBuilder(Translator::class)
->disableOriginalConstructor()
->setMethods(['t'])
->getMock();
$this->columnRenderer = new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\TranslateColumnRenderer($this->langMock);
$this->columnRenderer = new TranslateColumnRenderer($this->langMock);
parent::setUp();
}
......@@ -46,4 +50,4 @@ class TranslateColumnRendererTest extends AbstractColumnRendererTest
$expected = '<td>{NEWS_NEWS}</td>';
$this->compareResults($expected);
}
}
\ No newline at end of file
}
<?php
namespace ACP3\Core\Test\Helpers;
class DataGridTest extends PHPUnit_Framework_TestCase
use ACP3\Core\ACL;
use ACP3\Core\Helpers\DataGrid;
use ACP3\Core\Helpers\Formatter\MarkEntries;
use ACP3\Core\I18n\Translator;
class DataGridTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \ACP3\Core\Helpers\DataGrid|PHPUnit_Framework_MockObject_MockObject
* @var DataGrid|\PHPUnit_Framework_MockObject_MockObject
*/
protected $dataGrid;
/**
* @var \ACP3\Core\ACL|PHPUnit_Framework_MockObject_MockObject
* @var ACL|\PHPUnit_Framework_MockObject_MockObject
*/
protected $aclMock;
/**
* @var \ACP3\Core\I18n\Translator|PHPUnit_Framework_MockObject_MockObject
* @var Translator|\PHPUnit_Framework_MockObject_MockObject
*/
protected $langMock;
protected function setUp()
{
$this->aclMock = $this->getMockBuilder(\ACP3\Core\ACL::class)
$this->aclMock = $this->getMockBuilder(ACL::class)
->disableOriginalConstructor()
->setMethods(['hasPermission'])
->getMock();
$this->langMock = $this->getMockBuilder(\ACP3\Core\I18n\Translator::class)
$this->langMock = $this->getMockBuilder(Translator::class)
->disableOriginalConstructor()
->setMethods(['t'])
->getMock();
$this->dataGrid = new \ACP3\Core\Helpers\DataGrid(
$this->dataGrid = new DataGrid(
$this->aclMock,
$this->langMock
);
......@@ -76,18 +82,18 @@ class DataGridTest extends PHPUnit_Framework_TestCase
->method('hasPermission')
->willReturn(false);
/** @var \ACP3\Core\Helpers\Formatter\MarkEntries|PHPUnit_Framework_MockObject_MockObject $markEntriesMock */
$markEntriesMock = $this->getMockBuilder(\ACP3\Core\Helpers\Formatter\MarkEntries::class)
/** @var MarkEntries|\PHPUnit_Framework_MockObject_MockObject $markEntriesMock */
$markEntriesMock = $this->getMockBuilder(MarkEntries::class)
->disableOriginalConstructor()
->getMock();
$this->dataGrid->registerColumnRenderer(new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\HeaderColumnRenderer($markEntriesMock));
$this->dataGrid->registerColumnRenderer(new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\TextColumnRenderer());
$this->dataGrid->registerColumnRenderer(new DataGrid\ColumnRenderer\HeaderColumnRenderer($markEntriesMock));
$this->dataGrid->registerColumnRenderer(new DataGrid\ColumnRenderer\TextColumnRenderer());
$this->dataGrid->addColumn([
'label' => 'Foo',
'fields' => ['title'],
'type' => \ACP3\Core\Helpers\DataGrid\ColumnRenderer\TextColumnRenderer::class
'type' => DataGrid\ColumnRenderer\TextColumnRenderer::class
], 10);
$expected = array_merge(
......@@ -122,18 +128,18 @@ class DataGridTest extends PHPUnit_Framework_TestCase
->method('hasPermission')
->willReturn(false);
/** @var \ACP3\Core\Helpers\Formatter\MarkEntries|PHPUnit_Framework_MockObject_MockObject $markEntriesMock */
$markEntriesMock = $this->getMockBuilder(\ACP3\Core\Helpers\Formatter\MarkEntries::class)
/** @var MarkEntries|\PHPUnit_Framework_MockObject_MockObject $markEntriesMock */
$markEntriesMock = $this->getMockBuilder(MarkEntries::class)
->disableOriginalConstructor()
->getMock();
$this->dataGrid->registerColumnRenderer(new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\HeaderColumnRenderer($markEntriesMock));
$this->dataGrid->registerColumnRenderer(new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\TextColumnRenderer());
$this->dataGrid->registerColumnRenderer(new DataGrid\ColumnRenderer\HeaderColumnRenderer($markEntriesMock));
$this->dataGrid->registerColumnRenderer(new DataGrid\ColumnRenderer\TextColumnRenderer());
$this->dataGrid->addColumn([
'label' => 'Foo',
'fields' => ['title'],
'type' => \ACP3\Core\Helpers\DataGrid\ColumnRenderer\TextColumnRenderer::class
'type' => DataGrid\ColumnRenderer\TextColumnRenderer::class
], 10);
$this->dataGrid->setResults($data);
......@@ -154,4 +160,4 @@ class DataGridTest extends PHPUnit_Framework_TestCase
$this->assertEquals($expected, $this->dataGrid->render());
}
}
\ No newline at end of file
}
<?php
class DateRangeTest extends PHPUnit_Framework_TestCase
namespace ACP3\Core\Test\Helpers\Formatter;
use ACP3\Core\Config;
use ACP3\Core\Date;
use ACP3\Core\Date\DateTranslator;
use ACP3\Core\Helpers\Formatter\DateRange;
use ACP3\Core\I18n\Translator;
use ACP3\Core\User;
class DateRangeTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \ACP3\Core\Config|PHPUnit_Framework_MockObject_MockObject
* @var Config|\PHPUnit_Framework_MockObject_MockObject
*/
private $configMock;
/**
* @var \ACP3\Core\Date\DateTranslator
* @var DateTranslator
*/
private $dateTranslator;
/**
* @var \ACP3\Core\Date
* @var Date
*/
private $date;
/**
* @var \ACP3\Core\I18n\Translator|\PHPUnit_Framework_MockObject_MockObject
* @var Translator|\PHPUnit_Framework_MockObject_MockObject
*/
private $langMock;
/**
* @var \ACP3\Core\User|PHPUnit_Framework_MockObject_MockObject
* @var User|\PHPUnit_Framework_MockObject_MockObject
*/
private $userMock;
/**
* @var \ACP3\Core\Helpers\Formatter\DateRange
* @var DateRange
*/
private $dateRange;
protected function setUp()
{
$this->langMock = $this->getMockBuilder(\ACP3\Core\I18n\Translator::class)
$this->langMock = $this->getMockBuilder(Translator::class)
->disableOriginalConstructor()
->setMethods(['t'])
->getMock();
$this->userMock = $this->getMockBuilder(\ACP3\Core\User::class)
$this->userMock = $this->getMockBuilder(User::class)
->disableOriginalConstructor()
->getMock();
$this->configMock = $this->getMockBuilder(\ACP3\Core\Config::class)
$this->configMock = $this->getMockBuilder(Config::class)
->disableOriginalConstructor()
->getMock();
......@@ -49,14 +58,14 @@ class DateRangeTest extends PHPUnit_Framework_TestCase
'time_zone' => 'Europe/Berlin',
]);
$this->dateTranslator = new \ACP3\Core\Date\DateTranslator($this->langMock);
$this->dateTranslator = new DateTranslator($this->langMock);
$this->date = new \ACP3\Core\Date(
$this->date = new Date(
$this->userMock,
$this->dateTranslator,
$this->configMock
);
$this->dateRange = new \ACP3\Core\Helpers\Formatter\DateRange(
$this->dateRange = new DateRange(
$this->date,
$this->langMock
);
......@@ -122,4 +131,4 @@ class DateRangeTest extends PHPUnit_Framework_TestCase
$this->assertEquals($expected, $this->dateRange->formatTimeRange($dateStart, $dateEnd, 'short'));
}
}
\ No newline at end of file
}