5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Bookshelf;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Models\Page;
9 use BookStack\Entities\Repos\BaseRepo;
10 use BookStack\Entities\Repos\BookRepo;
11 use Illuminate\Support\Str;
12 use Illuminate\Testing\TestResponse;
13 use Tests\Uploads\UsesImages;
15 class OpenGraphTest extends TestCase
19 public function test_page_tags()
21 $page = Page::query()->first();
22 $resp = $this->asEditor()->get($page->getUrl());
23 $tags = $this->getOpenGraphTags($resp);
25 $this->assertEquals($page->getShortName() . ' | BookStack', $tags['title']);
26 $this->assertEquals($page->getUrl(), $tags['url']);
27 $this->assertEquals(Str::limit($page->text, 100, '...'), $tags['description']);
30 public function test_chapter_tags()
32 $chapter = Chapter::query()->first();
33 $resp = $this->asEditor()->get($chapter->getUrl());
34 $tags = $this->getOpenGraphTags($resp);
36 $this->assertEquals($chapter->getShortName() . ' | BookStack', $tags['title']);
37 $this->assertEquals($chapter->getUrl(), $tags['url']);
38 $this->assertEquals(Str::limit($chapter->description, 100, '...'), $tags['description']);
41 public function test_book_tags()
43 $book = Book::query()->first();
44 $resp = $this->asEditor()->get($book->getUrl());
45 $tags = $this->getOpenGraphTags($resp);
47 $this->assertEquals($book->getShortName() . ' | BookStack', $tags['title']);
48 $this->assertEquals($book->getUrl(), $tags['url']);
49 $this->assertEquals(Str::limit($book->description, 100, '...'), $tags['description']);
50 $this->assertArrayNotHasKey('image', $tags);
52 // Test image set if image has cover image
53 $bookRepo = app(BookRepo::class);
54 $bookRepo->updateCoverImage($book, $this->getTestImage('image.png'));
55 $resp = $this->asEditor()->get($book->getUrl());
56 $tags = $this->getOpenGraphTags($resp);
58 $this->assertEquals($book->getBookCover(), $tags['image']);
61 public function test_shelf_tags()
63 $shelf = Bookshelf::query()->first();
64 $resp = $this->asEditor()->get($shelf->getUrl());
65 $tags = $this->getOpenGraphTags($resp);
67 $this->assertEquals($shelf->getShortName() . ' | BookStack', $tags['title']);
68 $this->assertEquals($shelf->getUrl(), $tags['url']);
69 $this->assertEquals(Str::limit($shelf->description, 100, '...'), $tags['description']);
70 $this->assertArrayNotHasKey('image', $tags);
72 // Test image set if image has cover image
73 $baseRepo = app(BaseRepo::class);
74 $baseRepo->updateCoverImage($shelf, $this->getTestImage('image.png'));
75 $resp = $this->asEditor()->get($shelf->getUrl());
76 $tags = $this->getOpenGraphTags($resp);
78 $this->assertEquals($shelf->getBookCover(), $tags['image']);
82 * Parse the open graph tags from a test response.
84 protected function getOpenGraphTags(TestResponse $resp): array
88 libxml_use_internal_errors(true);
89 $doc = new \DOMDocument();
90 $doc->loadHTML($resp->getContent());
91 $metaElems = $doc->getElementsByTagName('meta');
92 /** @var \DOMElement $elem */
93 foreach ($metaElems as $elem) {
94 $prop = $elem->getAttribute('property');
95 $name = explode(':', $prop)[1] ?? null;
97 $tags[$name] = $elem->getAttribute('content');