3 use BookStack\Entities\Models\Book;
4 use BookStack\Entities\Models\Bookshelf;
5 use BookStack\Entities\Models\Chapter;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Entities\Repos\BookRepo;
8 use BookStack\Entities\Repos\BookshelfRepo;
9 use Illuminate\Support\Str;
10 use Tests\Uploads\UsesImages;
12 class OpenGraphTest extends TestCase
16 public function test_page_tags()
18 $page = Page::query()->first();
19 $resp = $this->asEditor()->get($page->getUrl());
20 $tags = $this->getOpenGraphTags($resp);
22 $this->assertEquals($page->getShortName() . ' | BookStack', $tags['title']);
23 $this->assertEquals($page->getUrl(), $tags['url']);
24 $this->assertEquals(Str::limit($page->text, 100, '...'), $tags['description']);
27 public function test_chapter_tags()
29 $chapter = Chapter::query()->first();
30 $resp = $this->asEditor()->get($chapter->getUrl());
31 $tags = $this->getOpenGraphTags($resp);
33 $this->assertEquals($chapter->getShortName() . ' | BookStack', $tags['title']);
34 $this->assertEquals($chapter->getUrl(), $tags['url']);
35 $this->assertEquals(Str::limit($chapter->description, 100, '...'), $tags['description']);
38 public function test_book_tags()
40 $book = Book::query()->first();
41 $resp = $this->asEditor()->get($book->getUrl());
42 $tags = $this->getOpenGraphTags($resp);
44 $this->assertEquals($book->getShortName() . ' | BookStack', $tags['title']);
45 $this->assertEquals($book->getUrl(), $tags['url']);
46 $this->assertEquals(Str::limit($book->description, 100, '...'), $tags['description']);
47 $this->assertArrayNotHasKey('image', $tags);
49 // Test image set if image has cover image
50 $bookRepo = app(BookRepo::class);
51 $bookRepo->updateCoverImage($book, $this->getTestImage('image.png'));
52 $resp = $this->asEditor()->get($book->getUrl());
53 $tags = $this->getOpenGraphTags($resp);
55 $this->assertEquals($book->getBookCover(), $tags['image']);
58 public function test_shelf_tags()
60 $shelf = Bookshelf::query()->first();
61 $resp = $this->asEditor()->get($shelf->getUrl());
62 $tags = $this->getOpenGraphTags($resp);
64 $this->assertEquals($shelf->getShortName() . ' | BookStack', $tags['title']);
65 $this->assertEquals($shelf->getUrl(), $tags['url']);
66 $this->assertEquals(Str::limit($shelf->description, 100, '...'), $tags['description']);
67 $this->assertArrayNotHasKey('image', $tags);
69 // Test image set if image has cover image
70 $shelfRepo = app(BookshelfRepo::class);
71 $shelfRepo->updateCoverImage($shelf, $this->getTestImage('image.png'));
72 $resp = $this->asEditor()->get($shelf->getUrl());
73 $tags = $this->getOpenGraphTags($resp);
75 $this->assertEquals($shelf->getBookCover(), $tags['image']);
79 * Parse the open graph tags from a test response.
81 protected function getOpenGraphTags(TestResponse $resp): array
85 libxml_use_internal_errors(true);
86 $doc = new \DOMDocument();
87 $doc->loadHTML($resp->getContent());
88 $metaElems = $doc->getElementsByTagName('meta');
89 /** @var \DOMElement $elem */
90 foreach ($metaElems as $elem) {
91 $prop = $elem->getAttribute('property');
92 $name = explode(':', $prop)[1] ?? null;
94 $tags[$name] = $elem->getAttribute('content');