|
| 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