5 use BookStack\Entities\Repos\BaseRepo;
6 use BookStack\Entities\Repos\BookRepo;
7 use Illuminate\Support\Str;
8 use Illuminate\Testing\TestResponse;
9 use Tests\Uploads\UsesImages;
11 class OpenGraphTest extends TestCase
15 public function test_page_tags()
17 $page = $this->entities->page();
18 $resp = $this->asEditor()->get($page->getUrl());
19 $tags = $this->getOpenGraphTags($resp);
21 $this->assertEquals($page->getShortName() . ' | BookStack', $tags['title']);
22 $this->assertEquals($page->getUrl(), $tags['url']);
23 $this->assertEquals(Str::limit($page->text, 100, '...'), $tags['description']);
26 public function test_chapter_tags()
28 $chapter = $this->entities->chapter();
29 $resp = $this->asEditor()->get($chapter->getUrl());
30 $tags = $this->getOpenGraphTags($resp);
32 $this->assertEquals($chapter->getShortName() . ' | BookStack', $tags['title']);
33 $this->assertEquals($chapter->getUrl(), $tags['url']);
34 $this->assertEquals(Str::limit($chapter->description, 100, '...'), $tags['description']);
37 public function test_book_tags()
39 $book = $this->entities->book();
40 $resp = $this->asEditor()->get($book->getUrl());
41 $tags = $this->getOpenGraphTags($resp);
43 $this->assertEquals($book->getShortName() . ' | BookStack', $tags['title']);
44 $this->assertEquals($book->getUrl(), $tags['url']);
45 $this->assertEquals(Str::limit($book->description, 100, '...'), $tags['description']);
46 $this->assertArrayNotHasKey('image', $tags);
48 // Test image set if image has cover image
49 $bookRepo = app(BookRepo::class);
50 $bookRepo->updateCoverImage($book, $this->getTestImage('image.png'));
51 $resp = $this->asEditor()->get($book->getUrl());
52 $tags = $this->getOpenGraphTags($resp);
54 $this->assertEquals($book->getBookCover(), $tags['image']);
57 public function test_shelf_tags()
59 $shelf = $this->entities->shelf();
60 $resp = $this->asEditor()->get($shelf->getUrl());
61 $tags = $this->getOpenGraphTags($resp);
63 $this->assertEquals($shelf->getShortName() . ' | BookStack', $tags['title']);
64 $this->assertEquals($shelf->getUrl(), $tags['url']);
65 $this->assertEquals(Str::limit($shelf->description, 100, '...'), $tags['description']);
66 $this->assertArrayNotHasKey('image', $tags);
68 // Test image set if image has cover image
69 $baseRepo = app(BaseRepo::class);
70 $baseRepo->updateCoverImage($shelf, $this->getTestImage('image.png'));
71 $resp = $this->asEditor()->get($shelf->getUrl());
72 $tags = $this->getOpenGraphTags($resp);
74 $this->assertEquals($shelf->getBookCover(), $tags['image']);
78 * Parse the open graph tags from a test response.
80 protected function getOpenGraphTags(TestResponse $resp): array
84 libxml_use_internal_errors(true);
85 $doc = new \DOMDocument();
86 $doc->loadHTML($resp->getContent());
87 $metaElems = $doc->getElementsByTagName('meta');
88 /** @var \DOMElement $elem */
89 foreach ($metaElems as $elem) {
90 $prop = $elem->getAttribute('property');
91 $name = explode(':', $prop)[1] ?? null;
93 $tags[$name] = $elem->getAttribute('content');