5 use BookStack\Entities\Repos\BaseRepo;
6 use BookStack\Entities\Repos\BookRepo;
7 use Illuminate\Support\Str;
8 use Illuminate\Testing\TestResponse;
10 class OpenGraphTest extends TestCase
12 public function test_page_tags()
14 $page = $this->entities->page();
15 $resp = $this->asEditor()->get($page->getUrl());
16 $tags = $this->getOpenGraphTags($resp);
18 $this->assertEquals($page->getShortName() . ' | BookStack', $tags['title']);
19 $this->assertEquals($page->getUrl(), $tags['url']);
20 $this->assertEquals(Str::limit($page->text, 100, '...'), $tags['description']);
23 public function test_chapter_tags()
25 $chapter = $this->entities->chapter();
26 $resp = $this->asEditor()->get($chapter->getUrl());
27 $tags = $this->getOpenGraphTags($resp);
29 $this->assertEquals($chapter->getShortName() . ' | BookStack', $tags['title']);
30 $this->assertEquals($chapter->getUrl(), $tags['url']);
31 $this->assertEquals(Str::limit($chapter->description, 100, '...'), $tags['description']);
34 public function test_book_tags()
36 $book = $this->entities->book();
37 $resp = $this->asEditor()->get($book->getUrl());
38 $tags = $this->getOpenGraphTags($resp);
40 $this->assertEquals($book->getShortName() . ' | BookStack', $tags['title']);
41 $this->assertEquals($book->getUrl(), $tags['url']);
42 $this->assertEquals(Str::limit($book->description, 100, '...'), $tags['description']);
43 $this->assertArrayNotHasKey('image', $tags);
45 // Test image set if image has cover image
46 $bookRepo = app(BookRepo::class);
47 $bookRepo->updateCoverImage($book, $this->files->uploadedImage('image.png'));
48 $resp = $this->asEditor()->get($book->getUrl());
49 $tags = $this->getOpenGraphTags($resp);
51 $this->assertEquals($book->getBookCover(), $tags['image']);
54 public function test_shelf_tags()
56 $shelf = $this->entities->shelf();
57 $resp = $this->asEditor()->get($shelf->getUrl());
58 $tags = $this->getOpenGraphTags($resp);
60 $this->assertEquals($shelf->getShortName() . ' | BookStack', $tags['title']);
61 $this->assertEquals($shelf->getUrl(), $tags['url']);
62 $this->assertEquals(Str::limit($shelf->description, 100, '...'), $tags['description']);
63 $this->assertArrayNotHasKey('image', $tags);
65 // Test image set if image has cover image
66 $baseRepo = app(BaseRepo::class);
67 $baseRepo->updateCoverImage($shelf, $this->files->uploadedImage('image.png'));
68 $resp = $this->asEditor()->get($shelf->getUrl());
69 $tags = $this->getOpenGraphTags($resp);
71 $this->assertEquals($shelf->getBookCover(), $tags['image']);
75 * Parse the open graph tags from a test response.
77 protected function getOpenGraphTags(TestResponse $resp): array
81 libxml_use_internal_errors(true);
82 $doc = new \DOMDocument();
83 $doc->loadHTML($resp->getContent());
84 $metaElems = $doc->getElementsByTagName('meta');
85 /** @var \DOMElement $elem */
86 foreach ($metaElems as $elem) {
87 $prop = $elem->getAttribute('property');
88 $name = explode(':', $prop)[1] ?? null;
90 $tags[$name] = $elem->getAttribute('content');