Skip to content

Commit

Permalink
Test options callback
Browse files Browse the repository at this point in the history
  • Loading branch information
bytehead committed Jan 12, 2022
1 parent d960754 commit 5c9f0a5
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,32 +111,24 @@ public function optionsCallback(DataContainer $dc): array
{
$options = [];
$types = $GLOBALS['TL_DCA'][$dc->table]['fields'][$dc->field]['eval']['modules'] ?? [];
$hasTypes = \count($types) > 0;

$statement = $this->connection->executeQuery(
"SELECT m.id, m.name
$result = $this->connection->executeQuery(
"SELECT m.id, m.name, m.type
FROM tl_module m
WHERE m.type <> 'root_page_dependent_modules' AND
m.pid = ?
ORDER BY m.name",
[$dc->activeRecord->pid]
);

if (\count($types)) {
$statement = $this->connection->executeQuery(
"SELECT m.id, m.name
FROM tl_module m
WHERE m.type IN (?) AND
m.type <> 'root_page_dependent_modules' AND
m.pid = ?
ORDER BY m.name",
[$types, $dc->activeRecord->pid],
[Connection::PARAM_STR_ARRAY]
);
}

$modules = $statement->fetchAllAssociative();
$modules = $result->fetchAllAssociative();

foreach ($modules as $module) {
if ($hasTypes && !in_array($module['type'], $types, true)) {
continue;
}

$options[$module['id']] = $module['name'];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,7 @@ public function testAddRootPagesToFieldConfig(): void
$requestStack = new RequestStack();
$requestStack->push($request);

$connection = $this->mockGetRootPages([
['id' => 1, 'title' => 'title-1', 'language' => 'language-1'],
['id' => 2, 'title' => 'title-2', 'language' => 'language-2'],
['id' => 3, 'title' => 'title-3', 'language' => 'language-3'],
]);
$connection = $this->mockGetRootPages();

$listener = new RootPageDependentSelectListener(
$connection,
Expand Down Expand Up @@ -248,11 +244,7 @@ public function testSavesValuesRelatedToRootPage(): void
{
$dataContainer = $this->mockClassWithProperties(DataContainer::class);

$connection = $this->mockGetRootPages([
['id' => 1, 'title' => 'title-1', 'language' => 'language-1'],
['id' => 2, 'title' => 'title-2', 'language' => 'language-2'],
['id' => 3, 'title' => 'title-3', 'language' => 'language-3'],
]);
$connection = $this->mockGetRootPages();

$listener = new RootPageDependentSelectListener(
$connection,
Expand All @@ -273,6 +265,74 @@ public function testSavesValuesRelatedToRootPage(): void
);
}

public function testReturnsAllTypesOfModulesAsOption(): void
{
$this->populateGlobalsArray([]);

$dataContainer = $this->mockClassWithProperties(DataContainer::class);
$dataContainer->activeRecord = new \stdClass();
$dataContainer->activeRecord->pid = 1;
$dataContainer->table = 'tl_module';
$dataContainer->field = 'field';

$connection = $this->mockGetModules();

$listener = new RootPageDependentSelectListener(
$connection,
$this->createMock(TranslatorInterface::class),
$this->createMock(ScopeMatcher::class),
$this->createMock(RequestStack::class),
$this->createMock(CsrfTokenManagerInterface::class),
'contao_csrf_token'
);

$this->assertSame([
10 => 'name-10',
20 => 'name-20',
30 => 'name-30',
],
$listener->optionsCallback($dataContainer)
);
}

public function testReturnsSelectedTypesOfModulesAsOption(): void
{
$this->populateGlobalsArray([
'field' => [
'eval' => [
'modules' => [
'foo',
'bar',
]
]
]
]);

$dataContainer = $this->mockClassWithProperties(DataContainer::class);
$dataContainer->activeRecord = new \stdClass();
$dataContainer->activeRecord->pid = 1;
$dataContainer->table = 'tl_module';
$dataContainer->field = 'field';

$connection = $this->mockGetModules();

$listener = new RootPageDependentSelectListener(
$connection,
$this->createMock(TranslatorInterface::class),
$this->createMock(ScopeMatcher::class),
$this->createMock(RequestStack::class),
$this->createMock(CsrfTokenManagerInterface::class),
'contao_csrf_token'
);

$this->assertSame([
10 => 'name-10',
20 => 'name-20',
],
$listener->optionsCallback($dataContainer)
);
}

private function populateGlobalsArray(array $data): void
{
$GLOBALS['TL_DCA']['tl_module']['fields'] = $data;
Expand Down Expand Up @@ -304,13 +364,17 @@ private function mockScopeMatcher(bool $hasBackendUser, Request $request): Scope
return $scopeMatcher;
}

private function mockGetRootPages(array $data): Connection
private function mockGetRootPages(): Connection
{
$result = $this->createMock(Result::class);
$result
->expects($this->once())
->method('fetchAllAssociative')
->willReturn($data)
->willReturn([
['id' => 1, 'title' => 'title-1', 'language' => 'language-1'],
['id' => 2, 'title' => 'title-2', 'language' => 'language-2'],
['id' => 3, 'title' => 'title-3', 'language' => 'language-3'],
])
;

$statement = $this->createMock(Statement::class);
Expand All @@ -329,4 +393,27 @@ private function mockGetRootPages(array $data): Connection

return $connection;
}

private function mockGetModules(): Connection
{
$result = $this->createMock(Result::class);
$result
->expects($this->once())
->method('fetchAllAssociative')
->willReturn([
['id' => 10, 'name' => 'name-10', 'type' => 'foo'],
['id' => 20, 'name' => 'name-20', 'type' => 'bar'],
['id' => 30, 'name' => 'name-30', 'type' => 'baz'],
])
;

$connection = $this->createMock(Connection::class);
$connection
->expects($this->once())
->method('executeQuery')
->willReturn($result)
;

return $connection;
}
}

0 comments on commit 5c9f0a5

Please sign in to comment.