-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TASK: Add more functional tests, add testing configuration
- Loading branch information
1 parent
fe39b33
commit be274c1
Showing
9 changed files
with
308 additions
and
14 deletions.
There are no files selected for viewing
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,10 @@ | ||
'Sitegeist.LostInTranslation.Testing:NodeWithAutomaticTranslation': | ||
superTypes: | ||
'Neos.Neos:Node': true | ||
properties: | ||
inlineEditableStringProperty: | ||
type: string | ||
ui: | ||
inlineEditable: true | ||
stringProperty: | ||
type: string |
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,9 @@ | ||
roles: | ||
'Neos.Flow:Everybody': | ||
privileges: | ||
- | ||
privilegeTarget: 'Neos.Neos:Backend.GeneralAccess' | ||
permission: GRANT | ||
- | ||
privilegeTarget: 'Sitegeist.LostInTranslation:AccessBackendModule' | ||
permission: GRANT |
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,56 @@ | ||
Neos: | ||
Flow: | ||
persistence: | ||
backendOptions: | ||
dbname: 'flow_functional_testing' | ||
i18n: | ||
defaultLocale: de | ||
ContentRepository: | ||
contentDimensions: | ||
language: | ||
label: Languages | ||
icon: language | ||
# The default dimension that is applied when creating nodes without specifying a dimension | ||
default: de | ||
# The default preset to use if no URI segment was given when resolving languages in the router | ||
defaultPreset: de | ||
presets: | ||
de: | ||
label: Deutsch | ||
values: | ||
- de | ||
uriSegment: '' | ||
en: | ||
label: English | ||
values: | ||
- en | ||
- de | ||
uriSegment: 'en' | ||
options: | ||
translationStrategy: 'sync' | ||
it: | ||
label: Italiano | ||
values: | ||
- it | ||
- de | ||
uriSegment: 'it' | ||
options: | ||
translationStrategy: 'once' | ||
#Sitegeist: | ||
# LostInTranslation: | ||
# nodeTranslation: | ||
# skipAuthorizationChecks: true | ||
# DeepLApi: | ||
# enableCache: true | ||
# authenticationKey: '%env:DEEPL_AUTHENTICATION_KEY%' | ||
# defaultOptions: | ||
# splitting_tags: 'br' | ||
# ignoredTerms: | ||
# - 'Code Q' | ||
# - 'Innsbruck Marketing GmbH' | ||
# - 'Innsbruck Marketing' | ||
# - 'Nordkette' | ||
# - 'Mag.' | ||
# - 'Dr.' | ||
# - 'Dipl.Ing.' | ||
# - 'Dipl.-Ing.' |
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,10 @@ | ||
<?php | ||
|
||
namespace Sitegeist\LostInTranslation\Tests\Functional; | ||
|
||
use Neos\Flow\Tests\FunctionalTestCase; | ||
|
||
abstract class AbstractFunctionalTestCase extends FunctionalTestCase | ||
{ | ||
protected static $testablePersistenceEnabled = true; | ||
} |
213 changes: 213 additions & 0 deletions
213
Tests/Functional/ContentRepository/NodeTranslationServiceTest.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,213 @@ | ||
<?php | ||
|
||
namespace Sitegeist\LostInTranslation\Tests\Functional\ContentRepository; | ||
|
||
use Faker\Factory; | ||
use Faker\Generator; | ||
use Neos\ContentRepository\Domain\Model\NodeInterface; | ||
use Neos\ContentRepository\Domain\Model\NodeTemplate; | ||
use Neos\ContentRepository\Domain\Model\NodeType; | ||
use Neos\ContentRepository\Domain\Model\Workspace; | ||
use Neos\ContentRepository\Domain\Repository\WorkspaceRepository; | ||
use Neos\ContentRepository\Domain\Service\Context; | ||
use Neos\ContentRepository\Domain\Service\ContextFactoryInterface; | ||
use Neos\ContentRepository\Domain\Service\NodeTypeManager; | ||
use Neos\ContentRepository\Exception\NodeException; | ||
use Neos\Flow\Utility\Algorithms; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Sitegeist\LostInTranslation\ContentRepository\NodeTranslationService; | ||
use Sitegeist\LostInTranslation\Infrastructure\DeepL\DeepLTranslationService; | ||
use Sitegeist\LostInTranslation\Tests\Functional\AbstractFunctionalTestCase; | ||
|
||
class NodeTranslationServiceTest extends AbstractFunctionalTestCase | ||
{ | ||
/** | ||
* @var Context | ||
*/ | ||
protected $germanUserContext; | ||
|
||
/** | ||
* @var Context | ||
*/ | ||
protected $germanLiveContext; | ||
|
||
/** | ||
* @var Context | ||
*/ | ||
protected $englishLiveContext; | ||
|
||
/** | ||
* @var Context | ||
*/ | ||
protected $italianLiveContext; | ||
|
||
/** | ||
* @var Context | ||
*/ | ||
protected $italianUserContext; | ||
|
||
/** | ||
* @var ContextFactoryInterface | ||
*/ | ||
protected $contextFactory; | ||
|
||
/** | ||
* @var Workspace | ||
*/ | ||
protected $liveWorkspace; | ||
|
||
/** | ||
* @var Workspace | ||
*/ | ||
protected $userWorkspace; | ||
|
||
/** | ||
* @var DeepLTranslationService|MockObject | ||
*/ | ||
protected $deeplServiceMock; | ||
|
||
/** | ||
* @var Generator | ||
*/ | ||
protected $germanFaker; | ||
|
||
/** | ||
* @var Generator | ||
*/ | ||
protected $englishFaker; | ||
|
||
/** | ||
* @var Generator | ||
*/ | ||
protected $italianFaker; | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$workspaceRepository = $this->objectManager->get(WorkspaceRepository::class); | ||
$this->liveWorkspace = new Workspace('live'); | ||
$workspaceRepository->add($this->liveWorkspace); | ||
$this->userWorkspace = new Workspace('test', $this->liveWorkspace); | ||
$workspaceRepository->add($this->userWorkspace); | ||
$this->persistenceManager->persistAll(); | ||
$this->contextFactory = $this->objectManager->get(ContextFactoryInterface::class); | ||
// The root live context, where our original content is live | ||
$this->germanLiveContext = $this->contextFactory->create(['workspaceName' => 'live', 'dimensions' => ['language' => ['de']]]); | ||
// The root user context, where the test user creates content, which is eventually published to live | ||
$this->germanUserContext = $this->contextFactory->create(['workspaceName' => 'test', 'dimensions' => ['language' => ['de']]]); | ||
// The English live context, where content from the German live context is synced automatically | ||
$this->englishLiveContext = $this->contextFactory->create(['workspaceName' => 'live', 'dimensions' => ['language' => ['en']]]); | ||
// The Italian live context, where content from the German live context is only copied once on node adoption | ||
$this->italianLiveContext = $this->contextFactory->create(['workspaceName' => 'live', 'dimensions' => ['language' => ['it']]]); | ||
$this->italianUserContext = $this->contextFactory->create(['workspaceName' => 'test', 'dimensions' => ['language' => ['it']]]); | ||
|
||
// Mock | ||
$this->deeplServiceMock = $this->getMockBuilder(DeepLTranslationService::class)->getMock(); | ||
$nodeTranslationService = $this->objectManager->get(NodeTranslationService::class); | ||
$this->inject($nodeTranslationService, 'translationService', $this->deeplServiceMock); | ||
|
||
// Fakers | ||
$this->germanFaker = Factory::create('de_DE'); | ||
$this->englishFaker = Factory::create('en_GB'); | ||
$this->italianFaker = Factory::create('it_IT'); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
$this->inject($this->contextFactory, 'contextInstances', []); | ||
} | ||
|
||
/** | ||
* @test | ||
* @return void | ||
* @throws NodeException | ||
*/ | ||
public function newNodeInGermanIsAutomaticallyAndCorrectlySyncedToEnglish(): void | ||
{ | ||
$germanString = $this->germanFaker->text(100); | ||
$englishString = $this->englishFaker->text(100); | ||
$this->deeplServiceMock->method('translate')->with(['inlineEditableStringProperty' => $germanString], 'en')->willReturn(['inlineEditableStringProperty' => $englishString]); | ||
|
||
$nodeInGerman = $this->createTestNode(['inlineEditableStringProperty' => $germanString, 'stringProperty' => $germanString]); | ||
$this->userWorkspace->publishNode($nodeInGerman, $this->liveWorkspace); | ||
$nodeInEnglish = $this->englishLiveContext->getNode('/new-node'); | ||
|
||
$this->assertTrue(!is_null($nodeInEnglish), 'The node in German was automatically synced into English'); | ||
$this->assertEquals($englishString, $nodeInEnglish->getProperty('inlineEditableStringProperty'), 'The inline editable property was translated into English'); | ||
$this->assertEquals($germanString, $nodeInEnglish->getProperty('stringProperty'), 'The non-inline editable property was not translated into English'); | ||
} | ||
|
||
/** | ||
* @test | ||
* @return void | ||
* @throws NodeException | ||
*/ | ||
public function newNodeInGermanIsNotAutomaticallySyncedToItalian(): void | ||
{ | ||
$this->deeplServiceMock->method('translate') | ||
->withConsecutive([['inlineEditableStringProperty' => 'Hallo Welt!'], 'en'], [['inlineEditableStringProperty' => 'Hallo Welt!'], 'it']) | ||
->willReturnOnConsecutiveCalls(['inlineEditableStringProperty' => 'Hello World!'], ['inlineEditableStringProperty' => 'Hello World!']); | ||
|
||
$nodeInGerman = $this->createTestNode(); | ||
$this->userWorkspace->publishNode($nodeInGerman, $this->liveWorkspace); | ||
|
||
$nodeInItalian = $this->italianLiveContext->getNode('/new-node'); | ||
$this->assertTrue(is_null($nodeInItalian), 'The node in German was not automatically synced into Italian'); | ||
} | ||
|
||
/** | ||
* @test | ||
* @return void | ||
* @throws NodeException | ||
*/ | ||
public function newNodeInGermanIsOnAdoptionSyncedToItalian(): void | ||
{ | ||
$this->deeplServiceMock->method('translate') | ||
->withConsecutive([['inlineEditableStringProperty' => 'Hallo Welt!'], 'en'], [['inlineEditableStringProperty' => 'Hallo Welt!'], 'it']) | ||
->willReturnOnConsecutiveCalls(['inlineEditableStringProperty' => 'Hello World!'], ['inlineEditableStringProperty' => 'Ciao mondo!']); | ||
|
||
$nodeInGerman = $this->createTestNode(['inlineEditableStringProperty' => 'Hallo Welt!', 'stringProperty' => 'Hallo Welt!']); | ||
$this->userWorkspace->publishNode($nodeInGerman, $this->liveWorkspace); | ||
$nodeInItalian = $this->italianUserContext->adoptNode($nodeInGerman); | ||
|
||
$this->assertTrue(!is_null($nodeInItalian), 'The node in German was automatically synced into Italian'); | ||
$this->assertEquals('Ciao mondo!', $nodeInItalian->getProperty('inlineEditableStringProperty'), 'The inline editable property was translated into Italian'); | ||
$this->assertEquals('Hallo Welt!', $nodeInItalian->getProperty('stringProperty'), 'The non-inline editable property was not translated into Italian'); | ||
} | ||
|
||
protected function getNodeType(string $nodeType): ?NodeType | ||
{ | ||
$nodeTypeManager = $this->objectManager->get(NodeTypeManager::class); | ||
return $nodeTypeManager->getNodeType($nodeType); | ||
} | ||
|
||
/** | ||
* @param array $properties | ||
* @param string $name | ||
* | ||
* @return NodeInterface | ||
* @throws \Exception | ||
*/ | ||
protected function createTestNode(array $properties = [], string $name = 'new-node'): NodeInterface | ||
{ | ||
$identifier = Algorithms::generateUUID(); | ||
$template = new NodeTemplate(); | ||
$template->setName($name); | ||
$template->setIdentifier($identifier); | ||
$template->setNodeType($this->getNodeType('Sitegeist.LostInTranslation.Testing:NodeWithAutomaticTranslation')); | ||
foreach ($properties as $propertyName => $propertyValue) { | ||
$template->setProperty($propertyName, $propertyValue); | ||
} | ||
|
||
$rootNodeInSourceContext = $this->germanUserContext->getRootNode(); | ||
|
||
return $rootNodeInSourceContext->createNodeFromTemplate($template); | ||
} | ||
} |
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
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