Skip to content

Instantly share code, notes, and snippets.

@prateeksachan
Last active December 23, 2015 08:09
Show Gist options
  • Save prateeksachan/6605383 to your computer and use it in GitHub Desktop.
Save prateeksachan/6605383 to your computer and use it in GitHub Desktop.
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* PHPUnit globalsearch tests
*
* @package globalsearch
* @category phpunit
* @copyright
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
class gs_generator_testcase extends advanced_testcase {
public function test_forum_advance() {
global $USER, $CFG, $DB;
require_once($CFG->dirroot . '/search/' . $CFG->SEARCH_ENGINE . '/connection.php');
require_once($CFG->dirroot . '/search/lib.php');
require_once($CFG->dirroot . '/mod/forum/tests/generator/lib.php');
require_once($CFG->dirroot . '/mod/forum/lib.php');
$this->resetAfterTest(true);
//$client->delete_by_query('*:*');
//$client->commit();
//create users
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
//create courses
$course1 = self::getDataGenerator()->create_course();
//enrol user1, user2, user3 to course1
$enrol = enrol_get_plugin('manual');
$this->getDataGenerator()->enrol_user($user1->id, $course1->id);
$this->getDataGenerator()->enrol_user($user2->id, $course1->id);
$this->getDataGenerator()->enrol_user($user3->id, $course1->id);
//create group1(user1, user2) & group2(user3)
$group1 = $this->getDataGenerator()->create_group(array('courseid'=>$course1->id));
$group2 = $this->getDataGenerator()->create_group(array('courseid'=>$course1->id));
$this->assertTrue(groups_add_member($group1, $user1));
$this->assertTrue(groups_add_member($group1, $user2));
$this->assertTrue(groups_add_member($group2, $user3));
$record = new stdClass();
$record->course = $course1->id;
$record->groupmode = SEPARATEGROUPS;
$forum1 = self::getDataGenerator()->create_module('forum', $record);
//create discussion1
$record = new stdClass();
$record->course = $course1->id;
$record->userid = $user1->id;
$record->forum = $forum1->id;
$record->message = 'Im Batman';
$record->groupid = $group1->id;
$discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
//create post1 in discussion1
$record = new stdClass();
$record->discussion = $discussion1->id;
$record->parent = $discussion1->firstpost;
$record->userid = $user2->id;
$record->message = 'Im Joker';
$discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
//check forum1 creation
$this->assertEquals(1, $DB->count_records_select('forum', 'id = :forum1',
array('forum1' => $forum1->id)));
//check discussion1 creation
$this->assertEquals(1, $DB->count_records_select('forum_discussions', 'forum = :forum1',
array('forum1' => $forum1->id)));
//check posts creation
$this->assertEquals(2, $DB->count_records_select('forum_posts', 'discussion = :discussion1',
array('discussion1' => $discussion1->id)));
//get SolrInputDocument of post1
$post1 = forum_search_get_documents(1);
//add the returned SolrInputDocument to solr object $client
foreach ($post1 as $post) {
$client->add_document($post);
}
//commit
$client->commit();
//set user1 as testing user
$this->setUser($user1);
$this->assertEquals(3, $user1->id);
//create a query
$query = new stdClass();
$query->queryfield = "Batman";
$results = solr_execute_query($client, $query);
is_array($results) ? $x=1 : $x=0;
$this->assertEquals(1, $x); //because the search was a success
//set user2 as testing user
$this->setUser($user2);
$this->assertEquals(4, $user2->id);
$query = new stdClass();
$query->queryfield = "Batman";
$results = solr_execute_query($client, $query);
is_array($results) ? $x=1 : $x=0;
$this->assertEquals(1, $x);//because, user2 is also part of group1
//set user3 as testing user
$this->setUser($user3);
$this->assertEquals(5, $user3->id);
$query = new stdClass();
$query->queryfield = "Batman";
$results = solr_execute_query($client, $query);
is_array($results) ? $x=1 : $x=0;
$this->assertEquals(0, $x);//because, user3 is not part of group1
//delete this forum post record from index
$client->delete_by_id('forum_1');
$client->commit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment