]> BookStack Code Mirror - bookstack/commitdiff
Added testing to favourite system 2748/head
authorDan Brown <redacted>
Sun, 23 May 2021 13:34:36 +0000 (14:34 +0100)
committerDan Brown <redacted>
Sun, 23 May 2021 13:34:36 +0000 (14:34 +0100)
- Also removed some old view service references.
- Updated TopFavourites query to be based on favourites table and join
  in the views instead of the other way around, so that favourites still
show even if they have no views.

app/Auth/Permissions/PermissionService.php
app/Entities/Queries/TopFavourites.php
app/Http/Controllers/SearchController.php
app/Providers/CustomFacadeProvider.php
tests/FavouriteTest.php

index 1c82f8e0845e412d13ea8cdbb35532ba5c522d5a..4565986535d6240a9a0c80211bc61f0929117aa6 100644 (file)
@@ -588,7 +588,7 @@ class PermissionService
 
         $q = $query->where(function ($query) use ($tableDetails, $action) {
             $query->whereExists(function ($permissionQuery) use (&$tableDetails, $action) {
-                $permissionQuery->select('id')->from('joint_permissions')
+                $permissionQuery->select(['role_id'])->from('joint_permissions')
                     ->whereRaw('joint_permissions.entity_id=' . $tableDetails['tableName'] . '.' . $tableDetails['entityIdColumn'])
                     ->whereRaw('joint_permissions.entity_type=' . $tableDetails['tableName'] . '.' . $tableDetails['entityTypeColumn'])
                     ->where('action', '=', $action)
index a527c2a4eac34f822dd7d5c7d911f8a73ecaceb6..1434180d33f09583eab01fa00b87b8b570f07867 100644 (file)
@@ -1,34 +1,33 @@
 <?php namespace BookStack\Entities\Queries;
 
-use BookStack\Actions\View;
+use BookStack\Actions\Favourite;
 use Illuminate\Database\Query\JoinClause;
-use Illuminate\Support\Facades\DB;
 
 class TopFavourites extends EntityQuery
 {
     public function run(int $count, int $skip = 0)
     {
         $user = user();
-        if ($user === null || $user->isDefault()) {
+        if (is_null($user) || $user->isDefault()) {
             return collect();
         }
 
         $query = $this->permissionService()
-            ->filterRestrictedEntityRelations(View::query(), 'views', 'viewable_id', 'viewable_type', 'view')
-            ->select('*', 'viewable_id', 'viewable_type', DB::raw('SUM(views) as view_count'))
-            ->groupBy('viewable_id', 'viewable_type')
-            ->rightJoin('favourites', function (JoinClause $join) {
-                $join->on('views.viewable_id', '=', 'favourites.favouritable_id');
-                $join->on('views.viewable_type', '=', 'favourites.favouritable_type');
-                $join->where('favourites.user_id', '=', user()->id);
+            ->filterRestrictedEntityRelations(Favourite::query(), 'favourites', 'favouritable_id', 'favouritable_type', 'view')
+            ->select('favourites.*')
+            ->leftJoin('views', function (JoinClause $join) {
+                $join->on('favourites.favouritable_id', '=', 'views.viewable_id');
+                $join->on('favourites.favouritable_type', '=', 'views.viewable_type');
+                $join->where('views.user_id', '=', user()->id);
             })
-            ->orderBy('view_count', 'desc');
+            ->orderBy('views.views', 'desc')
+            ->where('favourites.user_id', '=', user()->id);
 
-        return $query->with('viewable')
+        return $query->with('favouritable')
             ->skip($skip)
             ->take($count)
             ->get()
-            ->pluck('viewable')
+            ->pluck('favouritable')
             ->filter();
     }
 }
index 1f77e6377ed3bf13c8fb5aecce56036157e9a5fb..8598575004f04a5e1c679288e6f5d7060b57a54a 100644 (file)
@@ -1,6 +1,5 @@
 <?php namespace BookStack\Http\Controllers;
 
-use BookStack\Actions\ViewService;
 use BookStack\Entities\Queries\Popular;
 use BookStack\Entities\Tools\SearchRunner;
 use BookStack\Entities\Tools\ShelfContext;
@@ -10,16 +9,13 @@ use Illuminate\Http\Request;
 
 class SearchController extends Controller
 {
-    protected $viewService;
     protected $searchRunner;
     protected $entityContextManager;
 
     public function __construct(
-        ViewService $viewService,
         SearchRunner $searchRunner,
         ShelfContext $entityContextManager
     ) {
-        $this->viewService = $viewService;
         $this->searchRunner = $searchRunner;
         $this->entityContextManager = $entityContextManager;
     }
index f203f0fda592f5e27e7099b851395207bf8de71b..ca86b6607e2d2875ad006f884821616c180f6201 100644 (file)
@@ -3,7 +3,6 @@
 namespace BookStack\Providers;
 
 use BookStack\Actions\ActivityService;
-use BookStack\Actions\ViewService;
 use BookStack\Auth\Permissions\PermissionService;
 use BookStack\Theming\ThemeService;
 use BookStack\Uploads\ImageService;
@@ -32,10 +31,6 @@ class CustomFacadeProvider extends ServiceProvider
             return $this->app->make(ActivityService::class);
         });
 
-        $this->app->singleton('views', function () {
-            return $this->app->make(ViewService::class);
-        });
-
         $this->app->singleton('images', function () {
             return $this->app->make(ImageService::class);
         });
index ce5caf5c2276c37ac9012369d7a3e2c71b96e040..7209063c92ba3a5181ac9c905342e922776147ef 100644 (file)
@@ -1,6 +1,9 @@
 <?php
 
 use BookStack\Actions\Favourite;
+use BookStack\Entities\Models\Book;
+use BookStack\Entities\Models\Bookshelf;
+use BookStack\Entities\Models\Chapter;
 use BookStack\Entities\Models\Page;
 use Tests\TestCase;
 
@@ -56,4 +59,60 @@ class FavouriteTest extends TestCase
         ]);
     }
 
+    public function test_book_chapter_shelf_pages_contain_favourite_button()
+    {
+        $entities = [
+            Bookshelf::query()->first(),
+            Book::query()->first(),
+            Chapter::query()->first(),
+        ];
+        $this->actingAs($this->getEditor());
+
+        foreach ($entities as $entity) {
+            $resp = $this->get($entity->getUrl());
+            $resp->assertElementExists('form[method="POST"][action$="/favourites/add"]');
+        }
+    }
+
+    public function test_header_contains_link_to_favourites_page_when_logged_in()
+    {
+        $this->setSettings(['app-public' => 'true']);
+        $this->get('/')->assertElementNotContains('header', 'My Favourites');
+        $this->actingAs($this->getViewer())->get('/')->assertElementContains('header a', 'My Favourites');
+    }
+
+    public function test_favourites_shown_on_homepage()
+    {
+        $editor = $this->getEditor();
+
+        $resp = $this->actingAs($editor)->get('/');
+        $resp->assertElementNotExists('#top-favourites');
+
+        /** @var Page $page */
+        $page = Page::query()->first();
+        $page->favourites()->save((new Favourite)->forceFill(['user_id' => $editor->id]));
+
+        $resp = $this->get('/');
+        $resp->assertElementExists('#top-favourites');
+        $resp->assertElementContains('#top-favourites', $page->name);
+    }
+
+    public function test_favourites_list_page_shows_favourites_and_has_working_pagination()
+    {
+        /** @var Page $page */
+        $page = Page::query()->first();
+        $editor = $this->getEditor();
+
+        $resp = $this->actingAs($editor)->get('/favourites');
+        $resp->assertDontSee($page->name);
+
+        $page->favourites()->save((new Favourite)->forceFill(['user_id' => $editor->id]));
+
+        $resp = $this->get('/favourites');
+        $resp->assertSee($page->name);
+
+        $resp = $this->get('/favourites?page=2');
+        $resp->assertDontSee($page->name);
+    }
+
 }
\ No newline at end of file
Morty Proxy This is a proxified and sanitized view of the page, visit original site.