-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Move decision about embedding markup into event listener
- Loading branch information
1 parent
7ddf632
commit 54104ac
Showing
6 changed files
with
129 additions
and
59 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
Classes/EventListener/EmbedMarkupDependingOnNoIndexPageField.php
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,29 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the "schema" extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Brotkrueml\Schema\EventListener; | ||
|
||
use Brotkrueml\Schema\Event\ShouldEmbedMarkupEvent; | ||
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility; | ||
|
||
class EmbedMarkupDependingOnNoIndexPageField | ||
{ | ||
public function __invoke(ShouldEmbedMarkupEvent $event): void | ||
{ | ||
if (!ExtensionManagementUtility::isLoaded('seo')) { | ||
return; | ||
} | ||
|
||
$page = $event->getPage(); | ||
if ($page['no_index'] ?? false) { | ||
$event->setEmbedMarkup(false); | ||
} | ||
} | ||
} |
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
87 changes: 87 additions & 0 deletions
87
Tests/Unit/EventListener/EmbedMarkupDependingOnNoIndexPageFieldTest.php
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,87 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the "schema" extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Brotkrueml\Schema\Tests\Unit\EventListener; | ||
|
||
use Brotkrueml\Schema\Event\ShouldEmbedMarkupEvent; | ||
use Brotkrueml\Schema\EventListener\EmbedMarkupDependingOnNoIndexPageField; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use TYPO3\CMS\Core\Package\PackageManager; | ||
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility; | ||
|
||
/** | ||
* @coversDefaultClass \Brotkrueml\Schema\EventListener\EmbedMarkupDependingOnNoIndexPageField | ||
*/ | ||
class EmbedMarkupDependingOnNoIndexPageFieldTest extends TestCase | ||
{ | ||
/** @var EmbedMarkupDependingOnNoIndexPageField */ | ||
private $subject; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->subject = new EmbedMarkupDependingOnNoIndexPageField(); | ||
} | ||
|
||
/** | ||
* @test | ||
* @covers ::__invoke | ||
*/ | ||
public function seoExtensionIsNotInstalledThenMarkupShouldBeEmbedded(): void | ||
{ | ||
$this->setSeoExtensionInstallationState(false); | ||
|
||
$event = new ShouldEmbedMarkupEvent([], true); | ||
$this->subject->__invoke($event); | ||
|
||
self::assertTrue($event->getEmbedMarkup()); | ||
} | ||
|
||
/** | ||
* @test | ||
* @covers ::__invoke | ||
*/ | ||
public function seoExtensionIsInstalledAndNoIndexIsNotSetThenMarkupShouldBeEmbedded(): void | ||
{ | ||
$this->setSeoExtensionInstallationState(true); | ||
|
||
$event = new ShouldEmbedMarkupEvent(['no_index' => 0], true); | ||
$this->subject->__invoke($event); | ||
|
||
self::assertTrue($event->getEmbedMarkup()); | ||
} | ||
|
||
/** | ||
* @test | ||
* @covers ::__invoke | ||
*/ | ||
public function seoExtensionIsInstalledAndNoIndexIsSetThenMarkupShouldNotBeEmbedded(): void | ||
{ | ||
$this->setSeoExtensionInstallationState(true); | ||
|
||
$event = new ShouldEmbedMarkupEvent(['no_index' => 1], true); | ||
$this->subject->__invoke($event); | ||
|
||
self::assertFalse($event->getEmbedMarkup()); | ||
} | ||
|
||
protected function setSeoExtensionInstallationState(bool $state): void | ||
{ | ||
/** @var MockObject|PackageManager $packageManagerMock */ | ||
$packageManagerMock = $this->createMock(PackageManager::class); | ||
$packageManagerMock | ||
->expects(self::once()) | ||
->method('isPackageActive') | ||
->with('seo') | ||
->willReturn($state); | ||
|
||
ExtensionManagementUtility::setPackageManager($packageManagerMock); | ||
} | ||
} |
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