Skip to content

Commit

Permalink
Fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Jul 6, 2020
1 parent fc680b7 commit 534ccdd
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 9 deletions.
3 changes: 2 additions & 1 deletion core-bundle/src/DataCollector/ContaoDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ public function getAdditionalData(): array
$data['classes_set'],
$data['classes_aliased'],
$data['classes_composerized'],
$data['database_queries']
$data['database_queries'],
$data['legacy_routing']
);

return $data;
Expand Down
6 changes: 6 additions & 0 deletions core-bundle/src/Routing/Candidates/LegacyCandidates.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class LegacyCandidates extends Candidates

public function __construct(bool $prependLocale, string $urlSuffix)
{
// Do not call the parent constructor

$this->prependLocale = $prependLocale;
$this->urlSuffix = $urlSuffix;
}
Expand All @@ -55,6 +57,10 @@ public function getCandidates(Request $request)

private function removeSuffixAndLanguage(string $pathInfo): ?string
{
if ('' === $pathInfo) {
return '';
}

$suffixLength = \strlen($this->urlSuffix);

if (0 !== $suffixLength) {
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/src/Routing/Enhancer/InputEnhancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function enhance(array $defaults, Request $request): array
/** @var Input $input */
$input = $this->framework->getAdapter(Input::class);

if ('' !== $page->urlPrefix) {
if (!empty($page->urlPrefix)) {
$input->setGet('language', $page->rootLanguage);
}

Expand Down
1 change: 1 addition & 0 deletions core-bundle/tests/Asset/ContaoContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ private function getPageWithDetails(): PageModel

$container = $this->getContainerWithContaoConfiguration();
$container->set('contao.resource_finder', $finder);
$container->set('request_stack', new RequestStack());

System::setContainer($container);

Expand Down
99 changes: 96 additions & 3 deletions core-bundle/tests/DataCollector/ContaoDataCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
use Contao\ContentImage;
use Contao\ContentText;
use Contao\CoreBundle\DataCollector\ContaoDataCollector;
use Contao\CoreBundle\Tests\Fixtures\DataCollector\BundleTestClass;
use Contao\CoreBundle\Tests\Fixtures\DataCollector\TestClass;
use Contao\CoreBundle\Tests\TestCase;
use Contao\CoreBundle\Util\PackageUtil;
use Contao\LayoutModel;
use Contao\PageModel;
use Contao\System;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -35,7 +39,7 @@ public function testCollectsDataInBackEnd(): void
'additional_data' => 'data',
];

$collector = new ContaoDataCollector();
$collector = new ContaoDataCollector($this->createParameterBag());
$collector->collect(new Request(), new Response());

$this->assertSame(['ContentText' => ContentText::class], $collector->getClassesAliased());
Expand All @@ -52,6 +56,7 @@ public function testCollectsDataInBackEnd(): void
'preview' => false,
'layout' => '',
'template' => '',
'legacy_routing' => false,
],
$collector->getSummary()
);
Expand Down Expand Up @@ -81,7 +86,7 @@ public function testCollectsDataInFrontEnd(): void

$GLOBALS['objPage'] = $page;

$collector = new ContaoDataCollector();
$collector = new ContaoDataCollector($this->createParameterBag());
$collector->setFramework($framework);
$collector->collect(new Request(), new Response());

Expand All @@ -94,6 +99,7 @@ public function testCollectsDataInFrontEnd(): void
'preview' => false,
'layout' => 'Default (ID 2)',
'template' => 'fe_page',
'legacy_routing' => false,
],
$collector->getSummary()
);
Expand All @@ -105,13 +111,100 @@ public function testCollectsDataInFrontEnd(): void
unset($GLOBALS['objPage']);
}

