Skip to content

Commit

Permalink
MDL-44725 Availability: Unit test for tree::get_user_list_sql
Browse files Browse the repository at this point in the history
  • Loading branch information
sammarshallou authored and marinaglancy committed Sep 4, 2014
1 parent 4191191 commit 28f31f7
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions availability/tests/tree_test.php
Expand Up @@ -643,6 +643,68 @@ public function test_get_json() {
json_encode(tree::get_root_json(array($child, $child), tree::OP_AND, array(true, false))));
}

/**
* Tests get_user_list_sql.
*/
public function test_get_user_list_sql() {
global $DB;
$this->resetAfterTest();
$generator = $this->getDataGenerator();

// Create a test course with 2 groups and users in each combination of them.
$course = $generator->create_course();
$group1 = $generator->create_group(array('courseid' => $course->id));
$group2 = $generator->create_group(array('courseid' => $course->id));
$userin1 = $generator->create_user();
$userin2 = $generator->create_user();
$userinboth = $generator->create_user();
$userinneither = $generator->create_user();
$studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
foreach (array($userin1, $userin2, $userinboth, $userinneither) as $user) {
$generator->enrol_user($user->id, $course->id, $studentroleid);
}
groups_add_member($group1, $userin1);
groups_add_member($group2, $userin2);
groups_add_member($group1, $userinboth);
groups_add_member($group2, $userinboth);
$info = new \core_availability\mock_info($course);

// Tree with single group condition.
$tree = new tree(tree::get_root_json(array(
\availability_group\condition::get_json($group1->id)
)));
list($sql, $params) = $tree->get_user_list_sql(false, $info, false);
$result = $DB->get_fieldset_sql($sql, $params);
sort($result);
$this->assertEquals(array($userin1->id, $userinboth->id), $result);

// Tree with 'AND' of both group conditions.
$tree = new tree(tree::get_root_json(array(
\availability_group\condition::get_json($group1->id),
\availability_group\condition::get_json($group2->id)
)));
list($sql, $params) = $tree->get_user_list_sql(false, $info, false);
$result = $DB->get_fieldset_sql($sql, $params);
sort($result);
$this->assertEquals(array($userinboth->id), $result);

// Tree with 'AND' of both group conditions.
$tree = new tree(tree::get_root_json(array(
\availability_group\condition::get_json($group1->id),
\availability_group\condition::get_json($group2->id)
), tree::OP_OR));
list($sql, $params) = $tree->get_user_list_sql(false, $info, false);
$result = $DB->get_fieldset_sql($sql, $params);
sort($result);
$this->assertEquals(array($userin1->id, $userin2->id, $userinboth->id), $result);

// Check with flipped logic (NOT above level of tree).
list($sql, $params) = $tree->get_user_list_sql(true, $info, false);
$result = $DB->get_fieldset_sql($sql, $params);
sort($result);
$this->assertEquals(array($userinneither->id), $result);
}

/**
* Utility function to build the PHP structure representing a mock condition.
*
Expand Down

0 comments on commit 28f31f7

Please sign in to comment.