]> BookStack Code Mirror - bookstack/blob - tests/OpenGraphTest.php
Fix Crowdin name in the language_request issue template
[bookstack] / tests / OpenGraphTest.php
1 <?php
2
3 namespace Tests;
4
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;
13
14 class OpenGraphTest extends TestCase
15 {
16     use UsesImages;
17
18     public function test_page_tags()
19     {
20         $page = Page::query()->first();
21         $resp = $this->asEditor()->get($page->getUrl());
22         $tags = $this->getOpenGraphTags($resp);
23
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']);
27     }
28
29     public function test_chapter_tags()
30     {
31         $chapter = Chapter::query()->first();
32         $resp = $this->asEditor()->get($chapter->getUrl());
33         $tags = $this->getOpenGraphTags($resp);
34
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']);
38     }
39
40     public function test_book_tags()
41     {
42         $book = Book::query()->first();
43         $resp = $this->asEditor()->get($book->getUrl());
44         $tags = $this->getOpenGraphTags($resp);
45
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);
50
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);
56
57         $this->assertEquals($book->getBookCover(), $tags['image']);
58     }
59
60     public function test_shelf_tags()
61     {
62         $shelf = Bookshelf::query()->first();
63         $resp = $this->asEditor()->get($shelf->getUrl());
64         $tags = $this->getOpenGraphTags($resp);
65
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);
70
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);
76
77         $this->assertEquals($shelf->getBookCover(), $tags['image']);
78     }
79
80     /**
81      * Parse the open graph tags from a test response.
82      */
83     protected function getOpenGraphTags(TestResponse $resp): array
84     {
85         $tags = [];
86
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;
95             if ($name) {
96                 $tags[$name] = $elem->getAttribute('content');
97             }
98         }
99
100         return $tags;
101     }
102 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.