public function testStoresTheLegacyRoutingData(bool $legacyRouting = false, bool $prependLocale = false, string $urlSuffix = '.html'): void
{
$collector = new ContaoDataCollector($this->createParameterBag($legacyRouting, $prependLocale, $urlSuffix));
$collector->collect(new Request(), new Response());

$this->assertSame(
[
'enabled' => $legacyRouting,
'prepend_locale' => $prependLocale,
'url_suffix' => $urlSuffix,
'hooks' => [],
],
$collector->getLegacyRouting()
);
}

public function legacyRoutingProvider(): \Generator
{
yield [false, false, '.html'];

yield [true, false, '.html'];

yield [true, false, '.html'];

yield [true, true, '.html'];

yield [true, true, '.php'];
}

public function testIncludesTheLegacyRoutingHooks()
{
$GLOBALS['TL_HOOKS'] = [
'getPageIdFromUrl' => [
[TestClass::class, 'onGetPageIdFromUrl'],
['foo.service', 'onGetPageIdFromUrl'],
],
'getRootPageFromUrl' => [
[TestClass::class, 'onGetRootPageFromUrl'],
['bar.service', 'onGetRootPageFromUrl'],
],
];

require_once(__DIR__.'/../Fixtures/DataCollector/TestClass.php');
require_once(__DIR__.'/../Fixtures/DataCollector/vendor/foo/bar/BundleTestClass.php');

$systemAdapter = $this->mockAdapter(['importStatic']);
$systemAdapter
->expects($this->exactly(4))
->method('importStatic')
->withConsecutive([TestClass::class], ['foo.service'], [TestClass::class], ['bar.service'])
->willReturnOnConsecutiveCalls(new TestClass(), new BundleTestClass(), new TestClass(), new BundleTestClass())
;

$framework = $this->mockContaoFramework([
System::class => $systemAdapter,
]);

$parameterBag = $this->createParameterBag(true);
$parameterBag->set('kernel.project_dir', \dirname(__DIR__).'/Fixtures/DataCollector');

$collector = new ContaoDataCollector($parameterBag);
$collector->setFramework($framework);
$collector->collect(new Request(), new Response());

$this->assertSame(
[
['name' => 'getPageIdFromUrl', 'class' => TestClass::class, 'method' => 'onGetPageIdFromUrl', 'package' => ''],
['name' => 'getPageIdFromUrl', 'class' => BundleTestClass::class, 'method' => 'onGetPageIdFromUrl', 'package' => 'foo/bar'],
['name' => 'getRootPageFromUrl', 'class' => TestClass::class, 'method' => 'onGetRootPageFromUrl', 'package' => ''],
['name' => 'getRootPageFromUrl', 'class' => BundleTestClass::class, 'method' => 'onGetRootPageFromUrl', 'package' => 'foo/bar'],
],
$collector->getLegacyRouting()['hooks']
);
}

public function testReturnsAnEmptyArrayIfTheKeyIsUnknown(): void
{
$collector = new ContaoDataCollector();
$collector = new ContaoDataCollector($this->createParameterBag());

$method = new \ReflectionMethod($collector, 'getData');
$method->setAccessible(true);

$this->assertSame([], $method->invokeArgs($collector, ['foo']));
}

/**
* @return ParameterBagInterface
*/
private function createParameterBag(bool $legacyRouting = false, bool $prependLocale = false, string $urlSuffix = '.html'): ParameterBagInterface
{
return new ParameterBag([
'contao.legacy_routing' => $legacyRouting,
'contao.prepend_locale' => $prependLocale,
'contao.url_suffix' => $urlSuffix,
]);
}
}
25 changes: 25 additions & 0 deletions core-bundle/tests/DependencyInjection/ContaoCoreExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3631,4 +3631,29 @@ public function testRegistersTheImageTargetPath(): void

$this->assertSame($this->getTempDir().'/my/custom/dir', $container->getParameter('contao.image.target_dir'));
}

