/** * Adds a test document to the index and to the in-memory store. * * @param int $index Document index * @param stdClass $course Course object * @param string $content Text content * @param int $cmid If specified, cmid of document */ private static function add_test_document($index, $course, $content, $cmid = 0) { $doc = new local_ousearch_document(); $doc->courseid = $course->id; $doc->plugin = 'test_yeartablestest'; $doc->timemodified = $index; $doc->set_int_refs($index); if ($cmid) { $doc->coursemoduleid = $cmid; } $doc->update('Title', $content); self::$testdocuments[$index] = array('title' => 'Title', 'content' => $content); }
/** * Obtains a search document relating to a particular blog post. * * @param object $post Post object. Required fields: id (optionally also * groupid, userid save a db query) * @param object $cm Course-module object. Required fields: id, course * @return ousearch_doument */ function oublog_get_search_document($post, $cm) { global $DB; // Set up 'search document' to refer to this post $doc = new local_ousearch_document(); $doc->init_module_instance('oublog', $cm); if (!isset($post->userid) || !isset($post->groupid)) { $results = $DB->get_record_sql("\nSELECT\n p.groupid,i.userid\nFROM\n{oublog_posts} p\n INNER JOIN {oublog_instances} i ON p.oubloginstancesid=i.id\nWHERE\n p.id= ?", array($post->id)); if (!$results) { print_error('invalidblogdetails', 'oublog'); } $post->userid = $results->userid; $post->groupid = $results->groupid; } if ($post->groupid) { $doc->set_group_id($post->groupid); } $doc->set_user_id($post->userid); $doc->set_int_refs($post->id); return $doc; }
public function test_ousearch_query() { global $DB; $this->resetAfterTest(); // Create a bunch of search documents within test_zombie plugin. self::$zombiedocuments = array(1 => (object) array('title' => 'Document title', 'content' => 'First zombie document'), 2 => (object) array('title' => 'Another title', 'content' => 'Title title first'), 3 => (object) array('title' => 'Document title', 'content' => 'Not a zombie document title'), 4 => (object) array('title' => 'Delete me', 'content' => 'Delete me'), 100 => (object) array('title' => 'Bottle quantity', 'content' => 'There are this many bottles on the wall: 0')); for ($i = 101; $i <= 199; $i++) { self::$zombiedocuments[$i] = self::$zombiedocuments[100]; self::$zombiedocuments[$i]->content = str_replace(': 0', ': ' . ($i - 100), self::$zombiedocuments[$i]->content); } foreach (self::$zombiedocuments as $key => $content) { $doc = new local_ousearch_document(); $doc->init_test('zombie'); $doc->set_int_refs($key); $doc->update($content->title, $content->content, null, null, null); } // Search for single unique term. $result = $this->do_zombie_query('not'); $this->assertTrue($result->success); $this->assertEquals(array(3), $this->get_result_ids($result)); // Search for nonexistent word. $result = $this->do_zombie_query('xyzzy'); $this->assertFalse($result->success); $this->assertEquals('xyzzy', $result->problemword); // Search for nothing. $result = $this->do_zombie_query(' '); $this->assertFalse($result->success); $this->assertEquals('', $result->problemword); // Search for pair of terms. $result = $this->do_zombie_query('first document'); $this->assertTrue($result->success); $this->assertEquals(array(1), $this->get_result_ids($result)); // Search for quoted terms. $result = $this->do_zombie_query('"title title"'); $this->assertTrue($result->success); $this->assertEquals(array(2), $this->get_result_ids($result)); // Negative terms. $result = $this->do_zombie_query('title -not'); $this->assertTrue($result->success); $this->assertEquals(array(1, 2), $this->get_result_ids($result)); // Negative quoted terms. $result = $this->do_zombie_query('title -"not frog"'); $this->assertTrue($result->success); $this->assertEquals(array(1, 2, 3), $this->get_result_ids($result)); $result = $this->do_zombie_query('title -"not a"'); $this->assertTrue($result->success); $this->assertEquals(array(1, 2), $this->get_result_ids($result)); // Deleting stale results (those which the module responsible can no // longer find). $before = $DB->count_records('local_ousearch_documents'); unset(self::$zombiedocuments[4]); $result = $this->do_zombie_query('delete'); $this->assertEquals(1, count(phpunit_util::get_debugging_messages())); phpunit_util::reset_debugging(); $this->assertTrue($result->success); $this->assertEquals(array(), $this->get_result_ids($result)); $this->assertEquals($before - 1, $DB->count_records('local_ousearch_documents')); // Ranking based on title vs content and number of occurrences. $result = $this->do_zombie_query('title'); $this->assertTrue($result->success); $this->assertEquals(array(1, 2, 3), $this->get_result_ids($result)); $this->assertEquals(2, $result->results[0]->intref1); $this->assertEquals(18, $result->results[0]->totalscore); // Managing result lists. $found = array(); $dbstart = 0; for ($i = 0; $i < 10; $i++) { $result = $this->do_zombie_query('bottles', $dbstart); $this->assertTrue($result->success); $this->assertEquals(10, count($result->results)); foreach ($result->results as $thing) { $found[$thing->intref1] = true; } $dbstart = $result->dbstart; } $this->assertEquals(100, count($found)); $result = $this->do_zombie_query('bottles', $dbstart); $this->assertTrue($result->success); $this->assertEquals(0, count($result->results)); }
/** * Obtains search document representing this post. * @return local_ousearch_document Document object */ public function search_get_document() { $doc = new local_ousearch_document(); $doc->init_module_instance('forumng', $this->get_forum()->get_course_module(true)); if ($groupid = $this->discussion->get_group_id()) { $doc->set_group_id($groupid); } $doc->set_int_refs($this->get_id()); return $doc; }