Skip to content

Commit

Permalink
[FEATURE] Added dg/bypass-finals dev requirement to test final classe…
Browse files Browse the repository at this point in the history
…s, added more tests, fixed wrong doktype for backend_section
  • Loading branch information
RinyVT committed Oct 23, 2024
1 parent a05104b commit 852c5db
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 21 deletions.
8 changes: 7 additions & 1 deletion Classes/EventListener/RecordCanonicalListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace YoastSeoForTypo3\YoastSeo\EventListener;

use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Seo\Event\ModifyUrlForCanonicalTagEvent;
use YoastSeoForTypo3\YoastSeo\Record\Record;
use YoastSeoForTypo3\YoastSeo\Record\RecordService;
Expand All @@ -27,10 +28,15 @@ public function setCanonical(ModifyUrlForCanonicalTagEvent $event): void
}

$event->setUrl(
$GLOBALS['TSFE']->cObj->typoLink_URL([
$this->getContentObjectRenderer()->typoLink_URL([
'parameter' => $canonicalLink,
'forceAbsoluteUrl' => true,
])
);
}

protected function getContentObjectRenderer(): ContentObjectRenderer
{
return $GLOBALS['TSFE']->cObj;
}
}
2 changes: 1 addition & 1 deletion Classes/Utility/ConfigurationUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function getDefaultConfiguration(): array
return [
'allowedDoktypes' => [
'page' => 1,
'backend_section' => 5,
'backend_section' => 6,
],
'translations' => [
'availableLocales' => [
Expand Down
1 change: 1 addition & 0 deletions Classes/Utility/YoastUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static function getAllowedDoktypes(?array $configuration = null): array
$allowedDoktypes = array_map(function ($doktype) {
return (int)$doktype;
}, array_values((array)($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['yoast_seo']['allowedDoktypes'] ?? [])));
$allowedDoktypes = array_unique($allowedDoktypes);

if (isset($configuration['allowedDoktypes'])
&& is_array($configuration['allowedDoktypes'])
Expand Down
31 changes: 31 additions & 0 deletions Tests/Functional/AbstractFunctionalTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace YoastSeoForTypo3\YoastSeo\Tests\Functional;

use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Http\Uri;
use TYPO3\CMS\Core\Localization\LanguageServiceFactory;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;

abstract class AbstractFunctionalTestCase extends FunctionalTestCase
{
protected array $testExtensionsToLoad = [
'typo3conf/ext/yoast_seo',
];

protected array $coreExtensionsToLoad = [
'seo',
];

protected function setUpBackendRequest(): void
{
$GLOBALS['LANG'] = $this->getContainer()->get(LanguageServiceFactory::class)->create('default');

$request = new ServerRequest();
$request = $request->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_BE);
$GLOBALS['TYPO3_REQUEST'] = $request->withUri(new Uri('http://localhost/'));
}
}
45 changes: 45 additions & 0 deletions Tests/Functional/Backend/PageLayoutHeaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace YoastSeoForTypo3\YoastSeo\Tests\Functional\Backend;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Backend\Controller\PageLayoutController;
use YoastSeoForTypo3\YoastSeo\Backend\PageLayoutHeader;
use YoastSeoForTypo3\YoastSeo\Tests\Functional\AbstractFunctionalTestCase;

#[CoversClass(PageLayoutHeader::class)]
class PageLayoutHeaderTest extends AbstractFunctionalTestCase
{
protected PageLayoutHeader $pageLayoutHeader;

protected function setUp(): void
{
parent::setUp();

$this->importCSVDataSet(__DIR__ . '/../Fixtures/be_users.csv');
$this->importCSVDataSet(__DIR__ . '/../Fixtures/pages.csv');
$this->setUpBackendUser(1);
$this->setUpBackendRequest();

$this->pageLayoutHeader = $this->getContainer()->get(PageLayoutHeader::class);
}

#[Test]
public function testRenderReturnsEmptyStringWhenNoPageExists(): void
{
$_GET['id'] = '99999'; // page does not exist in the fixture
$result = $this->pageLayoutHeader->render(null, $this->getContainer()->get(PageLayoutController::class));
$this->assertSame('', $result);
}

#[Test]
public function testRenderReturnsOutputForExistingPage(): void
{
$_GET['id'] = '1'; // page exists in the fixture
$result = $this->pageLayoutHeader->render(null, $this->getContainer()->get(PageLayoutController::class));
$this->assertNotEmpty($result);
}
}
5 changes: 5 additions & 0 deletions Tests/Functional/Fixtures/pages.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"pages"
,"uid","pid","title","doktype"
,1,0,"Home",1
,2,1,"Page 1",1
,3,1,"Page 2 with invalid doktype",10
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
use YoastSeoForTypo3\YoastSeo\Service\PageLayoutHeader\VisibilityChecker;
use YoastSeoForTypo3\YoastSeo\Tests\Functional\AbstractFunctionalTestCase;

#[CoversClass(VisibilityChecker::class)]
class VisibilityCheckerTest extends FunctionalTestCase
class VisibilityCheckerTest extends AbstractFunctionalTestCase
{
/**
* @var VisibilityChecker&MockObject&AccessibleObjectInterface
Expand All @@ -28,11 +28,12 @@ class VisibilityCheckerTest extends FunctionalTestCase
],
];

public function setUp(): void
protected function setUp(): void
{
parent::setUp();

$this->importCSVDataSet(__DIR__ . '/../../Fixtures/be_users.csv');
$this->importCSVDataSet(__DIR__ . '/../../Fixtures/pages.csv');
$this->setUpBackendUser(1);
}

Expand Down
31 changes: 16 additions & 15 deletions Tests/Functional/Utility/YoastUtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
use YoastSeoForTypo3\YoastSeo\Tests\Functional\AbstractFunctionalTestCase;
use YoastSeoForTypo3\YoastSeo\Utility\YoastUtility;

#[CoversClass(YoastUtility::class)]
class YoastUtilityTest extends FunctionalTestCase
class YoastUtilityTest extends AbstractFunctionalTestCase
{
protected const EXTCONF_DOKTYPES = [1, 2, 3];
protected const DEFAULT_DOKTYPES = [1, 6];
protected const EXTCONF_DOKTYPES = [2, 3];

protected array $configurationToUseInTestInstance = [
'EXTCONF' => [
Expand All @@ -23,7 +24,7 @@ class YoastUtilityTest extends FunctionalTestCase
],
];

public function setUp(): void
protected function setUp(): void
{
parent::setUp();

Expand All @@ -36,7 +37,7 @@ public function getAllowedDoktypesReturnsAllowedDoktypes(): void
{
$allowedDoktypes = YoastUtility::getAllowedDoktypes();

self::assertSame(self::EXTCONF_DOKTYPES, $allowedDoktypes);
self::assertSame(array_merge(self::DEFAULT_DOKTYPES, self::EXTCONF_DOKTYPES), $allowedDoktypes);
}

#[DataProvider('areTheRightDoktypesExtractedFromConfigurationDataProvider')]
Expand All @@ -51,35 +52,35 @@ public function areTheRightDoktypesExtractedFromConfiguration(array $inputArray,
public static function areTheRightDoktypesExtractedFromConfigurationDataProvider(): array
{
return [
'empty configuration array should return EXTCONF allowedDoktypes' => [
'empty configuration array should return default and test EXTCONF allowedDoktypes' => [
[],
self::EXTCONF_DOKTYPES,
array_merge(self::DEFAULT_DOKTYPES, self::EXTCONF_DOKTYPES),
],
'configuration array with doktypes 1 and 6 should not return doktype 1 more than once' => [
'configuration array with doktypes 1 and 10 should not return doktype 1 more than once' => [
[
'allowedDoktypes' => [
'page' => 1,
'backend_user_section' => 6,
'custom' => 10,
],
],
array_merge(self::EXTCONF_DOKTYPES, [6]),
array_merge(self::DEFAULT_DOKTYPES, self::EXTCONF_DOKTYPES, [10]),
],
'configuration array with doktype 6 should return EXTCONF allowedDoktypes plus doktype 6' => [
'configuration array with doktype 10 should return default and test EXTCONF allowedDoktypes plus doktype 10' => [
[
'allowedDoktypes' => [
'backend_user_section' => 6,
'custom' => 10,
],
],
array_merge(self::EXTCONF_DOKTYPES, [6]),
array_merge(self::DEFAULT_DOKTYPES, self::EXTCONF_DOKTYPES, [10]),
],
'configuration array with doktypes 1 (different key) and 6 should not return doktype 1 more than once' => [
[
'allowedDoktypes' => [
'duplicateDoktype' => 1,
'backend_user_section' => 6,
'custom' => 10,
],
],
array_merge(self::EXTCONF_DOKTYPES, [6]),
array_merge(self::DEFAULT_DOKTYPES, self::EXTCONF_DOKTYPES, [10]),
],
];
}
Expand Down
2 changes: 2 additions & 0 deletions Tests/Unit/Controller/CrawlerControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace YoastSeoForTypo3\YoastSeo\Tests\Unit\Controller;

use DG\BypassFinals;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Core\Http\HtmlResponse;
Expand All @@ -24,6 +25,7 @@ final class CrawlerControllerTest extends UnitTestCase

protected function setUp(): void
{
BypassFinals::enable();
parent::setUp();

$this->subject = $this->getAccessibleMock(
Expand Down
74 changes: 74 additions & 0 deletions Tests/Unit/EventListener/RecordCanonicalListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace YoastSeoForTypo3\YoastSeo\Tests\Unit\EventListener;

use DG\BypassFinals;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Seo\Event\ModifyUrlForCanonicalTagEvent;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
use YoastSeoForTypo3\YoastSeo\EventListener\RecordCanonicalListener;
use YoastSeoForTypo3\YoastSeo\Record\Record;
use YoastSeoForTypo3\YoastSeo\Record\RecordService;

#[CoversClass(RecordCanonicalListener::class)]
class RecordCanonicalListenerTest extends UnitTestCase
{
protected RecordService $recordService;
protected RecordCanonicalListener $subject;

protected function setUp(): void
{
BypassFinals::enable();
parent::setUp();

$this->recordService = $this->createMock(RecordService::class);
$this->subject = new RecordCanonicalListener($this->recordService);
}

#[Test]
public function testSetCanonicalWithNoActiveRecord(): void
{
$event = $this->createMock(ModifyUrlForCanonicalTagEvent::class);
$this->recordService->method('getActiveRecord')->willReturn(null);

$event->expects($this->never())->method('setUrl');

$this->subject->setCanonical($event);
}

#[Test]
public function testSetCanonicalWithNoCanonicalLink(): void
{
$event = $this->createMock(ModifyUrlForCanonicalTagEvent::class);
$record = $this->createMock(Record::class);
$record->method('getRecordData')->willReturn([]);

$this->recordService->method('getActiveRecord')->willReturn($record);

$event->expects($this->never())->method('setUrl');

$this->subject->setCanonical($event);
}

#[Test]
public function testSetCanonicalWithCanonicalLink(): void
{
$event = $this->createMock(ModifyUrlForCanonicalTagEvent::class);
$record = $this->createMock(Record::class);
$record->method('getRecordData')->willReturn(['canonical_link' => 'https://example.com']);

$this->recordService->method('getActiveRecord')->willReturn($record);

$GLOBALS['TSFE'] = $this->createMock(\stdClass::class);
$GLOBALS['TSFE']->cObj = $this->createMock(ContentObjectRenderer::class);
$GLOBALS['TSFE']->cObj->method('typoLink_URL')->willReturn('https://example.com');

$event->expects($this->once())->method('setUrl')->with('https://example.com');

$this->subject->setCanonical($event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class VisibilityCheckerTest extends UnitTestCase
*/
protected VisibilityChecker $subject;

public function setUp(): void
protected function setUp(): void
{
parent::setUp();

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"typo3/cms-seo": "^11.5.25 || ^12.4.15 || ^13.4"
},
"require-dev": {
"dg/bypass-finals": "^1.8",
"ergebnis/composer-normalize": "^2.43",
"friendsofphp/php-cs-fixer": "^3.60.0",
"php-parallel-lint/php-parallel-lint": "^1.4",
Expand Down

0 comments on commit 852c5db

Please sign in to comment.