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