]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Fix: bulk add - delete existing link 1693/head
authorArthurHoaro <arthur@hoa.ro>
Tue, 19 Jan 2021 11:44:48 +0000 (12:44 +0100)
committerArthurHoaro <arthur@hoa.ro>
Tue, 19 Jan 2021 11:54:34 +0000 (12:54 +0100)
Do not send redirect response in bookmark delete controller if the request comes from bulk creation page.

Fixes #1683

application/front/controller/admin/ShaareManageController.php
assets/common/js/shaare-batch.js
tests/front/controller/admin/ShaareManageControllerTest/DeleteBookmarkTest.php

index 35837baac7cc5312db74dc55fce086fa83959f03..9633cd51696e09978e500d5b4be0ca90860afe89 100644 (file)
@@ -66,6 +66,10 @@ class ShaareManageController extends ShaarliAdminController
             return $response->write('<script>self.close();</script>');
         }
 
+        if ($request->getParam('source') === 'batch') {
+            return $response->withStatus(204);
+        }
+
         // Don't redirect to permalink after deletion.
         return $this->redirectFromReferer($request, $response, ['shaare/']);
     }
index 557325ee373f9c85fc6287674f110096f379e7f1..2b438045c5f401ce8a21305278fcd57e8e083b86 100644 (file)
@@ -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 {
index a276d988f0d1993c7278cbbf30debdab02055e17..42d0c0d658f395cfe0be64d0478baf93b1405347 100644 (file)
@@ -363,6 +363,7 @@ class DeleteBookmarkTest extends TestCase
         $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 @@ class DeleteBookmarkTest extends TestCase
         $result = $this->controller->deleteBookmark($request, $response);
 
         static::assertSame(200, $result->getStatusCode());
-        static::assertSame('<script>self.close();</script>', (string) $result->getBody('location'));
+        static::assertSame('<script>self.close();</script>', (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());
     }
 }