Skip to content

Commit dd81d1b

Browse files
committed
[TASK] Add regression test for issue #395
Issue #395 has been recently fixed for 4.x and 5.x with following pull-requests: * #397 * #398 Adding regression test has been omited due to issues within the `typo3/testing-framework` already. The major missing part boils down to an incomplete frontend backend user authentication setup, reported by Daniel Siepmann [1] and provided a pull-request [2]. * [1] Issue: TYPO3/testing-framework#535 * [2] Pull-request: TYPO3/testing-framework#536 The pull-request has been adopted by providing a test-fixture extension replacing the BackendUserHandler with a fixed version. Sadly, this required an additional temporary workaround within the functional test bootstrap, enforce loading package infor for the testing-framework extensions. That needs to be fixed within `typo3/testing-framework`, enforce loading the package info in Testbase->linkFrameworkExtensionsToInstance` when linking the framework extension - or refactor the linking all together. For ongoing work to add TYPO3 v13 support we take the workarounds to continue the work having regression test in place and do not wait until resolved in the testing-framework which needs adoptions in the TYPO3 monorepo and therefore some coordination in fix, release and upgrade. SiteBasedTestTrait related reporting for new test case are added to the baseline for now, needs to be addressed in general with a dedcated change. Used command(s): ```bash Build/Scripts/runTests.sh -t 12 -p 8.1 -s phpstanGenerateBaseline ``` Related: #395 Related: #398 Related: #397
1 parent 98af8ca commit dd81d1b

File tree

12 files changed

+359
-1
lines changed

12 files changed

+359
-1
lines changed

Build/phpstan/Core12/phpstan-baseline.neon

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,56 @@ parameters:
335335
count: 1
336336
path: ../../../Tests/Functional/Regression/LocalizationInlineRegressionTest.php
337337

338+
-
339+
message: "#^Method WebVision\\\\Deepltranslate\\\\Core\\\\Tests\\\\Functional\\\\Regression\\\\PreviewTranslationInformationTest\\:\\:buildDefaultLanguageConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
340+
count: 1
341+
path: ../../../Tests/Functional/Regression/PreviewTranslationInformationTest.php
342+
343+
-
344+
message: "#^Method WebVision\\\\Deepltranslate\\\\Core\\\\Tests\\\\Functional\\\\Regression\\\\PreviewTranslationInformationTest\\:\\:buildErrorHandlingConfiguration\\(\\) has parameter \\$codes with no value type specified in iterable type array\\.$#"
345+
count: 1
346+
path: ../../../Tests/Functional/Regression/PreviewTranslationInformationTest.php
347+
348+
-
349+
message: "#^Method WebVision\\\\Deepltranslate\\\\Core\\\\Tests\\\\Functional\\\\Regression\\\\PreviewTranslationInformationTest\\:\\:buildErrorHandlingConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
350+
count: 1
351+
path: ../../../Tests/Functional/Regression/PreviewTranslationInformationTest.php
352+
353+
-
354+
message: "#^Method WebVision\\\\Deepltranslate\\\\Core\\\\Tests\\\\Functional\\\\Regression\\\\PreviewTranslationInformationTest\\:\\:buildLanguageConfiguration\\(\\) has parameter \\$fallbackIdentifiers with no value type specified in iterable type array\\.$#"
355+
count: 1
356+
path: ../../../Tests/Functional/Regression/PreviewTranslationInformationTest.php
357+
358+
-
359+
message: "#^Method WebVision\\\\Deepltranslate\\\\Core\\\\Tests\\\\Functional\\\\Regression\\\\PreviewTranslationInformationTest\\:\\:buildLanguageConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
360+
count: 1
361+
path: ../../../Tests/Functional/Regression/PreviewTranslationInformationTest.php
362+
363+
-
364+
message: "#^Method WebVision\\\\Deepltranslate\\\\Core\\\\Tests\\\\Functional\\\\Regression\\\\PreviewTranslationInformationTest\\:\\:failIfArrayIsNotEmpty\\(\\) has parameter \\$items with no value type specified in iterable type array\\.$#"
365+
count: 1
366+
path: ../../../Tests/Functional/Regression/PreviewTranslationInformationTest.php
367+
368+
-
369+
message: "#^Method WebVision\\\\Deepltranslate\\\\Core\\\\Tests\\\\Functional\\\\Regression\\\\PreviewTranslationInformationTest\\:\\:mergeSiteConfiguration\\(\\) has parameter \\$overrides with no value type specified in iterable type array\\.$#"
370+
count: 1
371+
path: ../../../Tests/Functional/Regression/PreviewTranslationInformationTest.php
372+
373+
-
374+
message: "#^Method WebVision\\\\Deepltranslate\\\\Core\\\\Tests\\\\Functional\\\\Regression\\\\PreviewTranslationInformationTest\\:\\:writeSiteConfiguration\\(\\) has parameter \\$errorHandling with no value type specified in iterable type array\\.$#"
375+
count: 1
376+
path: ../../../Tests/Functional/Regression/PreviewTranslationInformationTest.php
377+
378+
-
379+
message: "#^Method WebVision\\\\Deepltranslate\\\\Core\\\\Tests\\\\Functional\\\\Regression\\\\PreviewTranslationInformationTest\\:\\:writeSiteConfiguration\\(\\) has parameter \\$languages with no value type specified in iterable type array\\.$#"
380+
count: 1
381+
path: ../../../Tests/Functional/Regression/PreviewTranslationInformationTest.php
382+
383+
-
384+
message: "#^Method WebVision\\\\Deepltranslate\\\\Core\\\\Tests\\\\Functional\\\\Regression\\\\PreviewTranslationInformationTest\\:\\:writeSiteConfiguration\\(\\) has parameter \\$site with no value type specified in iterable type array\\.$#"
385+
count: 1
386+
path: ../../../Tests/Functional/Regression/PreviewTranslationInformationTest.php
387+
338388
-
339389
message: "#^Cannot access property \\$code on DeepL\\\\Language\\|null\\.$#"
340390
count: 7

