|
32 | 32 | require_once($CFG->dirroot . '/search/engine/elastic/tests/fixtures/mock_search_area.php'); |
33 | 33 | require_once($CFG->dirroot . '/search/engine/elastic/tests/fixtures/testable_engine.php'); |
34 | 34 |
|
| 35 | +use core_mocksearch\search\mock_boost_area; |
| 36 | +use core_mocksearch\search\mock_search_area; |
35 | 37 | use \GuzzleHttp\Handler\MockHandler; |
36 | 38 | use \GuzzleHttp\HandlerStack; |
37 | 39 | use \GuzzleHttp\Psr7\Response; |
@@ -60,6 +62,21 @@ class engine_test extends \advanced_testcase { |
60 | 62 | */ |
61 | 63 | protected $engine = null; |
62 | 64 |
|
| 65 | + /** |
| 66 | + * @var string the Apache Lucene version of the attached Elasticsearch / OpenSearch service. |
| 67 | + */ |
| 68 | + protected $luceneversion; |
| 69 | + |
| 70 | + /** |
| 71 | + * @var mock_search_area |
| 72 | + */ |
| 73 | + protected $area; |
| 74 | + |
| 75 | + /** |
| 76 | + * @var mock_boost_area |
| 77 | + */ |
| 78 | + protected $areaboost; |
| 79 | + |
63 | 80 | public function setUp(): void { |
64 | 81 | $this->resetAfterTest(); |
65 | 82 | set_config('enableglobalsearch', true); |
@@ -1015,6 +1032,8 @@ public function test_search_wildcardstart_enabled() { |
1015 | 1032 | * Test the wildcard search functionality when wildcardend is enabled. |
1016 | 1033 | */ |
1017 | 1034 | public function test_search_wildcardend_enabled() { |
| 1035 | + $searchuser = $this->getDataGenerator()->create_user(); |
| 1036 | + $this->setUser($searchuser); |
1018 | 1037 | set_config('wildcardend', 1, 'search_elastic'); |
1019 | 1038 |
|
1020 | 1039 | // Construct the search object and add it to the engine. |
@@ -1088,4 +1107,45 @@ public function test_broken_index() { |
1088 | 1107 | $this->assertFalse($result); |
1089 | 1108 | } |
1090 | 1109 |
|
| 1110 | + /** Test docoffset is incremented correctly for multiple pages of search results. */ |
| 1111 | + public function test_execute_query_docoffset() { |
| 1112 | + $searchuser = $this->getDataGenerator()->create_user(); |
| 1113 | + $this->setUser($searchuser); |
| 1114 | + // Generate 2000 indexed documents. |
| 1115 | + for ($i = 1, $j = 2000; $i <= $j; $i++) { |
| 1116 | + $rec = (object)['content' => 'test message ' . $i]; |
| 1117 | + // Deny user access to all but 100 documents, spaced evenly throughout the index. |
| 1118 | + // The engine returns 1000 results at a time, so there should be 50 visible in the first set, and 50 in the second. |
| 1119 | + if ($i % 20 != 0) { |
| 1120 | + $rec->denyuserids = [$searchuser->id]; |
| 1121 | + } |
| 1122 | + $area = $this->area; |
| 1123 | + $record = $this->generator->create_record($rec); |
| 1124 | + $doc = $area->get_document($record); |
| 1125 | + $this->engine->add_document($doc, false, $this->luceneversion); |
| 1126 | + } |
| 1127 | + // We need to wait for Elastic search to update its index |
| 1128 | + // this happens in near realtime, not immediately. |
| 1129 | + sleep(1); |
| 1130 | + |
| 1131 | + $querydata = (object) [ |
| 1132 | + 'q' => '*', |
| 1133 | + 'timestart' => 0, |
| 1134 | + 'timeend' => 0, |
| 1135 | + ]; |
| 1136 | + |
| 1137 | + // Execute the search. |
| 1138 | + $results = $this->search->search($querydata); |
| 1139 | + |
| 1140 | + $this->assertCount(100, $results); |
| 1141 | + |
| 1142 | + // Confirm that each result is unique. This proves that we are not re-querying the same documents multiple times. |
| 1143 | + $seenresults = []; |
| 1144 | + foreach ($results as $result) { |
| 1145 | + $content = $result->get('content'); |
| 1146 | + $this->assertFalse(in_array($content, $seenresults), "'{$content}' included multiple times in search results."); |
| 1147 | + $seenresults[] = $content; |
| 1148 | + } |
| 1149 | + } |
| 1150 | + |
1091 | 1151 | } |
0 commit comments