Skip to content

Commit

Permalink
Merge branch '5.3' into 5.x
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
#	calendar-bundle/contao/languages/it/tl_module.xlf
#	core-bundle/config/listener.yaml
#	core-bundle/contao/languages/it/default.xlf
#	core-bundle/contao/languages/it/tl_article.xlf
#	core-bundle/contao/languages/it/tl_content.xlf
#	core-bundle/contao/languages/it/tl_maintenance.xlf
#	core-bundle/contao/languages/it/tl_member.xlf
#	core-bundle/contao/languages/it/tl_module.xlf
#	core-bundle/contao/languages/it/tl_page.xlf
#	core-bundle/contao/languages/it/tl_theme.xlf
#	core-bundle/contao/languages/it/tl_user.xlf
#	core-bundle/contao/languages/sl/default.xlf
#	core-bundle/contao/languages/sl/exception.xlf
#	core-bundle/contao/languages/sl/explain.xlf
#	core-bundle/contao/languages/sl/modules.xlf
#	core-bundle/contao/languages/sl/tl_article.xlf
#	core-bundle/contao/languages/sl/tl_content.xlf
#	core-bundle/contao/languages/sl/tl_files.xlf
#	core-bundle/contao/languages/sl/tl_image_size.xlf
#	core-bundle/contao/languages/sl/tl_log.xlf
#	core-bundle/contao/languages/sl/tl_maintenance.xlf
#	core-bundle/contao/languages/sl/tl_member.xlf
#	core-bundle/contao/languages/sl/tl_module.xlf
#	core-bundle/contao/languages/sl/tl_page.xlf
#	core-bundle/contao/languages/sl/tl_settings.xlf
#	core-bundle/contao/languages/sl/tl_theme.xlf
#	core-bundle/contao/languages/sl/tl_user.xlf
#	news-bundle/contao/languages/it/tl_module.xlf
  • Loading branch information
leofeyer committed May 7, 2024
2 parents db87fd8 + 2bea527 commit 9beb068
Show file tree
Hide file tree
Showing 88 changed files with 1,248 additions and 852 deletions.
12 changes: 6 additions & 6 deletions calendar-bundle/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ services:
arguments:
- '@contao.framework'

contao_calendar.insert_tag.event:
class: Contao\CalendarBundle\InsertTag\EventInsertTag
arguments:
- '@contao.framework'
- '@contao.routing.content_url_generator'

contao_calendar.listener.data_container.start_stop_validation:
class: Contao\CoreBundle\EventListener\DataContainer\StartStopValidationListener
arguments:
Expand All @@ -24,12 +30,6 @@ services:
arguments:
- '@contao.framework'

contao_calendar.listener.insert_tags:
class: Contao\CalendarBundle\EventListener\InsertTagsListener
arguments:
- '@contao.framework'
- '@contao.routing.content_url_generator'

