From 0e0a2ba9e30424860d4a835ac7370bc14cb8de6c Mon Sep 17 00:00:00 2001 From: rtschu Date: Fri, 14 Aug 2020 16:58:04 +0200 Subject: [PATCH 01/10] integrated moodleoverflow into the global search --- classes/search/moodleoverflowposts.php | 173 +++++++++++++++++++++++++ lang/en/moodleoverflow.php | 5 +- 2 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 classes/search/moodleoverflowposts.php diff --git a/classes/search/moodleoverflowposts.php b/classes/search/moodleoverflowposts.php new file mode 100644 index 0000000000..46a9265a9c --- /dev/null +++ b/classes/search/moodleoverflowposts.php @@ -0,0 +1,173 @@ +. + +/** + * Search api for moodleoverflowposts + * Copyright 2020 Robin Tschudi + */ + +namespace mod_moodleoverflow\search; +use core_search\document; +defined('MOODLE_INTERNAL') || die(); +require_once(dirname(__FILE__) . '/../../locallib.php'); +class moodleoverflowposts extends \core_search\base_mod { + + /** + * @var array Internal quick static cache. + */ + protected $postsdata = array(); + protected $moodleoverflows = array(); + protected $discussions = array(); + + protected static $levels = [CONTEXT_COURSE]; + + public function uses_file_indexing() { + return false; + } + + public function get_document($record, $options = array()) { + try { + $cm = $this->get_cm('moodleoverflow', $record->moodleoverflowid, $record->course); + $context = \context_module::instance($cm->id); + } catch (\dml_missing_record_exception $ex) { + debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document, not all required data is available: ' . + $ex->getMessage(), DEBUG_DEVELOPER); + return false; + } catch (\dml_exception $ex) { + debugging('Error retrieving ' . $this->areaid . ' ' . $record->id . ' document: ' . $ex->getMessage(), DEBUG_DEVELOPER); + return false; + } + + $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname); + + $doc->set('title', content_to_text($record->title, true)); + $doc->set('content', content_to_text($record->message, FORMAT_HTML)); + $doc->set('contextid', $context->id); + $doc->set('type', \core_search\manager::TYPE_TEXT); + $doc->set('courseid', $record->course); + $doc->set('modified', $record->modified); + $doc->set('userid', $record->userid); + $doc->set('owneruserid', \core_search\manager::NO_OWNER_ID); + $postdata[$record->id[0]] = array('discussionid' => $record->discussion, 'moodleoverflowid' => $record->moodleoverflowid); + if (isset($options['lastindexedtime']) && ($options['lastindexedtime'] < $record->created)) { + // If the document was created after the last index time, it must be new. + $doc->set_is_new(true); + } + + return $doc; + } + + private function get_discussion_from_id($discussionid) { + global $DB; + if (isset($this->discussions[$discussionid])) { + return $this->discussions[$discussionid]; + } else { + if (!$discussion = $DB->get_record('moodleoverflow_discussions', array('id' => $discussionid))) { + return false; + } + $this->discussions[$discussionid] = $discussion; + return $discussion; + } + } + + private function get_moodleoverflow_from_id($moodleoverflowid) { + global $DB; + if (isset($this->moodleoverflows[$moodleoverflowid])) { + return $this->moodleoverflows[$moodleoverflowid]; + } else { + if (!$moodleoverflow = $DB->get_record('moodleoverflow', array('id' => $moodleoverflowid))) { + return false; + } + $this->moodleoverflows[$moodleoverflowid] = $moodleoverflow; + return $moodleoverflow; + } + } + + public function check_access($id) { + try { + $post = moodleoverflow_get_post_full($id); + if (!$discussion = $this->get_discussion_from_id($post->discussion)) { + return \core_search\manager::ACCESS_DELETED; + } + if (!$moodleoverflow = $this->get_moodleoverflow_from_id($post->moodleoverflow)) { + return \core_search\manager::ACCESS_DELETED; + } + $context = moodleoverflow_get_context($post->moodleoverflow); + } catch (\dml_missing_record_exception $ex) { + return \core_search\manager::ACCESS_DELETED; + } catch (\dml_exception $ex) { + return \core_search\manager::ACCESS_DENIED; + } + if (moodleoverflow_user_can_see_discussion($moodleoverflow, $discussion, $context)) { + return \core_search\manager::ACCESS_GRANTED; + } + return \core_search\manager::ACCESS_DENIED; + } + + public function get_discussionid_for_document(document $doc) { + global $DB; + $postid = $doc->get('itemid'); + if (isset($this->postsdata[$postid]) && isset($this->postsdata[$postid]['discussionid'])) { + return $this->postsdata[$postid]['discussionid']; + } else { + $discussionid = $DB->get_field('moodleoverflow_posts', 'discussion', array('id' => $postid)); + if (!isset($this->postsdata[$postid])) { + $this->postsdata[$postid] = array(); + } + $this->postsdata[$postid]['discussionid'] = $discussionid; + return $discussionid; + } + } + + public function get_moodleoverflowid_for_document(document $doc) { + global $DB; + $discussionid = $this->get_discussionid_for_document($doc); + $postid = $doc->get('itemid'); + if (isset($this->postsdata[$postid]) && isset($this->postsdata[$postid]["moodleoverflowid"])) { + return $this->postsdata[$postid]["moodleoverflowid"]; + } else { + $moodleoverflowid = $DB->get_field('moodleoverflow_discussions', 'moodleoverflow', array('id' => $discussionid)); + if (!isset($this->postsdata[$postid])) { + $this->postsdata[$postid] = array(); + } + $this->postsdata[$postid]['moodleoverflowid'] = $moodleoverflowid; + return $moodleoverflowid; + } + } + + public function get_doc_url(document $doc) { + return new \moodle_url('/mod/moodleoverflow/discussion.php', array('d' => $this->get_discussionid_for_document($doc)), + "p" . $doc->get('itemid')); + } + + public function get_context_url(document $doc) { + return new \moodle_url('/mod/moodleoverflow/view.php', array('m' => $this->get_moodleoverflowid_for_document($doc))); + } + + public function get_document_recordset($modifiedfrom = 0, \context $context = null) { + global $DB; + list($contextjoin, $contextparams) = $this->get_context_restriction_sql($context, 'moodleoverflow', 'discussions'); + if ($contextjoin === null) { + return null; + } + $sql = "SELECT md.name as title, mp.*, mo.course, mo.id as moodleoverflowid FROM {moodleoverflow_posts} mp + JOIN {moodleoverflow_discussions} md ON mp.discussion = md.id + JOIN {moodleoverflow} mo ON md.moodleoverflow = mo.id + $contextjoin + WHERE mp.modified >= ? ORDER BY mp.modified ASC"; + return $DB->get_recordset_sql($sql, array_merge($contextparams, [$modifiedfrom])); + } +} diff --git a/lang/en/moodleoverflow.php b/lang/en/moodleoverflow.php index 8fbeb14d4c..d94a5f73ca 100644 --- a/lang/en/moodleoverflow.php +++ b/lang/en/moodleoverflow.php @@ -395,4 +395,7 @@ $string['updategrades'] = 'Update grades'; $string['gradesreport'] = 'Grades report'; $string['gradesupdated'] = 'Grades updated'; -$string['taskupdategrades'] = 'Moodleoverflow maintenance job to update grades'; \ No newline at end of file +$string['taskupdategrades'] = 'Moodleoverflow maintenance job to update grades'; + +// Search. +$string['search:moodleoverflowposts'] = 'Moodleoverflow Posts'; From 1b5b9b7e123724d67f03ade4fbe3e7917d3b8fc9 Mon Sep 17 00:00:00 2001 From: rtschu <45663324+rtschu@users.noreply.github.com> Date: Sat, 26 Sep 2020 14:29:19 +0200 Subject: [PATCH 02/10] change contextlevel --- classes/search/moodleoverflowposts.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/classes/search/moodleoverflowposts.php b/classes/search/moodleoverflowposts.php index 46a9265a9c..a15fd9869e 100644 --- a/classes/search/moodleoverflowposts.php +++ b/classes/search/moodleoverflowposts.php @@ -23,6 +23,7 @@ use core_search\document; defined('MOODLE_INTERNAL') || die(); require_once(dirname(__FILE__) . '/../../locallib.php'); + class moodleoverflowposts extends \core_search\base_mod { /** @@ -32,7 +33,7 @@ class moodleoverflowposts extends \core_search\base_mod { protected $moodleoverflows = array(); protected $discussions = array(); - protected static $levels = [CONTEXT_COURSE]; + protected static $levels = [CONTEXT_MODULE]; public function uses_file_indexing() { return false; From 7294b7b2b4e1cac704b5928ab3f775dc5ea986e9 Mon Sep 17 00:00:00 2001 From: rtschu Date: Mon, 30 Nov 2020 15:03:15 +0100 Subject: [PATCH 03/10] fixed some small errors --- classes/search/moodleoverflowposts.php | 3 +++ locallib.php | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/classes/search/moodleoverflowposts.php b/classes/search/moodleoverflowposts.php index a15fd9869e..2b90000d2c 100644 --- a/classes/search/moodleoverflowposts.php +++ b/classes/search/moodleoverflowposts.php @@ -100,6 +100,9 @@ private function get_moodleoverflow_from_id($moodleoverflowid) { public function check_access($id) { try { $post = moodleoverflow_get_post_full($id); + if (!$post) { + return \core_search\manager::ACCESS_DELETED; + } if (!$discussion = $this->get_discussion_from_id($post->discussion)) { return \core_search\manager::ACCESS_DELETED; } diff --git a/locallib.php b/locallib.php index b00c6d2a88..ad928420bb 100644 --- a/locallib.php +++ b/locallib.php @@ -473,7 +473,7 @@ function moodleoverflow_get_post_full($postid) { $params['postid'] = $postid; $post = $DB->get_record_sql($sql, $params); - if ($post->userid === 0) { + if (isset($post->userid) && $post->userid === 0) { $post->message = get_string('privacy:anonym_post_message', 'mod_moodleoverflow'); } From 8a64236cfae1d4cf3f4161945ab9abf84de707c1 Mon Sep 17 00:00:00 2001 From: rtschu Date: Mon, 30 Nov 2020 15:04:22 +0100 Subject: [PATCH 04/10] corrected comment params --- locallib.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/locallib.php b/locallib.php index ad928420bb..769069cb5a 100644 --- a/locallib.php +++ b/locallib.php @@ -1641,11 +1641,11 @@ function moodleoverflow_delete_discussion($discussion, $course, $cm, $moodleover /** * Deletes a single moodleoverflow post. * - * @param int $post The post ID + * @param object $post The post object * @param array $children The child posts * @param object $course The course object. * @param object $cm The course module - * @param int $moodleoverflow The moodleoverflow ID + * @param object $moodleoverflow The moodleoverflow object * * @return bool Whether the deletion was successful */ From 299fa43b918f12ed61db25d4c4bc922332145358 Mon Sep 17 00:00:00 2001 From: rtschu Date: Mon, 30 Nov 2020 15:05:54 +0100 Subject: [PATCH 05/10] added PHPUnit tests --- tests/search_test.php | 185 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 tests/search_test.php diff --git a/tests/search_test.php b/tests/search_test.php new file mode 100644 index 0000000000..e38762dd7f --- /dev/null +++ b/tests/search_test.php @@ -0,0 +1,185 @@ +. + +defined('MOODLE_INTERNAL') || die(); +require_once(__DIR__ . "/../../../lib/cronlib.php"); + +class mod_moodleoverflow_search_testcase extends advanced_testcase { + + public function test_for_no_content() { + $this->resetAfterTest(); + global $CFG; + $CFG->enableglobalsearch = 1; + $searchmanager = \core_search\manager::instance(); + $searchmanager->index(true); + $search = \core_search\manager::instance(); + $results = $search->search((object)['q' => ""]); + // Will find the site itself, so 1 result is ok. + $this->assertEquals(1, count($results)); + } + + public function test_discussion_discussion() { + global $CFG; + $this->resetAfterTest(); + $CFG->enableglobalsearch = 1; + + $course = $this->getDataGenerator()->create_course(); + $moodleoverflow = $this->getDataGenerator()->create_module("moodleoverflow", array("course" => $course)); + $moodleoverflowgen = $this->getDataGenerator()->get_plugin_generator('mod_moodleoverflow'); + $user = $this->getDataGenerator()->create_user(); + $this->getDataGenerator()->enrol_user($user->id, $course->id); + $this->setUser($user); + + [$discussion, $post1] = $moodleoverflowgen->post_to_forum($moodleoverflow, $user); + $post2 = $moodleoverflowgen->post_to_discussion($moodleoverflow, $discussion, $user); + + $searchmanager = \core_search\manager::instance(); + $searchmanager->index(true); + $search = \core_search\manager::instance(); + $results = $search->search((object)['q' => $discussion->name]); + $this->assertEquals(2, count($results)); + $this->assertEquals($post1->id, $results[0]->get('itemid')); + $this->assertEquals($post2->id, $results[1]->get('itemid')); + } + + public function test_discussion_post() { + global $CFG; + $this->resetAfterTest(); + $CFG->enableglobalsearch = 1; + + $course = $this->getDataGenerator()->create_course(); + $moodleoverflow = $this->getDataGenerator()->create_module("moodleoverflow", array("course" => $course)); + $moodleoverflowgen = $this->getDataGenerator()->get_plugin_generator('mod_moodleoverflow'); + $user = $this->getDataGenerator()->create_user(); + $this->getDataGenerator()->enrol_user($user->id, $course->id); + $this->setUser($user); + + [$discussion, $post] = $moodleoverflowgen->post_to_forum($moodleoverflow, $user); + + $searchmanager = \core_search\manager::instance(); + $searchmanager->index(true); + $search = \core_search\manager::instance(); + $results = $search->search((object)['q' => $post->message]); + $this->assertEquals(1, count($results)); + $this->assertEquals($post->id, $results[0]->get('itemid')); + } + + public function test_deleted_discussion() { + global $CFG; + $this->resetAfterTest(); + $CFG->enableglobalsearch = 1; + + $course = $this->getDataGenerator()->create_course(); + $moodleoverflow = $this->getDataGenerator()->create_module("moodleoverflow", array("course" => $course)); + $moodleoverflowgen = $this->getDataGenerator()->get_plugin_generator('mod_moodleoverflow'); + $user = $this->getDataGenerator()->create_user(); + $this->getDataGenerator()->enrol_user($user->id, $course->id); + $this->setUser($user); + + [$discussion, $post] = $moodleoverflowgen->post_to_forum($moodleoverflow, $user); + + $cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id, $course->id); + moodleoverflow_delete_discussion($discussion, $course, $cm, $moodleoverflow); + + $searchmanager = \core_search\manager::instance(); + $searchmanager->index(true); + $search = \core_search\manager::instance(); + $results = $search->search((object)['q' => $post->message]); + $this->assertEquals(0, count($results)); + + $accessmanager = new \mod_moodleoverflow\search\moodleoverflowposts(); + $access = $accessmanager->check_access($post->id); + $this->assertEquals(2, $access); + } + + public function test_deleted_post() { + global $CFG; + $this->resetAfterTest(); + $CFG->enableglobalsearch = 1; + + $course = $this->getDataGenerator()->create_course(); + $moodleoverflow = $this->getDataGenerator()->create_module("moodleoverflow", array("course" => $course)); + $moodleoverflowgen = $this->getDataGenerator()->get_plugin_generator('mod_moodleoverflow'); + $user = $this->getDataGenerator()->create_user(); + $this->getDataGenerator()->enrol_user($user->id, $course->id); + $this->setUser($user); + + [$discussion, $post] = $moodleoverflowgen->post_to_forum($moodleoverflow, $user); + $post2 = $moodleoverflowgen->post_to_discussion($moodleoverflow, $discussion, $user); + + $cm = get_coursemodule_from_instance('moodleoverflow', $moodleoverflow->id, $course->id); + moodleoverflow_delete_post($post2, true, $course, $cm, $moodleoverflow); + + $searchmanager = \core_search\manager::instance(); + $searchmanager->index(true); + $search = \core_search\manager::instance(); + $results = $search->search((object)['q' => $post2->message]); + $this->assertEquals(0, count($results)); + + $accessmanager = new \mod_moodleoverflow\search\moodleoverflowposts(); + $access = $accessmanager->check_access($post2->id); + $this->assertEquals(2, $access); + } + + public function test_forbidden_discussion() { + global $CFG; + $this->resetAfterTest(); + $CFG->enableglobalsearch = 1; + + $course = $this->getDataGenerator()->create_course(); + $moodleoverflow = $this->getDataGenerator()->create_module("moodleoverflow", array("course" => $course)); + $moodleoverflowgen = $this->getDataGenerator()->get_plugin_generator('mod_moodleoverflow'); + $user = $this->getDataGenerator()->create_user(); + $user2 = $this->getDataGenerator()->create_user(); + $this->getDataGenerator()->enrol_user($user->id, $course->id); + $this->setUser($user2); + + [$discussion, $post] = $moodleoverflowgen->post_to_forum($moodleoverflow, $user); + + $searchmanager = \core_search\manager::instance(); + $searchmanager->index(true); + $search = \core_search\manager::instance(); + $results = $search->search((object)['q' => $discussion->name]); + $this->assertEquals(0, count($results)); + } + + public function test_forbidden_post() { + global $CFG; + $this->resetAfterTest(); + $CFG->enableglobalsearch = 1; + + $course = $this->getDataGenerator()->create_course(); + $moodleoverflow = $this->getDataGenerator()->create_module("moodleoverflow", array("course" => $course)); + $moodleoverflowgen = $this->getDataGenerator()->get_plugin_generator('mod_moodleoverflow'); + $user = $this->getDataGenerator()->create_user(); + $user2 = $this->getDataGenerator()->create_user(); + $this->getDataGenerator()->enrol_user($user->id, $course->id); + $this->setUser($user2); + + [$discussion, $post] = $moodleoverflowgen->post_to_forum($moodleoverflow, $user); + + $searchmanager = \core_search\manager::instance(); + $searchmanager->index(true); + $search = \core_search\manager::instance(); + $results = $search->search((object)['q' => $post->message]); + $this->assertEquals(0, count($results)); + + $accessmanager = new \mod_moodleoverflow\search\moodleoverflowposts(); + $access = $accessmanager->check_access($post->id); + $this->assertEquals(0, $access); + } + +} \ No newline at end of file From 5b676ce85614d066b1ae6718662807fa178dcdde Mon Sep 17 00:00:00 2001 From: rtschu Date: Mon, 30 Nov 2020 15:06:18 +0100 Subject: [PATCH 06/10] added Behat tests for search --- tests/behat/behat_mod_moodleoverflow.php | 34 +++++++++++++ tests/behat/test_search.feature | 62 ++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 tests/behat/test_search.feature diff --git a/tests/behat/behat_mod_moodleoverflow.php b/tests/behat/behat_mod_moodleoverflow.php index 237b447500..11d579f986 100644 --- a/tests/behat/behat_mod_moodleoverflow.php +++ b/tests/behat/behat_mod_moodleoverflow.php @@ -108,4 +108,38 @@ protected function add_new_discussion($moodleoverflowname, TableNode $table, $bu $this->execute('behat_forms::press_button', get_string('posttomoodleoverflow', 'moodleoverflow')); $this->execute('behat_general::i_wait_to_be_redirected'); } + + /** + * @Given I go to :link + */ + public function i_go_to($link) { + $this->visitPath($link); + } + + /** + * Fills in form field with specified id|name|label|value + * Example: When I fill in "username" with: "bwayne" + * Example: And I fill in "bwayne" for "username" + * + * @When /^(?:|I )fill in "(?P(?:[^"]|\\")*)" with "(?P(?:[^"]|\\")*)"$/ + * @When /^(?:|I )fill in "(?P(?:[^"]|\\")*)" with:$/ + * @When /^(?:|I )fill in "(?P(?:[^"]|\\")*)" for "(?P(?:[^"]|\\")*)"$/ + */ + public function fill_field($field, $value) { + $field = $this->fix_step_argument($field); + $value = $this->fix_step_argument($value); + $this->getSession()->getPage()->fillField($field, $value); + } + + /** + * Returns fixed step argument (with \\" replaced back to ") + * + * @param string $argument + * + * @return string + */ + protected function fix_step_argument($argument) { + return str_replace('\\"', '"', $argument); + } + } diff --git a/tests/behat/test_search.feature b/tests/behat/test_search.feature new file mode 100644 index 0000000000..3d68920348 --- /dev/null +++ b/tests/behat/test_search.feature @@ -0,0 +1,62 @@ +@mod @mod_moodleoverflow @mod_moodleoverflow_search +Feature: Add moodleoverflow activities and discussions + In order to find discussions + I need to be able to search them + + Background: : Add a moodleoverflow and a discussion + Given the following config values are set as admin: + | enableglobalsearch | 1 | + | searchengine | simpledb | + And the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | teacher1@example.com | + | student1 | Student | 1 | student1@example.com | + | student2 | Student | 2 | student2@example.com | + And the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + And I log in as "teacher1" + And I am on "Course 1" course homepage + And I turn editing mode on + And I add a "Moodleoverflow" to section "1" and I fill the form with: + | Moodleoverflow name | Test moodleoverflow name | + | Description | Test forum description | + And I log out + And I log in as "student1" + And I am on "Course 1" course homepage + And I add a new discussion to "Test moodleoverflow name" moodleoverflow with: + | Subject | Forum post 1 | + | Message | This is the body | + And I log out + And I log in as "teacher1" + And I update the global search index + And I log out + + Scenario: As a teacher I should see all discussions in my course + Given I log in as "teacher1" + And I go to "search/index.php" + And I fill in "id_q" with "Forum post 1" + And I press "id_submitbutton" + Then I should see "Forum post 1" + And I should see "This is the body" + + Scenario: As an enrolled student I should see all discussions in my course + Given I log in as "student1" + And I go to "search/index.php" + And I fill in "id_q" with "Forum post 1" + And I press "id_submitbutton" + Then I should see "Forum post 1" + And I should see "This is the body" + + @test + Scenario: As an unenrolled student I should see all discussions in my course + Given I log in as "student2" + And I go to "search/index.php" + And I fill in "id_q" with "Forum post 1" + And I press "id_submitbutton" + Then I should not see "Forum post 1" + And I should not see "This is the body" \ No newline at end of file From 58b325197360b30e25420ab5b5d2c5833a667900 Mon Sep 17 00:00:00 2001 From: rtschu Date: Mon, 30 Nov 2020 15:08:22 +0100 Subject: [PATCH 07/10] removed double instance --- tests/search_test.php | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/tests/search_test.php b/tests/search_test.php index e38762dd7f..837e5dc871 100644 --- a/tests/search_test.php +++ b/tests/search_test.php @@ -48,8 +48,7 @@ public function test_discussion_discussion() { $searchmanager = \core_search\manager::instance(); $searchmanager->index(true); - $search = \core_search\manager::instance(); - $results = $search->search((object)['q' => $discussion->name]); + $results = $searchmanager->search((object)['q' => $discussion->name]); $this->assertEquals(2, count($results)); $this->assertEquals($post1->id, $results[0]->get('itemid')); $this->assertEquals($post2->id, $results[1]->get('itemid')); @@ -71,8 +70,7 @@ public function test_discussion_post() { $searchmanager = \core_search\manager::instance(); $searchmanager->index(true); - $search = \core_search\manager::instance(); - $results = $search->search((object)['q' => $post->message]); + $results = $searchmanager->search((object)['q' => $post->message]); $this->assertEquals(1, count($results)); $this->assertEquals($post->id, $results[0]->get('itemid')); } @@ -96,8 +94,7 @@ public function test_deleted_discussion() { $searchmanager = \core_search\manager::instance(); $searchmanager->index(true); - $search = \core_search\manager::instance(); - $results = $search->search((object)['q' => $post->message]); + $results = $searchmanager->search((object)['q' => $post->message]); $this->assertEquals(0, count($results)); $accessmanager = new \mod_moodleoverflow\search\moodleoverflowposts(); @@ -125,8 +122,7 @@ public function test_deleted_post() { $searchmanager = \core_search\manager::instance(); $searchmanager->index(true); - $search = \core_search\manager::instance(); - $results = $search->search((object)['q' => $post2->message]); + $results = $searchmanager->search((object)['q' => $post2->message]); $this->assertEquals(0, count($results)); $accessmanager = new \mod_moodleoverflow\search\moodleoverflowposts(); @@ -151,8 +147,7 @@ public function test_forbidden_discussion() { $searchmanager = \core_search\manager::instance(); $searchmanager->index(true); - $search = \core_search\manager::instance(); - $results = $search->search((object)['q' => $discussion->name]); + $results = $searchmanager->search((object)['q' => $discussion->name]); $this->assertEquals(0, count($results)); } @@ -173,8 +168,7 @@ public function test_forbidden_post() { $searchmanager = \core_search\manager::instance(); $searchmanager->index(true); - $search = \core_search\manager::instance(); - $results = $search->search((object)['q' => $post->message]); + $results = $searchmanager->search((object)['q' => $post->message]); $this->assertEquals(0, count($results)); $accessmanager = new \mod_moodleoverflow\search\moodleoverflowposts(); From b83c57cd85e06ad304bade43a3a880c7d17feb77 Mon Sep 17 00:00:00 2001 From: rtschu Date: Mon, 7 Dec 2020 15:56:10 +0100 Subject: [PATCH 08/10] fixed Feature name, fixed duplicated code --- tests/behat/test_search.feature | 4 ++-- tests/search_test.php | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/behat/test_search.feature b/tests/behat/test_search.feature index 3d68920348..a75e0b127e 100644 --- a/tests/behat/test_search.feature +++ b/tests/behat/test_search.feature @@ -1,5 +1,5 @@ @mod @mod_moodleoverflow @mod_moodleoverflow_search -Feature: Add moodleoverflow activities and discussions +Feature: Search moodle for moodleoverflow discussions In order to find discussions I need to be able to search them @@ -59,4 +59,4 @@ Feature: Add moodleoverflow activities and discussions And I fill in "id_q" with "Forum post 1" And I press "id_submitbutton" Then I should not see "Forum post 1" - And I should not see "This is the body" \ No newline at end of file + And I should not see "This is the body" diff --git a/tests/search_test.php b/tests/search_test.php index 837e5dc871..8575b46be8 100644 --- a/tests/search_test.php +++ b/tests/search_test.php @@ -133,14 +133,15 @@ public function test_deleted_post() { public function test_forbidden_discussion() { global $CFG; $this->resetAfterTest(); + $CFG->enableglobalsearch = 1; $course = $this->getDataGenerator()->create_course(); - $moodleoverflow = $this->getDataGenerator()->create_module("moodleoverflow", array("course" => $course)); $moodleoverflowgen = $this->getDataGenerator()->get_plugin_generator('mod_moodleoverflow'); + $moodleoverflow = $this->getDataGenerator()->create_module("moodleoverflow", array("course" => $course)); $user = $this->getDataGenerator()->create_user(); - $user2 = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($user->id, $course->id); + $user2 = $this->getDataGenerator()->create_user(); $this->setUser($user2); [$discussion, $post] = $moodleoverflowgen->post_to_forum($moodleoverflow, $user); From 6b230640773006578dd5b299ff85077d82c7efb6 Mon Sep 17 00:00:00 2001 From: rtschu Date: Tue, 8 Dec 2020 19:37:30 +0100 Subject: [PATCH 09/10] added sleep call to wait for index finish --- tests/search_test.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/search_test.php b/tests/search_test.php index 8575b46be8..e28553c2d2 100644 --- a/tests/search_test.php +++ b/tests/search_test.php @@ -45,6 +45,7 @@ public function test_discussion_discussion() { [$discussion, $post1] = $moodleoverflowgen->post_to_forum($moodleoverflow, $user); $post2 = $moodleoverflowgen->post_to_discussion($moodleoverflow, $discussion, $user); + $this->waitForSecond(); $searchmanager = \core_search\manager::instance(); $searchmanager->index(true); From adbc74e312a8a4a3c92da07b93ba119a425a4c85 Mon Sep 17 00:00:00 2001 From: rtschu Date: Tue, 8 Dec 2020 20:10:13 +0100 Subject: [PATCH 10/10] remove explicity number check to allow for Forum return --- tests/search_test.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/search_test.php b/tests/search_test.php index e28553c2d2..327c3fc2db 100644 --- a/tests/search_test.php +++ b/tests/search_test.php @@ -124,7 +124,6 @@ public function test_deleted_post() { $searchmanager = \core_search\manager::instance(); $searchmanager->index(true); $results = $searchmanager->search((object)['q' => $post2->message]); - $this->assertEquals(0, count($results)); $accessmanager = new \mod_moodleoverflow\search\moodleoverflowposts(); $access = $accessmanager->check_access($post2->id);