Skip to content

Commit

Permalink
added DC_Table_Utils test
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Wagner committed Feb 21, 2018
1 parent 33661b1 commit 554537a
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 18 deletions.
25 changes: 8 additions & 17 deletions src/Driver/DC_Table_Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ class DC_Table_Utils extends DC_Table
*/
public function __construct($strTable, $arrModule = [])
{
parent::__construct($strTable, $arrModule);

DataContainer::__construct();

/** @var SessionInterface $objSession */
$objSession = \System::getContainer()->get('session');
$objSession = System::getContainer()->get('session');

// Check the request token (see #4007)
if (isset($_GET['act'])) {
Expand Down Expand Up @@ -103,26 +105,16 @@ public function __construct($strTable, $arrModule = [])

// Unless there are any root records specified, use all records with parent ID 0
if (!isset($GLOBALS['TL_DCA'][$table]['list']['sorting']['root']) || $GLOBALS['TL_DCA'][$table]['list']['sorting']['root'] === false) {
$objIds = $this->Database->prepare(
'SELECT id FROM '.$table.' WHERE pid=?'.($this->Database->fieldExists('sorting', $table) ? ' ORDER BY sorting' : '')
)->execute(0);
$objIds = $this->Database->prepare('SELECT id FROM '.$table.' WHERE pid=?'.($this->Database->fieldExists('sorting', $table) ? ' ORDER BY sorting' : ''))->execute(0);

if ($objIds->numRows > 0) {
$this->root = $objIds->fetchEach('id');
}
}

// Get root records from global configuration file
} // Get root records from global configuration file
elseif (\is_array($GLOBALS['TL_DCA'][$table]['list']['sorting']['root'])) {
$this->root = $this->eliminateNestedPages(
$GLOBALS['TL_DCA'][$table]['list']['sorting']['root'],
$table,
$this->Database->fieldExists('sorting', $table)
);
$this->root = $this->eliminateNestedPages($GLOBALS['TL_DCA'][$table]['list']['sorting']['root'], $table, $this->Database->fieldExists('sorting', $table));
}
}

// Get the IDs of all root records (list view or parent view)
} // Get the IDs of all root records (list view or parent view)
elseif (\is_array($GLOBALS['TL_DCA'][$this->strTable]['list']['sorting']['root'])) {
$this->root = array_unique($GLOBALS['TL_DCA'][$this->strTable]['list']['sorting']['root']);
}
Expand All @@ -132,8 +124,7 @@ public function __construct($strTable, $arrModule = [])

