From a21ca446332a7020e54bea15171d4b6233ea3ab8 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Wed, 20 Dec 2023 17:21:09 +0000 Subject: [PATCH] Input WYSIWYG: Fixed existing tests, fixed empty description handling --- app/Util/HtmlDescriptionFilter.php | 4 ++++ database/seeders/DummyContentSeeder.php | 5 +++-- tests/Api/SearchApiTest.php | 2 +- tests/Entity/BookTest.php | 2 ++ tests/Entity/ConvertTest.php | 2 ++ tests/References/ReferencesTest.php | 6 +++--- tests/Settings/RegenerateReferencesTest.php | 4 ++-- 7 files changed, 17 insertions(+), 8 deletions(-) diff --git a/app/Util/HtmlDescriptionFilter.php b/app/Util/HtmlDescriptionFilter.php index 4494a73ce..7287586d1 100644 --- a/app/Util/HtmlDescriptionFilter.php +++ b/app/Util/HtmlDescriptionFilter.php @@ -31,6 +31,10 @@ class HtmlDescriptionFilter public static function filterFromString(string $html): string { + if (empty(trim($html))) { + return ''; + } + $doc = new HtmlDocument($html); $topLevel = [...$doc->getBodyChildren()]; diff --git a/database/seeders/DummyContentSeeder.php b/database/seeders/DummyContentSeeder.php index 47e8d1d7c..a4383be50 100644 --- a/database/seeders/DummyContentSeeder.php +++ b/database/seeders/DummyContentSeeder.php @@ -3,6 +3,7 @@ namespace Database\Seeders; use BookStack\Api\ApiToken; +use BookStack\Entities\Models\Book; use BookStack\Entities\Models\Bookshelf; use BookStack\Entities\Models\Chapter; use BookStack\Entities\Models\Page; @@ -38,7 +39,7 @@ class DummyContentSeeder extends Seeder $byData = ['created_by' => $editorUser->id, 'updated_by' => $editorUser->id, 'owned_by' => $editorUser->id]; - \BookStack\Entities\Models\Book::factory()->count(5)->create($byData) + Book::factory()->count(5)->create($byData) ->each(function ($book) use ($byData) { $chapters = Chapter::factory()->count(3)->create($byData) ->each(function ($chapter) use ($book, $byData) { @@ -50,7 +51,7 @@ class DummyContentSeeder extends Seeder $book->pages()->saveMany($pages); }); - $largeBook = \BookStack\Entities\Models\Book::factory()->create(array_merge($byData, ['name' => 'Large book' . Str::random(10)])); + $largeBook = Book::factory()->create(array_merge($byData, ['name' => 'Large book' . Str::random(10)])); $pages = Page::factory()->count(200)->make($byData); $chapters = Chapter::factory()->count(50)->make($byData); $largeBook->pages()->saveMany($pages); diff --git a/tests/Api/SearchApiTest.php b/tests/Api/SearchApiTest.php index cdc954ec3..2a186e8d6 100644 --- a/tests/Api/SearchApiTest.php +++ b/tests/Api/SearchApiTest.php @@ -52,7 +52,7 @@ class SearchApiTest extends TestCase public function test_all_endpoint_returns_items_with_preview_html() { $book = $this->entities->book(); - $book->update(['name' => 'name with superuniquevalue within', 'description' => 'Description with superuniquevalue within']); + $book->forceFill(['name' => 'name with superuniquevalue within', 'description' => 'Description with superuniquevalue within'])->save(); $book->indexForSearch(); $resp = $this->actingAsApiAdmin()->getJson($this->baseEndpoint . '?query=superuniquevalue'); diff --git a/tests/Entity/BookTest.php b/tests/Entity/BookTest.php index 3e37e61f7..c4872785b 100644 --- a/tests/Entity/BookTest.php +++ b/tests/Entity/BookTest.php @@ -307,6 +307,8 @@ class BookTest extends TestCase $resp->assertRedirect($copy->getUrl()); $this->assertEquals($book->getDirectChildren()->count(), $copy->getDirectChildren()->count()); + + $this->get($copy->getUrl())->assertSee($book->description_html, false); } public function test_copy_does_not_copy_non_visible_content() diff --git a/tests/Entity/ConvertTest.php b/tests/Entity/ConvertTest.php index decda5224..d9b1ee466 100644 --- a/tests/Entity/ConvertTest.php +++ b/tests/Entity/ConvertTest.php @@ -42,6 +42,7 @@ class ConvertTest extends TestCase $this->assertEquals('Penguins', $newBook->tags->first()->value); $this->assertEquals($chapter->name, $newBook->name); $this->assertEquals($chapter->description, $newBook->description); + $this->assertEquals($chapter->description_html, $newBook->description_html); $this->assertActivityExists(ActivityType::BOOK_CREATE_FROM_CHAPTER, $newBook); } @@ -105,6 +106,7 @@ class ConvertTest extends TestCase $this->assertEquals('Ducks', $newShelf->tags->first()->value); $this->assertEquals($book->name, $newShelf->name); $this->assertEquals($book->description, $newShelf->description); + $this->assertEquals($book->description_html, $newShelf->description_html); $this->assertEquals($newShelf->books()->count(), $bookChapterCount + 1); $this->assertEquals($systemBookCount + $bookChapterCount, Book::query()->count()); $this->assertActivityExists(ActivityType::BOOKSHELF_CREATE_FROM_BOOK, $newShelf); diff --git a/tests/References/ReferencesTest.php b/tests/References/ReferencesTest.php index bc691b0cd..715f71435 100644 --- a/tests/References/ReferencesTest.php +++ b/tests/References/ReferencesTest.php @@ -102,13 +102,13 @@ class ReferencesTest extends TestCase foreach ($entities as $entity) { $resp = $this->get($entity->getUrl()); - $resp->assertSee('Referenced on 1 page'); - $resp->assertDontSee('Referenced on 1 pages'); + $resp->assertSee('Referenced by 1 item'); + $resp->assertDontSee('Referenced by 1 items'); } $this->createReference($otherPage, $entities['page']); $resp = $this->get($entities['page']->getUrl()); - $resp->assertSee('Referenced on 2 pages'); + $resp->assertSee('Referenced by 2 items'); } public function test_references_to_visible_on_references_page() diff --git a/tests/Settings/RegenerateReferencesTest.php b/tests/Settings/RegenerateReferencesTest.php index 25832b03e..0511f2624 100644 --- a/tests/Settings/RegenerateReferencesTest.php +++ b/tests/Settings/RegenerateReferencesTest.php @@ -21,7 +21,7 @@ class RegenerateReferencesTest extends TestCase public function test_action_runs_reference_regen() { $this->mock(ReferenceStore::class) - ->shouldReceive('updateForAllPages') + ->shouldReceive('updateForAll') ->once(); $resp = $this->asAdmin()->post('/settings/maintenance/regenerate-references'); @@ -45,7 +45,7 @@ class RegenerateReferencesTest extends TestCase public function test_action_failed_shown_as_error_notification() { $this->mock(ReferenceStore::class) - ->shouldReceive('updateForAllPages') + ->shouldReceive('updateForAll') ->andThrow(\Exception::class, 'A badger stopped the task'); $resp = $this->asAdmin()->post('/settings/maintenance/regenerate-references');