]> BookStack Code Mirror - bookstack/commitdiff
Tags: Stopped recycle bin tags being counted on index
authorDan Brown <redacted>
Mon, 15 Apr 2024 17:44:59 +0000 (18:44 +0100)
committerDan Brown <redacted>
Mon, 15 Apr 2024 17:44:59 +0000 (18:44 +0100)
For #4892
Added test to cover.

app/Activity/TagRepo.php
tests/Entity/TagTest.php
tests/Helpers/EntityProvider.php

index 4f2dbed5936b5d05214f583f4b7f3d2d26a1ac96..82c26b00e281854bcd5b3f6b2ee7a1ea35d091ef 100644 (file)
@@ -38,7 +38,8 @@ class TagRepo
                 DB::raw('SUM(IF(entity_type = \'book\', 1, 0)) as book_count'),
                 DB::raw('SUM(IF(entity_type = \'bookshelf\', 1, 0)) as shelf_count'),
             ])
-            ->orderBy($sort, $listOptions->getOrder());
+            ->orderBy($sort, $listOptions->getOrder())
+            ->whereHas('entity');
 
         if ($nameFilter) {
             $query->where('name', '=', $nameFilter);
index c1240e955387ee9c037fa78424eed3f48e0754f5..729f9390358d5ff1e044a00bed7e67ddb82cf23e 100644 (file)
@@ -9,7 +9,7 @@ use Tests\TestCase;
 
 class TagTest extends TestCase
 {
-    protected $defaultTagCount = 20;
+    protected int $defaultTagCount = 20;
 
     /**
      * Get an instance of a page that has many tags.
@@ -193,6 +193,24 @@ class TagTest extends TestCase
         $resp->assertSee('Tags can be assigned via the page editor sidebar');
     }
 
+    public function test_tag_index_does_not_include_tags_on_recycle_bin_items()
+    {
+        $page = $this->entities->page();
+        $page->tags()->create(['name' => 'DeleteRecord', 'value' => 'itemToDeleteTest']);
+
+        $resp = $this->asEditor()->get('/tags');
+        $resp->assertSee('DeleteRecord');
+        $resp = $this->asEditor()->get('/tags?name=DeleteRecord');
+        $resp->assertSee('itemToDeleteTest');
+
+        $this->entities->sendToRecycleBin($page);
+
+        $resp = $this->asEditor()->get('/tags');
+        $resp->assertDontSee('DeleteRecord');
+        $resp = $this->asEditor()->get('/tags?name=DeleteRecord');
+        $resp->assertDontSee('itemToDeleteTest');
+    }
+
     public function test_tag_classes_visible_on_entities()
     {
         $this->asEditor();
index 982063421453139c95470d4ac637a45f55f2b63e..1897abefa28501009366f76661a925b1fb50214a 100644 (file)
@@ -207,6 +207,29 @@ class EntityProvider
         return $draftPage;
     }
 
+    /**
+     * Send an entity to the recycle bin.
+     */
+    public function sendToRecycleBin(Entity $entity)
+    {
+        $trash = app()->make(TrashCan::class);
+
+        if ($entity instanceof Page) {
+            $trash->softDestroyPage($entity);
+        } elseif ($entity instanceof Chapter) {
+            $trash->softDestroyChapter($entity);
+        } elseif ($entity instanceof Book) {
+            $trash->softDestroyBook($entity);
+        } elseif ($entity instanceof Bookshelf) {
+            $trash->softDestroyBookshelf($entity);
+        }
+
+        $entity->refresh();
+        if (is_null($entity->deleted_at)) {
+            throw new \Exception("Could not send entity type [{$entity->getMorphClass()}] to the recycle bin");
+        }
+    }
+
     /**
      * Fully destroy the given entity from the system, bypassing the recycle bin
      * stage. Still runs through main app deletion logic.