From 93175b6e9d5cfa4d4319bd58b9de01824d596288 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 19 Jan 2021 12:44:48 +0100 Subject: [PATCH] Fix: bulk add - delete existing link Do not send redirect response in bookmark delete controller if the request comes from bulk creation page. Fixes #1683 --- .../admin/ShaareManageController.php | 4 ++ assets/common/js/shaare-batch.js | 4 +- .../DeleteBookmarkTest.php | 45 ++++++++++++++++++- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/application/front/controller/admin/ShaareManageController.php b/application/front/controller/admin/ShaareManageController.php index 35837baa..9633cd51 100644 --- a/application/front/controller/admin/ShaareManageController.php +++ b/application/front/controller/admin/ShaareManageController.php @@ -66,6 +66,10 @@ public function deleteBookmark(Request $request, Response $response): Response return $response->write(''); } + if ($request->getParam('source') === 'batch') { + return $response->withStatus(204); + } + // Don't redirect to permalink after deletion. return $this->redirectFromReferer($request, $response, ['shaare/']); } diff --git a/assets/common/js/shaare-batch.js b/assets/common/js/shaare-batch.js index 557325ee..2b438045 100644 --- a/assets/common/js/shaare-batch.js +++ b/assets/common/js/shaare-batch.js @@ -26,9 +26,9 @@ const sendBookmarkForm = (basePath, formElement) => { const sendBookmarkDelete = (buttonElement, formElement) => ( new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); - xhr.open('GET', buttonElement.href); + xhr.open('GET', `${buttonElement.href}&source=batch`); xhr.onload = () => { - if (xhr.status !== 200) { + if (xhr.status !== 204) { alert(`An error occurred. Return code: ${xhr.status}`); reject(); } else { diff --git a/tests/front/controller/admin/ShaareManageControllerTest/DeleteBookmarkTest.php b/tests/front/controller/admin/ShaareManageControllerTest/DeleteBookmarkTest.php index a276d988..42d0c0d6 100644 --- a/tests/front/controller/admin/ShaareManageControllerTest/DeleteBookmarkTest.php +++ b/tests/front/controller/admin/ShaareManageControllerTest/DeleteBookmarkTest.php @@ -363,6 +363,7 @@ public function testDeleteBookmarkFromBookmarklet(): void $this->container->bookmarkService->method('get')->with('123')->willReturn( (new Bookmark())->setId(123)->setUrl('http://domain.tld')->setTitle('Title 123') ); + $this->container->bookmarkService->expects(static::once())->method('remove'); $this->container->formatterFactory = $this->createMock(FormatterFactory::class); $this->container->formatterFactory @@ -379,6 +380,48 @@ public function testDeleteBookmarkFromBookmarklet(): void $result = $this->controller->deleteBookmark($request, $response); static::assertSame(200, $result->getStatusCode()); - static::assertSame('', (string) $result->getBody('location')); + static::assertSame('', (string) $result->getBody()); + } + + /** + * Delete bookmark - from batch view + */ + public function testDeleteBookmarkFromBatch(): void + { + $parameters = [ + 'id' => '123', + 'source' => 'batch', + ]; + + $request = $this->createMock(Request::class); + $request + ->method('getParam') + ->willReturnCallback(function (string $key) use ($parameters): ?string { + return $parameters[$key] ?? null; + }) + ; + $response = new Response(); + + $this->container->bookmarkService->method('get')->with('123')->willReturn( + (new Bookmark())->setId(123)->setUrl('http://domain.tld')->setTitle('Title 123') + ); + $this->container->bookmarkService->expects(static::once())->method('remove'); + + $this->container->formatterFactory = $this->createMock(FormatterFactory::class); + $this->container->formatterFactory + ->expects(static::once()) + ->method('getFormatter') + ->willReturnCallback(function (): BookmarkFormatter { + $formatter = $this->createMock(BookmarkFormatter::class); + $formatter->method('format')->willReturn(['formatted']); + + return $formatter; + }) + ; + + $result = $this->controller->deleteBookmark($request, $response); + + static::assertSame(204, $result->getStatusCode()); + static::assertEmpty((string) $result->getBody()); } }