Skip to content

Commit

Permalink
[TASK] Extend tests for ViewHelper namespace inheritance (#994)
Browse files Browse the repository at this point in the history
  • Loading branch information
s2b authored Aug 26, 2024
1 parent 958f74a commit 9f02f8d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 10 deletions.
94 changes: 84 additions & 10 deletions tests/Functional/Core/Rendering/NamespaceInheritanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use TYPO3Fluid\Fluid\Core\Parser\UnknownNamespaceException;
use TYPO3Fluid\Fluid\Tests\Functional\AbstractFunctionalTestCase;
use TYPO3Fluid\Fluid\View\TemplateView;

Expand All @@ -22,36 +23,109 @@ public static function namespacesAreInheritedToLayoutAndPartialsDataProvider():
'namespace provided via php api' => [
'<f:layout name="NamespaceInheritanceLayout" /><f:section name="Main"><test:tagBasedTest location="Template" /></f:section>',
['test' => 'TYPO3Fluid\\Fluid\\Tests\\Functional\\Fixtures\\ViewHelpers'],
'',
[],
],
// @todo this should probably not work
'namespace provided via inline namespace declaration in template' => [
'namespace provided to layout and partials via inline namespace declaration in template' => [
'{namespace test=TYPO3Fluid\\Fluid\\Tests\\Functional\\Fixtures\\ViewHelpers}<f:layout name="NamespaceInheritanceLayout" /><f:section name="Main"><test:tagBasedTest location="Template" /></f:section>',
[],
'',
[],
],
// @todo this should probably not work
'namespace provided via xml namespace declaration in template' => [
'namespace provided to layout and partials via xml namespace declaration in template' => [
'<html xmlns:test="http://typo3.org/ns/TYPO3Fluid/Fluid/Tests/Functional/Fixtures/ViewHelpers"><f:layout name="NamespaceInheritanceLayout" /><f:section name="Main"><test:tagBasedTest location="Template" /></f:section>',
[],
'',
[],
],
// @todo this should probably not work
'namespace inherited from template to dynamic layout' => [
'<html xmlns:test="http://typo3.org/ns/TYPO3Fluid/Fluid/Tests/Functional/Fixtures/ViewHelpers"><f:layout name="{myLayout}" /><f:section name="Main"><test:tagBasedTest location="Template" /></f:section>',
[],
['myLayout' => 'NamespaceInheritanceLayout'],
],
];
}

#[Test]
#[DataProvider('namespacesAreInheritedToLayoutAndPartialsDataProvider')]
public function namespacesAreInheritedToLayoutAndPartials(string $source, array $predefinedNamespaces): void
public function namespacesAreInheritedToLayoutAndPartials(string $source, array $predefinedNamespaces, array $variables): void
{
$expectedResult = '<div location="Layout" />' . "\n\n" . '<div location="Template" />' . "\n\n" . '<div location="NestedPartial" />' . "\n\n" . '<div location="Partial" />' . "\n\n";

// Uncached
$view = new TemplateView();
$view->getRenderingContext()->setCache(self::$cache);
$view->getRenderingContext()->getViewHelperResolver()->addNamespaces($predefinedNamespaces);
$view->assignMultiple($variables);
$view->getRenderingContext()->getTemplatePaths()->setTemplateSource($source);
$view->getRenderingContext()->getTemplatePaths()->setLayoutRootPaths([__DIR__ . '/../../Fixtures/Layouts/']);
$view->getRenderingContext()->getTemplatePaths()->setPartialRootPaths([__DIR__ . '/../../Fixtures/Partials/']);
self::assertSame($expectedResult, $view->render());

// Cached
$view = new TemplateView();
$view->getRenderingContext()->setCache(self::$cache);
$view->getRenderingContext()->getViewHelperResolver()->addNamespaces($predefinedNamespaces);
$view->assignMultiple($variables);
$view->getRenderingContext()->getTemplatePaths()->setTemplateSource($source);
$view->getRenderingContext()->getTemplatePaths()->setLayoutRootPaths([__DIR__ . '/../../Fixtures/Layouts/']);
$view->getRenderingContext()->getTemplatePaths()->setPartialRootPaths([__DIR__ . '/../../Fixtures/Partials/']);
self::assertSame($expectedResult, $view->render());
}

public static function namespaceDefinedInParentNotValidInChildrenDataProvider(): array
{
return [
'namespace provided via namespace declaration in layout' => [
'<f:layout name="DefineNamespaceLayout" /><f:section name="Main"><test:tagBasedTest location="Template" /></f:section>',
[],
],
'namespace provided via namespace declaration in variable layout' => [
'<f:layout name="{myLayout}" /><f:section name="Main"><test:tagBasedTest location="Template" /></f:section>',
['myLayout' => 'DefineNamespaceLayout'],
],
];
}

#[Test]
#[DataProvider('namespaceDefinedInParentNotValidInChildrenDataProvider')]
public function namespaceDefinedInParentNotValidInChildren(string $source, array $variables): void
{
self::expectException(UnknownNamespaceException::class);
$view = new TemplateView();
$view->getRenderingContext()->setCache(self::$cache);
$view->getRenderingContext()->getTemplatePaths()->setTemplateSource($source);
$view->assignMultiple($variables);
$view->getRenderingContext()->getTemplatePaths()->setLayoutRootPaths([__DIR__ . '/../../Fixtures/Layouts/']);
$view->getRenderingContext()->getTemplatePaths()->setPartialRootPaths([__DIR__ . '/../../Fixtures/Partials/']);
$view->render();
}

#[Test]
#[DataProvider('namespaceDefinedInParentNotValidInChildrenDataProvider')]
public function namespaceDefinedInParentNotValidInChildrenInCachedTemplates(string $source, array $variables): void
{
self::expectException(UnknownNamespaceException::class);

// Uncached
try {
$view = new TemplateView();
$view->getRenderingContext()->setCache(self::$cache);
$view->getRenderingContext()->getTemplatePaths()->setTemplateSource($source);
$view->assignMultiple($variables);
$view->getRenderingContext()->getTemplatePaths()->setLayoutRootPaths([__DIR__ . '/../../Fixtures/Layouts/']);
$view->getRenderingContext()->getTemplatePaths()->setPartialRootPaths([__DIR__ . '/../../Fixtures/Partials/']);
$view->render();
} catch (UnknownNamespaceException) {
}

// Cached
$view = new TemplateView();
$view->getRenderingContext()->setCache(self::$cache);
$view->getRenderingContext()->getTemplatePaths()->setTemplateSource($source);
$view->assignMultiple($variables);
$view->getRenderingContext()->getTemplatePaths()->setLayoutRootPaths([__DIR__ . '/../../Fixtures/Layouts/']);
$view->getRenderingContext()->getTemplatePaths()->setPartialRootPaths([__DIR__ . '/../../Fixtures/Partials/']);
self::assertSame(
'<div location="Layout" />' . "\n\n" . '<div location="Template" />' . "\n\n" . '<div location="NestedPartial" />' . "\n\n" . '<div location="Partial" />' . "\n\n",
$view->render(),
);
$view->render();
}
}
6 changes: 6 additions & 0 deletions tests/Functional/Fixtures/Layouts/DefineNamespaceLayout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{namespace test=TYPO3Fluid\\Fluid\\Tests\\Functional\\Fixtures\\ViewHelpers}
<test:tagBasedTest location="Layout" />

<f:render section="Main" />

<f:render partial="NamespaceInheritancePartial" />

0 comments on commit 9f02f8d

Please sign in to comment.