]> BookStack Code Mirror - bookstack/blob - tests/OpenGraphTest.php
Merge pull request #3545 from BookStackApp/l10n_development
[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\BaseRepo;
10 use BookStack\Entities\Repos\BookRepo;
11 use Illuminate\Support\Str;
12 use Illuminate\Testing\TestResponse;
13 use Tests\Uploads\UsesImages;
14
15 class OpenGraphTest extends TestCase
16 {
17     use UsesImages;
18
19     public function test_page_tags()
20     {
21         $page = Page::query()->first();
22         $resp = $this->asEditor()->get($page->getUrl());
23         $tags = $this->getOpenGraphTags($resp);
24
25         $this->assertEquals($page->getShortName() . ' | BookStack', $tags['title']);
26         $this->assertEquals($page->getUrl(), $tags['url']);
27         $this->assertEquals(Str::limit($page->text, 100, '...'), $tags['description']);
28     }
29
30     public function test_chapter_tags()
31     {
32         $chapter = Chapter::query()->first();
33         $resp = $this->asEditor()->get($chapter->getUrl());
34         $tags = $this->getOpenGraphTags($resp);
35
36         $this->assertEquals($chapter->getShortName() . ' | BookStack', $tags['title']);
37         $this->assertEquals($chapter->getUrl(), $tags['url']);
38         $this->assertEquals(Str::limit($chapter->description, 100, '...'), $tags['description']);
39     }
40
41     public function test_book_tags()
42     {
43         $book = Book::query()->first();
44         $resp = $this->asEditor()->get($book->getUrl());
45         $tags = $this->getOpenGraphTags($resp);
46
47         $this->assertEquals($book->getShortName() . ' | BookStack', $tags['title']);
48         $this->assertEquals($book->getUrl(), $tags['url']);
49         $this->assertEquals(Str::limit($book->description, 100, '...'), $tags['description']);
50         $this->assertArrayNotHasKey('image', $tags);
51
52         // Test image set if image has cover image
53         $bookRepo = app(BookRepo::class);
54         $bookRepo->updateCoverImage($book, $this->getTestImage('image.png'));
55         $resp = $this->asEditor()->get($book->getUrl());
56         $tags = $this->getOpenGraphTags($resp);
57
58         $this->assertEquals($book->getBookCover(), $tags['image']);
59     }
60
61     public function test_shelf_tags()
62     {
63         $shelf = Bookshelf::query()->first();
64         $resp = $this->asEditor()->get($shelf->getUrl());
65         $tags = $this->getOpenGraphTags($resp);
66
67         $this->assertEquals($shelf->getShortName() . ' | BookStack', $tags['title']);
68         $this->assertEquals($shelf->getUrl(), $tags['url']);
69         $this->assertEquals(Str::limit($shelf->description, 100, '...'), $tags['description']);
70         $this->assertArrayNotHasKey('image', $tags);
71
72         // Test image set if image has cover image
73         $baseRepo = app(BaseRepo::class);
74         $baseRepo->updateCoverImage($shelf, $this->getTestImage('image.png'));
75         $resp = $this->asEditor()->get($shelf->getUrl());
76         $tags = $this->getOpenGraphTags($resp);
77
78         $this->assertEquals($shelf->getBookCover(), $tags['image']);
79     }
80
81     /**
82      * Parse the open graph tags from a test response.
83      */
84     protected function getOpenGraphTags(TestResponse $resp): array
85     {
86         $tags = [];
87
88         libxml_use_internal_errors(true);
89         $doc = new \DOMDocument();
90         $doc->loadHTML($resp->getContent());
91         $metaElems = $doc->getElementsByTagName('meta');
92         /** @var \DOMElement $elem */
93         foreach ($metaElems as $elem) {
94             $prop = $elem->getAttribute('property');
95             $name = explode(':', $prop)[1] ?? null;
96             if ($name) {
97                 $tags[$name] = $elem->getAttribute('content');
98             }
99         }
100
101         return $tags;
102     }
103 }