Skip to content

Commit 592f020

Browse files
committed
SW-27057 - fix first run wizard
1 parent 2f42825 commit 592f020

File tree

2 files changed

+145
-9
lines changed

2 files changed

+145
-9
lines changed

engine/Shopware/Controllers/Backend/FirstRunWizardPluginManager.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929

3030
class Shopware_Controllers_Backend_FirstRunWizardPluginManager extends Shopware_Controllers_Backend_ExtJs
3131
{
32+
/**
33+
* @var array<string, LocaleStruct>
34+
*/
35+
public static array $locales = [];
36+
3237
/**
3338
* Loads integrated plugins from SBP
3439
*/
@@ -251,25 +256,23 @@ private function getVersion(): string
251256
*/
252257
private function getCurrentLocale(): ?LocaleStruct
253258
{
254-
static $locales;
255-
256-
if (\is_array($locales) && empty($locales)) {
259+
if (empty(self::$locales)) {
257260
$accountManagerService = $this->container->get('shopware_plugininstaller.account_manager_service');
258261

259262
foreach ($accountManagerService->getLocales() as $serverLocale) {
260-
$locales[$serverLocale->getName()] = $serverLocale;
263+
self::$locales[$serverLocale->getName()] = $serverLocale;
261264
}
262265
}
263266

264-
$localeCode = Shopware()->Container()->get('auth')->getIdentity()->locale->getLocale();
267+
$localeCode = $this->container->get('auth')->getIdentity()->locale->getLocale();
265268

266-
if (\array_key_exists($localeCode, $locales)) {
267-
return $locales[$localeCode];
269+
if (\array_key_exists($localeCode, self::$locales)) {
270+
return self::$locales[$localeCode];
268271
}
269272

270273
// Fallback to english locale when available
271-
if (\array_key_exists('en_GB', $locales)) {
272-
return $locales['en_GB'];
274+
if (\array_key_exists('en_GB', self::$locales)) {
275+
return self::$locales['en_GB'];
273276
}
274277

275278
return null;
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* Shopware 5
6+
* Copyright (c) shopware AG
7+
*
8+
* According to our dual licensing model, this program can be used either
9+
* under the terms of the GNU Affero General Public License, version 3,
10+
* or under a proprietary license.
11+
*
12+
* The texts of the GNU Affero General Public License with an additional
13+
* permission and of our proprietary license can be found at and
14+
* in the LICENSE file you have received along with this program.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* "Shopware" is a registered trademark of shopware AG.
22+
* The licensing of the program under the AGPLv3 does not imply a
23+
* trademark license. Therefore any rights, title and interest in
24+
* our trademarks remain entirely with us.
25+
*/
26+
27+
namespace Shopware\Tests\Unit\Controllers\Backend;
28+
29+
use PHPUnit\Framework\TestCase;
30+
use Shopware\Bundle\PluginInstallerBundle\Service\AccountManagerService;
31+
use Shopware\Bundle\PluginInstallerBundle\Struct\LocaleStruct;
32+
use Shopware\Components\DependencyInjection\Container;
33+
use Shopware\Tests\TestReflectionHelper;
34+
use Shopware_Components_Auth;
35+
use Shopware_Controllers_Backend_FirstRunWizardPluginManager;
36+
37+
class FirstRunWizardPluginManagerTest extends TestCase
38+
{
39+
public function setUp(): void
40+
{
41+
Shopware_Controllers_Backend_FirstRunWizardPluginManager::$locales = [];
42+
}
43+
44+
public function testGetCurrentLocaleWIthouthContent(): void
45+
{
46+
$controller = new Shopware_Controllers_Backend_FirstRunWizardPluginManager();
47+
48+
$localeStruct = new LocaleStruct();
49+
$localeStruct->setName('foo_bar');
50+
$testLocale = 'bar_foo';
51+
52+
$mock = $this->getMockContainer($localeStruct, $testLocale);
53+
54+
$controller->setContainer($mock);
55+
56+
$currentLocaleMethode = TestReflectionHelper::getMethod(\get_class($controller), 'getCurrentLocale');
57+
58+
static::assertNull($currentLocaleMethode->invoke($controller));
59+
}
60+
61+
public function testGetCurrentLocaleWithEnglish(): void
62+
{
63+
$controller = new Shopware_Controllers_Backend_FirstRunWizardPluginManager();
64+
65+
$localeStruct = new LocaleStruct();
66+
$localeStruct->setName('en_GB');
67+
$testLocale = 'bar_foo';
68+
69+
$mock = $this->getMockContainer($localeStruct, $testLocale);
70+
71+
$controller->setContainer($mock);
72+
73+
$currentLocaleMethode = TestReflectionHelper::getMethod(\get_class($controller), 'getCurrentLocale');
74+
75+
static::assertSame('en_GB', $currentLocaleMethode->invoke($controller)->getName());
76+
}
77+
78+
public function testGetCurrentLocaleWithSameContent(): void
79+
{
80+
$controller = new Shopware_Controllers_Backend_FirstRunWizardPluginManager();
81+
82+
$localeStruct = new LocaleStruct();
83+
$localeStruct->setName('de_DE');
84+
$testLocale = 'de_DE';
85+
86+
$mock = $this->getMockContainer($localeStruct, $testLocale);
87+
88+
$controller->setContainer($mock);
89+
90+
$currentLocaleMethode = TestReflectionHelper::getMethod(\get_class($controller), 'getCurrentLocale');
91+
92+
static::assertSame('de_DE', $currentLocaleMethode->invoke($controller)->getName());
93+
}
94+
95+
private function getMockContainer(LocaleStruct $localeStruct, string $testLocale): Container
96+
{
97+
$accountManagerServiceMock = $this->getMockBuilder(AccountManagerService::class)->disableOriginalConstructor()->getMock();
98+
$accountManagerServiceMock->method('getLocales')->willReturn([$localeStruct]);
99+
100+
$auth = $this->getMockBuilder(Shopware_Components_Auth::class)->disableOriginalConstructor()->getMock();
101+
$auth->method('getIdentity')->willReturn(new class($testLocale) {
102+
/**
103+
* @var object
104+
*/
105+
public $locale;
106+
107+
public function __construct(string $testLocale)
108+
{
109+
$this->locale = new class($testLocale) {
110+
/**
111+
* @var string
112+
*/
113+
public $locale;
114+
115+
public function __construct(string $testLocale)
116+
{
117+
$this->locale = $testLocale;
118+
}
119+
120+
public function getLocale(): string
121+
{
122+
return $this->locale;
123+
}
124+
};
125+
}
126+
});
127+
128+
$mock = $this->getMockBuilder(Container::class)->getMock();
129+
$mock->method('get')->willReturnOnConsecutiveCalls($accountManagerServiceMock, $auth);
130+
131+
return $mock;
132+
}
133+
}

0 commit comments

Comments
 (0)