Skip to content

Commit

Permalink
[BUGFIX] Handle author rss links with detail pages
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminkott committed May 17, 2023
1 parent 86037cf commit 9466f9c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 33 deletions.
1 change: 1 addition & 0 deletions Build/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ parameters:

ignoreErrors:
- "#Casting to string something that's already string.#"
- "#^Call to an undefined method TYPO3Fluid\\\\Fluid\\\\Core\\\\Rendering\\\\RenderingContextInterface\\:\\:getRequest\\(\\)\\.$#"

paths:
- %currentWorkingDirectory%/Classes/
Expand Down
40 changes: 8 additions & 32 deletions Classes/ViewHelpers/Link/AuthorViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use T3G\AgencyPack\Blog\Domain\Model\Author;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;

class AuthorViewHelper extends AbstractTagBasedViewHelper
Expand All @@ -36,34 +37,24 @@ public function initializeArguments(): void

public function render(): string
{
$rssFormat = (bool)$this->arguments['rss'];
/** @var Author $author */
$author = $this->arguments['author'];
$rssFormat = (bool)$this->arguments['rss'];

if ((int)$author->getDetailsPage() > 0) {
if ((int)$author->getDetailsPage() > 0 && !$rssFormat) {
return $this->buildUriFromDetailsPage($author, $rssFormat);
}

return $this->buildUriFromDefaultPage($author, $rssFormat);
}

/**
* @param Author $author
* @param bool $rssFormat
* @return mixed|string
*/
protected function buildUriFromDetailsPage(Author $author, bool $rssFormat)
protected function buildUriFromDetailsPage(Author $author, bool $rssFormat): string
{
$uriBuilder = $this->getUriBuilder($author->getDetailsPage(), [], $rssFormat);
$uriBuilder = $this->getUriBuilder((int) $author->getDetailsPage(), [], $rssFormat);
return $this->buildAnchorTag($uriBuilder->build(), $author);
}

/**
* @param Author $author
* @param bool $rssFormat
* @return mixed|string
*/
protected function buildUriFromDefaultPage(Author $author, bool $rssFormat)
protected function buildUriFromDefaultPage(Author $author, bool $rssFormat): string
{
$uriBuilder = $this->getUriBuilder((int)$this->getTypoScriptFrontendController()->tmpl->setup['plugin.']['tx_blog.']['settings.']['authorUid'], [], $rssFormat);
$arguments = [
Expand All @@ -72,15 +63,8 @@ protected function buildUriFromDefaultPage(Author $author, bool $rssFormat)
return $this->buildAnchorTag($uriBuilder->uriFor('listPostsByAuthor', $arguments, 'Post', 'Blog', 'AuthorPosts'), $author);
}

/**
* @param int $pageUid
* @param array $additionalParams
* @param bool $rssFormat
* @return UriBuilder
*/
protected function getUriBuilder(int $pageUid, array $additionalParams, bool $rssFormat): UriBuilder
{
/** @var UriBuilder $uriBuilder */
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
$uriBuilder->reset()
->setRequest($this->renderingContext->getRequest())
Expand All @@ -94,12 +78,7 @@ protected function getUriBuilder(int $pageUid, array $additionalParams, bool $rs
return $uriBuilder;
}

/**
* @param string $uri
* @param Author $author
* @return mixed|string
*/
protected function buildAnchorTag(string $uri, Author $author)
protected function buildAnchorTag(string $uri, Author $author): string
{
if ($uri !== '') {
$linkText = $this->renderChildren() ?? $author->getName();
Expand All @@ -111,10 +90,7 @@ protected function buildAnchorTag(string $uri, Author $author)
return $this->renderChildren();
}

/**
* @return mixed|\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
*/
protected function getTypoScriptFrontendController()
protected function getTypoScriptFrontendController(): TypoScriptFrontendController
{
return $GLOBALS['TSFE'];
}
Expand Down
76 changes: 75 additions & 1 deletion Tests/Functional/ViewHelpers/Link/AuthorViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function render(string $template, string $expected): void
{
$this->createTestSite();

(new ConnectionPool())->getConnectionForTable('pages')->insert(
(new ConnectionPool())->getConnectionForTable('tx_blog_domain_model_author')->insert(
'tx_blog_domain_model_author',
[
'uid' => 100,
Expand Down Expand Up @@ -77,4 +77,78 @@ public static function renderDataProvider(): array
],
];
}

/**
* @test
* @dataProvider renderDetailPageDataProvider
*/
public function renderDetailPage(string $template, string $expected): void
{
$this->createTestSite();

(new ConnectionPool())->getConnectionForTable('pages')->insert(
'pages',
[
'uid' => 100,
'pid' => 1,
'doktype' => 1,
'title' => 'Detail Page TYPO3 Inc Team',
'slug' => '/detail-typo3-inc-team'
]
);

(new ConnectionPool())->getConnectionForTable('tx_blog_domain_model_author')->insert(
'tx_blog_domain_model_author',
[
'uid' => 100,
'pid' => self::STORAGE_UID,
'name' => 'TYPO3 Inc Team',
'slug' => 'typo3-inc-team',
'details_page' => 100,
]
);

$instructions = [
[
'type' => 'author',
'uid' => 100,
'as' => 'author',
]
];

self::assertSame(
$expected,
$this->renderFluidTemplateInTestSite($template, $instructions)
);
}

public static function renderDetailPageDataProvider(): array
{
return [
'simple' => [
'<blogvh:link.author author="{test.author}" />',
'<a href="/detail-typo3-inc-team">TYPO3 Inc Team</a>',
],
'target' => [
'<blogvh:link.author author="{test.author}" target="_blank" />',
'<a target="_blank" href="/detail-typo3-inc-team">TYPO3 Inc Team</a>',
],
'rel' => [
'<blogvh:link.author author="{test.author}" rel="noreferrer" />',
'<a rel="noreferrer" href="/detail-typo3-inc-team">TYPO3 Inc Team</a>',
],
'rss' => [
'<blogvh:link.author author="{test.author}" rss="true" />',
'<a href="/author/author/typo3-inc-team/blog.author.xml">TYPO3 Inc Team</a>',
],
'content' => [
'<blogvh:link.author author="{test.author}">Hello</blogvh:link.author>',
'<a href="/detail-typo3-inc-team">Hello</a>',
],
'class' => [
'<blogvh:link.author author="{test.author}" class="class" />',
'<a class="class" href="/detail-typo3-inc-team">TYPO3 Inc Team</a>',
],
];
}
}

0 comments on commit 9466f9c

Please sign in to comment.