mirror of
https://github.com/SkillDisplay/TYPO3ContentElements.git
synced 2024-11-21 19:16:08 +01:00
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
This commit is contained in:
parent
4f09af857e
commit
01d25e4c49
8 changed files with 354 additions and 10 deletions
55
Classes/CampaignsFactory.php
Normal file
55
Classes/CampaignsFactory.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
namespace SkillDisplay\Typo3Extension;
|
||||
|
||||
/*
|
||||
* Copyright (C) 2021 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 GuzzleHttp\Client;
|
||||
use SkillDisplay\PHPToolKit\Api\Campaigns;
|
||||
use SkillDisplay\Typo3Extension\SettingsFactory;
|
||||
|
||||
class CampaignsFactory
|
||||
{
|
||||
/**
|
||||
* @var SettingsFactory
|
||||
*/
|
||||
private $settingsFactory;
|
||||
|
||||
/**
|
||||
* @var Client
|
||||
*/
|
||||
private $client;
|
||||
|
||||
public function __construct(
|
||||
SettingsFactory $settingsFactory,
|
||||
Client $client
|
||||
) {
|
||||
$this->settingsFactory = $settingsFactory;
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
public function createFromPageUid(int $pageUid): Campaigns
|
||||
{
|
||||
return new Campaigns(
|
||||
$this->settingsFactory->createFromPageUid($pageUid),
|
||||
$this->client
|
||||
);
|
||||
}
|
||||
}
|
|
@ -25,9 +25,22 @@ namespace SkillDisplay\Typo3Extension;
|
|||
|
||||
use SkillDisplay\PHPToolKit\Configuration\Settings;
|
||||
use TYPO3\CMS\Core\Http\ServerRequest;
|
||||
use TYPO3\CMS\Core\Site\Entity\Site;
|
||||
use TYPO3\CMS\Core\Site\SiteFinder;
|
||||
|
||||
class SettingsFactory
|
||||
{
|
||||
/**
|
||||
* @var SiteFinder
|
||||
*/
|
||||
private $siteFinder;
|
||||
|
||||
public function __construct(
|
||||
SiteFinder $siteFinder
|
||||
) {
|
||||
$this->siteFinder = $siteFinder;
|
||||
}
|
||||
|
||||
public function createFromCurrentSiteConfiguration(): Settings
|
||||
{
|
||||
$site = $this->getRequest()->getAttribute('site');
|
||||
|
@ -35,6 +48,18 @@ class SettingsFactory
|
|||
throw new \Exception('Could not determine current site.', 1599721652);
|
||||
}
|
||||
|
||||
return $this->createFromSite($site);
|
||||
}
|
||||
|
||||
public function createFromPageUid(int $pageUid): Settings
|
||||
{
|
||||
$site = $this->siteFinder->getSiteByPageId($pageUid);
|
||||
|
||||
return $this->createFromSite($site);
|
||||
}
|
||||
|
||||
private function createFromSite(Site $site): Settings
|
||||
{
|
||||
$config = $site->getConfiguration();
|
||||
|
||||
return new Settings(
|
||||
|
|
|
@ -4,19 +4,30 @@ declare(strict_types=1);
|
|||
|
||||
namespace SkillDisplay\Typo3Extension;
|
||||
|
||||
use SkillDisplay\PHPToolKit\Api\Campaigns;
|
||||
use SkillDisplay\PHPToolKit\Entity\Campaign;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
class TcaEnhancer
|
||||
{
|
||||
public function getCampaignsForTCA(array $params): void
|
||||
/**
|
||||
* @var CampaignsFactory
|
||||
*/
|
||||
private $campaignsFactory;
|
||||
|
||||
public function __construct(
|
||||
CampaignsFactory $campaignsFactory
|
||||
) {
|
||||
$this->campaignsFactory = $campaignsFactory;
|
||||
}
|
||||
|
||||
public function getCampaignsForTCA(array &$params): void
|
||||
{
|
||||
$params['items'] = [
|
||||
['', 0],
|
||||
];
|
||||
|
||||
$campaigns = GeneralUtility::makeInstance(Campaigns::class)->getForUser();
|
||||
$campaigns = $this->campaignsFactory
|
||||
->createFromPageUid($params['row']['pid'])
|
||||
->getForUser();
|
||||
/** @var Campaign $campaign */
|
||||
foreach ($campaigns as $campaign) {
|
||||
$params['items'][] = [
|
||||
|
|
|
@ -39,3 +39,6 @@ services:
|
|||
|
||||
SkillDisplay\Typo3Extension\Backend\Preview:
|
||||
public: true
|
||||
|
||||
SkillDisplay\Typo3Extension\TcaEnhancer:
|
||||
public: true
|
||||
|
|
75
Tests/Unit/CampaignsFactoryTest.php
Normal file
75
Tests/Unit/CampaignsFactoryTest.php
Normal file
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
namespace SkillDisplay\Typo3Extension\Tests\Unit;
|
||||
|
||||
/*
|
||||
* Copyright (C) 2021 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 GuzzleHttp\Client;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use SkillDisplay\PHPToolKit\Api\Campaigns;
|
||||
use SkillDisplay\PHPToolKit\Configuration\Settings;
|
||||
use SkillDisplay\Typo3Extension\CampaignsFactory;
|
||||
use SkillDisplay\Typo3Extension\SettingsFactory;
|
||||
|
||||
/**
|
||||
* @covers SkillDisplay\Typo3Extension\CampaignsFactory
|
||||
*/
|
||||
class CampaignsFactoryTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function instanceCanBeCreated(): void
|
||||
{
|
||||
$settingsFactory = $this->prophesize(SettingsFactory::class);
|
||||
$client = $this->prophesize(Client::class);
|
||||
|
||||
$subject = new CampaignsFactory(
|
||||
$settingsFactory->reveal(),
|
||||
$client->reveal()
|
||||
);
|
||||
|
||||
self::assertInstanceOf(CampaignsFactory::class, $subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function returnsCampaignsFromPageUid(): void
|
||||
{
|
||||
$settingsFactory = $this->prophesize(SettingsFactory::class);
|
||||
$settings = $this->prophesize(Settings::class);
|
||||
$client = $this->prophesize(Client::class);
|
||||
|
||||
$settingsFactory->createFromPageUid(10)->willReturn($settings->reveal());
|
||||
|
||||
$subject = new CampaignsFactory(
|
||||
$settingsFactory->reveal(),
|
||||
$client->reveal()
|
||||
);
|
||||
|
||||
$result = $subject->createFromPageUid(10);
|
||||
|
||||
self::assertInstanceOf(Campaigns::class, $result);
|
||||
}
|
||||
}
|
|
@ -28,6 +28,7 @@ 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;
|
||||
|
||||
/**
|
||||
|
@ -42,15 +43,18 @@ class SettingsFactoryTest extends TestCase
|
|||
*/
|
||||
public function instanceCanBeCreated(): void
|
||||
{
|
||||
$subject = new SettingsFactory();
|
||||
$siteFinder = $this->prophesize(SiteFinder::class);
|
||||
$subject = new SettingsFactory($siteFinder->reveal());
|
||||
static::assertInstanceOf(SettingsFactory::class, $subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function returnsDefaultSettingsIfNothingIsConfigured(): void
|
||||
public function returnsDefaultSettingsIfNothingIsConfiguredInCurrentSite(): void
|
||||
{
|
||||
$siteFinder = $this->prophesize(SiteFinder::class);
|
||||
|
||||
$site = $this->prophesize(Site::class);
|
||||
$site->getConfiguration()->willReturn([]);
|
||||
|
||||
|
@ -58,7 +62,7 @@ class SettingsFactoryTest extends TestCase
|
|||
$request->getAttribute('site')->willReturn($site->reveal());
|
||||
|
||||
$GLOBALS['TYPO3_REQUEST'] = $request->reveal();
|
||||
$subject = new SettingsFactory();
|
||||
$subject = new SettingsFactory($siteFinder->reveal());
|
||||
|
||||
$settings = $subject->createFromCurrentSiteConfiguration();
|
||||
static::assertInstanceOf(Settings::class, $settings);
|
||||
|
@ -74,6 +78,8 @@ class SettingsFactoryTest extends TestCase
|
|||
*/
|
||||
public function returnsSettingsFromCurrentSite(): void
|
||||
{
|
||||
$siteFinder = $this->prophesize(SiteFinder::class);
|
||||
|
||||
$site = $this->prophesize(Site::class);
|
||||
$site->getConfiguration()->willReturn([
|
||||
'skilldisplay_api_key' => '---YOUR-API-KEY---',
|
||||
|
@ -85,7 +91,7 @@ class SettingsFactoryTest extends TestCase
|
|||
$request->getAttribute('site')->willReturn($site->reveal());
|
||||
|
||||
$GLOBALS['TYPO3_REQUEST'] = $request->reveal();
|
||||
$subject = new SettingsFactory();
|
||||
$subject = new SettingsFactory($siteFinder->reveal());
|
||||
|
||||
$settings = $subject->createFromCurrentSiteConfiguration();
|
||||
static::assertInstanceOf(Settings::class, $settings);
|
||||
|
@ -101,14 +107,64 @@ class SettingsFactoryTest extends TestCase
|
|||
*/
|
||||
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();
|
||||
$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());
|
||||
}
|
||||
}
|
||||
|
|
118
Tests/Unit/TcaEnhancerTest.php
Normal file
118
Tests/Unit/TcaEnhancerTest.php
Normal file
|
@ -0,0 +1,118 @@
|
|||
<?php
|
||||
|
||||
namespace SkillDisplay\Typo3Extension\Tests\Unit;
|
||||
|
||||
/*
|
||||
* Copyright (C) 2021 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 PHPUnit\Framework\TestCase;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use SkillDisplay\PHPToolKit\Api\Campaigns;
|
||||
use SkillDisplay\PHPToolKit\Entity\Campaign;
|
||||
use SkillDisplay\Typo3Extension\CampaignsFactory;
|
||||
use SkillDisplay\Typo3Extension\TcaEnhancer;
|
||||
|
||||
/**
|
||||
* @covers SkillDisplay\Typo3Extension\TcaEnhancer
|
||||
*/
|
||||
class TcaEnhancerTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function instanceCanBeCreated(): void
|
||||
{
|
||||
$campaignsFactory = $this->prophesize(CampaignsFactory::class);
|
||||
$subject = new TcaEnhancer(
|
||||
$campaignsFactory->reveal()
|
||||
);
|
||||
|
||||
self::assertInstanceOf(TcaEnhancer::class, $subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function addsDefaultItemOnlyWhenNoCampaignsAreAvailable(): void
|
||||
{
|
||||
$parameters = [
|
||||
'row' => [
|
||||
'pid' => 10,
|
||||
],
|
||||
];
|
||||
|
||||
$campaigns = $this->prophesize(Campaigns::class);
|
||||
$campaigns->getForUser()->willReturn([]);
|
||||
|
||||
$campaignsFactory = $this->prophesize(CampaignsFactory::class);
|
||||
$campaignsFactory->createFromPageUid(10)->willReturn($campaigns->reveal());
|
||||
|
||||
$subject = new TcaEnhancer(
|
||||
$campaignsFactory->reveal()
|
||||
);
|
||||
$subject->getCampaignsForTCA($parameters);
|
||||
|
||||
self::assertArrayHasKey('items', $parameters);
|
||||
self::assertSame([
|
||||
['', 0],
|
||||
], $parameters['items']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function addsFetchedCampaignsBesideDefault(): void
|
||||
{
|
||||
$parameters = [
|
||||
'row' => [
|
||||
'pid' => 10,
|
||||
],
|
||||
];
|
||||
|
||||
$campaign1 = $this->prophesize(Campaign::class);
|
||||
$campaign1->getTitle()->willReturn('Campaign Title 1');
|
||||
$campaign1->getId()->willReturn(10);
|
||||
$campaign2 = $this->prophesize(Campaign::class);
|
||||
$campaign2->getTitle()->willReturn('Campaign Title 2');
|
||||
$campaign2->getId()->willReturn(20);
|
||||
|
||||
$campaigns = $this->prophesize(Campaigns::class);
|
||||
$campaigns->getForUser()->willReturn([
|
||||
$campaign1->reveal(),
|
||||
$campaign2->reveal(),
|
||||
]);
|
||||
|
||||
$campaignsFactory = $this->prophesize(CampaignsFactory::class);
|
||||
$campaignsFactory->createFromPageUid(10)->willReturn($campaigns->reveal());
|
||||
|
||||
$subject = new TcaEnhancer(
|
||||
$campaignsFactory->reveal()
|
||||
);
|
||||
$subject->getCampaignsForTCA($parameters);
|
||||
|
||||
self::assertArrayHasKey('items', $parameters);
|
||||
self::assertSame([
|
||||
['', 0],
|
||||
['Campaign Title 1', 10],
|
||||
['Campaign Title 2', 20],
|
||||
], $parameters['items']);
|
||||
}
|
||||
}
|
|
@ -31,7 +31,8 @@
|
|||
"skilldisplay/phptoolkit": "^1.2",
|
||||
"typo3/cms-backend": "^10.4",
|
||||
"typo3/cms-frontend": "^10.4",
|
||||
"typo3fluid/fluid": "^2.6"
|
||||
"typo3fluid/fluid": "^2.6",
|
||||
"guzzlehttp/guzzle": "^6.5"
|
||||
},
|
||||
"require-dev": {
|
||||
"squizlabs/php_codesniffer": "^3.5",
|
||||
|
|
Loading…
Reference in a new issue