Skip to content

Commit 0352670

Browse files
authored
fix #1772 テーマファイルAPI ファイル削除 (#1783)
1 parent 1edad4c commit 0352670

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,37 @@ public function edit(ThemeFilesServiceInterface $service)
9494
* [API] テーマファイル ファイル削除
9595
*
9696
* @param ThemeFilesServiceInterface $service
97+
*
98+
* @checked
99+
* @noTodo
100+
* @unitTest
97101
*/
98102
public function delete(ThemeFilesServiceInterface $service)
99103
{
100-
//todo テーマファイルAPI ファイル削除 #1772
104+
$this->request->allowMethod(['post', 'put']);
105+
try {
106+
$data = $this->getRequest()->getData();
107+
$data['fullpath'] = $service->getFullpath($data['theme'], $data['type'], $data['path']);
108+
if ($service->delete($data['fullpath'])) {
109+
$message = __d('baser', 'ファイル「{0}」を削除しました。', $data['path']);
110+
} else {
111+
$message = __d('baser', 'ファイル「{0}」の削除に失敗しました。', $data['path']);
112+
}
113+
} catch (BcFormFailedException $e) {
114+
$this->setResponse($this->response->withStatus(400));
115+
$errors = $e->getForm()->getErrors();
116+
$message = __d('baser', '入力エラーです。内容を修正してください。' . $e->getMessage());
117+
} catch (\Throwable $e) {
118+
$this->setResponse($this->response->withStatus(400));
119+
$message = __d('baser', '処理中にエラーが発生しました。');
120+
}
121+
122+
$this->set([
123+
'message' => $message,
124+
'entity' => $entity ?? null,
125+
'errors' => $errors ?? null
126+
]);
127+
$this->viewBuilder()->setOption('serialize', ['message', 'entity', 'errors']);
101128
}
102129

103130
/**

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,32 @@ public function test_edit()
154154
*/
155155
public function test_delete()
156156
{
157-
$this->markTestIncomplete('このテストは未実装です。');
157+
//テストファイルを作成
158+
$fullpath = BASER_PLUGINS . 'BcThemeSample' . '/templates/layout/';
159+
new File($fullpath . 'base_name_1.php', true);
160+
//POSTデータを生成
161+
$data = [
162+
'theme' => 'BcThemeSample',
163+
'type' => 'layout',
164+
'path' => 'base_name_1.php'
165+
];
166+
//APIをコール
167+
$this->post('/baser/api/bc-theme-file/theme_files/delete.json?token=' . $this->accessToken, $data);
168+
//レスポンスコードを確認
169+
$this->assertResponseSuccess();
170+
//戻る値を確認
171+
$result = json_decode((string)$this->_response->getBody());
172+
$this->assertEquals('ファイル「base_name_1.php」を削除しました。', $result->message);
173+
//実際にファイルが削除されいてるか確認すること
174+
$this->assertFalse(file_exists($fullpath . 'base_name_1.php'));
175+
176+
//もう一度APIをコールする場合、エラーを出る
177+
$this->post('/baser/api/bc-theme-file/theme_files/delete.json?token=' . $this->accessToken, $data);
178+
//レスポンスコードを確認
179+
$this->assertResponseSuccess();
180+
//戻る値を確認
181+
$result = json_decode((string)$this->_response->getBody());
182+
$this->assertEquals('ファイル「base_name_1.php」の削除に失敗しました。', $result->message);
158183
}
159184

160185
/**

0 commit comments

Comments
 (0)