5 use BookStack\Entities\Repos\BaseRepo;
6 use BookStack\Entities\Repos\BookRepo;
7 use Illuminate\Support\Str;
8 use Illuminate\Testing\TestResponse;
11 class OpenGraphTest extends TestCase
13 public function test_page_tags()
15 $page = $this->entities->page();
16 $resp = $this->asEditor()->get($page->getUrl());
17 $tags = $this->getOpenGraphTags($resp);
19 $this->assertEquals($page->getShortName() . ' | BookStack', $tags['title']);
20 $this->assertEquals($page->getUrl(), $tags['url']);
21 $this->assertEquals(Str::limit($page->text, 100, '...'), $tags['description']);
24 public function test_chapter_tags()
26 $chapter = $this->entities->chapter();
27 $resp = $this->asEditor()->get($chapter->getUrl());
28 $tags = $this->getOpenGraphTags($resp);
30 $this->assertEquals($chapter->getShortName() . ' | BookStack', $tags['title']);
31 $this->assertEquals($chapter->getUrl(), $tags['url']);
32 $this->assertEquals(Str::limit($chapter->description, 100, '...'), $tags['description']);
35 public function test_book_tags()
37 $book = $this->entities->book();
38 $resp = $this->asEditor()->get($book->getUrl());
39 $tags = $this->getOpenGraphTags($resp);
41 $this->assertEquals($book->getShortName() . ' | BookStack', $tags['title']);
42 $this->assertEquals($book->getUrl(), $tags['url']);
43 $this->assertEquals(Str::limit($book->description, 100, '...'), $tags['description']);
44 $this->assertArrayNotHasKey('image', $tags);
46 // Test image set if image has cover image
47 $bookRepo = app(BookRepo::class);
48 $bookRepo->updateCoverImage($book, $this->files->uploadedImage('image.png'));
49 $resp = $this->asEditor()->get($book->getUrl());
50 $tags = $this->getOpenGraphTags($resp);
52 $this->assertEquals($book->getBookCover(), $tags['image']);
55 public function test_shelf_tags()
57 $shelf = $this->entities->shelf();
58 $resp = $this->asEditor()->get($shelf->getUrl());
59 $tags = $this->getOpenGraphTags($resp);
61 $this->assertEquals($shelf->getShortName() . ' | BookStack', $tags['title']);
62 $this->assertEquals($shelf->getUrl(), $tags['url']);
63 $this->assertEquals(Str::limit($shelf->description, 100, '...'), $tags['description']);
64 $this->assertArrayNotHasKey('image', $tags);
66 // Test image set if image has cover image
67 $baseRepo = app(BaseRepo::class);
68 $baseRepo->updateCoverImage($shelf, $this->files->uploadedImage('image.png'));
69 $resp = $this->asEditor()->get($shelf->getUrl());
70 $tags = $this->getOpenGraphTags($resp);
72 $this->assertEquals($shelf->getBookCover(), $tags['image']);
76 * Parse the open graph tags from a test response.
78 protected function getOpenGraphTags(TestResponse $resp): array
82 libxml_use_internal_errors(true);
83 $doc = new \DOMDocument();
84 $doc->loadHTML($resp->getContent());
85 $metaElems = $doc->getElementsByTagName('meta');
86 /** @var \DOMElement $elem */
87 foreach ($metaElems as $elem) {
88 $prop = $elem->getAttribute('property');
89 $name = explode(':', $prop)[1] ?? null;
91 $tags[$name] = $elem->getAttribute('content');