TYPO3ContentElements/Tests/Unit/SettingsFactoryTest.php
Daniel Siepmann 01d25e4c49
Allow creation of content elements
Content elements could not be created, since they needed campaigns.
In order to retrieve campaigns, the API needs to be created.
The API needs settings from current site, which is not available in that
context.

Therefore the factory is extended to also fetch site and their settings
based on page uid. The page uid is available in the context.

Since we now interact with guzzle, we add the dependency.
Wouldn't be necessary if SDK would have PSR compatible type hints.

Also tests were added for code, to keep code coverage.

Fixes: #5
2021-01-23 15:53:07 +01:00

170 lines
6.3 KiB
PHP

<?php
declare(strict_types=1);
namespace SkillDisplay\Typo3Extension\Tests\Unit;
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use Prophecy\PhpUnit\ProphecyTrait;
use SkillDisplay\PHPToolKit\Configuration\Settings;
use SkillDisplay\Typo3Extension\SettingsFactory;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase as TestCase;
/**
* @covers SkillDisplay\Typo3Extension\SettingsFactory
*/
class SettingsFactoryTest extends TestCase
{
use ProphecyTrait;
/**
* @test
*/
public function instanceCanBeCreated(): void
{
$siteFinder = $this->prophesize(SiteFinder::class);
$subject = new SettingsFactory($siteFinder->reveal());
static::assertInstanceOf(SettingsFactory::class, $subject);
}
/**
* @test
*/
public function returnsDefaultSettingsIfNothingIsConfiguredInCurrentSite(): void
{
$siteFinder = $this->prophesize(SiteFinder::class);
$site = $this->prophesize(Site::class);
$site->getConfiguration()->willReturn([]);
$request = $this->prophesize(ServerRequest::class);
$request->getAttribute('site')->willReturn($site->reveal());
$GLOBALS['TYPO3_REQUEST'] = $request->reveal();
$subject = new SettingsFactory($siteFinder->reveal());
$settings = $subject->createFromCurrentSiteConfiguration();
static::assertInstanceOf(Settings::class, $settings);
static::assertSame(0, $settings->getVerifierID());
static::assertSame('', $settings->getUserSecret());
static::assertSame('', $settings->getApiKey());
static::assertSame('https://www.skilldisplay.eu', $settings->getAPIUrl());
static::assertSame('https://my.skilldisplay.eu', $settings->getMySkillDisplayUrl());
}
/**
* @test
*/
public function returnsSettingsFromCurrentSite(): void
{
$siteFinder = $this->prophesize(SiteFinder::class);
$site = $this->prophesize(Site::class);
$site->getConfiguration()->willReturn([
'skilldisplay_api_key' => '---YOUR-API-KEY---',
'skilldisplay_verifier_id' => 10,
'skilldisplay_user_secret' => '---USER-SECRET---',
]);
$request = $this->prophesize(ServerRequest::class);
$request->getAttribute('site')->willReturn($site->reveal());
$GLOBALS['TYPO3_REQUEST'] = $request->reveal();
$subject = new SettingsFactory($siteFinder->reveal());
$settings = $subject->createFromCurrentSiteConfiguration();
static::assertInstanceOf(Settings::class, $settings);
static::assertSame(10, $settings->getVerifierID());
static::assertSame('---USER-SECRET---', $settings->getUserSecret());
static::assertSame('---YOUR-API-KEY---', $settings->getApiKey());
static::assertSame('https://www.skilldisplay.eu', $settings->getAPIUrl());
static::assertSame('https://my.skilldisplay.eu', $settings->getMySkillDisplayUrl());
}
/**
* @test
*/
public function throwsExceptionIfCurrentSiteCanNotBeFetched(): void
{
$siteFinder = $this->prophesize(SiteFinder::class);
$request = $this->prophesize(ServerRequest::class);
$request->getAttribute('site')->willReturn(null);
$GLOBALS['TYPO3_REQUEST'] = $request->reveal();
$subject = new SettingsFactory($siteFinder->reveal());
$this->expectExceptionMessage('Could not determine current site.');
$this->expectExceptionCode(1599721652);
$subject->createFromCurrentSiteConfiguration();
}
/**
* @test
*/
public function returnsDefaultSettingsIfNothingIsConfiguredForPageUidBasesSite(): void
{
$site = $this->prophesize(Site::class);
$site->getConfiguration()->willReturn([]);
$siteFinder = $this->prophesize(SiteFinder::class);
$siteFinder->getSiteByPageId(10)->willReturn($site->reveal());
$subject = new SettingsFactory($siteFinder->reveal());
$settings = $subject->createFromPageUid(10);
static::assertInstanceOf(Settings::class, $settings);
static::assertSame(0, $settings->getVerifierID());
static::assertSame('', $settings->getUserSecret());
static::assertSame('', $settings->getApiKey());
static::assertSame('https://www.skilldisplay.eu', $settings->getAPIUrl());
static::assertSame('https://my.skilldisplay.eu', $settings->getMySkillDisplayUrl());
}
/**
* @test
*/
public function returnsSettingsFromPageUidBasedSite(): void
{
$site = $this->prophesize(Site::class);
$site->getConfiguration()->willReturn([
'skilldisplay_api_key' => '---YOUR-API-KEY---',
'skilldisplay_verifier_id' => 10,
'skilldisplay_user_secret' => '---USER-SECRET---',
]);
$siteFinder = $this->prophesize(SiteFinder::class);
$siteFinder->getSiteByPageId(10)->willReturn($site->reveal());
$subject = new SettingsFactory($siteFinder->reveal());
$settings = $subject->createFromPageUid(10);
static::assertInstanceOf(Settings::class, $settings);
static::assertSame(10, $settings->getVerifierID());
static::assertSame('---USER-SECRET---', $settings->getUserSecret());
static::assertSame('---YOUR-API-KEY---', $settings->getApiKey());
static::assertSame('https://www.skilldisplay.eu', $settings->getAPIUrl());
static::assertSame('https://my.skilldisplay.eu', $settings->getMySkillDisplayUrl());
}
}