]> BookStack Code Mirror - bookstack/blob - tests/References/CrossLinkParserTest.php
Fixed phpstan wanring about usage of static
[bookstack] / tests / References / CrossLinkParserTest.php
1 <?php
2
3 namespace Tests\References;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Page;
7 use BookStack\References\CrossLinkParser;
8 use Tests\TestCase;
9
10 class CrossLinkParserTest extends TestCase
11 {
12
13     public function test_instance_with_entity_resolvers_matches_entity_links()
14     {
15         $entities = $this->getEachEntityType();
16         $otherPage = Page::query()->where('id', '!=', $entities['page']->id)->first();
17
18         $html = '
19 <a href="' . url('/link/' . $otherPage->id) . '#cat">Page Permalink</a>
20 <a href="' . $entities['page'] ->getUrl(). '?a=b">Page Link</a>
21 <a href="' . $entities['chapter']->getUrl() . '?cat=mouse#donkey">Chapter Link</a>
22 <a href="' . $entities['book']->getUrl() . '/edit">Book Link</a>
23 <a href="' . $entities['bookshelf']->getUrl() . '/edit?cat=happy#hello">Shelf Link</a>
24 <a href="' . url('/settings') . '">Settings Link</a>
25         ';
26
27         $parser = CrossLinkParser::createWithEntityResolvers();
28         $results = $parser->extractLinkedModels($html);
29
30         $this->assertCount(5, $results);
31         $this->assertEquals(get_class($otherPage), get_class($results[0]));
32         $this->assertEquals($otherPage->id, $results[0]->id);
33         $this->assertEquals(get_class($entities['page']), get_class($results[1]));
34         $this->assertEquals($entities['page']->id, $results[1]->id);
35         $this->assertEquals(get_class($entities['chapter']), get_class($results[2]));
36         $this->assertEquals($entities['chapter']->id, $results[2]->id);
37         $this->assertEquals(get_class($entities['book']), get_class($results[3]));
38         $this->assertEquals($entities['book']->id, $results[3]->id);
39         $this->assertEquals(get_class($entities['bookshelf']), get_class($results[4]));
40         $this->assertEquals($entities['bookshelf']->id, $results[4]->id);
41     }
42
43     public function test_similar_page_and_book_reference_links_dont_conflict()
44     {
45         $page = Page::query()->first();
46         $book = $page->book;
47
48         $html = '
49 <a href="' . $page->getUrl() . '">Page Link</a>
50 <a href="' . $book->getUrl() . '">Book Link</a>
51         ';
52
53         $parser = CrossLinkParser::createWithEntityResolvers();
54         $results = $parser->extractLinkedModels($html);
55
56         $this->assertCount(2, $results);
57         $this->assertEquals(get_class($page), get_class($results[0]));
58         $this->assertEquals($page->id, $results[0]->id);
59         $this->assertEquals(get_class($book), get_class($results[1]));
60         $this->assertEquals($book->id, $results[1]->id);
61     }
62 }