]> BookStack Code Mirror - bookstack/blob - tests/Search/SearchIndexingTest.php
Deps & Tests: Updated PHP deps, fixed test namespaces
[bookstack] / tests / Search / SearchIndexingTest.php
1 <?php
2
3 namespace Tests\Search;
4
5 use Tests\TestCase;
6
7 class SearchIndexingTest extends TestCase
8 {
9     public function test_terms_in_headers_have_an_adjusted_index_score()
10     {
11         $page = $this->entities->newPage(['name' => 'Test page A', 'html' => '
12             <p>TermA</p>
13             <h1>TermB <strong>TermNested</strong></h1>
14             <h2>TermC</h2>
15             <h3>TermD</h3>
16             <h4>TermE</h4>
17             <h5>TermF</h5>
18             <h6>TermG</h6>
19         ']);
20
21         $scoreByTerm = $page->searchTerms()->pluck('score', 'term');
22
23         $this->assertEquals(1, $scoreByTerm->get('TermA'));
24         $this->assertEquals(10, $scoreByTerm->get('TermB'));
25         $this->assertEquals(10, $scoreByTerm->get('TermNested'));
26         $this->assertEquals(5, $scoreByTerm->get('TermC'));
27         $this->assertEquals(4, $scoreByTerm->get('TermD'));
28         $this->assertEquals(3, $scoreByTerm->get('TermE'));
29         $this->assertEquals(2, $scoreByTerm->get('TermF'));
30         // Is 1.5 but stored as integer, rounding up
31         $this->assertEquals(2, $scoreByTerm->get('TermG'));
32     }
33
34     public function test_indexing_works_as_expected_for_page_with_lots_of_terms()
35     {
36         $this->markTestSkipped('Time consuming test');
37
38         $count = 100000;
39         $text = '';
40         $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_#';
41         for ($i = 0; $i < $count; $i++) {
42             $text .= substr(str_shuffle($chars), 0, 5) . ' ';
43         }
44
45         $page = $this->entities->newPage(['name' => 'Test page A', 'html' => '<p>' . $text . '</p>']);
46
47         $termCount = $page->searchTerms()->count();
48
49         // Expect at least 90% unique rate
50         $this->assertGreaterThan($count * 0.9, $termCount);
51     }
52
53     public function test_name_and_content_terms_are_merged_to_single_score()
54     {
55         $page = $this->entities->newPage(['name' => 'TermA', 'html' => '
56             <p>TermA</p>
57         ']);
58
59         $scoreByTerm = $page->searchTerms()->pluck('score', 'term');
60
61         // Scores 40 for being in the name then 1 for being in the content
62         $this->assertEquals(41, $scoreByTerm->get('TermA'));
63     }
64
65     public function test_tag_names_and_values_are_indexed_for_search()
66     {
67         $page = $this->entities->newPage(['name' => 'PageA', 'html' => '<p>content</p>', 'tags' => [
68             ['name' => 'Animal', 'value' => 'MeowieCat'],
69             ['name' => 'SuperImportant'],
70         ]]);
71
72         $scoreByTerm = $page->searchTerms()->pluck('score', 'term');
73         $this->assertEquals(5, $scoreByTerm->get('MeowieCat'));
74         $this->assertEquals(3, $scoreByTerm->get('Animal'));
75         $this->assertEquals(3, $scoreByTerm->get('SuperImportant'));
76     }
77
78     public function test_terms_containing_guillemets_handled()
79     {
80         $page = $this->entities->newPage(['html' => '<p>«Hello there» and « there »</p>']);
81
82         $scoreByTerm = $page->searchTerms()->pluck('score', 'term');
83         $expected = ['Hello', 'there', 'and'];
84         foreach ($expected as $term) {
85             $this->assertNotNull($scoreByTerm->get($term), "Failed asserting that \"$term\" is indexed");
86         }
87
88         $nonExpected = ['«', '»'];
89         foreach ($nonExpected as $term) {
90             $this->assertNull($scoreByTerm->get($term), "Failed asserting that \"$term\" is not indexed");
91         }
92     }
93
94     public function test_terms_containing_punctuation_within_retain_original_form_and_split_form_in_index()
95     {
96         $page = $this->entities->newPage(['html' => '<p>super.duper awesome-beans big- barry cheese.</p><p>biscuits</p><p>a-bs</p>']);
97
98         $scoreByTerm = $page->searchTerms()->pluck('score', 'term');
99         $expected = ['super', 'duper', 'super.duper', 'awesome-beans', 'awesome', 'beans', 'big', 'barry', 'cheese', 'biscuits', 'a-bs', 'a', 'bs'];
100         foreach ($expected as $term) {
101             $this->assertNotNull($scoreByTerm->get($term), "Failed asserting that \"$term\" is indexed");
102         }
103
104         $nonExpected = ['big-', 'big-barry', 'cheese.', 'cheese.biscuits'];
105         foreach ($nonExpected as $term) {
106             $this->assertNull($scoreByTerm->get($term), "Failed asserting that \"$term\" is not indexed");
107         }
108     }
109 }