]> BookStack Code Mirror - bookstack/blob - tests/OpenGraphTest.php
Extracted test file handling to its own class
[bookstack] / tests / OpenGraphTest.php
1 <?php
2
3 namespace Tests;
4
5 use BookStack\Entities\Repos\BaseRepo;
6 use BookStack\Entities\Repos\BookRepo;
7 use Illuminate\Support\Str;
8 use Illuminate\Testing\TestResponse;
9
10 class OpenGraphTest extends TestCase
11 {
12     public function test_page_tags()
13     {
14         $page = $this->entities->page();
15         $resp = $this->asEditor()->get($page->getUrl());
16         $tags = $this->getOpenGraphTags($resp);
17
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']);
21     }
22
23     public function test_chapter_tags()
24     {
25         $chapter = $this->entities->chapter();
26         $resp = $this->asEditor()->get($chapter->getUrl());
27         $tags = $this->getOpenGraphTags($resp);
28
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']);
32     }
33
34     public function test_book_tags()
35     {
36         $book = $this->entities->book();
37         $resp = $this->asEditor()->get($book->getUrl());
38         $tags = $this->getOpenGraphTags($resp);
39
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);
44
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);
50
51         $this->assertEquals($book->getBookCover(), $tags['image']);
52     }
53
54     public function test_shelf_tags()
55     {
56         $shelf = $this->entities->shelf();
57         $resp = $this->asEditor()->get($shelf->getUrl());
58         $tags = $this->getOpenGraphTags($resp);
59
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);
64
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);
70
71         $this->assertEquals($shelf->getBookCover(), $tags['image']);
72     }
73
74     /**
75      * Parse the open graph tags from a test response.
76      */
77     protected function getOpenGraphTags(TestResponse $resp): array
78     {
79         $tags = [];
80
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;
89             if ($name) {
90                 $tags[$name] = $elem->getAttribute('content');
91             }
92         }
93
94         return $tags;
95     }
96 }