contao_calendar.listener.preview_url_convert:
class: Contao\CalendarBundle\EventListener\PreviewUrlConvertListener
arguments:
Expand Down
2 changes: 1 addition & 1 deletion calendar-bundle/contao/classes/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ private function getPageWithDetails($intPageId)
/**
* Creates a sub request for the given URI.
*/
private function createSubRequest(string $uri, Request $request = null): Request
private function createSubRequest(string $uri, Request|null $request = null): Request
{
$cookies = null !== $request ? $request->cookies->all() : array();
$server = null !== $request ? $request->server->all() : array();
Expand Down
102 changes: 0 additions & 102 deletions calendar-bundle/src/EventListener/InsertTagsListener.php

This file was deleted.

116 changes: 116 additions & 0 deletions calendar-bundle/src/InsertTag/EventInsertTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\CalendarBundle\InsertTag;

use Contao\CalendarEventsModel;
use Contao\CalendarFeedModel;
use Contao\CoreBundle\DependencyInjection\Attribute\AsInsertTag;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\InsertTag\Exception\InvalidInsertTagException;
use Contao\CoreBundle\InsertTag\InsertTagResult;
use Contao\CoreBundle\InsertTag\OutputType;
use Contao\CoreBundle\InsertTag\ResolvedInsertTag;
use Contao\CoreBundle\InsertTag\Resolver\InsertTagResolverNestedResolvedInterface;
use Contao\CoreBundle\Routing\ContentUrlGenerator;
use Contao\StringUtil;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
* @internal
*/
#[AsInsertTag('event')]
#[AsInsertTag('event_open')]
#[AsInsertTag('event_url')]
#[AsInsertTag('event_title')]
#[AsInsertTag('event_teaser')]
#[AsInsertTag('calendar_feed')]
class EventInsertTag implements InsertTagResolverNestedResolvedInterface
{
public function __construct(
private readonly ContaoFramework $framework,
private readonly ContentUrlGenerator $urlGenerator,
) {
}

public function __invoke(ResolvedInsertTag $insertTag): InsertTagResult
{
if ('calendar_feed' === $insertTag->getName()) {
return $this->replaceCalendarFeedInsertTag($insertTag->getParameters()->get(0));
}

return $this->replaceEventInsertTag(
$insertTag->getName(),
$insertTag->getParameters()->get(0),
\array_slice($insertTag->getParameters()->all(), 1),
);
}

private function replaceCalendarFeedInsertTag(string $feedId): InsertTagResult
{
$this->framework->initialize();

$adapter = $this->framework->getAdapter(CalendarFeedModel::class);

if (!$feed = $adapter->findById($feedId)) {
return new InsertTagResult('');
}

return new InsertTagResult(sprintf('%sshare/%s.xml', $feed->feedBase, $feed->alias), OutputType::url);
}

private function replaceEventInsertTag(string $insertTag, string $idOrAlias, array $arguments): InsertTagResult
{
$this->framework->initialize();

$adapter = $this->framework->getAdapter(CalendarEventsModel::class);

if (!$model = $adapter->findByIdOrAlias($idOrAlias)) {
return new InsertTagResult('');
}

$generateUrl = fn () => $this->urlGenerator->generate(
$model,
[],
\in_array('absolute', $arguments, true) ? UrlGeneratorInterface::ABSOLUTE_URL : UrlGeneratorInterface::ABSOLUTE_PATH,
);

return match ($insertTag) {
'event' => new InsertTagResult(
sprintf(
'<a href="%s" title="%s"%s>%s</a>',
StringUtil::specialcharsAttribute($generateUrl()),
StringUtil::specialcharsAttribute($model->title),
\in_array('blank', $arguments, true) ? ' target="_blank" rel="noreferrer noopener"' : '',
$model->title,
),
OutputType::html,
),
'event_open' => new InsertTagResult(
sprintf(
'<a href="%s" title="%s"%s>',
StringUtil::specialcharsAttribute($generateUrl()),
StringUtil::specialcharsAttribute($model->title),
\in_array('blank', $arguments, true) ? ' target="_blank" rel="noreferrer noopener"' : '',
),
OutputType::html,
),
'event_url' => new InsertTagResult(
StringUtil::specialcharsAttribute($generateUrl()),
OutputType::url,
),
'event_title' => new InsertTagResult($model->title),
'event_teaser' => new InsertTagResult($model->teaser ?? '', OutputType::html),
default => throw new InvalidInsertTagException(),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@
* @license LGPL-3.0-or-later
*/

namespace Contao\CalendarBundle\Tests\EventListener;
namespace Contao\CalendarBundle\Tests\InsertTag;

use Contao\CalendarBundle\EventListener\InsertTagsListener;
use Contao\CalendarBundle\InsertTag\EventInsertTag;
use Contao\CalendarEventsModel;
use Contao\CalendarFeedModel;
use Contao\CoreBundle\InsertTag\InsertTagResult;
use Contao\CoreBundle\InsertTag\OutputType;
use Contao\CoreBundle\InsertTag\ResolvedInsertTag;
use Contao\CoreBundle\InsertTag\ResolvedParameters;
use Contao\CoreBundle\Routing\ContentUrlGenerator;
use Contao\TestCase\ContaoTestCase;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class InsertTagsListenerTest extends ContaoTestCase
class EventInsertTagTest extends ContaoTestCase
{
public function testReplacesTheCalendarFeedTag(): void
{
Expand All @@ -34,10 +38,10 @@ public function testReplacesTheCalendarFeedTag(): void
$framework = $this->mockContaoFramework($adapters);
$urlGenerator = $this->createMock(ContentUrlGenerator::class);

$listener = new InsertTagsListener($framework, $urlGenerator);
$url = $listener('calendar_feed::2', false, null, []);
$listener = new EventInsertTag($framework, $urlGenerator);
$url = $listener(new ResolvedInsertTag('calendar_feed', new ResolvedParameters(['2']), []));

$this->assertSame('http://localhost/share/events.xml', $url);
$this->assertEquals(new InsertTagResult('http://localhost/share/events.xml', OutputType::url), $url);
}

public function testReplacesTheEventTags(): void
Expand Down Expand Up @@ -80,77 +84,69 @@ public function testReplacesTheEventTags(): void
)
;

$listener = new InsertTagsListener($this->mockContaoFramework($adapters), $urlGenerator);
$listener = new EventInsertTag($this->mockContaoFramework($adapters), $urlGenerator);

$this->assertSame(
'<a href="events/the-foobar-event.html" title="The &quot;foobar&quot; event">The "foobar" event</a>',
$listener('event::2', false, null, []),
$listener(new ResolvedInsertTag('event', new ResolvedParameters(['2']), []))->getValue(),
);

$this->assertSame(
'<a href="events/the-foobar-event.html" title="The &quot;foobar&quot; event" target="_blank" rel="noreferrer noopener">The "foobar" event</a>',
$listener('event::2::blank', false, null, []),
$listener(new ResolvedInsertTag('event', new ResolvedParameters(['2', 'blank']), []))->getValue(),
);

$this->assertSame(
'<a href="events/the-foobar-event.html" title="The &quot;foobar&quot; event">',
$listener('event_open::2', false, null, []),
$listener(new ResolvedInsertTag('event_open', new ResolvedParameters(['2']), []))->getValue(),
);

$this->assertSame(
'<a href="events/the-foobar-event.html" title="The &quot;foobar&quot; event" target="_blank" rel="noreferrer noopener">',
$listener('event_open::2::blank', false, null, []),
$listener(new ResolvedInsertTag('event_open', new ResolvedParameters(['2', 'blank']), []))->getValue(),
);

$this->assertSame(
'<a href="http://domain.tld/events/the-foobar-event.html" title="The &quot;foobar&quot; event" target="_blank" rel="noreferrer noopener">',
$listener('event_open::2::blank::absolute', false, null, []),
$listener(new ResolvedInsertTag('event_open', new ResolvedParameters(['2', 'blank', 'absolute']), []))->getValue(),
);

$this->assertSame(
'<a href="http://domain.tld/events/the-foobar-event.html" title="The &quot;foobar&quot; event" target="_blank" rel="noreferrer noopener">',
$listener('event_open::2::absolute::blank', false, null, []),
$listener(new ResolvedInsertTag('event_open', new ResolvedParameters(['2', 'absolute', 'blank']), []))->getValue(),
);

$this->assertSame(
'events/the-foobar-event.html',
$listener('event_url::2', false, null, []),
$listener(new ResolvedInsertTag('event_url', new ResolvedParameters(['2']), []))->getValue(),
);

$this->assertSame(
'http://domain.tld/events/the-foobar-event.html',
$listener('event_url::2', false, null, ['absolute']),
$listener(new ResolvedInsertTag('event_url', new ResolvedParameters(['2', 'absolute']), []))->getValue(),
);

$this->assertSame(
'http://domain.tld/events/the-foobar-event.html',
$listener('event_url::2::absolute', false, null, []),
$listener(new ResolvedInsertTag('event_url', new ResolvedParameters(['2', 'absolute']), []))->getValue(),
);

$this->assertSame(
'http://domain.tld/events/the-foobar-event.html',
$listener('event_url::2::blank::absolute', false, null, []),
$listener(new ResolvedInsertTag('event_url', new ResolvedParameters(['2', 'blank', 'absolute']), []))->getValue(),
);

$this->assertSame(
'The &quot;foobar&quot; event',
$listener('event_title::2', false, null, []),
$this->assertEquals(
new InsertTagResult('The "foobar" event'),
$listener(new ResolvedInsertTag('event_title', new ResolvedParameters(['2']), [])),
);

$this->assertSame(
'<p>The annual foobar event.</p>',
$listener('event_teaser::2', false, null, []),
$listener(new ResolvedInsertTag('event_teaser', new ResolvedParameters(['2']), []))->getValue(),
);
}

public function testReturnsFalseIfTheTagIsUnknown(): void
{
$urlGenerator = $this->createMock(ContentUrlGenerator::class);
$listener = new InsertTagsListener($this->mockContaoFramework(), $urlGenerator);

$this->assertFalse($listener('link_url::2', false, null, []));
}

public function testReturnsAnEmptyStringIfThereIsNoModel(): void
{
$adapters = [
Expand All @@ -159,9 +155,9 @@ public function testReturnsAnEmptyStringIfThereIsNoModel(): void
];

$urlGenerator = $this->createMock(ContentUrlGenerator::class);
$listener = new InsertTagsListener($this->mockContaoFramework($adapters), $urlGenerator);
$listener = new EventInsertTag($this->mockContaoFramework($adapters), $urlGenerator);

$this->assertSame('', $listener('calendar_feed::3', false, null, []));
$this->assertSame('', $listener('event_url::3', false, null, []));
$this->assertSame('', $listener(new ResolvedInsertTag('calendar_feed', new ResolvedParameters(['3']), []))->getValue());
$this->assertSame('', $listener(new ResolvedInsertTag('event_url', new ResolvedParameters(['3']), []))->getValue());
}
}
Loading

0 comments on commit 9beb068

Please sign in to comment.