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