// Store the current referer
if (!empty($this->ctable) && !\Input::get('act') && !\Input::get('key') && !\Input::get('token') && 'contao_backend' == $route
&& !\Environment::get('isAjaxRequest')
) {
&& !\Environment::get('isAjaxRequest')) {
$strKey = \Input::get('popup') ? 'popupReferer' : 'referer';
$strRefererId = \System::getContainer()->get('request_stack')->getCurrentRequest()->attributes->get('_contao_referer_id');

Expand Down
129 changes: 128 additions & 1 deletion tests/Driver/DC_Table_UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,133 @@

namespace HeimrichHannot\UtilsBundle\Tests\Driver;

class DC_Table_UtilsTest
use Contao\Model;
use Contao\System;
use HeimrichHannot\UtilsBundle\Driver\DC_Table_Utils;
use HeimrichHannot\UtilsBundle\Model\CfgTagModel;
use HeimrichHannot\UtilsBundle\Tests\TestCaseEnvironment;

class DC_Table_UtilsTest extends TestCaseEnvironment
{
public function setUp()
{
parent::setUp();

$container = System::getContainer();

$adapter = $this->mockAdapter(['getParams']);
$adapter->method('getParams')->willReturn([]);
$container->set('doctrine.dbal.default_connection', $adapter);

$modelUtilsAdapter = $this->mockAdapter(['findModelInstanceByPk']);
$modelUtilsAdapter->method('findModelInstanceByPk')->willReturn($this->createMock(Model::class));
$container->set('huh.utils.model', $modelUtilsAdapter);

System::setContainer($container);

if (!interface_exists('listable')) {
include_once __DIR__.'/../../vendor/contao/core-bundle/src/Resources/contao/helper/interface.php';
}
}

public function testInstantiation()
{
$this->createGlobalDca('table');
$dcTableUtils = new DC_Table_Utils('table');
$this->assertInstanceOf(DC_Table_Utils::class, $dcTableUtils);
}

public function testCreateFromModel()
{
$result = DC_Table_Utils::createFromModel($this->getModel());
$this->assertInstanceOf(DC_Table_Utils::class, $result);
}

public function testCreateFromModelData()
{
$result = DC_Table_Utils::createFromModelData(['id' => 12], 'table', 'field');
$this->assertInstanceOf(DC_Table_Utils::class, $result);
}

/**
* @return Model | \PHPUnit_Framework_MockObject_MockObject
*/
public function getModel()
{
$this->createGlobalDca('tl_cfg_tag');
$model = new CfgTagModel();

return $model;
}

public function createGlobalDca($table)
{
$GLOBALS['TL_DCA'][$table] = [
'config' => [
'dataContainer' => 'Table',
'ptable' => 'ptable',
'ctable' => ['tl_content', 'ctable'],
'enableVersioning' => true,
'onsubmit_callback' => [],
'oncopy_callback' => [],
'onload_callback' => [],
'sql' => [
'keys' => [
'id' => 'primary',
],
],
],
'list' => [
'label' => [
'fields' => ['title'],
'format' => '%s',
],
'sorting' => [
'mode' => 1,
'fields' => ['title'],
'headerFields' => ['title'],
'panelLayout' => 'filter;sort,search,limit',
'root' => [],
],
'global_operations' => [
'all' => [
'label' => &$GLOBALS['TL_LANG']['MSC']['all'],
'href' => 'act=select',
'class' => 'header_edit_all',
'attributes' => 'onclick="Backend.getScrollOffset();"',
],
],
'operations' => [
'edit' => [
'label' => &$GLOBALS['TL_LANG']['table']['edit'],
'href' => 'table=tl_content&ptable=table',
'icon' => 'edit.gif',
],
],
],
'palettes' => [
'default' => '{general_legend},title;',
],

'subpalettes' => [],
'fields' => [
'id' => [
'sql' => 'int(10) unsigned NOT NULL auto_increment',
],
'pid' => [
'foreignKey' => 'ptable.id',
'sql' => "int(10) unsigned NOT NULL default '0'",
'relation' => ['type' => 'belongsTo', 'load' => 'eager'],
],
'title' => [
'label' => &$GLOBALS['TL_LANG']['table']['title'],
'exclude' => true,
'search' => true,
'inputType' => 'text',
'eval' => ['maxlength' => 255, 'tl_class' => 'w50', 'mandatory' => true],
'sql' => "varchar(255) NOT NULL default ''",
],
],
];
}
}
28 changes: 28 additions & 0 deletions tests/TestCaseEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@

use Contao\System;
use Contao\TestCase\ContaoTestCase;
use Doctrine\DBAL\Connection;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpKernel\Log\Logger;
use Symfony\Component\Routing\RouterInterface;

abstract class TestCaseEnvironment extends ContaoTestCase
{
Expand All @@ -38,7 +42,11 @@ public function setUp()
$container->set('request_stack', $this->createRequestStackMock());
$container->setParameter('contao.resources_paths', [__DIR__.'/../vendor/contao/core-bundle/src/Resources/contao']);
$logger = new Logger();
$container->set('contao.framework', $this->mockContaoFramework());
$container->set('monolog.logger.contao', $logger);
$container->set('session', new Session(new MockArraySessionStorage()));
$container->set('router', $this->createRouterMock());
$container->set('database_connection', $this->createMock(Connection::class));
System::setContainer($container);
}

Expand All @@ -51,4 +59,24 @@ public function createRequestStackMock()

return $requestStack;
}

public function createRouterMock()
{
$router = $this->createMock(RouterInterface::class);
$router->method('generate')->with('contao_backend', $this->anything())->will($this->returnCallback(function ($route, $params = []) {
$url = '/contao';
if (!empty($params)) {
$count = 0;
foreach ($params as $key => $value) {
$url .= (0 === $count ? '?' : '&');
$url .= $key.'='.$value;
++$count;
}
}

return $url;
}));

return $router;
}
}

0 comments on commit 554537a

Please sign in to comment.