]> BookStack Code Mirror - bookstack/blob - tests/Search/SiblingSearchTest.php
Deps & Tests: Updated PHP deps, fixed test namespaces
[bookstack] / tests / Search / SiblingSearchTest.php
1 <?php
2
3 namespace Tests\Search;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Bookshelf;
7 use Tests\TestCase;
8
9 class SiblingSearchTest extends TestCase
10 {
11     public function test_sibling_search_for_pages()
12     {
13         $chapter = $this->entities->chapterHasPages();
14         $this->assertGreaterThan(2, count($chapter->pages), 'Ensure we\'re testing with at least 1 sibling');
15         $page = $chapter->pages->first();
16
17         $search = $this->actingAs($this->users->viewer())->get("/search/entity/siblings?entity_id={$page->id}&entity_type=page");
18         $search->assertSuccessful();
19         foreach ($chapter->pages as $page) {
20             $search->assertSee($page->name);
21         }
22
23         $search->assertDontSee($chapter->name);
24     }
25
26     public function test_sibling_search_for_pages_without_chapter()
27     {
28         $page = $this->entities->pageNotWithinChapter();
29         $bookChildren = $page->book->getDirectVisibleChildren();
30         $this->assertGreaterThan(2, count($bookChildren), 'Ensure we\'re testing with at least 1 sibling');
31
32         $search = $this->actingAs($this->users->viewer())->get("/search/entity/siblings?entity_id={$page->id}&entity_type=page");
33         $search->assertSuccessful();
34         foreach ($bookChildren as $child) {
35             $search->assertSee($child->name);
36         }
37
38         $search->assertDontSee($page->book->name);
39     }
40
41     public function test_sibling_search_for_chapters()
42     {
43         $chapter = $this->entities->chapter();
44         $bookChildren = $chapter->book->getDirectVisibleChildren();
45         $this->assertGreaterThan(2, count($bookChildren), 'Ensure we\'re testing with at least 1 sibling');
46
47         $search = $this->actingAs($this->users->viewer())->get("/search/entity/siblings?entity_id={$chapter->id}&entity_type=chapter");
48         $search->assertSuccessful();
49         foreach ($bookChildren as $child) {
50             $search->assertSee($child->name);
51         }
52
53         $search->assertDontSee($chapter->book->name);
54     }
55
56     public function test_sibling_search_for_books()
57     {
58         $books = Book::query()->take(10)->get();
59         $book = $books->first();
60         $this->assertGreaterThan(2, count($books), 'Ensure we\'re testing with at least 1 sibling');
61
62         $search = $this->actingAs($this->users->viewer())->get("/search/entity/siblings?entity_id={$book->id}&entity_type=book");
63         $search->assertSuccessful();
64         foreach ($books as $expectedBook) {
65             $search->assertSee($expectedBook->name);
66         }
67     }
68
69     public function test_sibling_search_for_shelves()
70     {
71         $shelves = Bookshelf::query()->take(10)->get();
72         $shelf = $shelves->first();
73         $this->assertGreaterThan(2, count($shelves), 'Ensure we\'re testing with at least 1 sibling');
74
75         $search = $this->actingAs($this->users->viewer())->get("/search/entity/siblings?entity_id={$shelf->id}&entity_type=bookshelf");
76         $search->assertSuccessful();
77         foreach ($shelves as $expectedShelf) {
78             $search->assertSee($expectedShelf->name);
79         }
80     }
81
82     public function test_sibling_search_for_books_provides_results_in_alphabetical_order()
83     {
84         $contextBook = $this->entities->book();
85         $searchBook = $this->entities->book();
86
87         $searchBook->name = 'Zebras';
88         $searchBook->save();
89
90         $search = $this->actingAs($this->users->viewer())->get("/search/entity/siblings?entity_id={$contextBook->id}&entity_type=book");
91         $this->withHtml($search)->assertElementNotContains('a:first-child', 'Zebras');
92
93         $searchBook->name = '1AAAAAAArdvarks';
94         $searchBook->save();
95
96         $search = $this->actingAs($this->users->viewer())->get("/search/entity/siblings?entity_id={$contextBook->id}&entity_type=book");
97         $this->withHtml($search)->assertElementContains('a:first-child', '1AAAAAAArdvarks');
98     }
99
100     public function test_sibling_search_for_shelves_provides_results_in_alphabetical_order()
101     {
102         $contextShelf = $this->entities->shelf();
103         $searchShelf = $this->entities->shelf();
104
105         $searchShelf->name = 'Zebras';
106         $searchShelf->save();
107
108         $search = $this->actingAs($this->users->viewer())->get("/search/entity/siblings?entity_id={$contextShelf->id}&entity_type=bookshelf");
109         $this->withHtml($search)->assertElementNotContains('a:first-child', 'Zebras');
110
111         $searchShelf->name = '1AAAAAAArdvarks';
112         $searchShelf->save();
113
114         $search = $this->actingAs($this->users->viewer())->get("/search/entity/siblings?entity_id={$contextShelf->id}&entity_type=bookshelf");
115         $this->withHtml($search)->assertElementContains('a:first-child', '1AAAAAAArdvarks');
116     }
117 }