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