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\BookRepo;
10 use BookStack\Entities\Repos\BookshelfRepo;
11 use Illuminate\Support\Str;
12 use Tests\Uploads\UsesImages;
14 class OpenGraphTest extends TestCase
18 public function test_page_tags()
20 $page = Page::query()->first();
21 $resp = $this->asEditor()->get($page->getUrl());
22 $tags = $this->getOpenGraphTags($resp);
24 $this->assertEquals($page->getShortName() . ' | BookStack', $tags['title']);
25 $this->assertEquals($page->getUrl(), $tags['url']);
26 $this->assertEquals(Str::limit($page->text, 100, '...'), $tags['description']);
29 public function test_chapter_tags()
31 $chapter = Chapter::query()->first();
32 $resp = $this->asEditor()->get($chapter->getUrl());
33 $tags = $this->getOpenGraphTags($resp);
35 $this->assertEquals($chapter->getShortName() . ' | BookStack', $tags['title']);
36 $this->assertEquals($chapter->getUrl(), $tags['url']);
37 $this->assertEquals(Str::limit($chapter->description, 100, '...'), $tags['description']);
40 public function test_book_tags()
42 $book = Book::query()->first();
43 $resp = $this->asEditor()->get($book->getUrl());
44 $tags = $this->getOpenGraphTags($resp);
46 $this->assertEquals($book->getShortName() . ' | BookStack', $tags['title']);
47 $this->assertEquals($book->getUrl(), $tags['url']);
48 $this->assertEquals(Str::limit($book->description, 100, '...'), $tags['description']);
49 $this->assertArrayNotHasKey('image', $tags);
51 // Test image set if image has cover image
52 $bookRepo = app(BookRepo::class);
53 $bookRepo->updateCoverImage($book, $this->getTestImage('image.png'));
54 $resp = $this->asEditor()->get($book->getUrl());
55 $tags = $this->getOpenGraphTags($resp);
57 $this->assertEquals($book->getBookCover(), $tags['image']);
60 public function test_shelf_tags()
62 $shelf = Bookshelf::query()->first();
63 $resp = $this->asEditor()->get($shelf->getUrl());
64 $tags = $this->getOpenGraphTags($resp);
66 $this->assertEquals($shelf->getShortName() . ' | BookStack', $tags['title']);
67 $this->assertEquals($shelf->getUrl(), $tags['url']);
68 $this->assertEquals(Str::limit($shelf->description, 100, '...'), $tags['description']);
69 $this->assertArrayNotHasKey('image', $tags);
71 // Test image set if image has cover image
72 $shelfRepo = app(BookshelfRepo::class);
73 $shelfRepo->updateCoverImage($shelf, $this->getTestImage('image.png'));
74 $resp = $this->asEditor()->get($shelf->getUrl());
75 $tags = $this->getOpenGraphTags($resp);
77 $this->assertEquals($shelf->getBookCover(), $tags['image']);
81 * Parse the open graph tags from a test response.
83 protected function getOpenGraphTags(TestResponse $resp): array
87 libxml_use_internal_errors(true);
88 $doc = new \DOMDocument();
89 $doc->loadHTML($resp->getContent());
90 $metaElems = $doc->getElementsByTagName('meta');
91 /** @var \DOMElement $elem */
92 foreach ($metaElems as $elem) {
93 $prop = $elem->getAttribute('property');
94 $name = explode(':', $prop)[1] ?? null;
96 $tags[$name] = $elem->getAttribute('content');