Skip to content

Commit 3fcb4ee

Browse files
authored
fix #1803 テーマフォルダAPI フォルダ削除 (#1816)
1 parent 724efe7 commit 3fcb4ee

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public function delete(ThemeFilesServiceInterface $service)
108108
try {
109109
$data = $this->getRequest()->getData();
110110
$data['fullpath'] = $service->getFullpath($data['theme'], $data['type'], $data['path']);
111+
$themeFile = $service->get($data['fullpath']);
111112
if ($service->delete($data['fullpath'])) {
112113
$message = __d('baser', 'ファイル「{0}」を削除しました。', $data['path']);
113114
} else {
@@ -123,11 +124,11 @@ public function delete(ThemeFilesServiceInterface $service)
123124
}
124125

125126
$this->set([
127+
'themeFile' => $themeFile ?? null,
126128
'message' => $message,
127-
'entity' => $entity ?? null,
128129
'errors' => $errors ?? null
129130
]);
130-
$this->viewBuilder()->setOption('serialize', ['message', 'entity', 'errors']);
131+
$this->viewBuilder()->setOption('serialize', ['themeFile', 'message', 'errors']);
131132
}
132133

133134
/**

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,38 @@ public function edit(ThemeFoldersServiceInterface $service)
186186
*
187187
* @param ThemeFoldersServiceInterface $service
188188
* @return void
189+
*
190+
* @checked
191+
* @noTodo
192+
* @unitTest
189193
*/
190194
public function delete(ThemeFoldersServiceInterface $service)
191195
{
192-
//todo テーマフォルダAPI テーマフォルダ削除
196+
$this->request->allowMethod(['post', 'put']);
197+
try {
198+
$data = $this->getRequest()->getData();
199+
$data['fullpath'] = $service->getFullpath($data['theme'], $data['type'], $data['path']);
200+
$themeFolder = $service->get($data['fullpath']);
201+
if ($service->delete($data['fullpath'])) {
202+
$message = __d('baser', 'フォルダ「{0}」を削除しました。', $data['path']);
203+
} else {
204+
$message = __d('baser', 'フォルダ「{0}」の削除に失敗しました。', $data['path']);
205+
}
206+
} catch (BcFormFailedException $e) {
207+
$this->setResponse($this->response->withStatus(400));
208+
$errors = $e->getForm()->getErrors();
209+
$message = __d('baser', '入力エラーです。内容を修正してください。' . $e->getMessage());
210+
} catch (\Throwable $e) {
211+
$this->setResponse($this->response->withStatus(400));
212+
$message = __d('baser', '処理中にエラーが発生しました。');
213+
}
214+
215+
$this->set([
216+
'themeFolder' => $themeFolder ?? null,
217+
'message' => $message,
218+
'errors' => $errors ?? null
219+
]);
220+
$this->viewBuilder()->setOption('serialize', ['themeFolder', 'message', 'errors']);
193221
}
194222

195223
/**

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,33 @@ public function test_edit()
150150
*/
151151
public function test_delete()
152152
{
153-
$this->markTestIncomplete('このテストは未実装です。');
153+
//テストテーマフォルダを作成
154+
$fullpath = BASER_PLUGINS . 'BcThemeSample' . '/templates/layout/';
155+
mkdir($fullpath . 'delete_folder');
156+
//Postデータを生成
157+
$data = [
158+
'theme' => 'BcThemeSample',
159+
'type' => 'layout',
160+
'path' => 'delete_folder',
161+
];
162+
//APIをコール
163+
$this->post('/baser/api/bc-theme-file/theme_folders/delete.json?token=' . $this->accessToken, $data);
164+
//レスポンスコードを確認
165+
$this->assertResponseSuccess();
166+
//戻る値を確認
167+
$result = json_decode((string)$this->_response->getBody());
168+
$this->assertEquals('フォルダ「delete_folder」を削除しました。', $result->message);
169+
$this->assertNotNull($result->themeFolder);
170+
//実際にフォルダが削除されいてるか確認すること
171+
$this->assertFalse(file_exists($fullpath . 'delete_folder'));
172+
173+
//もう一度APIをコールする場合、エラーを出る
174+
$this->post('/baser/api/bc-theme-file/theme_folders/delete.json?token=' . $this->accessToken, $data);
175+
//レスポンスコードを確認
176+
$this->assertResponseSuccess();
177+
//戻る値を確認
178+
$result = json_decode((string)$this->_response->getBody());
179+
$this->assertEquals('フォルダ「delete_folder」の削除に失敗しました。', $result->message);
154180
}
155181

156182
/**

0 commit comments

Comments
 (0)