class BaseRepo
{
- protected $tagRepo;
- protected $imageRepo;
+ protected TagRepo $tagRepo;
+ protected ImageRepo $imageRepo;
public function __construct(TagRepo $tagRepo, ImageRepo $imageRepo)
{
if (isset($input['tags'])) {
$this->tagRepo->saveTagsToEntity($entity, $input['tags']);
+ $entity->touch();
}
$entity->rebuildPermissions();
class BookshelfApiController extends ApiController
{
- /**
- * @var BookshelfRepo
- */
- protected $bookshelfRepo;
+ protected BookshelfRepo $bookshelfRepo;
protected $rules = [
'create' => [
'name' => ['required', 'string', 'max:255'],
'description' => ['string', 'max:1000'],
'books' => ['array'],
+ 'tags' => ['array'],
],
'update' => [
'name' => ['string', 'min:1', 'max:255'],
'description' => ['string', 'max:1000'],
'books' => ['array'],
+ 'tags' => ['array'],
],
];
class PageApiController extends ApiController
{
- protected $pageRepo;
+ protected PageRepo $pageRepo;
protected $rules = [
'create' => [
'tags' => ['array'],
],
'update' => [
- 'book_id' => ['required', 'integer'],
- 'chapter_id' => ['required', 'integer'],
+ 'book_id' => ['integer'],
+ 'chapter_id' => ['integer'],
'name' => ['string', 'min:1', 'max:255'],
'html' => ['string'],
'markdown' => ['string'],
*/
public function update(Request $request, string $id)
{
+ $requestData = $this->validate($request, $this->rules['update']);
+
$page = $this->pageRepo->getById($id, []);
$this->checkOwnablePermission('page-update', $page);
}
}
- $updatedPage = $this->pageRepo->update($page, $request->all());
+ $updatedPage = $this->pageRepo->update($page, $requestData);
return response()->json($updatedPage->forJsonDisplay());
}
namespace Tests\Api;
use BookStack\Entities\Models\Book;
+use Carbon\Carbon;
+use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class BooksApiTest extends TestCase
{
use TestsApi;
- protected $baseEndpoint = '/api/books';
+ protected string $baseEndpoint = '/api/books';
public function test_index_endpoint_returns_expected_book()
{
$this->assertActivityExists('book_update', $book);
}
+ public function test_update_increments_updated_date_if_only_tags_are_sent()
+ {
+ $this->actingAsApiEditor();
+ $book = Book::visible()->first();
+ DB::table('books')->where('id', '=', $book->id)->update(['updated_at' => Carbon::now()->subWeek()]);
+
+ $details = [
+ 'tags' => [['name' => 'Category', 'value' => 'Testing']]
+ ];
+
+ $this->putJson($this->baseEndpoint . "/{$book->id}", $details);
+ $book->refresh();
+ $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $book->updated_at->unix());
+ }
+
public function test_delete_endpoint()
{
$this->actingAsApiEditor();
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Chapter;
+use Carbon\Carbon;
+use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class ChaptersApiTest extends TestCase
{
use TestsApi;
- protected $baseEndpoint = '/api/chapters';
+ protected string $baseEndpoint = '/api/chapters';
public function test_index_endpoint_returns_expected_chapter()
{
$this->assertActivityExists('chapter_update', $chapter);
}
+ public function test_update_increments_updated_date_if_only_tags_are_sent()
+ {
+ $this->actingAsApiEditor();
+ $chapter = Chapter::visible()->first();
+ DB::table('chapters')->where('id', '=', $chapter->id)->update(['updated_at' => Carbon::now()->subWeek()]);
+
+ $details = [
+ 'tags' => [['name' => 'Category', 'value' => 'Testing']]
+ ];
+
+ $this->putJson($this->baseEndpoint . "/{$chapter->id}", $details);
+ $chapter->refresh();
+ $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $chapter->updated_at->unix());
+ }
+
public function test_delete_endpoint()
{
$this->actingAsApiEditor();
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Page;
+use Carbon\Carbon;
+use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class PagesApiTest extends TestCase
{
use TestsApi;
- protected $baseEndpoint = '/api/pages';
+ protected string $baseEndpoint = '/api/pages';
public function test_index_endpoint_returns_expected_page()
{
$this->assertEquals($originalContent, $page->html);
}
+ public function test_update_increments_updated_date_if_only_tags_are_sent()
+ {
+ $this->actingAsApiEditor();
+ $page = Page::visible()->first();
+ DB::table('pages')->where('id', '=', $page->id)->update(['updated_at' => Carbon::now()->subWeek()]);
+
+ $details = [
+ 'tags' => [['name' => 'Category', 'value' => 'Testing']]
+ ];
+
+ $this->putJson($this->baseEndpoint . "/{$page->id}", $details);
+ $page->refresh();
+ $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $page->updated_at->unix());
+ }
+
public function test_delete_endpoint()
{
$this->actingAsApiEditor();
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Bookshelf;
+use Carbon\Carbon;
+use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class ShelvesApiTest extends TestCase
{
use TestsApi;
- protected $baseEndpoint = '/api/shelves';
+ protected string $baseEndpoint = '/api/shelves';
public function test_index_endpoint_returns_expected_shelf()
{
$this->assertActivityExists('bookshelf_update', $shelf);
}
+ public function test_update_increments_updated_date_if_only_tags_are_sent()
+ {
+ $this->actingAsApiEditor();
+ $shelf = Bookshelf::visible()->first();
+ DB::table('bookshelves')->where('id', '=', $shelf->id)->update(['updated_at' => Carbon::now()->subWeek()]);
+
+ $details = [
+ 'tags' => [['name' => 'Category', 'value' => 'Testing']]
+ ];
+
+ $this->putJson($this->baseEndpoint . "/{$shelf->id}", $details);
+ $shelf->refresh();
+ $this->assertGreaterThan(Carbon::now()->subDay()->unix(), $shelf->updated_at->unix());
+ }
+
public function test_update_only_assigns_books_if_param_provided()
{
$this->actingAsApiEditor();