Skip to content

Commit

Permalink
Add test for wizardCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
bytehead committed Jan 11, 2022
1 parent 126ed6a commit 64833ff
Showing 1 changed file with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,24 @@

use Contao\CoreBundle\EventListener\DataContainer\RootPageDependentSelectListener;
use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\DataContainer;
use Contao\System;
use Contao\TestCase\ContaoTestCase;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Statement;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class RootPageDependentSelectListenerTest extends ContaoTestCase
{
private ContainerBuilder $container;

public function testReturnsIfTheRequestIsNotABackendRequest(): void
{
$request = new Request([], [], ['_scope' => 'frontend']);
Expand Down Expand Up @@ -183,6 +189,68 @@ public function testAddRootPagesToFieldConfig(): void
$this->unsetGlobalsArray();
}

public function testDoesNotAddWizardWhenNoValuesSet(): void
{
$listener = new RootPageDependentSelectListener(
$this->createMock(Connection::class),
$this->createMock(TranslatorInterface::class),
$this->createMock(ScopeMatcher::class),
$this->createMock(RequestStack::class),
$this->createMock(CsrfTokenManagerInterface::class),
'contao_csrf_token'
);

$dataContainer = $this->mockClassWithProperties(DataContainer::class);
$dataContainer->value = serialize([]);

$this->assertSame('', $listener->wizardCallback($dataContainer));
}

public function testAddWizardToSelectWhenModuleIsSet(): void
{
$translator = $this->createMock(TranslatorInterface::class);
$translator
->expects($this->exactly(3))
->method('trans')
;

$token = $this->createMock(CsrfToken::class);
$token
->expects($this->exactly(3))
->method('getValue')
;

$csrfTokenManager = $this->createMock(CsrfTokenManagerInterface::class);
$csrfTokenManager
->expects($this->exactly(3))
->method('getToken')
->willReturn($token)
;


$this->container = $this->getContainerWithContaoConfiguration('/directory/project');
System::setContainer($this->container);

$listener = new RootPageDependentSelectListener(
$this->createMock(Connection::class),
$translator,
$this->createMock(ScopeMatcher::class),
$this->createMock(RequestStack::class),
$csrfTokenManager,
'contao_csrf_token'
);

$dataContainer = $this->mockClassWithProperties(DataContainer::class);
$dataContainer->value = serialize([
'1' => '10',
'2' => '20',
'3' => '30',
'4' => '',
]);

$this->assertCount(3, unserialize($listener->wizardCallback($dataContainer)));
}

private function populateGlobalsArray(array $data): void
{
$GLOBALS['TL_DCA']['tl_module']['fields'] = $data;
Expand Down

0 comments on commit 64833ff

Please sign in to comment.