Skip to content

Commit b9dae41

Browse files
authored
fix #1802 テーマフォルダAPI フォルダ編集 (#1815)
1 parent 581b43a commit b9dae41

File tree

7 files changed

+202
-39
lines changed

7 files changed

+202
-39
lines changed

plugins/bc-theme-file/src/Controller/Api/ThemeFoldersController.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ public function add(ThemeFoldersServiceInterface $service)
125125
$data = $this->getRequest()->getData();
126126
$data['fullpath'] = $service->getFullpath($data['theme'], $data['type'], $data['path']);
127127
$form = $service->create($data);
128-
$entity = $service->get($form->getData('fullpath'));
129-
$message = __d('baser', 'フォルダ「{0}」を作成しました。', $entity->name);
128+
$themeFolder = $service->get($form->getData('fullpath'));
129+
$message = __d('baser', 'フォルダ「{0}」を作成しました。', $themeFolder->name);
130130
} catch (BcFormFailedException $e) {
131131
$this->setResponse($this->response->withStatus(400));
132132
$errors = $e->getForm()->getErrors();
@@ -138,21 +138,47 @@ public function add(ThemeFoldersServiceInterface $service)
138138

139139
$this->set([
140140
'message' => $message,
141-
'entity' => $entity ?? null,
141+
'themeFolder' => $themeFolder ?? null,
142142
'errors' => $errors ?? null
143143
]);
144-
$this->viewBuilder()->setOption('serialize', ['message', 'entity', 'errors']);
144+
$this->viewBuilder()->setOption('serialize', ['message', 'themeFolder', 'errors']);
145145
}
146146

147147
/**
148148
* テーマフォルダAPI テーマフォルダ編集
149149
*
150150
* @param ThemeFoldersServiceInterface $service
151151
* @return void
152+
*
153+
* @checked
154+
* @noTodo
155+
* @unitTest
152156
*/
153157
public function edit(ThemeFoldersServiceInterface $service)
154158
{
155-
//todo テーマフォルダAPI テーマフォルダ編集
159+
$this->request->allowMethod(['post', 'put']);
160+
161+
try {
162+
$data = $this->getRequest()->getData();
163+
$data['fullpath'] = $service->getFullpath($data['theme'], $data['type'], $data['path']);
164+
$form = $service->update($data);
165+
$themeFolder = $service->get($form->getData('fullpath'));
166+
$message = __d('baser', 'フォルダ名を「{0}」に変更しました。', $themeFolder->name);
167+
} catch (BcFormFailedException $e) {
168+
$this->setResponse($this->response->withStatus(400));
169+
$errors = $e->getForm()->getErrors();
170+
$message = __d('baser', '入力エラーです。内容を修正してください。' . $e->getMessage());
171+
} catch (\Throwable $e) {
172+
$this->setResponse($this->response->withStatus(400));
173+
$message = __d('baser', '処理中にエラーが発生しました。');
174+
}
175+
176+
$this->set([
177+
'message' => $message,
178+
'themeFolder' => $themeFolder ?? null,
179+
'errors' => $errors ?? null
180+
]);
181+
$this->viewBuilder()->setOption('serialize', ['message', 'themeFolder', 'errors']);
156182
}
157183