Build/phpstan/Core12/phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ parameters:
1616
- ../../../.Build/*
1717
- ../../../Tests/Functional/Updates/Fixtures/Extension/test_extension/ext_emconf.php
1818
- ../../../Tests/Functional/Fixtures/Extensions/test_services_override/ext_emconf.php
19+
- ../../../Tests/Functional/Fixtures/Extensions/testing_framework_backenduserhandler_replacement/ext_emconf.php
1920

2021
typo3:
2122
contextApiGetAspectMapping:
2223
'frontend.preview': TYPO3\CMS\Frontend\Aspect\PreviewAspect
24+
requestGetAttributeMapping:
25+
'typo3.testing.context': TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequestContext

Build/phpunit/FunctionalTestsBootstrap.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,30 @@
2323
* before instantiating the test suites.
2424
*/
2525
(static function () {
26+
/**
27+
* @todo Fix testing-framework extension package information loading within the framework and remove workaround
28+
* here after upgrade to testing-framework release containing the fix.
29+
*/
30+
$frameworkExtension = [
31+
'Resources/Core/Functional/Extensions/json_response',
32+
'Resources/Core/Functional/Extensions/private_container',
33+
];
34+
$composerPackageManager = new \TYPO3\TestingFramework\Composer\ComposerPackageManager();
35+
$testingFrameworkPath = $composerPackageManager->getPackageInfo('typo3/testing-framework')->getRealPath();
36+
foreach ($frameworkExtension as $frameworkExtensionPath) {
37+
$packageInfo = $composerPackageManager->getPackageInfoWithFallback(rtrim($testingFrameworkPath, '/') . '/' . $frameworkExtensionPath);
38+
if ($packageInfo === null) {
39+
throw new \RuntimeException(
40+
sprintf(
41+
'Could not preload "typo3/testing-framework" extension "%s".',
42+
basename($frameworkExtensionPath),
43+
),
44+
1734217315,
45+
);
46+
}
47+
}
48+
49+
// Original typo3/testing-framework bootstrap
2650
$testbase = new \TYPO3\TestingFramework\Core\Testbase();
2751
$testbase->defineOriginalRootPath();
2852
$testbase->createDirectory(ORIGINAL_ROOT . 'typo3temp/var/tests');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WebVision\TestingFrameworkBackendUserHandlerReplacement\Middleware;
6+
7+
use Psr\Http\Message\ResponseInterface;
8+
use Psr\Http\Message\ServerRequestInterface;
9+
use Psr\Http\Server\MiddlewareInterface;
10+
use Psr\Http\Server\RequestHandlerInterface;
11+
use TYPO3\CMS\Backend\FrontendBackendUserAuthentication;
12+
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
13+
use TYPO3\CMS\Core\Context\Context;
14+
use TYPO3\CMS\Core\Context\UserAspect;
15+
use TYPO3\CMS\Core\Context\WorkspaceAspect;
16+
use TYPO3\CMS\Core\Database\ConnectionPool;
17+
use TYPO3\CMS\Core\Utility\GeneralUtility;
18+
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequestContext;
19+
20+
/**
21+
* Handler for backend user
22+
*/
23+
class BackendUserHandler implements \TYPO3\CMS\Core\SingletonInterface, MiddlewareInterface
24+
{
25+
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
26+
{
27+
/** @var InternalRequestContext $internalRequestContext */
28+
$internalRequestContext = $request->getAttribute('typo3.testing.context');
29+
$backendUserId = $internalRequestContext->getBackendUserId();
30+
$workspaceId = $internalRequestContext->getWorkspaceId() ?? 0;
31+
32+
if ((int)$backendUserId === 0) {
33+
// Skip if $backendUserId is invalid, typically null or 0
34+
return $handler->handle($request);
35+
}
36+
37+
$row = GeneralUtility::makeInstance(ConnectionPool::class)
38+
->getConnectionForTable('be_users')
39+
->select(['*'], 'be_users', ['uid' => $backendUserId])
40+
->fetchAssociative();
41+
if ($row !== false) {
42+
// Init backend user if found in database
43+
$backendUser = GeneralUtility::makeInstance(FrontendBackendUserAuthentication::class);
44+
$backendUser->user = $row;
45+
$backendUser->uc = isset($row['uc']) ? unserialize($row['uc']) : [];
46+
$backendUser->initializeUserSessionManager();
47+
$backendUser->setTemporaryWorkspace($workspaceId);
48+
$GLOBALS['BE_USER'] = $backendUser;
49+
$this->setBackendUserAspect(GeneralUtility::makeInstance(Context::class), $backendUser);
50+
}
51+
return $handler->handle($request);
52+
}
53+
54+
/**
55+
* Register the backend user as aspect
56+
*/
57+
protected function setBackendUserAspect(Context $context, BackendUserAuthentication $user): void
58+
{
59+
$context->setAspect('backend.user', GeneralUtility::makeInstance(UserAspect::class, $user));
60+
$context->setAspect('workspace', GeneralUtility::makeInstance(WorkspaceAspect::class, $user->workspace));
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
return [
4+
'frontend' => [
5+
'typo3/json-response/backend-user-authentication' => [
6+
/**
7+
* Replace {@see \TYPO3\JsonResponse\Middleware\BackendUserHandler} as target here to incorporate
8+
* `typo3/testing-framework` pull-request https://github.com/TYPO3/testing-framework/pull/536 as
9+
* a workaround until resolved within testing-framework.
10+
*
11+
* @todo Remove test-fixture extension completely when fixed within typo3/testing-framework. Also requires
12+
* temporary workaround in {@see Build/phpunit/FunctionalTestsBootstrap.php} to force framework
13+
* extension loading to have have package information available - otherwise dependency ordering would
14+
* not work. Needs to be resolved in the testing-framework.
15+
*/
16+
'target' => \WebVision\TestingFrameworkBackendUserHandlerReplacement\Middleware\BackendUserHandler::class,
17+
],
18+
],
19+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
services:
2+
_defaults:
3+
autowire: true
4+
autoconfigure: true
5+
public: false
6+
7+
WebVision\TestingFrameworkBackendUserHandlerReplacement\:
8+
resource: '../Classes/*'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "web-vision/testing-framework-backenduserhandler-replacement",
3+
"type": "typo3-cms-extension",
4+
"description": "Change service registrations for testing purposes.",
5+
"license": ["GPL-2.0-or-later"],
6+
"extra": {
7+
"typo3/cms": {
8+
"extension-key": "testing_framework_backenduserhandler_replacement"
9+
}
10+
},
11+
"require": {
12+
"typo3/cms-core": "12.*.*@dev || 13.*.*@dev",
13+
"typo3/testing-json-response": "*"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"WebVision\\TestingFrameworkBackendUserHandlerReplacement\\": "Classes"
18+
}
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
$EM_CONF[$_EXTKEY] = [
4+
'title' => 'TF BackendUserHandler replacement',
5+
'description' => 'TF BackendUserHandler replacement',
6+
'category' => 'example',
7+
'version' => '1.0.0',
8+
'state' => 'beta',
9+
'createDirs' => '',
10+
'clearCacheOnLoad' => 0,
11+
'author' => 'Stefan Bürk',
12+
'author_email' => '[email protected]',
13+
'author_company' => '',
14+
'constraints' => [
15+
'depends' => [
16+
'typo3' => '12.4.0-13.4.99',
17+
'json_response' => '*',
18+
],
19+
'conflicts' => [],
20+
'suggests' => [],
21+
],
22+
];
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pages
2+
,"uid","pid",doktype,"hidden","title",subtitle,"sys_language_uid","l10n_parent","slug","tx_wvdeepltranslate_content_not_checked","tx_wvdeepltranslate_translated_time"
3+
,1,0,1,0,"Deepl-Functional-Test","",0,0,"/",0,0
4+
,2,0,1,0,"Artikel","",1,2,"//",0,0
5+
,3,1,1,0,"Articles","",0,0,"/articles/",0,0
6+
,4,1,1,1,"Artikel","",1,3,"/artikel/",1,1734104657
7+
"be_users"
8+
,"uid","pid","tstamp","username","password","admin","disable","starttime","endtime","options","crdate","workspace_perms","deleted","TSconfig","lastlogin","workspace_id","db_mountpoints"
9+
# The password is "password"
10+
,1,0,1366642540,"admin","$1$tCrlLajZ$C0sikFQQ3SWaFAZ1Me0Z/1",1,0,0,0,0,1366642540,1,0,,1371033743,0,1
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
page = PAGE
2+
page.100 = CONTENT
3+
page.100 {
4+
table = tt_content
5+
select {
6+
orderBy = sorting
7+
where = {#colPos}=0
8+
}
9+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WebVision\Deepltranslate\Core\Tests\Functional\Regression;
6+
7+
use TYPO3\CMS\Core\Localization\LanguageServiceFactory;
8+
use TYPO3\CMS\Core\Utility\GeneralUtility;
9+
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
10+
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequestContext;
11+
use WebVision\Deepltranslate\Core\Tests\Functional\AbstractDeepLTestCase;
12+
use WebVision\Deepltranslate\Core\Tests\Functional\Fixtures\Traits\SiteBasedTestTrait;
13+
14+
final class PreviewTranslationInformationTest extends AbstractDeepLTestCase
15+
{
16+
use SiteBasedTestTrait;
17+
18+
protected const LANGUAGE_PRESETS = [
19+
'EN' => [
20+
'id' => 0,
21+
'title' => 'English',
22+
'locale' => 'en_US.UTF-8',
23+
'iso' => 'en',
24+
'hrefLang' => 'en-US',
25+
'direction' => '',
26+
'custom' => [
27+
'deeplTargetLanguage' => '',
28+
'deeplAllowedAutoTranslate' => false,
29+
'deeplAllowedReTranslate' => false,
30+
],
31+
],
32+
'DE' => [
33+
'id' => 1,
34+
'title' => 'Deutsch',
35+
'locale' => 'de_DE',
36+
'iso' => 'de',
37+
'hrefLang' => 'de-DE',
38+
'direction' => '',
39+
'custom' => [
40+
'deeplTargetLanguage' => 'DE',
41+
'deeplAllowedAutoTranslate' => true,
42+
'deeplAllowedReTranslate' => true,
43+
],
44+
],
45+
];
46+
47+
protected array $configurationToUseInTestInstance = [
48+
'EXTENSIONS' => [
49+
'wv_deepltranslate' => [
50+
'apiKey' => 'mock_server',
51+
],
52+
],
53+
];
54+
55+
protected array $pathsToProvideInTestInstance = [
56+
'typo3conf/ext/deepltranslate_core/Tests/Functional/Regression/Fixtures/Files' => 'fileadmin',
57+
];
58+
59+
protected function setUp(): void
60+
{
61+
$this->coreExtensionsToLoad[] = 'typo3/cms-fluid-styled-content';
62+
$this->testExtensionsToLoad[] = __DIR__ . '/../Fixtures/Extensions/testing_framework_backenduserhandler_replacement';
63+
parent::setUp();
64+
$this->importCSVDataSet(__DIR__ . '/Fixtures/PreviewTranslationInformation.csv');
65+
$this->writeSiteConfiguration(
66+
'acme',
67+
$this->buildSiteConfiguration(1, 'https://acme.com/', 'Home', [
68+
'deeplAllowedAutoTranslate' => true,
69+
'deeplAllowedReTranslate' => true,
70+
]),
71+
[
72+
$this->buildDefaultLanguageConfiguration('EN', 'https://acme.com/'),
73+
$this->buildLanguageConfiguration('DE', 'https://acme.com/de/', ['EN'], 'strict'),
74+
]
75+
);
76+
$this->setUpFrontendRootPage(
77+
1,
78+
[
79+
'constants' => [
80+
'EXT:fluid_styled_content/Configuration/TypoScript/constants.typoscript',
81+
'EXT:fluid_styled_content/Configuration/TypoScript/Styling/constants.typoscript',
82+
],
83+
'setup' => [
84+
'EXT:fluid_styled_content/Configuration/TypoScript/setup.typoscript',
85+
'EXT:fluid_styled_content/Configuration/TypoScript/Styling/setup.typoscript',
86+
'EXT:deepltranslate_core/Tests/Functional/Regression/Fixtures/PreviewTranslationInformation.typoscript',
87+
],
88+
],
89+
[
90+
'title' => 'ACME Root',
91+
]
92+
);
93+
$this->setUpBackendUser(1);
94+
$GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageServiceFactory::class)
95+
->createFromUserPreferences($GLOBALS['BE_USER']);
96+
}
97+
98+
/**
99+
* @test
100+
*/
101+
public function previewTranslationInformationIsRenderedForTranslatedPage(): void
102+
{
103+
$styles = [];
104+
$styles[] = 'position: fixed';
105+
$styles[] = 'top: 65px';
106+
$styles[] = 'right: 15px';
107+
$styles[] = 'padding: 8px 18px';
108+
$styles[] = 'background: #006494';
109+
$styles[] = 'border: 1px solid #006494';
110+
$styles[] = 'font-family: sans-serif';
111+
$styles[] = 'font-size: 14px';
112+
$styles[] = 'font-weight: bold';
113+
$styles[] = 'color: #fff';
114+
$styles[] = 'z-index: 20000';
115+
$styles[] = 'user-select: none';
116+
$styles[] = 'pointer-events: none';
117+
$styles[] = 'text-align: center';
118+
$styles[] = 'border-radius: 2px';
119+
$expectedContent = '<div id="deepl-preview-info" style="' . implode(';', $styles) . '">' . htmlspecialchars('Translated with DeepL') . '</div>';
120+
121+
$requestContext = (new InternalRequestContext())->withBackendUserId(1);
122+
$request = new InternalRequest('https://acme.com/de/artikel/');
123+
$response = $this->executeFrontendSubRequest($request, $requestContext);
124+
static::assertSame(200, $response->getStatusCode());
125+
126+
$content = (string)$response->getBody();
127+
static::assertNotEmpty($content);
128+
static::assertStringContainsString($expectedContent, $content, 'preview translation label is rendered in frontend preview');
129+
}
130+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@
125125
},
126126
"autoload-dev": {
127127
"psr-4": {
128-
"WebVision\\Deepltranslate\\Core\\Tests\\": "Tests"
128+
"WebVision\\Deepltranslate\\Core\\Tests\\": "Tests",
129+
"WebVision\\TestingFrameworkBackendUserHandlerReplacement\\": "Tests/Functional/Fixtures/Extensions/testing_framework_backenduserhandler_replacement/Classes"
129130
}
130131
},
131132
"scripts": {

0 commit comments

Comments
 (0)