]> BookStack Code Mirror - bookstack/blob - tests/Entity/EntitySearchTest.php
Fixed tag searches and added tag search regression test
[bookstack] / tests / Entity / EntitySearchTest.php
1 <?php
2
3 use Illuminate\Support\Facades\DB;
4
5 class EntitySearchTest extends TestCase
6 {
7
8     public function test_page_search()
9     {
10         $book = \BookStack\Book::all()->first();
11         $page = $book->pages->first();
12
13         $this->asAdmin()
14             ->visit('/')
15             ->type($page->name, 'term')
16             ->press('header-search-box-button')
17             ->see('Search Results')
18             ->see($page->name)
19             ->click($page->name)
20             ->seePageIs($page->getUrl());
21     }
22
23     public function test_invalid_page_search()
24     {
25         $this->asAdmin()
26             ->visit('/')
27             ->type('<p>test</p>', 'term')
28             ->press('header-search-box-button')
29             ->see('Search Results')
30             ->seeStatusCode(200);
31     }
32
33     public function test_empty_search_redirects_back()
34     {
35         $this->asAdmin()
36             ->visit('/')
37             ->visit('/search/all')
38             ->seePageIs('/');
39     }
40
41     public function test_book_search()
42     {
43         $book = \BookStack\Book::all()->first();
44         $page = $book->pages->last();
45         $chapter = $book->chapters->last();
46
47         $this->asAdmin()
48             ->visit('/search/book/' . $book->id . '?term=' . urlencode($page->name))
49             ->see($page->name)
50
51             ->visit('/search/book/' . $book->id  . '?term=' . urlencode($chapter->name))
52             ->see($chapter->name);
53     }
54
55     public function test_empty_book_search_redirects_back()
56     {
57         $book = \BookStack\Book::all()->first();
58         $this->asAdmin()
59             ->visit('/books')
60             ->visit('/search/book/' . $book->id . '?term=')
61             ->seePageIs('/books');
62     }
63
64
65     public function test_pages_search_listing()
66     {
67         $page = \BookStack\Page::all()->last();
68         $this->asAdmin()->visit('/search/pages?term=' . $page->name)
69             ->see('Page Search Results')->see('.entity-list', $page->name);
70     }
71
72     public function test_chapters_search_listing()
73     {
74         $chapter = \BookStack\Chapter::all()->last();
75         $this->asAdmin()->visit('/search/chapters?term=' . $chapter->name)
76             ->see('Chapter Search Results')->seeInElement('.entity-list', $chapter->name);
77     }
78
79     public function test_search_quote_term_preparation()
80     {
81         $termString = '"192" cat "dog hat"';
82         $repo = $this->app[\BookStack\Repos\EntityRepo::class];
83         $preparedTerms = $repo->prepareSearchTerms($termString);
84         $this->assertTrue($preparedTerms === ['"192"','"dog hat"', 'cat']);
85     }
86
87     public function test_books_search_listing()
88     {
89         $book = \BookStack\Book::all()->last();
90         $this->asAdmin()->visit('/search/books?term=' . $book->name)
91             ->see('Book Search Results')->see('.entity-list', $book->name);
92     }
93
94     public function test_searching_hypen_doesnt_break()
95     {
96         $this->visit('/search/all?term=cat+-')
97             ->seeStatusCode(200);
98     }
99
100     public function test_tag_search()
101     {
102         $newTags = [
103             new \BookStack\Tag([
104                 'name' => 'animal',
105                 'value' => 'cat'
106             ]),
107             new \BookStack\Tag([
108                 'name' => 'color',
109                 'value' => 'red'
110             ])
111         ];
112
113         $pageA = \BookStack\Page::first();
114         $pageA->tags()->saveMany($newTags);
115
116         $pageB = \BookStack\Page::all()->last();
117         $pageB->tags()->create(['name' => 'animal', 'value' => 'dog']);
118
119         $this->asAdmin()->visit('/search/all?term=%5Banimal%5D')
120             ->seeLink($pageA->name)
121             ->seeLink($pageB->name);
122
123         $this->visit('/search/all?term=%5Bcolor%5D')
124             ->seeLink($pageA->name)
125             ->dontSeeLink($pageB->name);
126
127         $this->visit('/search/all?term=%5Banimal%3Dcat%5D')
128             ->seeLink($pageA->name)
129             ->dontSeeLink($pageB->name);
130
131     }
132
133     public function test_ajax_entity_search()
134     {
135         $page = \BookStack\Page::all()->last();
136         $notVisitedPage = \BookStack\Page::first();
137         $this->visit($page->getUrl());
138         $this->asAdmin()->visit('/ajax/search/entities?term=' . $page->name)->see('.entity-list', $page->name);
139         $this->asAdmin()->visit('/ajax/search/entities?types=book&term=' . $page->name)->dontSee('.entity-list', $page->name);
140         $this->asAdmin()->visit('/ajax/search/entities')->see('.entity-list', $page->name)->dontSee($notVisitedPage->name);
141     }
142 }