+
+ 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());
+ }
+
+ public function test_search_ranks_common_words_lower()
+ {
+ $this->newPage(['name' => 'Test page A', 'html' => '<p>dog biscuit dog dog</p>']);
+ $this->newPage(['name' => 'Test page B', 'html' => '<p>cat biscuit</p>']);
+
+ $search = $this->asEditor()->get('/search?term=cat+dog+biscuit');
+ $this->withHtml($search)->assertElementContains('.entity-list > .page:nth-child(1)', 'Test page A');
+ $this->withHtml($search)->assertElementContains('.entity-list > .page:nth-child(2)', 'Test page B');
+
+ for ($i = 0; $i < 2; $i++) {
+ $this->newPage(['name' => 'Test page ' . $i, 'html' => '<p>dog</p>']);
+ }
+
+ $search = $this->asEditor()->get('/search?term=cat+dog+biscuit');
+ $this->withHtml($search)->assertElementContains('.entity-list > .page:nth-child(1)', 'Test page B');
+ $this->withHtml($search)->assertElementContains('.entity-list > .page:nth-child(2)', 'Test page A');
+ }
+
+ public function test_terms_in_headers_have_an_adjusted_index_score()
+ {
+ $page = $this->newPage(['name' => 'Test page A', 'html' => '
+ <p>TermA</p>
+ <h1>TermB <strong>TermNested</strong></h1>
+ <h2>TermC</h2>
+ <h3>TermD</h3>
+ <h4>TermE</h4>
+ <h5>TermF</h5>
+ <h6>TermG</h6>
+ ']);
+
+ $scoreByTerm = $page->searchTerms()->pluck('score', 'term');
+
+ $this->assertEquals(1, $scoreByTerm->get('TermA'));
+ $this->assertEquals(10, $scoreByTerm->get('TermB'));
+ $this->assertEquals(10, $scoreByTerm->get('TermNested'));
+ $this->assertEquals(5, $scoreByTerm->get('TermC'));
+ $this->assertEquals(4, $scoreByTerm->get('TermD'));
+ $this->assertEquals(3, $scoreByTerm->get('TermE'));
+ $this->assertEquals(2, $scoreByTerm->get('TermF'));
+ // Is 1.5 but stored as integer, rounding up
+ $this->assertEquals(2, $scoreByTerm->get('TermG'));
+ }
+
+ public function test_name_and_content_terms_are_merged_to_single_score()
+ {
+ $page = $this->newPage(['name' => 'TermA', 'html' => '
+ <p>TermA</p>
+ ']);
+
+ $scoreByTerm = $page->searchTerms()->pluck('score', 'term');
+
+ // Scores 40 for being in the name then 1 for being in the content
+ $this->assertEquals(41, $scoreByTerm->get('TermA'));
+ }
+
+ public function test_tag_names_and_values_are_indexed_for_search()
+ {
+ $page = $this->newPage(['name' => 'PageA', 'html' => '<p>content</p>', 'tags' => [
+ ['name' => 'Animal', 'value' => 'MeowieCat'],
+ ['name' => 'SuperImportant'],
+ ]]);
+
+ $scoreByTerm = $page->searchTerms()->pluck('score', 'term');
+ $this->assertEquals(5, $scoreByTerm->get('MeowieCat'));
+ $this->assertEquals(3, $scoreByTerm->get('Animal'));
+ $this->assertEquals(3, $scoreByTerm->get('SuperImportant'));
+ }
+
+ public function test_matching_terms_in_search_results_are_highlighted()
+ {
+ $this->newPage(['name' => 'My Meowie Cat', 'html' => '<p>A superimportant page about meowieable animals</p>', 'tags' => [
+ ['name' => 'Animal', 'value' => 'MeowieCat'],
+ ['name' => 'SuperImportant'],
+ ]]);
+
+ $search = $this->asEditor()->get('/search?term=SuperImportant+Meowie');
+ // Title
+ $search->assertSee('My <strong>Meowie</strong> Cat', false);
+ // Content
+ $search->assertSee('A <strong>superimportant</strong> page about <strong>meowie</strong>able animals', false);
+ // Tag name
+ $this->withHtml($search)->assertElementContains('.tag-name.highlight', 'SuperImportant');
+ // Tag value
+ $this->withHtml($search)->assertElementContains('.tag-value.highlight', 'MeowieCat');
+ }
+
+ public function test_match_highlighting_works_with_multibyte_content()
+ {
+ $this->newPage([
+ 'name' => 'Test Page',
+ 'html' => '<p>На мен ми трябва нещо добро test</p>',
+ ]);
+
+ $search = $this->asEditor()->get('/search?term=' . urlencode('На мен ми трябва нещо добро'));
+ $search->assertSee('<strong>На</strong> <strong>мен</strong> <strong>ми</strong> <strong>трябва</strong> <strong>нещо</strong> <strong>добро</strong> test', false);
+ }
+
+ public function test_html_entities_in_item_details_remains_escaped_in_search_results()
+ {
+ $this->newPage(['name' => 'My <cool> TestPageContent', 'html' => '<p>My supercool <great> TestPageContent page</p>']);
+
+ $search = $this->asEditor()->get('/search?term=TestPageContent');
+ $search->assertSee('My <cool> <strong>TestPageContent</strong>', false);
+ $search->assertSee('My supercool <great> <strong>TestPageContent</strong> page', false);
+ }
+
+ public function test_words_adjacent_to_lines_breaks_can_be_matched_with_normal_terms()
+ {
+ $page = $this->newPage(['name' => 'TermA', 'html' => '
+ <p>TermA<br>TermB<br>TermC</p>
+ ']);
+
+ $search = $this->asEditor()->get('/search?term=' . urlencode('TermB TermC'));
+
+ $search->assertSee($page->getUrl(), false);
+ }
+
+ public function test_searches_with_user_filters_adds_them_into_advanced_search_form()
+ {
+ $resp = $this->asEditor()->get('/search?term=' . urlencode('test {updated_by:me} {created_by:dan}'));
+ $this->withHtml($resp)->assertElementExists('form input[type="hidden"][name="filters[updated_by]"][value="me"]');
+ $this->withHtml($resp)->assertElementExists('form input[type="hidden"][name="filters[created_by]"][value="dan"]');
+ }