Skip to content

Commit

Permalink
[FEATURE] Added functional and unit test for VisibilityChecker, fixed…
Browse files Browse the repository at this point in the history
… other tests to have working functional and unit tests
  • Loading branch information
RinyVT committed Oct 15, 2024
1 parent 8647b39 commit f21fd06
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 121 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ jobs:
run: Build/Scripts/runTests.sh -n -t ${{ matrix.typo3 }} -p ${{ matrix.php }} -s cgl -n

- name: PHPStan
run: Build/Scripts/runTests.sh -t ${{ matrix.typo3 }} -p ${{ matrix.php }} -s phpstan -e "--error-format=github"
run: Build/Scripts/runTests.sh -t ${{ matrix.typo3 }} -p ${{ matrix.php }} -s phpstan -e "--error-format=github"

- name: Functional tests
run: Build/Scripts/runTests.sh -t ${{ matrix.typo3 }} -p ${{ matrix.php }} -s functional

- name: Unit tests
run: Build/Scripts/runTests.sh -t ${{ matrix.typo3 }} -p ${{ matrix.php }} -s unit
4 changes: 4 additions & 0 deletions Build/phpunit/UnitTests.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
bootstrap="UnitTestsBootstrap.php"
cacheResult="false"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnSkippedTests="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnPhpunitDeprecations="true"
colors="true"
failOnRisky="true"
failOnWarning="true"
Expand Down
2 changes: 1 addition & 1 deletion Classes/Service/PageLayoutHeader/VisibilityChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function isSnippetPreviewEnabled(int $pageId, array $pageRecord): bool
{
$backendUser = $this->getBackendUser();

if (!$GLOBALS['BE_USER']->check('non_exclude_fields', 'pages:tx_yoastseo_snippetpreview')) {
if (!$backendUser->check('non_exclude_fields', 'pages:tx_yoastseo_snippetpreview')) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace YoastSeoForTypo3\YoastSeo\Tests\Functional\Service\PageLayoutHeader;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
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;

#[CoversClass(VisibilityChecker::class)]
class VisibilityCheckerTest extends FunctionalTestCase
{
/**
* @var VisibilityChecker&MockObject&AccessibleObjectInterface
*/
protected VisibilityChecker $subject;

protected array $configurationToUseInTestInstance = [
'EXTCONF' => [
'yoast_seo' => [
'allowedDoktypes' => [1,6],
],
],
];

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

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

#[Test]
#[DataProvider('isSnippetPreviewEnabledCorrectlyBasedOnPageDoktypeDataProvider')]
public function isSnippetPreviewEnabledCorrectlyBasedOnPageDoktype(int $doktype, bool $expected): void
{
$visibilityChecker = new VisibilityChecker();
self::assertEquals($expected, $visibilityChecker->shouldShowPreview(1, ['doktype' => $doktype]));
}

public static function isSnippetPreviewEnabledCorrectlyBasedOnPageDoktypeDataProvider(): array
{
return [
'doktype 1 should return true' => [
1,
true,
],
'doktype 6 should return true' => [
6,
true,
],
'doktype 2 should return false' => [
2,
false,
],
];
}
}
111 changes: 13 additions & 98 deletions Tests/Functional/Utility/YoastUtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,21 @@

namespace YoastSeoForTypo3\YoastSeo\Tests\Functional\Utility;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Core\Core\Bootstrap;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
use YoastSeoForTypo3\YoastSeo\Utility\YoastUtility;

/**
* @covers \YoastSeoForTypo3\YoastSeo\Utility\YoastUtility
*/
#[CoversClass(YoastUtility::class)]
class YoastUtilityTest extends FunctionalTestCase
{
protected const DOKTYPES_FROM_CONFIGURATION = [1, 2, 3];
protected const EXTCONF_DOKTYPES = [1, 2, 3];

protected array $configurationToUseInTestInstance = [
'EXTCONF' => [
'yoast_seo' => [
'allowedDoktypes' => self::DOKTYPES_FROM_CONFIGURATION,
'allowedDoktypes' => self::EXTCONF_DOKTYPES,
],
],
];
Expand All @@ -31,15 +29,14 @@ public function setUp(): void

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

#[Test]
public function getAllowedDoktypesReturnsAllowedDoktypes(): void
{
$allowedDoktypes = YoastUtility::getAllowedDoktypes();

self::assertSame(self::DOKTYPES_FROM_CONFIGURATION, $allowedDoktypes);
self::assertSame(self::EXTCONF_DOKTYPES, $allowedDoktypes);
}

#[DataProvider('areTheRightDoktypesExtractedFromConfigurationDataProvider')]
Expand All @@ -51,120 +48,38 @@ public function areTheRightDoktypesExtractedFromConfiguration(array $inputArray,
self::assertEquals($expected, $actual);
}

#[DataProvider('isSnippetPreviewEnabledCorrectlyBasedOnPageTsConfigurationDataProvider')]
#[Test]
public function isSnippetPreviewEnabledCorrectlyBasedOnPageTsConfiguration(int $pageId, array $config, bool $expected): void
{
$actual = YoastUtility::snippetPreviewEnabled($pageId, ['tx_yoastseo_hide_snippet_preview' => false], $config);

self::assertEquals($expected, $actual);
}

#[DataProvider('isSnippetPreviewEnabledCorrectlyBasedOnPageRecordDataProvider')]
#[Test]
public function isSnippetPreviewEnabledCorrectlyBasedOnPageRecord(int $pageId, array $pageRecord, bool $expected): void
{
$actual = YoastUtility::snippetPreviewEnabled($pageId, $pageRecord, []);

self::assertEquals($expected, $actual);
}

public static function areTheRightDoktypesExtractedFromConfigurationDataProvider(): array
{
return [
[
'empty configuration array should return EXTCONF allowedDoktypes' => [
[],
self::DOKTYPES_FROM_CONFIGURATION,
self::EXTCONF_DOKTYPES,
],
[
'configuration array with doktypes 1 and 6 should not return doktype 1 more than once' => [
[
'allowedDoktypes' => [
'page' => 1,
'backend_user_section' => 6,
],
],
array_merge(self::DOKTYPES_FROM_CONFIGURATION, [6]),
array_merge(self::EXTCONF_DOKTYPES, [6]),
],
[
'configuration array with doktype 6 should return EXTCONF allowedDoktypes plus doktype 6' => [
[
'allowedDoktypes' => [
'backend_user_section' => 6,
],
],
array_merge(self::DOKTYPES_FROM_CONFIGURATION, [6]),
array_merge(self::EXTCONF_DOKTYPES, [6]),
],
[
'configuration array with doktypes 1 (different key) and 6 should not return doktype 1 more than once' => [
[
'allowedDoktypes' => [
'duplicateDoktype' => 1,
'backend_user_section' => 6,
],
],
array_merge(self::DOKTYPES_FROM_CONFIGURATION, [6]),
],
];
}

public static function isSnippetPreviewEnabledCorrectlyBasedOnPageTsConfigurationDataProvider(): array
{
return [
[
1,
[],
true,
],
[
1,
[
'mod.' => [
'web_SeoPlugin.' => [
'disableSnippetPreview' => 0,
],
],
],
true,
],
[
1,
[
'mod.' => [
'web_SeoPlugin.' => [
'disableSnippetPreview' => 1,
],
],
],
false,
],
];
}

public static function isSnippetPreviewEnabledCorrectlyBasedOnPageRecordDataProvider(): array
{
return [
[
1,
[],
true,
],
[
1,
['tx_yoastseo_hide_snippet_preview' => '0'],
true,
],
[
1,
['tx_yoastseo_hide_snippet_preview' => false],
true,
],
[
1,
['tx_yoastseo_hide_snippet_preview' => '1'],
false,
],
[
1,
['tx_yoastseo_hide_snippet_preview' => true],
false,
array_merge(self::EXTCONF_DOKTYPES, [6]),
],
];
}
Expand Down
22 changes: 10 additions & 12 deletions Tests/Unit/Controller/CrawlerControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@

namespace YoastSeoForTypo3\YoastSeo\Tests\Unit\Controller;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Core\Http\HtmlResponse;
use TYPO3\CMS\Core\Http\RedirectResponse;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Mvc\Request;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
use YoastSeoForTypo3\YoastSeo\Controller\CrawlerController;
use YoastSeoForTypo3\YoastSeo\Service\Crawler\CrawlerJavascriptConfigService;
use YoastSeoForTypo3\YoastSeo\Service\Crawler\CrawlerService;

/**
* @covers \YoastSeoForTypo3\YoastSeo\Controller\CrawlerController
*/
#[CoversClass(CrawlerController::class)]
final class CrawlerControllerTest extends UnitTestCase
{
private CrawlerController $subject;
Expand Down Expand Up @@ -45,31 +46,28 @@ protected function setUp(): void
$request = $this->createMock(Request::class);
$this->subject->_set('request', $request);

$uriBuilder = $this->createMock(UriBuilder::class);
$this->subject->_set('uriBuilder', $uriBuilder);

$responseStub = $this->createStub(HtmlResponse::class);
$this->subject->method('returnResponse')->willReturn($responseStub);
}

/**
* @test
*/
#[Test]
public function isActionController(): void
{
self::assertInstanceOf(ActionController::class, $this->subject);
}

/**
* @test
*/
#[Test]
public function indexActionReturnsHtmlResponse(): void
{
$result = $this->subject->indexAction();

self::assertInstanceOf(HtmlResponse::class, $result);
}

/**
* @test
*/
#[Test]
public function resetProgressActionReturnsRedirectResponse(): void
{
$result = $this->subject->resetProgressAction(1, 1);
Expand Down
14 changes: 5 additions & 9 deletions Tests/Unit/Controller/DashboardControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace YoastSeoForTypo3\YoastSeo\Tests\Unit\Controller;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Core\Http\HtmlResponse;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
use YoastSeoForTypo3\YoastSeo\Controller\DashboardController;

/**
* @covers \YoastSeoForTypo3\YoastSeo\Controller\DashboardController
*/
#[CoversClass(DashboardController::class)]
final class DashboardControllerTest extends UnitTestCase
{
private DashboardController $subject;
Expand All @@ -32,17 +32,13 @@ protected function setUp(): void
$this->subject->method('returnResponse')->willReturn($responseStub);
}

/**
* @test
*/
#[Test]
public function isActionController(): void
{
self::assertInstanceOf(ActionController::class, $this->subject);
}

/**
* @test
*/
#[Test]
public function indexActionReturnsHtmlResponse(): void
{
$result = $this->subject->indexAction();
Expand Down
Loading

0 comments on commit f21fd06

Please sign in to comment.