Skip to content

Commit 26696ae

Browse files
Progi1984Progi1984
authored andcommitted
Added endpoints for domain "Theme"
1 parent 20fb223 commit 26696ae

File tree

4 files changed

+360
-0
lines changed

4 files changed

+360
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 [email protected] so we can send you a copy immediately.
15+
*
16+
* @author PrestaShop SA and Contributors <[email protected]>
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\ApiProperty;
26+
use ApiPlatform\Metadata\ApiResource;
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 PrestaShop\Module\APIResources\ApiPlatform\Serializer\Theme as ThemeSerializer;
37+
use Symfony\Component\HttpFoundation\Response;
38+
use Symfony\Component\Validator\Constraints as Assert;
39+
40+
#[ApiResource(
41+
operations: [
42+
new CQRSDelete(
43+
uriTemplate: '/theme/{themeName}',
44+
output: false,
45+
CQRSCommand: DeleteThemeCommand::class,
46+
scopes: ['theme_write'],
47+
denormalizationContext: [
48+
'callbacks' => [
49+
'themeName' => [ThemeSerializer::class, 'toThemeName']
50+
]
51+
]
52+
),
53+
new CQRSUpdate(
54+
uriTemplate: '/theme/{themeName}/adapt-to-rtl',
55+
output: false,
56+
allowEmptyBody: true,
57+
CQRSCommand: AdaptThemeToRTLLanguagesCommand::class,
58+
scopes: ['theme_write'],
59+
),
60+
new CQRSUpdate(
61+
uriTemplate: '/theme/{themeName}/enable',
62+
output: false,
63+
allowEmptyBody: true,
64+
CQRSCommand: EnableThemeCommand::class,
65+
scopes: ['theme_write'],
66+
exceptionToStatus: [
67+
CannotEnableThemeException::class => Response::HTTP_UNPROCESSABLE_ENTITY,
68+
ThemeConstraintException::class => Response::HTTP_UNPROCESSABLE_ENTITY,
69+
],
70+
),
71+
new CQRSUpdate(
72+
uriTemplate: '/theme/{themeName}/reset',
73+
output: false,
74+
allowEmptyBody: true,
75+
CQRSCommand: ResetThemeLayoutsCommand::class,
76+
scopes: ['theme_write'],
77+
),
78+
],
79+
normalizationContext: ['skip_null_values' => false],
80+
denormalizationContext: [
81+
'callbacks' => [
82+
'themeName' => [ThemeSerializer::class, 'toThemeName']
83+
]
84+
],
85+
)]
86+
class Theme
87+
{
88+
public ThemeName $themeName;
89+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 [email protected] so we can send you a copy immediately.
15+
*
16+
* @author PrestaShop SA and Contributors <[email protected]>
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\ApiProperty;
26+
use ApiPlatform\Metadata\ApiResource;
27+
use PrestaShop\PrestaShop\Core\Domain\Theme\Command\ImportThemeCommand;
28+
use PrestaShop\PrestaShop\Core\Domain\Theme\ValueObject\ThemeImportSource;
29+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSDelete;
30+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate;
31+
use PrestaShop\Module\APIResources\ApiPlatform\Serializer\Theme as ThemeSerializer;
32+
use Symfony\Component\HttpFoundation\Response;
33+
use Symfony\Component\Validator\Constraints as Assert;
34+
35+
#[ApiResource(
36+
operations: [
37+
new CQRSUpdate(
38+
uriTemplate: '/theme/import',
39+
output: false,
40+
CQRSCommand: ImportThemeCommand::class,
41+
CQRSCommandMapping: [
42+
'[importSource]' => '[importSource]',
43+
],
44+
normalizationContext: [
45+
'skip_null_values' => false,
46+
],
47+
denormalizationContext: [
48+
'disable_type_enforcement' => true,
49+
'allow_extra_attributes' => true,
50+
'callbacks' => [
51+
'importSource' => [ThemeSerializer::class, 'toThemeImportSource']
52+
],
53+
],
54+
),
55+
],
56+
)]
57+
class ThemeImport
58+
{
59+
public $importSource;
60+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 [email protected] so we can send you a copy immediately.
15+
*
16+
* @author PrestaShop SA and Contributors <[email protected]>
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\ThemeName;
26+
use PrestaShop\PrestaShop\Core\Domain\Theme\ValueObject\ThemeImportSource;
27+
use Symfony\Component\HttpFoundation\File\UploadedFile;
28+
29+
class Theme
30+
{
31+
public static function toThemeImportSource(array $value): ThemeImportSource
32+
{
33+
switch($value['sourceType']) {
34+
case ThemeImportSource::FROM_ARCHIVE:
35+
return ThemeImportSource::fromArchive($value['source']);
36+
break;
37+
case ThemeImportSource::FROM_WEB:
38+
return ThemeImportSource::fromWeb($value['source']);
39+
break;
40+
case ThemeImportSource::FROM_FTP:
41+
return ThemeImportSource::fromFtp($value['source']);
42+
break;
43+
}
44+
}
45+
46+
public static function toThemeName(string $value): ThemeName
47+
{
48+
return new ThemeName($value);
49+
}
50+
}
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 [email protected] so we can send you a copy immediately.
16+
*
17+
* @author PrestaShop SA and Contributors <[email protected]>
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+
'/theme/classic/adapt-to-rtl',
66+
];
67+
yield 'update endpoint (Enable)' => [
68+
'PUT',
69+
'/theme/classic/enable',
70+
];
71+
yield 'update endpoint (Reset)' => [
72+
'PUT',
73+
'/theme/classic/reset',
74+
];
75+
yield 'delete endpoint' => [
76+
'DELETE',
77+
'/theme/classic',
78+
];
79+
}
80+
81+
public function testAdaptToRtl(): void
82+
{
83+
$themeName = 'classic';
84+
85+
self::assertEquals(false, $this->isThemeAdaptedToRTL($themeName));
86+
$this->updateItem('/theme/' . $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('/theme/' . $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('/theme/' . $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('/theme/' . $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+
'/theme/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)