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