-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Plugin - Cron - Sitemap
- Loading branch information
Showing
6 changed files
with
205 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Composer | ||
/composer.lock | ||
/vendor/ | ||
|
||
# PhpUnit | ||
/.phpunit.result.cache | ||
/phpunit.xml | ||
|
||
# IDE | ||
/.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd" colors="true" bootstrap="vendor/autoload.php"> | ||
<coverage> | ||
<include> | ||
<directory>./src</directory> | ||
</include> | ||
</coverage> | ||
<php> | ||
<ini name="error_reporting" value="-1"/> | ||
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0"/> | ||
<env name="SYMFONY_PATCH_TYPE_DECLARATIONS" value="deprecations=0"/> | ||
</php> | ||
<testsuites> | ||
<testsuite name="contao-recommendation-bundle"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<listeners> | ||
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/> | ||
</listeners> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ContaoManager; | ||
|
||
use Contao\CoreBundle\ContaoCoreBundle; | ||
use Contao\ManagerPlugin\Bundle\Config\BundleConfig; | ||
use Contao\ManagerPlugin\Bundle\Parser\ParserInterface; | ||
use Oveleon\ContaoRecommendationBundle\ContaoManager\Plugin; | ||
use Oveleon\ContaoRecommendationBundle\ContaoRecommendationBundle; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PluginTest extends TestCase | ||
{ | ||
public function testReturnsTheBundles(): void | ||
{ | ||
$parser = $this->createMock(ParserInterface::class); | ||
|
||
/** @var BundleConfig $config */ | ||
$config = (new Plugin())->getBundles($parser)[0]; | ||
|
||
$this->assertInstanceOf(BundleConfig::class, $config); | ||
$this->assertSame(ContaoRecommendationBundle::class, $config->getName()); | ||
$this->assertSame([ContaoCoreBundle::class], $config->getLoadAfter()); | ||
$this->assertSame(['recommendation'], $config->getReplace()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cron; | ||
|
||
use Contao\Model\Collection; | ||
use Contao\TestCase\ContaoTestCase; | ||
use Oveleon\ContaoRecommendationBundle\Cron\PurgeRecommendationsCron; | ||
use Oveleon\ContaoRecommendationBundle\Model\RecommendationModel; | ||
|
||
class PurgeRecommendationsCronTest extends ContaoTestCase | ||
{ | ||
public function testDeletesUnactivatedRecommendations(): void | ||
{ | ||
$recommendationModel = $this->createMock(RecommendationModel::class); | ||
$recommendationModel | ||
->expects($this->once()) | ||
->method('delete') | ||
; | ||
|
||
$recommendationModelAdapter = $this->mockAdapter(['findExpiredRecommendations']); | ||
$recommendationModelAdapter | ||
->expects($this->once()) | ||
->method('findExpiredRecommendations') | ||
->willReturn(new Collection([$recommendationModel], RecommendationModel::getTable())) | ||
; | ||
|
||
$framework = $this->mockContaoFramework([RecommendationModel::class => $recommendationModelAdapter]); | ||
|
||
(new PurgeRecommendationsCron($framework, null))(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace EventListener; | ||
|
||
use Contao\CoreBundle\Event\SitemapEvent; | ||
use Contao\Database; | ||
use Oveleon\ContaoRecommendationBundle\EventListener\SitemapListener; | ||
use Contao\PageModel; | ||
use Contao\TestCase\ContaoTestCase; | ||
use Oveleon\ContaoRecommendationBundle\Model\RecommendationArchiveModel; | ||
use Oveleon\ContaoRecommendationBundle\Model\RecommendationModel; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class SitemapListenerTest extends ContaoTestCase | ||
{ | ||
protected function tearDown(): void | ||
{ | ||
unset($GLOBALS['TL_CONFIG']); | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
public function testNothingIsAddedIfNoPublishedArchive(): void | ||
{ | ||
$adapters = [ | ||
RecommendationArchiveModel::class => $this->mockConfiguredAdapter(['findByProtected' => null]), | ||
]; | ||
|
||
$sitemapEvent = $this->createSitemapEvent([]); | ||
$listener = $this->createListener([], $adapters); | ||
$listener($sitemapEvent); | ||
|
||
$this->assertStringNotContainsString('<url><loc>', (string) $sitemapEvent->getDocument()->saveXML()); | ||
} | ||
|
||
public function testRecommendationIsAdded(): void | ||
{ | ||
$jumpToPage = $this->mockClassWithProperties(PageModel::class, [ | ||
'published' => 1, | ||
'protected' => 0, | ||
]); | ||
|
||
$jumpToPage | ||
->method('getAbsoluteUrl') | ||
->willReturn('https://www.oveleon.de') | ||
; | ||
|
||
$adapters = [ | ||
RecommendationArchiveModel::class => $this->mockConfiguredAdapter([ | ||
'findByProtected' => [ | ||
$this->mockClassWithProperties(RecommendationArchiveModel::class, [ | ||
'jumpTo' => 21, | ||
]), | ||
], | ||
]), | ||
PageModel::class => $this->mockConfiguredAdapter([ | ||
'findWithDetails' => $jumpToPage, | ||
]), | ||
RecommendationModel::class => $this->mockConfiguredAdapter([ | ||
'findPublishedByPid' => [ | ||
$this->mockClassWithProperties(RecommendationModel::class, [ | ||
'jumpTo' => 21, | ||
]), | ||
], | ||
]), | ||
]; | ||
|
||
$sitemapEvent = $this->createSitemapEvent([1]); | ||
$listener = $this->createListener([1, 21], $adapters); | ||
$listener($sitemapEvent); | ||
|
||
$this->assertStringContainsString('<url><loc>https://www.oveleon.de</loc></url>', (string) $sitemapEvent->getDocument()->saveXML()); | ||
} | ||
|
||
private function createListener(array $allPages, array $adapters): SitemapListener | ||
{ | ||
$database = $this->createMock(Database::class); | ||
$database | ||
->method('getChildRecords') | ||
->willReturn($allPages) | ||
; | ||
|
||
$instances = [ | ||
Database::class => $database, | ||
]; | ||
|
||
$framework = $this->mockContaoFramework($adapters, $instances); | ||
|
||
return new SitemapListener($framework); | ||
} | ||
|
||
private function createSitemapEvent(array $rootPages): SitemapEvent | ||
{ | ||
$sitemap = new \DOMDocument('1.0', 'UTF-8'); | ||
$urlSet = $sitemap->createElementNS('https://www.sitemaps.org/schemas/sitemap/0.9', 'urlset'); | ||
$sitemap->appendChild($urlSet); | ||
|
||
return new SitemapEvent($sitemap, new Request(), $rootPages); | ||
} | ||
} |