158184
/**
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* baserCMS : Based Website Development Project <https://basercms.net>
4+
* Copyright (c) NPO baser foundation <https://baserfoundation.org/>
5+
*
6+
* @copyright Copyright (c) NPO baser foundation
7+
* @link https://basercms.net baserCMS Project
8+
* @since 5.0.0
9+
* @license https://basercms.net/license/index.html MIT License
10+
*/
11+
12+
namespace BcThemeFile\Service;
13+
14+
use BaserCore\Annotation\NoTodo;
15+
use BaserCore\Annotation\Checked;
16+
use BaserCore\Annotation\UnitTest;
17+
use Cake\Core\Plugin;
18+
19+
/**
20+
* BcThemeFileService
21+
*
22+
*/
23+
class BcThemeFileService implements BcThemeFileServiceInterface
24+
{
25+
/**
26+
* fullpathを作成
27+
* @param string $theme
28+
* @param string $type
29+
* @param string $path
30+
* @return string
31+
*
32+
* @checked
33+
* @noTodo
34+
*/
35+
public function getFullpath(string $theme, string $type, string $path)
36+
{
37+
$assets = [
38+
'css',
39+
'js',
40+
'img'
41+
];
42+
if (in_array($type, $assets)) {
43+
$viewPath = Plugin::path($theme) . 'webroot' . DS . $type . DS . $path;
44+
} else {
45+
$viewPath = Plugin::templatePath($theme) . $type . DS . $path;
46+
}
47+
return $viewPath;
48+
}
49+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* baserCMS : Based Website Development Project <https://basercms.net>
4+
* Copyright (c) NPO baser foundation <https://baserfoundation.org/>
5+
*
6+
* @copyright Copyright (c) NPO baser foundation
7+
* @link https://basercms.net baserCMS Project
8+
* @since 5.0.0
9+
* @license https://basercms.net/license/index.html MIT License
10+
*/
11+
12+
namespace BcThemeFile\Service;
13+
14+
use BaserCore\Annotation\NoTodo;
15+
use BaserCore\Annotation\Checked;
16+
use BaserCore\Annotation\UnitTest;
17+
18+
/**
19+
* BcThemeFileServiceInterface
20+
*/
21+
interface BcThemeFileServiceInterface
22+
{
23+
/**
24+
* fullpathを作成
25+
* @param string $theme
26+
* @param string $type
27+
* @param string $path
28+
* @return string
29+
*
30+
* @checked
31+
* @noTodo
32+
* @unitTest
33+
*/
34+
public function getFullpath(string $theme, string $type, string $path);
35+
}

plugins/bc-theme-file/src/Service/ThemeFilesService.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*
2828
* @property ThemeFileForm $ThemeFileForm
2929
*/
30-
class ThemeFilesService implements ThemeFilesServiceInterface
30+
class ThemeFilesService extends BcThemeFileService implements ThemeFilesServiceInterface
3131
{
3232

3333
/**
@@ -228,10 +228,4 @@ public function copyToTheme(array $params)
228228
return false;
229229
}
230230
}
231-
232-
public function getFullpath(string $theme, string $type, string $path)
233-
{
234-
return Plugin::templatePath($theme) . $type . DS . $path;
235-
}
236-
237231
}

plugins/bc-theme-file/src/Service/ThemeFoldersService.php

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/**
2727
* ThemeFoldersService
2828
*/
29-
class ThemeFoldersService implements ThemeFoldersServiceInterface
29+
class ThemeFoldersService extends BcThemeFileService implements ThemeFoldersServiceInterface
3030
{
3131

3232
/**
@@ -313,29 +313,4 @@ public function getForm(array $data)
313313
{
314314
return (new ThemeFolderForm())->setData($data);
315315
}
316-
317-
/**
318-
* fullpathを作成
319-
* @param string $theme
320-
* @param string $type
321-
* @param string $path
322-
* @return string
323-
*
324-
* @checked
325-
* @noTodo
326-
*/
327-
public function getFullpath(string $theme, string $type, string $path)
328-
{
329-
$assets = [
330-
'css',
331-
'js',
332-
'img'
333-
];
334-
if (in_array($type, $assets)) {
335-
$viewPath = Plugin::path($theme) . 'webroot' . DS . $type . DS . $path;
336-
} else {
337-
$viewPath = Plugin::templatePath($theme) . $type . DS . $path;
338-
}
339-
return $viewPath;
340-
}
341316
}

plugins/bc-theme-file/tests/TestCase/Controller/Api/ThemeFoldersControllerTest.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ public function test_add()
102102
$this->assertResponseSuccess();
103103
//戻る値を確認
104104
$result = json_decode((string)$this->_response->getBody());
105+
//themeFolderを確認
106+
$this->assertEquals($fullpath . 'new_folder', $result->themeFolder->fullpath);
107+
//メッセージを確認
105108
$this->assertEquals('フォルダ「new_folder」を作成しました。', $result->message);
106109
//実際にフォルダが作成されいてるか確認すること
107110
$this->assertTrue(is_dir($fullpath . 'new_folder'));
@@ -114,7 +117,32 @@ public function test_add()
114117
*/
115118
public function test_edit()
116119
{
117-
$this->markTestIncomplete('このテストは未実装です。');
120+
//テストテーマフォルダを作成
121+
$fullpath = BASER_PLUGINS . 'BcThemeSample' . '/templates/layout/';
122+
mkdir($fullpath . 'new_folder');
123+
//Postデータを生成
124+
$data = [
125+
'theme' => 'BcThemeSample',
126+
'type' => 'layout',
127+
'path' => 'new_folder',
128+
'name' => 'edit_folder',
129+
];
130+
//APIをコール
131+
$this->post('/baser/api/bc-theme-file/theme_folders/edit.json?token=' . $this->accessToken, $data);
132+
//レスポンスコードを確認
133+
$this->assertResponseSuccess();
134+
//戻る値を確認
135+
$result = json_decode((string)$this->_response->getBody());
136+
//themeFolderを確認
137+
$this->assertEquals($fullpath . 'edit_folder', $result->themeFolder->fullpath);
138+
//メッセージを確認
139+
$this->assertEquals('フォルダ名を「edit_folder」に変更しました。', $result->message);
140+
//実際にフォルダが変更されいてるか確認すること
141+
$this->assertTrue(is_dir($fullpath . 'edit_folder'));
142+
//変更前のフォルダが存在しないか確認すること
143+
$this->assertFalse(is_dir($fullpath . 'new_folder'));
144+
//変更されたフォルダを削除
145+
rmdir($fullpath . 'edit_folder');
118146
}
119147

120148
/**
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* baserCMS : Based Website Development Project <https://basercms.net>
4+
* Copyright (c) NPO baser foundation <https://baserfoundation.org/>
5+
*
6+
* @copyright Copyright (c) NPO baser foundation
7+
* @link https://basercms.net baserCMS Project
8+
* @since 5.0.0
9+
* @license https://basercms.net/license/index.html MIT License
10+
*/
11+
12+
namespace BcThemeFile\Test\TestCase\Service;
13+
14+
use BaserCore\TestSuite\BcTestCase;
15+
use BcThemeFile\Service\BcThemeFileService;
16+
17+
/**
18+
* BcThemeFileServiceTest
19+
*/
20+
class BcThemeFileServiceTest extends BcTestCase
21+
{
22+
23+
public $BcThemeFileService = null;
24+
25+
/**
26+
* set up
27+
*/
28+
public function setUp(): void
29+
{
30+
$this->setFixtureTruncate();
31+
parent::setUp();
32+
$this->BcThemeFileService = new BcThemeFileService();
33+
}
34+
35+
/**
36+
* tear down
37+
*/
38+
public function tearDown(): void
39+
{
40+
parent::tearDown();
41+
}
42+
43+
/**
44+
* test getFullpath
45+
*/
46+
public function test_getFullpath()
47+
{
48+
//typeが$assetsではない場合、
49+
$themePath = $this->BcThemeFileService->getFullpath('BcThemeSample', 'layout', 'default.php');
50+
$this->assertEquals('/var/www/html/plugins/BcThemeSample/templates/layout/default.php', $themePath);
51+
52+
//typeがimgの場合、
53+
$themePath = $this->BcThemeFileService->getFullpath('BcFront', 'img', 'logo.png');
54+
$this->assertEquals('/var/www/html/plugins/bc-front/webroot/img/logo.png', $themePath);
55+
}
56+
}

0 commit comments

Comments
 (0)