From bd14dc067b04f9eb3c7d72cf61f74b3ac9fcd48c Mon Sep 17 00:00:00 2001
From: Dan Brown <ssddanbrown@googlemail.com>
Date: Tue, 26 Jul 2022 12:35:50 +0100
Subject: [PATCH] Added 'Sort Book' action to chapters

Related to #2335
---
 resources/lang/en/entities.php          |  1 +
 resources/views/chapters/show.blade.php |  8 ++++++++
 tests/Entity/ChapterTest.php            | 12 ++++++++++++
 3 files changed, 21 insertions(+)

diff --git a/resources/lang/en/entities.php b/resources/lang/en/entities.php
index 83e3c12d9..db1e8027b 100644
--- a/resources/lang/en/entities.php
+++ b/resources/lang/en/entities.php
@@ -171,6 +171,7 @@ return [
     'chapters_permissions_active' => 'Chapter Permissions Active',
     'chapters_permissions_success' => 'Chapter Permissions Updated',
     'chapters_search_this' => 'Search this chapter',
+    'chapter_sort_book' => 'Sort Book',
 
     // Pages
     'page' => 'Page',
diff --git a/resources/views/chapters/show.blade.php b/resources/views/chapters/show.blade.php
index a7b90ceef..1ae2d6847 100644
--- a/resources/views/chapters/show.blade.php
+++ b/resources/views/chapters/show.blade.php
@@ -147,6 +147,14 @@
                 </a>
             @endif
 
+            @if($chapter->book && userCan('book-update', $chapter->book))
+                <hr class="primary-background"/>
+                <a href="{{ $chapter->book->getUrl('/sort') }}" class="icon-list-item">
+                    <span>@icon('sort')</span>
+                    <span>{{ trans('entities.chapter_sort_book') }}</span>
+                </a>
+            @endif
+
             <hr class="primary-background"/>
 
             @if(signedInUser())
diff --git a/tests/Entity/ChapterTest.php b/tests/Entity/ChapterTest.php
index 5a761b94f..d58b83da9 100644
--- a/tests/Entity/ChapterTest.php
+++ b/tests/Entity/ChapterTest.php
@@ -146,4 +146,16 @@ class ChapterTest extends TestCase
         $newChapter2 = Chapter::query()->where('name', '=', 'My copied again chapter')->first();
         $this->assertEquals($chapter->pages()->count(), $newChapter2->pages()->count());
     }
+
+    public function test_sort_book_action_visible_if_permissions_allow()
+    {
+        /** @var Chapter $chapter */
+        $chapter = Chapter::query()->first();
+
+        $resp = $this->actingAs($this->getViewer())->get($chapter->getUrl());
+        $this->withHtml($resp)->assertLinkNotExists($chapter->book->getUrl('sort'));
+
+        $resp = $this->asEditor()->get($chapter->getUrl());
+        $this->withHtml($resp)->assertLinkExists($chapter->book->getUrl('sort'));
+    }
 }