2020-04-03 20:16:05 -04:00
|
|
|
<?php namespace Tests\Entity;
|
2016-02-21 07:53:58 -05:00
|
|
|
|
2020-02-15 14:09:33 -05:00
|
|
|
use BookStack\Actions\Tag;
|
2020-11-21 19:17:45 -05:00
|
|
|
use BookStack\Entities\Models\Book;
|
|
|
|
use BookStack\Entities\Models\Bookshelf;
|
|
|
|
use BookStack\Entities\Models\Chapter;
|
|
|
|
use BookStack\Entities\Models\Page;
|
2020-04-03 20:16:05 -04:00
|
|
|
use Tests\TestCase;
|
2017-07-27 11:10:58 -04:00
|
|
|
|
2017-04-16 05:47:44 -04:00
|
|
|
class EntitySearchTest extends TestCase
|
2016-02-21 07:53:58 -05:00
|
|
|
{
|
|
|
|
|
|
|
|
public function test_page_search()
|
|
|
|
{
|
2020-02-15 14:09:33 -05:00
|
|
|
$book = Book::all()->first();
|
2016-02-21 07:53:58 -05:00
|
|
|
$page = $book->pages->first();
|
|
|
|
|
2017-04-16 05:47:44 -04:00
|
|
|
$search = $this->asEditor()->get('/search?term=' . urlencode($page->name));
|
|
|
|
$search->assertSee('Search Results');
|
|
|
|
$search->assertSee($page->name);
|
2016-02-21 07:53:58 -05:00
|
|
|
}
|
|
|
|
|
2018-09-23 07:34:30 -04:00
|
|
|
public function test_bookshelf_search()
|
|
|
|
{
|
|
|
|
$shelf = Bookshelf::first();
|
|
|
|
$search = $this->asEditor()->get('/search?term=' . urlencode(mb_substr($shelf->name, 0, 3)) . ' {type:bookshelf}');
|
|
|
|
$search->assertStatus(200);
|
|
|
|
$search->assertSee($shelf->name);
|
|
|
|
}
|
|
|
|
|
2016-02-21 07:53:58 -05:00
|
|
|
public function test_invalid_page_search()
|
|
|
|
{
|
2017-04-16 05:47:44 -04:00
|
|
|
$resp = $this->asEditor()->get('/search?term=' . urlencode('<p>test</p>'));
|
|
|
|
$resp->assertSee('Search Results');
|
|
|
|
$resp->assertStatus(200);
|
|
|
|
$this->get('/search?term=cat+-')->assertStatus(200);
|
2016-02-21 07:53:58 -05:00
|
|
|
}
|
|
|
|
|
2017-04-16 05:47:44 -04:00
|
|
|
public function test_empty_search_shows_search_page()
|
2016-02-21 07:53:58 -05:00
|
|
|
{
|
2017-04-16 05:47:44 -04:00
|
|
|
$res = $this->asEditor()->get('/search');
|
|
|
|
$res->assertStatus(200);
|
2016-02-21 07:53:58 -05:00
|
|
|
}
|
|
|
|
|
2017-04-16 05:47:44 -04:00
|
|
|
public function test_searching_accents_and_small_terms()
|
2016-02-21 07:53:58 -05:00
|
|
|
{
|
2017-10-15 14:24:06 -04:00
|
|
|
$page = $this->newPage(['name' => 'My new test quaffleachits', 'html' => 'some áéííúü¿¡ test content a2 orange dog']);
|
2017-04-16 05:47:44 -04:00
|
|
|
$this->asEditor();
|
2016-02-21 07:53:58 -05:00
|
|
|
|
2017-04-16 05:47:44 -04:00
|
|
|
$accentSearch = $this->get('/search?term=' . urlencode('áéíí'));
|
|
|
|
$accentSearch->assertStatus(200)->assertSee($page->name);
|
2016-02-21 07:53:58 -05:00
|
|
|
|
2017-10-15 14:24:06 -04:00
|
|
|
$smallSearch = $this->get('/search?term=' . urlencode('a2'));
|
2017-04-16 05:47:44 -04:00
|
|
|
$smallSearch->assertStatus(200)->assertSee($page->name);
|
2016-02-21 07:53:58 -05:00
|
|
|
}
|
|
|
|
|
2017-04-16 05:47:44 -04:00
|
|
|
public function test_book_search()
|
2016-02-21 07:53:58 -05:00
|
|
|
{
|
2020-02-15 14:09:33 -05:00
|
|
|
$book = Book::first();
|
2017-04-16 05:47:44 -04:00
|
|
|
$page = $book->pages->last();
|
|
|
|
$chapter = $book->chapters->last();
|
2016-02-21 07:53:58 -05:00
|
|
|
|
2017-04-16 05:47:44 -04:00
|
|
|
$pageTestResp = $this->asEditor()->get('/search/book/' . $book->id . '?term=' . urlencode($page->name));
|
|
|
|
$pageTestResp->assertSee($page->name);
|
2016-02-21 07:53:58 -05:00
|
|
|
|
2017-04-16 05:47:44 -04:00
|
|
|
$chapterTestResp = $this->asEditor()->get('/search/book/' . $book->id . '?term=' . urlencode($chapter->name));
|
|
|
|
$chapterTestResp->assertSee($chapter->name);
|
2016-08-25 12:17:26 -04:00
|
|
|
}
|
|
|
|
|
2017-04-16 05:47:44 -04:00
|
|
|
public function test_chapter_search()
|
2016-02-21 07:53:58 -05:00
|
|
|
{
|
2020-02-15 14:09:33 -05:00
|
|
|
$chapter = Chapter::has('pages')->first();
|
2017-04-16 05:47:44 -04:00
|
|
|
$page = $chapter->pages[0];
|
2016-06-12 07:48:06 -04:00
|
|
|
|
2017-04-16 05:47:44 -04:00
|
|
|
$pageTestResp = $this->asEditor()->get('/search/chapter/' . $chapter->id . '?term=' . urlencode($page->name));
|
|
|
|
$pageTestResp->assertSee($page->name);
|
2016-10-30 08:15:11 -04:00
|
|
|
}
|
|
|
|
|
2016-11-12 08:21:16 -05:00
|
|
|
public function test_tag_search()
|
|
|
|
{
|
|
|
|
$newTags = [
|
2020-02-15 14:09:33 -05:00
|
|
|
new Tag([
|
2016-11-12 08:21:16 -05:00
|
|
|
'name' => 'animal',
|
|
|
|
'value' => 'cat'
|
|
|
|
]),
|
2020-02-15 14:09:33 -05:00
|
|
|
new Tag([
|
2016-11-12 08:21:16 -05:00
|
|
|
'name' => 'color',
|
|
|
|
'value' => 'red'
|
|
|
|
])
|
|
|
|
];
|
|
|
|
|
2017-07-27 11:10:58 -04:00
|
|
|
$pageA = Page::first();
|
2016-11-12 08:21:16 -05:00
|
|
|
$pageA->tags()->saveMany($newTags);
|
|
|
|
|
2017-07-27 11:10:58 -04:00
|
|
|
$pageB = Page::all()->last();
|
2016-11-12 08:21:16 -05:00
|
|
|
$pageB->tags()->create(['name' => 'animal', 'value' => 'dog']);
|
|
|
|
|
2017-04-16 05:47:44 -04:00
|
|
|
$this->asEditor();
|
|
|
|
$tNameSearch = $this->get('/search?term=%5Banimal%5D');
|
|
|
|
$tNameSearch->assertSee($pageA->name)->assertSee($pageB->name);
|
2016-11-12 08:21:16 -05:00
|
|
|
|
2017-04-16 05:47:44 -04:00
|
|
|
$tNameSearch2 = $this->get('/search?term=%5Bcolor%5D');
|
|
|
|
$tNameSearch2->assertSee($pageA->name)->assertDontSee($pageB->name);
|
|
|
|
|
|
|
|
$tNameValSearch = $this->get('/search?term=%5Banimal%3Dcat%5D');
|
|
|
|
$tNameValSearch->assertSee($pageA->name)->assertDontSee($pageB->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_exact_searches()
|
|
|
|
{
|
|
|
|
$page = $this->newPage(['name' => 'My new test page', 'html' => 'this is a story about an orange donkey']);
|
2016-11-12 08:21:16 -05:00
|
|
|
|
2017-04-16 05:47:44 -04:00
|
|
|
$exactSearchA = $this->asEditor()->get('/search?term=' . urlencode('"story about an orange"'));
|
|
|
|
$exactSearchA->assertStatus(200)->assertSee($page->name);
|
2016-11-12 08:21:16 -05:00
|
|
|
|
2017-04-16 05:47:44 -04:00
|
|
|
$exactSearchB = $this->asEditor()->get('/search?term=' . urlencode('"story not about an orange"'));
|
|
|
|
$exactSearchB->assertStatus(200)->assertDontSee($page->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_search_filters()
|
|
|
|
{
|
|
|
|
$page = $this->newPage(['name' => 'My new test quaffleachits', 'html' => 'this is about an orange donkey danzorbhsing']);
|
|
|
|
$this->asEditor();
|
|
|
|
$editorId = $this->getEditor()->id;
|
2021-03-10 18:04:18 -05:00
|
|
|
$editorSlug = $this->getEditor()->slug;
|
2017-04-16 05:47:44 -04:00
|
|
|
|
|
|
|
// Viewed filter searches
|
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {not_viewed_by_me}'))->assertSee($page->name);
|
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {viewed_by_me}'))->assertDontSee($page->name);
|
|
|
|
$this->get($page->getUrl());
|
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {not_viewed_by_me}'))->assertDontSee($page->name);
|
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {viewed_by_me}'))->assertSee($page->name);
|
|
|
|
|
|
|
|
// User filters
|
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {created_by:me}'))->assertDontSee($page->name);
|
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {updated_by:me}'))->assertDontSee($page->name);
|
2021-03-10 18:04:18 -05:00
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {updated_by: '.$editorSlug.'}'))->assertDontSee($page->name);
|
2017-04-16 05:47:44 -04:00
|
|
|
$page->created_by = $editorId;
|
|
|
|
$page->save();
|
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {created_by:me}'))->assertSee($page->name);
|
2021-03-10 18:04:18 -05:00
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {created_by: '.$editorSlug.'}'))->assertSee($page->name);
|
2017-04-16 05:47:44 -04:00
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {updated_by:me}'))->assertDontSee($page->name);
|
|
|
|
$page->updated_by = $editorId;
|
|
|
|
$page->save();
|
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {updated_by:me}'))->assertSee($page->name);
|
2021-03-10 18:04:18 -05:00
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {updated_by: '.$editorSlug.'}'))->assertSee($page->name);
|
2017-04-16 05:47:44 -04:00
|
|
|
|
|
|
|
// Content filters
|
|
|
|
$this->get('/search?term=' . urlencode('{in_name:danzorbhsing}'))->assertDontSee($page->name);
|
|
|
|
$this->get('/search?term=' . urlencode('{in_body:danzorbhsing}'))->assertSee($page->name);
|
|
|
|
$this->get('/search?term=' . urlencode('{in_name:test quaffleachits}'))->assertSee($page->name);
|
|
|
|
$this->get('/search?term=' . urlencode('{in_body:test quaffleachits}'))->assertDontSee($page->name);
|
|
|
|
|
|
|
|
// Restricted filter
|
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {is_restricted}'))->assertDontSee($page->name);
|
|
|
|
$page->restricted = true;
|
|
|
|
$page->save();
|
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {is_restricted}'))->assertSee($page->name);
|
|
|
|
|
|
|
|
// Date filters
|
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {updated_after:2037-01-01}'))->assertDontSee($page->name);
|
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {updated_before:2037-01-01}'))->assertSee($page->name);
|
|
|
|
$page->updated_at = '2037-02-01';
|
|
|
|
$page->save();
|
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {updated_after:2037-01-01}'))->assertSee($page->name);
|
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {updated_before:2037-01-01}'))->assertDontSee($page->name);
|
|
|
|
|
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {created_after:2037-01-01}'))->assertDontSee($page->name);
|
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {created_before:2037-01-01}'))->assertSee($page->name);
|
|
|
|
$page->created_at = '2037-02-01';
|
|
|
|
$page->save();
|
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {created_after:2037-01-01}'))->assertSee($page->name);
|
|
|
|
$this->get('/search?term=' . urlencode('danzorbhsing {created_before:2037-01-01}'))->assertDontSee($page->name);
|
2016-11-12 08:21:16 -05:00
|
|
|
}
|
|
|
|
|
2016-06-12 07:48:06 -04:00
|
|
|
public function test_ajax_entity_search()
|
|
|
|
{
|
2018-09-23 10:15:44 -04:00
|
|
|
$page = $this->newPage(['name' => 'my ajax search test', 'html' => 'ajax test']);
|
2017-07-27 11:10:58 -04:00
|
|
|
$notVisitedPage = Page::first();
|
2017-04-16 05:47:44 -04:00
|
|
|
|
|
|
|
// Visit the page to make popular
|
|
|
|
$this->asEditor()->get($page->getUrl());
|
|
|
|
|
|
|
|
$normalSearch = $this->get('/ajax/search/entities?term=' . urlencode($page->name));
|
|
|
|
$normalSearch->assertSee($page->name);
|
|
|
|
|
|
|
|
$bookSearch = $this->get('/ajax/search/entities?types=book&term=' . urlencode($page->name));
|
|
|
|
$bookSearch->assertDontSee($page->name);
|
|
|
|
|
|
|
|
$defaultListTest = $this->get('/ajax/search/entities');
|
|
|
|
$defaultListTest->assertSee($page->name);
|
|
|
|
$defaultListTest->assertDontSee($notVisitedPage->name);
|
2016-06-12 07:48:06 -04:00
|
|
|
}
|
2017-07-27 11:10:58 -04:00
|
|
|
|
|
|
|
public function test_ajax_entity_serach_shows_breadcrumbs()
|
|
|
|
{
|
|
|
|
$chapter = Chapter::first();
|
|
|
|
$page = $chapter->pages->first();
|
|
|
|
$this->asEditor();
|
|
|
|
|
|
|
|
$pageSearch = $this->get('/ajax/search/entities?term=' . urlencode($page->name));
|
|
|
|
$pageSearch->assertSee($page->name);
|
2019-04-06 13:36:17 -04:00
|
|
|
$pageSearch->assertSee($chapter->getShortName(42));
|
|
|
|
$pageSearch->assertSee($page->book->getShortName(42));
|
2017-07-27 11:10:58 -04:00
|
|
|
|
|
|
|
$chapterSearch = $this->get('/ajax/search/entities?term=' . urlencode($chapter->name));
|
|
|
|
$chapterSearch->assertSee($chapter->name);
|
2019-04-06 13:36:17 -04:00
|
|
|
$chapterSearch->assertSee($chapter->book->getShortName(42));
|
2017-07-27 11:10:58 -04:00
|
|
|
}
|
2020-02-15 14:09:33 -05:00
|
|
|
|
|
|
|
public function test_sibling_search_for_pages()
|
|
|
|
{
|
|
|
|
$chapter = Chapter::query()->with('pages')->first();
|
|
|
|
$this->assertGreaterThan(2, count($chapter->pages), 'Ensure we\'re testing with at least 1 sibling');
|
|
|
|
$page = $chapter->pages->first();
|
|
|
|
|
|
|
|
$search = $this->actingAs($this->getViewer())->get("/search/entity/siblings?entity_id={$page->id}&entity_type=page");
|
|
|
|
$search->assertSuccessful();
|
|
|
|
foreach ($chapter->pages as $page) {
|
|
|
|
$search->assertSee($page->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
$search->assertDontSee($chapter->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_sibling_search_for_pages_without_chapter()
|
|
|
|
{
|
|
|
|
$page = Page::query()->where('chapter_id', '=', 0)->firstOrFail();
|
|
|
|
$bookChildren = $page->book->getDirectChildren();
|
|
|
|
$this->assertGreaterThan(2, count($bookChildren), 'Ensure we\'re testing with at least 1 sibling');
|
|
|
|
|
|
|
|
$search = $this->actingAs($this->getViewer())->get("/search/entity/siblings?entity_id={$page->id}&entity_type=page");
|
|
|
|
$search->assertSuccessful();
|
|
|
|
foreach ($bookChildren as $child) {
|
|
|
|
$search->assertSee($child->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
$search->assertDontSee($page->book->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_sibling_search_for_chapters()
|
|
|
|
{
|
|
|
|
$chapter = Chapter::query()->firstOrFail();
|
|
|
|
$bookChildren = $chapter->book->getDirectChildren();
|
|
|
|
$this->assertGreaterThan(2, count($bookChildren), 'Ensure we\'re testing with at least 1 sibling');
|
|
|
|
|
|
|
|
$search = $this->actingAs($this->getViewer())->get("/search/entity/siblings?entity_id={$chapter->id}&entity_type=chapter");
|
|
|
|
$search->assertSuccessful();
|
|
|
|
foreach ($bookChildren as $child) {
|
|
|
|
$search->assertSee($child->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
$search->assertDontSee($chapter->book->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_sibling_search_for_books()
|
|
|
|
{
|
|
|
|
$books = Book::query()->take(10)->get();
|
|
|
|
$book = $books->first();
|
|
|
|
$this->assertGreaterThan(2, count($books), 'Ensure we\'re testing with at least 1 sibling');
|
|
|
|
|
|
|
|
$search = $this->actingAs($this->getViewer())->get("/search/entity/siblings?entity_id={$book->id}&entity_type=book");
|
|
|
|
$search->assertSuccessful();
|
|
|
|
foreach ($books as $expectedBook) {
|
|
|
|
$search->assertSee($expectedBook->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_sibling_search_for_shelves()
|
|
|
|
{
|
|
|
|
$shelves = Bookshelf::query()->take(10)->get();
|
|
|
|
$shelf = $shelves->first();
|
|
|
|
$this->assertGreaterThan(2, count($shelves), 'Ensure we\'re testing with at least 1 sibling');
|
|
|
|
|
|
|
|
$search = $this->actingAs($this->getViewer())->get("/search/entity/siblings?entity_id={$shelf->id}&entity_type=bookshelf");
|
|
|
|
$search->assertSuccessful();
|
|
|
|
foreach ($shelves as $expectedShelf) {
|
|
|
|
$search->assertSee($expectedShelf->name);
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 19:46:13 -04:00
|
|
|
|
|
|
|
public function test_search_works_on_updated_page_content()
|
|
|
|
{
|
|
|
|
$page = Page::query()->first();
|
|
|
|
$this->asEditor();
|
|
|
|
|
|
|
|
$update = $this->put($page->getUrl(), [
|
|
|
|
'name' => $page->name,
|
|
|
|
'html' => '<p>dog pandabearmonster spaghetti</p>',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$search = $this->asEditor()->get('/search?term=pandabearmonster');
|
|
|
|
$search->assertStatus(200);
|
|
|
|
$search->assertSeeText($page->name);
|
|
|
|
$search->assertSee($page->getUrl());
|
|
|
|
}
|
2016-02-21 07:53:58 -05:00
|
|
|
}
|