Skip to content

Commit

Permalink
fix baserproject#1952 API カスタムフィールド 削除 (baserproject#1978)
Browse files Browse the repository at this point in the history
Co-authored-by: Đỗ Văn Hùng <HungDV2022>
Co-authored-by: ryuring <egashira@catchup.co.jp>
  • Loading branch information
HungDV2022 and ryuring committed Mar 12, 2023
1 parent 2a62888 commit 942635f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
Expand Up @@ -145,10 +145,32 @@ public function edit(CustomFieldsServiceInterface $service, int $id)
* 削除API
*
* @param CustomFieldsServiceInterface $service
* @param int $id
*
* @checked
* @noTodo
* @unitTest
*/
public function delete(CustomFieldsServiceInterface $service)
public function delete(CustomFieldsServiceInterface $service, int $id)
{
//todo 削除API
$this->request->allowMethod(['post', 'delete']);
$customField = null;
try {
$customField = $service->get($id);
$service->delete($id);
$message = __d('baser_core', 'フィールド「{0}」を削除しました。', $customField->title);
} catch (RecordNotFoundException $e) {
$this->setResponse($this->response->withStatus(404));
$message = __d('baser_core', 'データが見つかりません。');
} catch (\Throwable $e) {
$message = __d('baser_core', 'データベース処理中にエラーが発生しました。' . $e->getMessage());
$this->setResponse($this->response->withStatus(500));
}
$this->set([
'message' => $message,
'customField' => $customField
]);
$this->viewBuilder()->setOption('serialize', ['customField', 'message']);
}

/**
Expand Down
Expand Up @@ -11,8 +11,11 @@

namespace BcCustomContent\Test\TestCase\Controller\Api;

use BaserCore\Service\BcDatabaseServiceInterface;
use BaserCore\Test\Scenario\InitAppScenario;
use BaserCore\TestSuite\BcTestCase;
use BaserCore\Utility\BcContainerTrait;
use BcCustomContent\Service\CustomTablesServiceInterface;
use BcCustomContent\Test\Factory\CustomFieldFactory;
use BcCustomContent\Test\Scenario\CustomFieldsScenario;
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;
Expand All @@ -28,7 +31,7 @@ class CustomFieldsControllerTest extends BcTestCase
* ScenarioAwareTrait
*/
use ScenarioAwareTrait;
use IntegrationTestTrait;
use BcContainerTrait;

/**
* Fixtures
Expand All @@ -43,6 +46,7 @@ class CustomFieldsControllerTest extends BcTestCase
'plugin.BaserCore.Factory/UserGroups',
'plugin.BcCustomContent.Factory/CustomFields',
'plugin.BcCustomContent.Factory/CustomLinks',
'plugin.BcCustomContent.Factory/CustomTables',
];

/**
Expand Down Expand Up @@ -196,7 +200,40 @@ public function test_edit()
*/
public function test_delete()
{
$this->markTestIncomplete('このテストは、まだ実装されていません。');
//サービスをコル
$dataBaseService = $this->getService(BcDatabaseServiceInterface::class);
$customTable = $this->getService(CustomTablesServiceInterface::class);

//データを生成
$customTable->create([
'id' => 1,
'name' => 'recruit',
'title' => '求人情報',
'type' => '1',
'display_field' => 'title',
'has_child' => 0
]);
$dataBaseService->addColumn('custom_entry_1_recruit', 'recruit_category', 'text');
$this->loadFixtureScenario(CustomFieldsScenario::class);
//APIを呼ぶ
$this->post('/baser/api/bc-custom-content/custom_fields/delete/1.json?token=' . $this->accessToken);
//ステータスを確認
$this->assertResponseOk();
//戻る値を確認
$result = json_decode((string)$this->_response->getBody());
$this->assertEquals('フィールド「求人分類」を削除しました。', $result->message);
$this->assertEquals('求人分類', $result->customField->title);
//不要なテーブルを削除
$dataBaseService->dropTable('custom_entry_1_recruit');

//存在しないIDを指定したの場合、
//APIを呼ぶ
$this->post('/baser/api/bc-custom-content/custom_fields/delete/11.json?token=' . $this->accessToken);
//ステータスを確認
$this->assertResponseCode(404);
//戻る値を確認
$result = json_decode((string)$this->_response->getBody());
$this->assertEquals('データが見つかりません。', $result->message);
}

/**
Expand Down

0 comments on commit 942635f

Please sign in to comment.