Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

コンテンツ削除時の処理を調整 #648

Merged
merged 2 commits into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 0 additions & 37 deletions plugins/baser-core/src/Controller/Admin/ContentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,41 +404,4 @@ public function batch(ContentsServiceInterface $contentService)
}
return $this->response->withStringBody('true');
}

/**
* ゴミ箱を空にする
* @see bcTreeの処理はApi/trash_emptyに移行
* @param ContentsServiceInterface $contentService
* @return Response|null
* @checked
* @noTodo
* @unitTest
*/
public function trash_empty(ContentsServiceInterface $contentService)
{
$this->disableAutoRender();
if (!$this->request->getData()) {
$this->notFound();
}
$contents = $contentService->getTrashIndex()->order(['plugin', 'type']);

// EVENT Contents.beforetrash_empty
$this->dispatchLayerEvent('beforetrash_empty', [
'data' => $contents
]);
if ($contents) {
$result = true;
foreach($contents as $content) {
if(!$contentService->hardDeleteWithAssoc($content->id)) {
$result = false;
$this->BcMessage->setError(__d('baser', 'ゴミ箱の削除に失敗しました'));
}
}
}
// EVENT Contents.aftertrash_empty
$this->dispatchLayerEvent('aftertrash_empty', [
'data' => $result
]);
return $this->redirect(['action' => "trash_index"]);
}
}
14 changes: 10 additions & 4 deletions plugins/baser-core/src/Controller/Api/ContentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,19 @@ public function trash_empty(ContentsServiceInterface $contentService)
$this->request->allowMethod(['post', 'delete']);
$trash = $contentService->getTrashIndex($this->request->getQueryParams())->order(['plugin', 'type']);
$text = "ゴミ箱: ";
// EVENT Contents.beforetrash_empty
$this->dispatchLayerEvent('beforetrash_empty', [
'data' => $trash
]);
try {
foreach ($trash as $entity) {
if ($contentService->hardDeleteWithAssoc($entity->id)) {
$text .= "$entity->title($entity->type)" . "を削除しました。";
}
$contentService->hardDeleteWithAssoc($entity->id);
}
$message = __d('baser', $text);
$message = __d('baser', 'ゴミ箱を空にしました。');
// EVENT Contents.aftertrash_empty
$this->dispatchLayerEvent('aftertrash_empty', [
'data' => $result
]);
} catch (Exception $e) {
$this->setResponse($this->response->withStatus(500));
$message = __d('baser', 'データベース処理中にエラーが発生しました。') . $e->getMessage();
Expand Down
21 changes: 8 additions & 13 deletions plugins/baser-core/src/Service/ContentsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,24 +443,19 @@ public function hardDeleteWithAssoc($id): bool
/* @var Content $content */
$content = $this->getTrash($id);
$service = $content->plugin . '\\Service\\' . Inflector::pluralize($content->type) . 'ServiceInterface';
if(interface_exists($service)) {
if (interface_exists($service)) {
$target = $this->getService($service);
} else {
$target = TableRegistry::getTableLocator()->get($content->plugin . '.' . Inflector::pluralize($content->type));
}
$result = false;
if($target) {
try {
if(is_a($target, 'Cake\ORM\Table')) {
$result = $target->delete($content);
} else {
$result = $target->delete($content->entity_id);
}
} catch (\Exception $e) {
$result = false;
}
if (!$target) {
throw new \Cake\Datasource\Exception\RecordNotFoundException();
}
if (is_a($target, 'Cake\ORM\Table')) {
return $target->delete($content);
} else {
return $target->delete($content->entity_id);
}
return $result;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,21 +475,6 @@ public function testAdmin_ajax_change_status()
$this->markTestIncomplete('このテストは、まだ実装されていません。');
}

/**
* ゴミ箱を空にする
*/
public function testTrash_empty()
{
// BcAdminContentsTestはコンポネントのテスト用のため、一旦復活させtrash_emptyを実行
$this->ContentsService->restoreAll(['type' => 'BcAdminContentsTest']);
$this->request = $this->request->withData('test', 'テスト');
$this->ContentsController->setRequest($this->request);
$response = $this->ContentsController->trash_empty($this->ContentsService);
$this->assertTrue($this->ContentsService->getTrashIndex(['type' => "ContentFolder"])->all()->isEmpty());
$this->assertEquals(8, $this->ContentFoldersService->getIndex()->count());
$this->assertStringContainsString("/baser/admin/baser-core/contents/trash_index", $response->getHeaderLine('Location'));
}

/**
* コンテンツ表示
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public function testTrash_empty()
$this->post('/baser/api/baser-core/contents/trash_empty.json?type=ContentFolder&token=' . $this->accessToken);
$this->assertResponseOk();
$result = json_decode((string)$this->_response->getBody());
$this->assertEquals("ゴミ箱: 削除済みフォルダー(親)(ContentFolder)を削除しました。削除済みフォルダー(子)(ContentFolder)を削除しました。", $result->message);
$this->assertEquals("ゴミ箱を空にしました。", $result->message);
$this->get('/baser/api/baser-core/contents/index/trash.json?type=ContentFolder&token=' . $this->accessToken);
$result = json_decode((string)$this->_response->getBody());
$this->assertEmpty($result->contents);
Expand Down
22 changes: 18 additions & 4 deletions plugins/baser-core/tests/TestCase/Service/ContentsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,24 @@ public function testHardDeleteWithAssoc(): void
{
$content = $this->ContentsService->getTrash(16);
$this->assertTrue($this->ContentsService->hardDeleteWithAssoc(16));
$this->expectException('Cake\Datasource\Exception\RecordNotFoundException');
$this->ContentsService->getTrash(16);
$this->expectException('Cake\Datasource\Exception\RecordNotFoundException');
$this->ContentFoldersService->get($content->entity_id);
try {
$this->ContentsService->getTrash(16);
throw new \Exception();
} catch (\Exception $e) {
$this->assertSame('Cake\Datasource\Exception\RecordNotFoundException', get_class($e));
}
try {
$this->ContentFoldersService->get($content->entity_id);
throw new \Exception();
} catch (\Exception $e) {
$this->assertSame('Cake\Datasource\Exception\RecordNotFoundException', get_class($e));
}
try {
$this->assertTrue($this->ContentsService->hardDeleteWithAssoc(999));
throw new \Exception();
} catch (\Exception $e) {
$this->assertSame('Cake\Datasource\Exception\RecordNotFoundException', get_class($e));
}
}

/**
Expand Down