private function getContainerBuilder(array $params = null): ContainerBuilder
{
$container = new ContainerBuilder(
new ParameterBag([
'kernel.debug' => false,
'kernel.project_dir' => $this->getTempDir(),
'kernel.default_locale' => 'en',
])
);

if (null === $params) {
$params = [
'contao' => [
'encryption_key' => 'foobar',
'localconfig' => ['foo' => 'bar'],
],
];
}

$extension = new ContaoCoreExtension();
$extension->load($params, $container);

return $container;
}
}
16 changes: 16 additions & 0 deletions core-bundle/tests/Fixtures/DataCollector/TestClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Contao\CoreBundle\Tests\Fixtures\DataCollector;

class TestClass
{
public function onGetPageIdFromUrl()
{

}

public function onGetRootPageFromUrl()
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Contao\CoreBundle\Tests\Fixtures\DataCollector;

class BundleTestClass
{
public function onGetPageIdFromUrl()
{

}

public function onGetRootPageFromUrl()
{

}
}
9 changes: 5 additions & 4 deletions core-bundle/tests/Routing/Candidates/CandidatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Contao\CoreBundle\Tests\Routing\Candidates;

use Contao\CoreBundle\Routing\Candidates\Candidates;
use Contao\CoreBundle\Routing\Candidates\LegacyCandidates;
use Contao\CoreBundle\Routing\Content\PageProviderInterface;
use Contao\CoreBundle\Tests\TestCase;
use Doctrine\DBAL\Connection;
Expand All @@ -39,7 +40,7 @@ public function testGetCandidates(string $pathInfo, array $urlSuffixes, array $l
$connection = $this->mockConnectionWithLanguages($languages);
$providers = $this->mockPageProvidersWithUrlSuffix($urlSuffixes);

$candidates = (new Candidates($connection, $providers, false, '.html', false))->getCandidates($request);
$candidates = (new Candidates($connection, $providers))->getCandidates($request);

$this->assertSame($expected, $candidates);
}
Expand Down Expand Up @@ -68,7 +69,7 @@ public function testGetCandidatesInLegacyMode(string $pathInfo, array $urlSuffix
->method($this->anything())
;

$candidates = (new Candidates($connection, $providers, true, $urlSuffixes[0] ?? '', 0 !== \count($languages)))->getCandidates($request);
$candidates = (new LegacyCandidates(0 !== \count($languages), $urlSuffixes[0] ?? ''))->getCandidates($request);

$this->assertSame($expected, $candidates);
}
Expand All @@ -88,15 +89,15 @@ public function getCandidatesProvider()
['.html'],
[],
['index'],
[],
['index'],
];

yield [
'/',
[],
['en'],
['index'],
[],
['index'],
];

yield [
Expand Down
24 changes: 24 additions & 0 deletions core-bundle/tests/Routing/RouteProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,28 @@ public function getRootRoutes(): \Generator
0 => $this->createRootPage('de', 'german-root', false),
];

$routeNames = [
'tl_page.'.$pages[0]->id.'.root',
'tl_page.'.$pages[1]->id.'.root',
'tl_page.'.$pages[2]->id.'.root',
];

yield [
$pages,
['de', 'en'],
$routeNames,
];

$pages = [
2 => $this->createRootPage('en', 'english-root', true),
1 => $this->createPage('en', 'index', true),
0 => $this->createRootPage('de', 'german-root', false),
];

$pages[0]->urlPrefix = 'en';
$pages[1]->urlPrefix = 'en';
$pages[2]->urlPrefix = 'de';

$routeNames = [
'tl_page.'.$pages[0]->id.'.root',
'tl_page.'.$pages[1]->id.'.root',
Expand Down Expand Up @@ -671,6 +693,8 @@ private function createRootPage(string $language, string $alias, bool $fallback
$page->type = 'root';
$page->alias = $alias;
$page->domain = $domain;
$page->urlPrefix = '';
$page->urlSuffix = '.html';
$page->rootLanguage = $language;
$page->rootIsFallback = $fallback;
$page->rootUseSSL = 'https' === $scheme;
Expand Down

0 comments on commit 534ccdd

Please sign in to comment.