+ public function test_pages_leading_to_entity_updated_on_url_change()
+ {
+ $pageA = $this->entities->page();
+ $pageB = $this->entities->page();
+ $book = $this->entities->book();
+
+ foreach ([$pageA, $pageB] as $page) {
+ $page->html = '<a href="' . $book->getUrl() . '">Link</a>';
+ $page->save();
+ $this->createReference($page, $book);
+ }
+
+ $this->asEditor()->put($book->getUrl(), [
+ 'name' => 'my updated book slugaroo',
+ ]);
+
+ foreach ([$pageA, $pageB] as $page) {
+ $page->refresh();
+ $this->assertStringContainsString('href="http://localhost/books/my-updated-book-slugaroo"', $page->html);
+ $this->assertDatabaseHas('page_revisions', [
+ 'page_id' => $page->id,
+ 'summary' => 'System auto-update of internal links',
+ ]);
+ }
+ }
+
+ public function test_pages_linking_to_other_page_updated_on_parent_book_url_change()
+ {
+ $bookPage = $this->entities->page();
+ $otherPage = $this->entities->page();
+ $book = $bookPage->book;
+
+ $otherPage->html = '<a href="' . $bookPage->getUrl() . '">Link</a>';
+ $otherPage->save();
+ $this->createReference($otherPage, $bookPage);
+
+ $this->asEditor()->put($book->getUrl(), [
+ 'name' => 'my updated book slugaroo',
+ ]);
+
+ $otherPage->refresh();
+ $this->assertStringContainsString('href="http://localhost/books/my-updated-book-slugaroo/page/' . $bookPage->slug . '"', $otherPage->html);
+ $this->assertDatabaseHas('page_revisions', [
+ 'page_id' => $otherPage->id,
+ 'summary' => 'System auto-update of internal links',
+ ]);
+ }
+
+ public function test_pages_linking_to_chapter_updated_on_parent_book_url_change()
+ {
+ $bookChapter = $this->entities->chapter();
+ $otherPage = $this->entities->page();
+ $book = $bookChapter->book;
+
+ $otherPage->html = '<a href="' . $bookChapter->getUrl() . '">Link</a>';
+ $otherPage->save();
+ $this->createReference($otherPage, $bookChapter);
+
+ $this->asEditor()->put($book->getUrl(), [
+ 'name' => 'my updated book slugaroo',
+ ]);
+
+ $otherPage->refresh();
+ $this->assertStringContainsString('href="http://localhost/books/my-updated-book-slugaroo/chapter/' . $bookChapter->slug . '"', $otherPage->html);
+ $this->assertDatabaseHas('page_revisions', [
+ 'page_id' => $otherPage->id,
+ 'summary' => 'System auto-update of internal links',
+ ]);
+ }
+
+ public function test_markdown_links_leading_to_entity_updated_on_url_change()
+ {
+ $page = $this->entities->page();
+ $book = $this->entities->book();
+
+ $bookUrl = $book->getUrl();
+ $markdown = '
+ [An awesome link](' . $bookUrl . ')
+ [An awesome link with query & hash](' . $bookUrl . '?test=yes#cats)
+ [An awesome link with path](' . $bookUrl . '/an/extra/trail)
+ [An awesome link with title](' . $bookUrl . ' "title")
+ [ref]: ' . $bookUrl . '?test=yes#dogs
+ [ref_without_space]:' . $bookUrl . '
+ [ref_with_title]: ' . $bookUrl . ' "title"';
+ $page->markdown = $markdown;
+ $page->save();
+ $this->createReference($page, $book);
+
+ $this->asEditor()->put($book->getUrl(), [
+ 'name' => 'my updated book slugadoo',
+ ]);
+
+ $page->refresh();
+ $expected = str_replace($bookUrl, 'http://localhost/books/my-updated-book-slugadoo', $markdown);
+ $this->assertEquals($expected, $page->markdown);
+ }
+
+ public function test_description_links_from_book_chapter_shelf_updated_on_url_change()
+ {
+ $entities = [$this->entities->chapter(), $this->entities->book(), $this->entities->shelf()];
+ $shelf = $this->entities->shelf();
+ $this->asEditor();
+
+ foreach ($entities as $entity) {
+ $this->put($entity->getUrl(), [
+ 'name' => 'Reference test',
+ 'description_html' => '<a href="' . $shelf->getUrl() . '">Testing</a>',
+ ]);
+ }
+
+ $oldUrl = $shelf->getUrl();
+ $this->put($shelf->getUrl(), ['name' => 'My updated shelf link']);
+ $shelf->refresh();
+ $this->assertNotEquals($oldUrl, $shelf->getUrl());
+
+ foreach ($entities as $entity) {
+ $oldHtml = $entity->description_html;
+ $entity->refresh();
+ $this->assertNotEquals($oldHtml, $entity->description_html);
+ $this->assertStringContainsString($shelf->getUrl(), $entity->description_html);
+ }
+ }
+
+ public function test_reference_from_deleted_item_does_not_count_or_show_in_references_page()
+ {
+ $page = $this->entities->page();
+ $referencingPageA = $this->entities->page();
+ $referencingPageB = $this->entities->page();
+
+ $this->asEditor();
+ $this->createReference($referencingPageA, $page);
+ $this->createReference($referencingPageB, $page);
+
+ $resp = $this->get($page->getUrl());
+ $resp->assertSee('Referenced by 2 items');
+
+ $this->delete($referencingPageA->getUrl());
+
+ $resp = $this->get($page->getUrl());
+ $resp->assertSee('Referenced by 1 item');
+
+ $resp = $this->get($page->getUrl('/references'));
+ $resp->assertOk();
+ $resp->assertSee($referencingPageB->getUrl());
+ $resp->assertDontSee($referencingPageA->getUrl());
+ }
+
+ protected function createReference(Model $from, Model $to): void