Added regenerate-references command test

Also updated model resolvers to only fetch model ID, to prevent bringing
back way more data from database than desired.
This commit is contained in:
Dan Brown 2022-08-17 16:59:23 +01:00
parent 5d29d0cc7b
commit 3290ab3ac9
No known key found for this signature in database
GPG key ID: 46D9F943C24A2EF9
7 changed files with 38 additions and 6 deletions

View file

@ -19,7 +19,7 @@ class BookLinkModelResolver implements CrossLinkModelResolver
$bookSlug = $matches[1];
/** @var ?Book $model */
$model = Book::query()->where('slug', '=', $bookSlug)->first();
$model = Book::query()->where('slug', '=', $bookSlug)->first(['id']);
return $model;
}

View file

@ -19,7 +19,7 @@ class BookshelfLinkModelResolver implements CrossLinkModelResolver
$shelfSlug = $matches[1];
/** @var ?Bookshelf $model */
$model = Bookshelf::query()->where('slug', '=', $shelfSlug)->first();
$model = Bookshelf::query()->where('slug', '=', $shelfSlug)->first(['id']);
return $model;
}

View file

@ -20,7 +20,7 @@ class ChapterLinkModelResolver implements CrossLinkModelResolver
$chapterSlug = $matches[2];
/** @var ?Chapter $model */
$model = Chapter::query()->whereSlugs($bookSlug, $chapterSlug)->first();
$model = Chapter::query()->whereSlugs($bookSlug, $chapterSlug)->first(['id']);
return $model;
}

View file

@ -20,7 +20,7 @@ class PageLinkModelResolver implements CrossLinkModelResolver
$pageSlug = $matches[2];
/** @var ?Page $model */
$model = Page::query()->whereSlugs($bookSlug, $pageSlug)->first();
$model = Page::query()->whereSlugs($bookSlug, $pageSlug)->first(['id']);
return $model;
}

View file

@ -18,7 +18,7 @@ class PagePermalinkModelResolver implements CrossLinkModelResolver
$id = intval($matches[1]);
/** @var ?Page $model */
$model = Page::query()->find($id);
$model = Page::query()->find($id, ['id']);
return $model;
}

View file

@ -23,7 +23,7 @@ class ReferenceService
{
Reference::query()
->where('from_type', '=', (new Page())->getMorphClass())
->truncate();
->delete();
Page::query()->select(['id', 'html'])->chunk(100, function(Collection $pages) {
$this->updateForPages($pages->all());

View file

@ -0,0 +1,32 @@
<?php
namespace Tests\Commands;
use BookStack\Entities\Models\Page;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
class RegenerateReferencesCommandTest extends TestCase
{
public function test_regenerate_references_command()
{
/** @var Page $page */
$page = Page::query()->first();
$book = $page->book;
$page->html = '<a href="' . $book->getUrl() . '">Book Link</a>';
$page->save();
DB::table('references')->delete();
$this->artisan('bookstack:regenerate-references')
->assertExitCode(0);
$this->assertDatabaseHas('references', [
'from_id' => $page->id,
'from_type' => $page->getMorphClass(),
'to_id' => $book->id,
'to_type' => $book->getMorphClass(),
]);
}
}