Commit 811052e8 authored by Tino Goratsch's avatar Tino Goratsch

- refactored the ColumnRenderer once more

- added unit tests for the MassActionColumnRenderer
parent a22678c3
......@@ -58,7 +58,7 @@ class DataGrid
*/
protected $columns;
/**
* @var \ACP3\Core\Helpers\DataGrid\ColumnRenderer\ColumnRendererInterface[]
* @var \ACP3\Core\Helpers\DataGrid\ColumnRenderer\AbstractColumnRenderer[]
*/
protected $columnRenderer = [];
/**
......@@ -150,7 +150,7 @@ class DataGrid
*/
public function setEnableMassAction($enableMassAction)
{
$this->enableMassAction = (bool) $enableMassAction;
$this->enableMassAction = (bool)$enableMassAction;
return $this;
}
......@@ -162,7 +162,7 @@ class DataGrid
*/
public function setEnableOptions($enableOptions)
{
$this->enableOptions = (bool) $enableOptions;
$this->enableOptions = (bool)$enableOptions;
return $this;
}
......@@ -242,9 +242,10 @@ class DataGrid
foreach (clone $this->columns as $column) {
if (!empty($column['label'])) {
$header .= $this->columnRenderer['table_header']->fetchDataAndRenderColumn(
$column, [], $this->identifier, $this->primaryKey
);
$header .= $this->columnRenderer['table_header']
->setIdentifier($this->identifier)
->setPrimaryKey($this->primaryKey)
->fetchDataAndRenderColumn($column, []);
}
}
......@@ -262,9 +263,10 @@ class DataGrid
$results .= "<tr>\n";
foreach (clone $this->columns as $column) {
if (array_key_exists($column['type'], $this->columnRenderer) && !empty($column['label'])) {
$results .= $this->columnRenderer[$column['type']]->fetchDataAndRenderColumn(
$column, $result, $this->identifier, $this->primaryKey
);
$results .= $this->columnRenderer[$column['type']]
->setIdentifier($this->identifier)
->setPrimaryKey($this->primaryKey)
->fetchDataAndRenderColumn($column, $result);
}
}
......
......@@ -7,10 +7,43 @@ namespace ACP3\Core\Helpers\DataGrid\ColumnRenderer;
*/
abstract class AbstractColumnRenderer implements ColumnRendererInterface
{
/**
* @var string
*/
protected $identifier = '';
/**
* @var string
*/
protected $primaryKey = '';
/**
* @param string $identifier
*
* @return $this
*/
public function setIdentifier($identifier)
{
$this->identifier = $identifier;
return $this;
}
/**
* @param mixed $primaryKey
*
* @return $this
*/
public function setPrimaryKey($primaryKey)
{
$this->primaryKey = $primaryKey;
return $this;
}
/**
* @inheritdoc
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
public function fetchDataAndRenderColumn(array $column, array $dbResultRow)
{
return $this->render($column, $this->getValue($column, $dbResultRow));
}
......@@ -97,7 +130,7 @@ abstract class AbstractColumnRenderer implements ColumnRendererInterface
*
* @return string
*/
private function getDefaultValue(array $column)
protected function getDefaultValue(array $column)
{
if (isset($column['custom']['default_value'])) {
return $column['custom']['default_value'];
......
......@@ -11,14 +11,12 @@ interface ColumnRendererInterface
const TYPE_TD = 'td';
/**
* @param array $column
* @param array $dbResultRow
* @param string $identifier
* @param string $primaryKey
* @param array $column
* @param array $dbResultRow
*
* @return string
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey);
public function fetchDataAndRenderColumn(array $column, array $dbResultRow);
/**
* @return string
......
......@@ -34,7 +34,7 @@ class DateColumnRenderer extends AbstractColumnRenderer
/**
* @inheritdoc
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
public function fetchDataAndRenderColumn(array $column, array $dbResultRow)
{
$date = $this->getValue($column, $dbResultRow);
$column['attribute'] = [
......
......@@ -37,7 +37,7 @@ class DateRangeColumnRenderer extends AbstractColumnRenderer
/**
* @inheritdoc
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
public function fetchDataAndRenderColumn(array $column, array $dbResultRow)
{
$dateStart = $this->getValue($column, $dbResultRow);
$dateEnd = $dbResultRow[$column['fields'][1]];
......
......@@ -26,7 +26,7 @@ class HeaderColumnRenderer extends AbstractColumnRenderer
/**
* @inheritdoc
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
public function fetchDataAndRenderColumn(array $column, array $dbResultRow)
{
if ($column['type'] === 'mass_action') {
$id = preg_replace('=[^\w\d-_]=', '', $column['label']) . '-mark-all';
......
......@@ -10,14 +10,18 @@ class MassActionColumnRenderer extends AbstractColumnRenderer
/**
* @inheritdoc
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
protected function getValue(array $column, array $dbResultRow)
{
if ($column['custom']['can_delete'] === true) {
$value = '<input type="checkbox" name="entries[]" value="' . $dbResultRow[$primaryKey] . '">';
return $this->render($column, $value);
$value = null;
if (isset($column['custom']['can_delete']) && $column['custom']['can_delete'] === true) {
$value = '<input type="checkbox" name="entries[]" value="' . $dbResultRow[$this->primaryKey] . '">';
}
return '';
if ($value === null) {
$value = $this->getDefaultValue($column);
}
return $value;
}
/**
......
......@@ -45,18 +45,18 @@ class OptionsColumnRenderer extends AbstractColumnRenderer
/**
* @inheritdoc
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
public function fetchDataAndRenderColumn(array $column, array $dbResultRow)
{
$this->eventDispatcher->dispatch(
'data_grid.column_renderer.custom_option_before',
new CustomOptionEvent($this->optionRenderer, $dbResultRow, $identifier)
new CustomOptionEvent($this->optionRenderer, $dbResultRow, $this->identifier)
);
if ($column['custom']['can_edit']) {
$resourcePathEdit = $column['custom']['resource_path_edit'];
$resourcePathEdit .= !preg_match('=/$=', $resourcePathEdit) ? '/' : '';
$this->optionRenderer->addOption(
$resourcePathEdit . 'id_' . $dbResultRow[$primaryKey],
$resourcePathEdit . 'id_' . $dbResultRow[$this->primaryKey],
$this->lang->t('system', 'edit'),
'glyphicon-edit',
'btn-default'
......@@ -65,14 +65,14 @@ class OptionsColumnRenderer extends AbstractColumnRenderer
$this->eventDispatcher->dispatch(
'data_grid.column_renderer.custom_option_between',
new CustomOptionEvent($this->optionRenderer, $dbResultRow, $identifier)
new CustomOptionEvent($this->optionRenderer, $dbResultRow, $this->identifier)
);
if ($column['custom']['can_delete']) {
$resourcePathDelete = $column['custom']['resource_path_delete'];
$resourcePathDelete .= !preg_match('=/$=', $resourcePathDelete) ? '/' : '';
$this->optionRenderer->addOption(
$resourcePathDelete . 'entries_' . $dbResultRow[$primaryKey],
$resourcePathDelete . 'entries_' . $dbResultRow[$this->primaryKey],
$this->lang->t('system', 'delete'),
'glyphicon-remove',
'btn-danger'
......@@ -81,7 +81,7 @@ class OptionsColumnRenderer extends AbstractColumnRenderer
$this->eventDispatcher->dispatch(
'data_grid.column_renderer.custom_option_after',
new CustomOptionEvent($this->optionRenderer, $dbResultRow, $identifier)
new CustomOptionEvent($this->optionRenderer, $dbResultRow, $this->identifier)
);
return $this->render($column, $this->collectOptions());
......
......@@ -26,7 +26,7 @@ class PictureColumnRenderer extends AbstractColumnRenderer
/**
* @inheritdoc
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
public function fetchDataAndRenderColumn(array $column, array $dbResultRow)
{
if (isset($column['custom']['pattern'])) {
$dbValue = $this->getValue($column, $dbResultRow);
......
......@@ -19,7 +19,7 @@ class ReplaceValueColumnRenderer extends AbstractColumnRenderer
/**
* @inheritdoc
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
public function fetchDataAndRenderColumn(array $column, array $dbResultRow)
{
$this->search = $column['custom']['search'];
$this->replace = $column['custom']['replace'];
......
......@@ -39,7 +39,7 @@ class PictureSortColumnRenderer extends AbstractColumnRenderer
/**
* @inheritdoc
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
public function fetchDataAndRenderColumn(array $column, array $dbResultRow)
{
$dbValue = $this->getValue($column, $dbResultRow);
......
......@@ -38,7 +38,7 @@ class AccountStatusColumnRenderer extends AbstractColumnRenderer
/**
* @inheritdoc
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
public function fetchDataAndRenderColumn(array $column, array $dbResultRow)
{
$status = $this->getValue($column, $dbResultRow);
......
......@@ -60,7 +60,7 @@ class SeoRobotsColumnRenderer extends AbstractColumnRenderer
/**
* @inheritdoc
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
public function fetchDataAndRenderColumn(array $column, array $dbResultRow)
{
$value = $this->getValue($column, $dbResultRow);
......
......@@ -29,7 +29,7 @@ class UserRolesColumnRenderer extends AbstractColumnRenderer
/**
* @inheritdoc
*/
public function fetchDataAndRenderColumn(array $column, array $dbResultRow, $identifier, $primaryKey)
public function fetchDataAndRenderColumn(array $column, array $dbResultRow)
{
$value = $this->getValue($column, $dbResultRow);
......
......@@ -14,6 +14,14 @@ abstract class AbstractColumnRendererTest extends PHPUnit_Framework_TestCase
* @var array
*/
protected $dbData = [];
/**
* @var string
*/
protected $identifier = '';
/**
* @var string
*/
protected $primaryKey = '';
protected function setUp()
{
......@@ -49,7 +57,7 @@ abstract class AbstractColumnRendererTest extends PHPUnit_Framework_TestCase
]);
$expected = '<td data-foo="bar"></td>';
$this->compareResults($expected, $this->columnData);
$this->compareResults($expected);
}
public function testMultipleCustomHtmlAttributes()
......@@ -62,7 +70,7 @@ abstract class AbstractColumnRendererTest extends PHPUnit_Framework_TestCase
]);
$expected = '<td data-foo="bar" data-lorem="ipsum"></td>';
$this->compareResults($expected, $this->columnData);
$this->compareResults($expected);
}
public function testAddStyle()
......@@ -72,7 +80,7 @@ abstract class AbstractColumnRendererTest extends PHPUnit_Framework_TestCase
]);
$expected = '<td style="width:50%"></td>';
$this->compareResults($expected, $this->columnData);
$this->compareResults($expected);
}
public function testAddCssClass()
......@@ -82,7 +90,7 @@ abstract class AbstractColumnRendererTest extends PHPUnit_Framework_TestCase
]);
$expected = '<td class="foobar"></td>';
$this->compareResults($expected, $this->columnData);
$this->compareResults($expected);
}
public function testInvalidField()
......@@ -95,7 +103,7 @@ abstract class AbstractColumnRendererTest extends PHPUnit_Framework_TestCase
];
$expected = '<td></td>';
$this->compareResults($expected, $this->columnData, $this->dbData);
$this->compareResults($expected);
}
public function testValidField()
......@@ -108,7 +116,7 @@ abstract class AbstractColumnRendererTest extends PHPUnit_Framework_TestCase
];
$expected = '<td>Lorem Ipsum</td>';
$this->compareResults($expected, $this->columnData, $this->dbData);
$this->compareResults($expected);
}
public function testDefaultValueIfNull()
......@@ -124,7 +132,7 @@ abstract class AbstractColumnRendererTest extends PHPUnit_Framework_TestCase
];
$expected = '<td>Foo Bar</td>';
$this->compareResults($expected, $this->columnData, $this->dbData);
$this->compareResults($expected);
}
public function testDefaultValueIfNotFound()
......@@ -140,17 +148,18 @@ abstract class AbstractColumnRendererTest extends PHPUnit_Framework_TestCase
];
$expected = '<td>Foo Bar</td>';
$this->compareResults($expected, $this->columnData, $this->dbData);
$this->compareResults($expected);
}
/**
* @param string $expected
* @param array $column
* @param array $data
*/
protected function compareResults($expected, array $column, array $data = [])
protected function compareResults($expected)
{
$actual = $this->columnRenderer->fetchDataAndRenderColumn($column, $data, '', '');
$actual = $this->columnRenderer
->setIdentifier($this->identifier)
->setPrimaryKey($this->primaryKey)
->fetchDataAndRenderColumn($this->columnData, $this->dbData);
$this->assertEquals($expected, $actual);
}
......
......@@ -19,7 +19,7 @@ class IntegerColumnRendererTest extends AbstractColumnRendererTest
];
$expected = '<td>1</td>';
$this->compareResults($expected, $this->columnData, $this->dbData);
$this->compareResults($expected);
}
public function testValidFieldStringToIntegerConversion()
......@@ -32,6 +32,6 @@ class IntegerColumnRendererTest extends AbstractColumnRendererTest
];
$expected = '<td>0</td>';
$this->compareResults($expected, $this->columnData, $this->dbData);
$this->compareResults($expected);
}
}
\ No newline at end of file
<?php
class MassActionColumnRendererTest extends AbstractColumnRendererTest
{
protected function setUp()
{
$this->columnRenderer = new \ACP3\Core\Helpers\DataGrid\ColumnRenderer\MassActionColumnRenderer();
parent::setUp();
}
public function testValidField()
{
$this->columnData = array_merge($this->columnData, [
'custom' => [
'can_delete' => true
],
]);
$this->dbData = [
'id' => 1234
];
$this->primaryKey = 'id';
$expected = '<td><input type="checkbox" name="entries[]" value="1234"></td>';
$this->compareResults($expected);
}
}
\ No newline at end of file
......@@ -26,7 +26,7 @@ class Nl2pColumnRendererTest extends AbstractColumnRendererTest
];
$expected = '<td><p>Lorem Ipsum</p></td>';
$this->compareResults($expected, $this->columnData, $this->dbData);
$this->compareResults($expected);
}
public function testValidFieldWithMultipleLines()
......@@ -39,6 +39,6 @@ class Nl2pColumnRendererTest extends AbstractColumnRendererTest
];
$expected = "<td><p>Lorem Ipsum</p>\n<p>Dolor</p></td>";
$this->compareResults($expected, $this->columnData, $this->dbData);
$this->compareResults($expected);
}
}
\ No newline at end of file
......@@ -17,6 +17,23 @@ class ReplaceValueColumnRendererTest extends AbstractColumnRendererTest
);
}
public function testValidField()
{
$this->columnData = array_merge($this->columnData, [
'fields' => ['text'],
'custom' => [
'search' => ['Lorem'],
'replace' => ['Dolor']
]
]);
$this->dbData = [
'text' => 'Lorem Ipsum'
];
$expected = '<td>Dolor Ipsum</td>';
$this->compareResults($expected);
}
public function testDefaultValueIfNull()
{
$this->columnData = array_merge($this->columnData, [
......@@ -32,7 +49,7 @@ class ReplaceValueColumnRendererTest extends AbstractColumnRendererTest
];
$expected = '<td>Foo Bar</td>';
$this->compareResults($expected, $this->columnData, $this->dbData);
$this->compareResults($expected);
}
public function testDefaultValueIfNotFound()
......@@ -50,7 +67,7 @@ class ReplaceValueColumnRendererTest extends AbstractColumnRendererTest
];
$expected = '<td>Foo Bar</td>';
$this->compareResults($expected, $this->columnData, $this->dbData);
$this->compareResults($expected);
}
}
\ No newline at end of file
......@@ -39,11 +39,11 @@ class TranslateColumnRendererTest extends AbstractColumnRendererTest
$this->columnData = array_merge($this->columnData, [
'fields' => ['text']
]);
$data = [
$this->dbData = [
'text' => 'news'
];
$expected = '<td>{NEWS_NEWS}</td>';
$this->compareResults($expected, $this->columnData, $data);
$this->compareResults($expected);
}
}
\ 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