Skip to content

Commit 407476e

Browse files
Progi1984Progi1984
authored andcommitted
Added endpoints for domain "Theme"
1 parent e17f0e1 commit 407476e

File tree

4 files changed

+352
-0
lines changed

4 files changed

+352
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Academic Free License version 3.0
9+
* that is bundled with this package in the file LICENSE.md.
10+
* It is also available through the world-wide-web at this URL:
11+
* https://opensource.org/licenses/AFL-3.0
12+
* If you did not receive a copy of the license and are unable to
13+
* obtain it through the world-wide-web, please send an email
14+
* to license@prestashop.com so we can send you a copy immediately.
15+
*
16+
* @author PrestaShop SA and Contributors <contact@prestashop.com>
17+
* @copyright Since 2007 PrestaShop SA and Contributors
18+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\Theme;
24+
25+
use ApiPlatform\Metadata\ApiResource;
26+
use PrestaShop\Module\APIResources\ApiPlatform\Serializer\Theme as ThemeSerializer;
27+
use PrestaShop\PrestaShop\Core\Domain\Theme\Command\AdaptThemeToRTLLanguagesCommand;
28+
use PrestaShop\PrestaShop\Core\Domain\Theme\Command\DeleteThemeCommand;
29+
use PrestaShop\PrestaShop\Core\Domain\Theme\Command\EnableThemeCommand;
30+
use PrestaShop\PrestaShop\Core\Domain\Theme\Command\ResetThemeLayoutsCommand;
31+
use PrestaShop\PrestaShop\Core\Domain\Theme\Exception\CannotEnableThemeException;
32+
use PrestaShop\PrestaShop\Core\Domain\Theme\Exception\ThemeConstraintException;
33+
use PrestaShop\PrestaShop\Core\Domain\Theme\ValueObject\ThemeName;
34+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSDelete;
35+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate;
36+
use Symfony\Component\HttpFoundation\Response;
37+
38+
#[ApiResource(
39+
operations: [
40+
new CQRSDelete(
41+
uriTemplate: '/themes/{themeName}',
42+
output: false,
43+
CQRSCommand: DeleteThemeCommand::class,
44+
scopes: ['theme_write'],
45+
denormalizationContext: [
46+
'callbacks' => [
47+
'themeName' => [ThemeSerializer::class, 'toThemeName'],
48+
],
49+
]
50+
),
51+
new CQRSUpdate(
52+
uriTemplate: '/themes/{themeName}/adapt-to-rtl',
53+
output: false,
54+
allowEmptyBody: true,
55+
CQRSCommand: AdaptThemeToRTLLanguagesCommand::class,
56+
scopes: ['theme_write'],
57+
),
58+
new CQRSUpdate(
59+
uriTemplate: '/themes/{themeName}/enable',
60+
output: false,
61+
allowEmptyBody: true,
62+
CQRSCommand: EnableThemeCommand::class,
63+
scopes: ['theme_write'],
64+
exceptionToStatus: [
65+
CannotEnableThemeException::class => Response::HTTP_UNPROCESSABLE_ENTITY,
66+
ThemeConstraintException::class => Response::HTTP_UNPROCESSABLE_ENTITY,
67+
],
68+
),
69+
new CQRSUpdate(
70+
uriTemplate: '/themes/{themeName}/reset',
71+
output: false,
72+
allowEmptyBody: true,
73+
CQRSCommand: ResetThemeLayoutsCommand::class,
74+
scopes: ['theme_write'],
75+
),
76+
],
77+
normalizationContext: ['skip_null_values' => false],
78+
denormalizationContext: [
79+
'callbacks' => [
80+
'themeName' => [ThemeSerializer::class, 'toThemeName'],
81+
],
82+
],
83+
)]
84+
class Theme
85+
{
86+
public ThemeName $themeName;
87+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Academic Free License version 3.0
9+
* that is bundled with this package in the file LICENSE.md.
10+
* It is also available through the world-wide-web at this URL:
11+
* https://opensource.org/licenses/AFL-3.0
12+
* If you did not receive a copy of the license and are unable to
13+
* obtain it through the world-wide-web, please send an email
14+
* to license@prestashop.com so we can send you a copy immediately.
15+
*
16+
* @author PrestaShop SA and Contributors <contact@prestashop.com>
17+
* @copyright Since 2007 PrestaShop SA and Contributors
18+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\Theme;
24+
25+
use ApiPlatform\Metadata\ApiResource;
26+
use PrestaShop\Module\APIResources\ApiPlatform\Serializer\Theme as ThemeSerializer;
27+
use PrestaShop\PrestaShop\Core\Domain\Theme\Command\ImportThemeCommand;
28+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate;
29+
30+
#[ApiResource(
31+
operations: [
32+
new CQRSUpdate(
33+
uriTemplate: '/themes/import',
34+
output: false,
35+
CQRSCommand: ImportThemeCommand::class,
36+
CQRSCommandMapping: [
37+
'[importSource]' => '[importSource]',
38+
],
39+
normalizationContext: [
40+
'skip_null_values' => false,
41+
],
42+
denormalizationContext: [
43+
'disable_type_enforcement' => true,
44+
'allow_extra_attributes' => true,
45+
'callbacks' => [
46+
'importSource' => [ThemeSerializer::class, 'toThemeImportSource'],
47+
],
48+
],
49+
),
50+
],
51+
)]
52+
class ThemeImport
53+
{
54+
public $importSource;
55+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Academic Free License version 3.0
9+
* that is bundled with this package in the file LICENSE.md.
10+
* It is also available through the world-wide-web at this URL:
11+
* https://opensource.org/licenses/AFL-3.0
12+
* If you did not receive a copy of the license and are unable to
13+
* obtain it through the world-wide-web, please send an email
14+
* to license@prestashop.com so we can send you a copy immediately.
15+
*
16+
* @author PrestaShop SA and Contributors <contact@prestashop.com>
17+
* @copyright Since 2007 PrestaShop SA and Contributors
18+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
19+
*/
20+
21+
declare(strict_types=1);
22+
23+
namespace PrestaShop\Module\APIResources\ApiPlatform\Serializer;
24+
25+
use PrestaShop\PrestaShop\Core\Domain\Theme\ValueObject\ThemeImportSource;
26+
use PrestaShop\PrestaShop\Core\Domain\Theme\ValueObject\ThemeName;
27+
28+
class Theme
29+
{
30+
public static function toThemeImportSource(array $value): ThemeImportSource
31+
{
32+
switch ($value['sourceType']) {
33+
case ThemeImportSource::FROM_ARCHIVE:
34+
return ThemeImportSource::fromArchive($value['source']);
35+
break;
36+
case ThemeImportSource::FROM_WEB:
37+
return ThemeImportSource::fromWeb($value['source']);
38+
break;
39+
case ThemeImportSource::FROM_FTP:
40+
return ThemeImportSource::fromFtp($value['source']);
41+
break;
42+
}
43+
}
44+
45+
public static function toThemeName(string $value): ThemeName
46+
{
47+
return new ThemeName($value);
48+
}
49+
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
/**
4+
* Copyright since 2007 PrestaShop SA and Contributors
5+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
6+
*
7+
* NOTICE OF LICENSE
8+
*
9+
* This source file is subject to the Academic Free License version 3.0
10+
* that is bundled with this package in the file LICENSE.md.
11+
* It is also available through the world-wide-web at this URL:
12+
* https://opensource.org/licenses/AFL-3.0
13+
* If you did not receive a copy of the license and are unable to
14+
* obtain it through the world-wide-web, please send an email
15+
* to license@prestashop.com so we can send you a copy immediately.
16+
*
17+
* @author PrestaShop SA and Contributors <contact@prestashop.com>
18+
* @copyright Since 2007 PrestaShop SA and Contributors
19+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
20+
*/
21+
22+
declare(strict_types=1);
23+
24+
namespace PsApiResourcesTest\Integration\ApiPlatform;
25+
26+
use Db;
27+
use Symfony\Component\HttpFoundation\Response;
28+
use Tests\Resources\DatabaseDump;
29+
30+
class ThemeEndpointTest extends ApiTestCase
31+
{
32+
protected static $dirTheme = _PS_ROOT_DIR_ . '/themes/%s';
33+
34+
protected static $fileThemeRTL = _PS_ROOT_DIR_ . '/themes/%s/assets/css/theme_rtl.css';
35+
36+
public static function setUpBeforeClass(): void
37+
{
38+
parent::setUpBeforeClass();
39+
// Pre-create the API Client with the needed scopes, this way we reduce the number of created API Clients
40+
self::createApiClient(['theme_write']);
41+
42+
self::cleanThemes();
43+
}
44+
45+
public static function tearDownAfterClass(): void
46+
{
47+
parent::tearDownAfterClass();
48+
// Reset DB as it was before this test
49+
DatabaseDump::restoreTables(['shop']);
50+
51+
self::cleanThemes();
52+
}
53+
54+
public static function cleanThemes(): void
55+
{
56+
if (file_exists(sprintf(self::$fileThemeRTL, 'classic'))) {
57+
unlink(sprintf(self::$fileThemeRTL, 'classic'));
58+
}
59+
}
60+
61+
public static function getProtectedEndpoints(): iterable
62+
{
63+
yield 'update endpoint (Adapt To RTL)' => [
64+
'PUT',
65+
'/themes/classic/adapt-to-rtl',
66+
];
67+
yield 'update endpoint (Enable)' => [
68+
'PUT',
69+
'/themes/classic/enable',
70+
];
71+
yield 'update endpoint (Reset)' => [
72+
'PUT',
73+
'/themes/classic/reset',
74+
];
75+
yield 'delete endpoint' => [
76+
'DELETE',
77+
'/themes/classic',
78+
];
79+
}
80+
81+
public function testAdaptToRtl(): void
82+
{
83+
$themeName = 'classic';
84+
85+
self::assertEquals(false, $this->isThemeAdaptedToRTL($themeName));
86+
$this->updateItem('/themes/' . $themeName . '/adapt-to-rtl', [], ['theme_write'], Response::HTTP_NO_CONTENT);
87+
self::assertEquals(true, $this->isThemeAdaptedToRTL($themeName));
88+
}
89+
90+
/**
91+
* @depends testAdaptToRtl
92+
*/
93+
public function testReset(): void
94+
{
95+
$themeName = 'classic';
96+
97+
self::assertEquals('classic', $this->getCurrentTheme());
98+
$this->updateItem('/themes/' . $themeName . '/reset', [], ['theme_write']);
99+
self::assertEquals('classic', $this->getCurrentTheme());
100+
}
101+
102+
/**
103+
* @depends testReset
104+
*/
105+
public function testEnable(): void
106+
{
107+
$themeName = 'hummingbird';
108+
109+
self::assertEquals('classic', $this->getCurrentTheme());
110+
$this->updateItem('/themes/' . $themeName . '/enable', [], ['theme_write'], Response::HTTP_UNPROCESSABLE_ENTITY);
111+
}
112+
113+
/**
114+
* @depends testEnable
115+
*/
116+
public function testDelete(): void
117+
{
118+
$themeName = 'hummingbird';
119+
120+
self::assertEquals(true, $this->hasTheme($themeName));
121+
$this->deleteItem('/themes/' . $themeName, ['theme_write']);
122+
self::assertEquals(false, $this->hasTheme($themeName));
123+
}
124+
125+
/**
126+
* @depends testDelete
127+
*/
128+
public function testImport(): void
129+
{
130+
$themeName = 'hummingbird';
131+
132+
self::assertEquals(false, $this->hasTheme($themeName));
133+
$this->updateItem(
134+
'/themes/import',
135+
[
136+
'importSource' => [
137+
'sourceType' => 'from_web',
138+
'source' => 'https://github.com/PrestaShop/hummingbird/releases/download/v1.0.1/hummingbird.zip',
139+
],
140+
],
141+
['theme_write'],
142+
Response::HTTP_NO_CONTENT
143+
);
144+
self::assertEquals(true, $this->hasTheme($themeName));
145+
}
146+
147+
protected function hasTheme(string $themeName): bool
148+
{
149+
return is_dir(sprintf(self::$dirTheme, $themeName));
150+
}
151+
152+
protected function isThemeAdaptedToRTL(string $themeName): bool
153+
{
154+
return file_exists(sprintf(self::$fileThemeRTL, $themeName));
155+
}
156+
157+
protected function getCurrentTheme(): string
158+
{
159+
return \Db::getInstance()->getValue('SELECT theme_name FROM `' . _DB_PREFIX_ . 'shop` WHERE id_shop="1"');
160+
}
161+
}

0 commit comments

Comments
 (0)