Commit 57384a79 authored by Tino Goratsch's avatar Tino Goratsch

- refactored the column renderers a little bit to make the customization a bit more easy

- added unit tests for the TranslateColumnRenderer
parent 08e2d56c
......@@ -12,7 +12,7 @@ abstract class AbstractColumnRenderer implements ColumnRendererInterface
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
{
return $this->render($column, $this->getDbFieldValueIfExists($column, $dbResultRow));
return $this->render($column, $this->getValue($column, $dbResultRow));
}
/**
......@@ -69,14 +69,36 @@ abstract class AbstractColumnRenderer implements ColumnRendererInterface
*
* @return string
*/
protected function getDbFieldValueIfExists(array $column, array $dbResultRow)
protected function getValue(array $column, array $dbResultRow)
{
$field = $this->getFirstDbField($column);
$value = $this->getDbValueIfExists($dbResultRow, $field);
if (isset($dbResultRow[$field])) {
return $dbResultRow[$field];
if ($value === null) {
$value = $this->getDefaultValue($column);
}
return $value;
}
/**
* @param array $dbResultRow
* @param string $field
*
* @return null
*/
protected function getDbValueIfExists(array $dbResultRow, $field)
{
return isset($dbResultRow[$field]) ? $dbResultRow[$field] : null;
}
/**
* @param array $column
*
* @return string
*/
private function getDefaultValue(array $column)
{
if (isset($column['custom']['default_value'])) {
return $column['custom']['default_value'];
}
......
......@@ -36,7 +36,7 @@ class DateColumnRenderer extends AbstractColumnRenderer
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
{
$date = $this->getDbFieldValueIfExists($column, $dbResultRow);
$date = $this->getValue($column, $dbResultRow);
$column['attribute'] = [
'data-order' => $this->date->format($date, 'U')
];
......
......@@ -39,7 +39,7 @@ class DateRangeColumnRenderer extends AbstractColumnRenderer
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
{
$dateStart = $this->getDbFieldValueIfExists($column, $dbResultRow);
$dateStart = $this->getValue($column, $dbResultRow);
$dateEnd = $dbResultRow[$column['fields'][1]];
$column['attribute'] = [
'data-order' => $this->date->format($dateStart, 'U')
......
......@@ -8,24 +8,14 @@ namespace ACP3\Core\Helpers\DataGrid\ColumnRenderer;
class IntegerColumnRenderer extends AbstractColumnRenderer
{
/**
* @param array $column
* @param array $dbResultRow
* @param array $dbResultRow
* @param string $field
*
* @return string
* @return int|null
*/
protected function getDbFieldValueIfExists(array $column, array $dbResultRow)
protected function getDbValueIfExists(array $dbResultRow, $field)
{
$field = $this->getFirstDbField($column);
if (isset($dbResultRow[$field])) {
return (int)$dbResultRow[$field];
}
if (isset($column['custom']['default_value'])) {
return $column['custom']['default_value'];
}
return '';
return isset($dbResultRow[$field]) ? (int)$dbResultRow[$field] : null;
}
/**
......
......@@ -28,7 +28,7 @@ class Nl2pColumnRenderer extends AbstractColumnRenderer
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
{
$value = $this->getDbFieldValueIfExists($column, $dbResultRow);
$value = $this->getValue($column, $dbResultRow);
return $this->render($column, $this->stringFormatter->nl2p($value));
}
......
......@@ -29,7 +29,7 @@ class PictureColumnRenderer extends AbstractColumnRenderer
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
{
if (isset($column['custom']['pattern'])) {
$dbValue = $this->getDbFieldValueIfExists($column, $dbResultRow);
$dbValue = $this->getValue($column, $dbResultRow);
$value = '<img src="' . $this->getUrl($column['custom'], $dbValue) . '" alt="">';
} else {
$value = '';
......
......@@ -13,7 +13,7 @@ class ReplaceValueColumnRenderer extends AbstractColumnRenderer
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
{
$value = $this->getDbFieldValueIfExists($column, $dbResultRow);
$value = $this->getValue($column, $dbResultRow);
$search = $column['custom']['search'];
$replace = $column['custom']['replace'];
......
......@@ -26,11 +26,15 @@ class TranslateColumnRenderer extends AbstractColumnRenderer
/**
* @inheritdoc
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
protected function getDbValueIfExists(array $dbResultRow, $field)
{
$value = $this->getDbFieldValueIfExists($column, $dbResultRow);
if (isset($dbResultRow[$field])) {
$value = $dbResultRow[$field];
return $this->render($column, $this->lang->t($value, $value));
return $this->lang->t($value, $value);
}
return null;
}
/**
......
......@@ -41,7 +41,7 @@ class PictureSortColumnRenderer extends AbstractColumnRenderer
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
{
$dbValue = $this->getDbFieldValueIfExists($column, $dbResultRow);
$dbValue = $this->getValue($column, $dbResultRow);
$value = '';
if ($dbResultRow['last'] != $dbValue) {
......
......@@ -40,7 +40,7 @@ class AccountStatusColumnRenderer extends AbstractColumnRenderer
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
{
$status = $this->getDbFieldValueIfExists($column, $dbResultRow);
$status = $this->getValue($column, $dbResultRow);
if ($status == 0) {
$route = $this->router->route('acp/newsletter/accounts/activate/id_' . $dbResultRow['id']);
......
......@@ -62,7 +62,7 @@ class SeoRobotsColumnRenderer extends AbstractColumnRenderer
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
{
$value = $this->getDbFieldValueIfExists($column, $dbResultRow);
$value = $this->getValue($column, $dbResultRow);
if (empty($this->replace) === true) {
$this->replace = $this->_setReplaceParams();
......
......@@ -31,7 +31,7 @@ class UserRolesColumnRenderer extends AbstractColumnRenderer
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
{
$value = $this->getDbFieldValueIfExists($column, $dbResultRow);
$value = $this->getValue($column, $dbResultRow);
return $this->render($column, implode(', ', $this->acl->getUserRoleNames($value)));
}
......
......@@ -136,9 +136,9 @@ abstract class AbstractColumnRendererTest extends PHPUnit_Framework_TestCase
}
/**
* @param $expected
* @param array $column
* @param array $data
* @param string $expected
* @param array $column
* @param array $data
*/
protected function compareResults($expected, array $column, array $data = [])
{
......
<?php
class TranslateColumnRendererTest extends AbstractColumnRendererTest
{
/**
* @var \ACP3\Core\Lang|PHPUnit_Framework_MockObject_MockObject
*/
protected $langMock;
protected function setUp()
{
$this->langMock = $this->getMockBuilder(\ACP3\Core\Lang::class)
->disableOriginalConstructor()
->setMethods(['t'])
->getMock();
$this->columnRenderer = new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\TranslateColumnRenderer($this->langMock);
}
/**
* @param string $langKey
* @param string $langValue
*/
private function setUpLangMockExpectation($langKey, $langValue)
{
$this->langMock->expects($this->once())
->method('t')
->with($langKey, $langKey)
->willReturn($langValue);
}
public function testValidField()
{
$this->setUpLangMockExpectation('news', '{NEWS_NEWS}');
$column = array_merge($this->getColumnDefaults(), [
'fields' => ['text']
]);
$data = [
'text' => 'news'
];
$expected = '<td>{NEWS_NEWS}</td>';
$this->compareResults($expected, $column, $data);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment