mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-08-25 11:45:58 +00:00
Added Popular books list with relevant tests
This commit is contained in:
parent
9f435553dc
commit
f1c2866fbc
@ -42,8 +42,9 @@ class BookController extends Controller
|
|||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$books = $this->bookRepo->getAllPaginated(10);
|
$books = $this->bookRepo->getAllPaginated(10);
|
||||||
$recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed(10, 0) : false;
|
$recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed(4, 0) : false;
|
||||||
return view('books/index', ['books' => $books, 'recents' => $recents]);
|
$popular = $this->bookRepo->getPopular(4, 0);
|
||||||
|
return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -77,6 +77,11 @@ class BookRepo
|
|||||||
return Views::getUserRecentlyViewed($count, $page, $this->book);
|
return Views::getUserRecentlyViewed($count, $page, $this->book);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getPopular($count = 10, $page = 0)
|
||||||
|
{
|
||||||
|
return Views::getPopular($count, $page, $this->book);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a book by slug
|
* Get a book by slug
|
||||||
* @param $slug
|
* @param $slug
|
||||||
|
@ -44,6 +44,29 @@ class ViewService
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the entities with the most views.
|
||||||
|
* @param int $count
|
||||||
|
* @param int $page
|
||||||
|
* @param bool|false $filterModel
|
||||||
|
*/
|
||||||
|
public function getPopular($count = 10, $page = 0, $filterModel = false)
|
||||||
|
{
|
||||||
|
$skipCount = $count * $page;
|
||||||
|
$query = $this->view->select('id', 'viewable_id', 'viewable_type', \DB::raw('SUM(views) as view_count'))
|
||||||
|
->groupBy('viewable_id', 'viewable_type')
|
||||||
|
->orderBy('view_count', 'desc');
|
||||||
|
|
||||||
|
if($filterModel) $query->where('viewable_type', '=', get_class($filterModel));
|
||||||
|
|
||||||
|
$views = $query->with('viewable')->skip($skipCount)->take($count)->get();
|
||||||
|
$viewedEntities = $views->map(function ($item) {
|
||||||
|
return $item->viewable()->getResults();
|
||||||
|
});
|
||||||
|
return $viewedEntities;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all recently viewed entities for the current user.
|
* Get all recently viewed entities for the current user.
|
||||||
* @param int $count
|
* @param int $count
|
||||||
|
@ -34,12 +34,23 @@
|
|||||||
@endif
|
@endif
|
||||||
</div>
|
</div>
|
||||||
<div class="col-sm-4 col-sm-offset-1">
|
<div class="col-sm-4 col-sm-offset-1">
|
||||||
<div class="margin-top large"> </div>
|
<div id="recents">
|
||||||
@if($recents)
|
@if($recents)
|
||||||
|
<div class="margin-top large"> </div>
|
||||||
<h3>Recently Viewed</h3>
|
<h3>Recently Viewed</h3>
|
||||||
@include('partials/entity-list', ['entities' => $recents])
|
@include('partials/entity-list', ['entities' => $recents])
|
||||||
@endif
|
@endif
|
||||||
</div>
|
</div>
|
||||||
|
<div class="margin-top large"> </div>
|
||||||
|
<div id="popular">
|
||||||
|
<h3>Popular Books</h3>
|
||||||
|
@if(count($popular) > 0)
|
||||||
|
@include('partials/entity-list', ['entities' => $popular])
|
||||||
|
@else
|
||||||
|
<p class="text-muted">The most popular books will appear here.</p>
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
@if(count($entities) > 0)
|
@if(count($entities) > 0)
|
||||||
@foreach($entities as $entity)
|
@foreach($entities as $index => $entity)
|
||||||
@if($entity->isA('page'))
|
@if($entity->isA('page'))
|
||||||
@include('pages/list-item', ['page' => $entity])
|
@include('pages/list-item', ['page' => $entity])
|
||||||
@elseif($entity->isA('book'))
|
@elseif($entity->isA('book'))
|
||||||
@ -8,7 +8,11 @@
|
|||||||
@elseif($entity->isA('chapter'))
|
@elseif($entity->isA('chapter'))
|
||||||
@include('chapters/list-item', ['chapter' => $entity, 'hidePages' => true])
|
@include('chapters/list-item', ['chapter' => $entity, 'hidePages' => true])
|
||||||
@endif
|
@endif
|
||||||
|
|
||||||
|
@if($index !== count($entities) - 1)
|
||||||
<hr>
|
<hr>
|
||||||
|
@endif
|
||||||
|
|
||||||
@endforeach
|
@endforeach
|
||||||
@else
|
@else
|
||||||
<p class="text-muted">
|
<p class="text-muted">
|
||||||
|
38
tests/ActivityTrackingTest.php
Normal file
38
tests/ActivityTrackingTest.php
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
|
|
||||||
|
class ActivityTrackingTest extends TestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
public function testRecentlyViewedBooks()
|
||||||
|
{
|
||||||
|
$books = \BookStack\Book::all()->take(10);
|
||||||
|
|
||||||
|
$this->asAdmin()->visit('/books')
|
||||||
|
->dontSeeInElement('#recents', $books[0]->name)
|
||||||
|
->dontSeeInElement('#recents', $books[1]->name)
|
||||||
|
->visit($books[0]->getUrl())
|
||||||
|
->visit($books[1]->getUrl())
|
||||||
|
->visit('/books')
|
||||||
|
->seeInElement('#recents', $books[0]->name)
|
||||||
|
->seeInElement('#recents', $books[1]->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testPopularBooks()
|
||||||
|
{
|
||||||
|
$books = \BookStack\Book::all()->take(10);
|
||||||
|
|
||||||
|
$this->asAdmin()->visit('/books')
|
||||||
|
->dontSeeInElement('#popular', $books[0]->name)
|
||||||
|
->dontSeeInElement('#popular', $books[1]->name)
|
||||||
|
->visit($books[0]->getUrl())
|
||||||
|
->visit($books[1]->getUrl())
|
||||||
|
->visit($books[0]->getUrl())
|
||||||
|
->visit('/books')
|
||||||
|
->seeInNthElement('#popular .book', 0, $books[0]->name)
|
||||||
|
->seeInNthElement('#popular .book', 1, $books[1]->name);
|
||||||
|
}
|
||||||
|
}
|
@ -48,4 +48,31 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
|
|||||||
$settings->put($key, $value);
|
$settings->put($key, $value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert that a given string is seen inside an element.
|
||||||
|
*
|
||||||
|
* @param bool|string|null $element
|
||||||
|
* @param integer $position
|
||||||
|
* @param string $text
|
||||||
|
* @param bool $negate
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
protected function seeInNthElement($element, $position, $text, $negate = false)
|
||||||
|
{
|
||||||
|
$method = $negate ? 'assertNotRegExp' : 'assertRegExp';
|
||||||
|
|
||||||
|
$rawPattern = preg_quote($text, '/');
|
||||||
|
|
||||||
|
$escapedPattern = preg_quote(e($text), '/');
|
||||||
|
|
||||||
|
$content = $this->crawler->filter($element)->eq($position)->html();
|
||||||
|
|
||||||
|
$pattern = $rawPattern == $escapedPattern
|
||||||
|
? $rawPattern : "({$rawPattern}|{$escapedPattern})";
|
||||||
|
|
||||||
|
$this->$method("/$pattern/i", $content);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user