Skip to content

Commit

Permalink
Merge pull request #7 from learnweb/feature/unit-tests
Browse files Browse the repository at this point in the history
Further unit tests
Deprecated function get_all_allocations replaced for two cases
  • Loading branch information
tobiasreischmann committed Feb 10, 2015
2 parents b512b86 + 6559159 commit b4a0273
Show file tree
Hide file tree
Showing 6 changed files with 362 additions and 11 deletions.
9 changes: 5 additions & 4 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,12 @@ private function process_action_allocation_to_grouping(){
}

// add all participants in the correct group
$allocations = $this->get_all_allocations();
foreach ($allocations as $userid => $choice) {
$choice_id = array_keys($choice)[0];
$allocations = $this->get_allocations();
foreach ($allocations as $id => $allocation) {
$choice_id = $allocation->choiceid;
$user_id = $allocation->userid;
$choiceidnumber = $group_identifier_from_choice_id($choice_id);
groups_add_member($choice_identifiers[$choiceidnumber]['groupid'], $userid);
groups_add_member($choice_identifiers[$choiceidnumber]['groupid'], $user_id);
}
// Invalidate the grouping cache for the course
cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($this->course->id));
Expand Down
6 changes: 3 additions & 3 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,10 @@ public function distribution_table_for_ratingallocate(ratingallocate $ratingallo
// Count the number of allocations with a specific rating
$distributiondata = array();

$memberships = $ratingallocate->get_all_allocations();
$memberships = $ratingallocate->get_allocations();

foreach ($memberships as $userid => $choice) {
$rating = array_shift($choice);
foreach ($memberships as $id => $membership) {
$rating = $membership->rating;
if (key_exists($rating, $distributiondata)) {
$distributiondata[$rating] ++;
} else {
Expand Down
17 changes: 17 additions & 0 deletions tests/generator/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,23 @@ public static function get_ratingallocate_for_user(advanced_testcase $tc, $ratin

return new ratingallocate($ratingallocate_db, $course, $cm, $context);
}

public static function get_open_ratingallocate_for_teacher(advanced_testcase $tc) {
return self::get_ratingallocate_for_teacher_open_in(0, $tc);
}

public static function get_closed_ratingallocate_for_teacher(advanced_testcase $tc) {
return self::get_ratingallocate_for_teacher_open_in(-7, $tc);
}

private static function get_ratingallocate_for_teacher_open_in($num_days, advanced_testcase $tc) {
$record = mod_ratingallocate_generator::get_default_values();
$record['accesstimestart'] = time() + ($num_days * 24 * 60 * 60);
$record['accesstimestop'] = time() + (($num_days + 6) * 24 * 60 * 60);
$record['publishdate'] = time() + (($num_days + 7) * 24 * 60 * 60);
$test_module = new mod_ratingallocate_generated_module($tc,$record);
return mod_ratingallocate_generator::get_ratingallocate_for_user($tc, $test_module->mod_db, $test_module->teacher);
}
}

class mod_ratingallocate_generated_module {
Expand Down
87 changes: 83 additions & 4 deletions tests/locallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,92 @@ public function test_simple() {
$alloc1 = self::filter_allocations_by_choice($allocations,$choice1->{this_db\ratingallocate_choices::ID});
$alloc2 = self::filter_allocations_by_choice($allocations,$choice2->{this_db\ratingallocate_choices::ID});

$this->assertEquals(array($student_1->id,$student_2->id), array_values(array_map($map_user_id, $alloc1)));
$this->assertEquals(array($student_3->id,$student_4->id), array_values(array_map($map_user_id, $alloc2)));

//Assert, that student 1 was allocated to choice 1
$this->assertContains($student_1->id, array_map($map_user_id, $alloc1));
//Assert, that student 2 was allocated to choice 1
$this->assertContains($student_2->id, array_map($map_user_id, $alloc1));
//Assert, that student 3 was allocated to choice 2
$this->assertContains($student_3->id, array_map($map_user_id, $alloc2));
//Assert, that student 4 was allocated to choice 2
$this->assertContains($student_4->id, array_map($map_user_id, $alloc2));
}
private static function filter_allocations_by_choice($allocations, $choiceid) {
$filter_choice_id = function($elem) use ($choiceid) { return $elem->{this_db\ratingallocate_allocations::CHOICEID} == $choiceid; };
return array_filter($allocations, $filter_choice_id);
}

/**
* Default data has two choices but only one is active.
* Test if count of rateable choices is 1.
*/
public function test_get_ratable_choices(){
$record = mod_ratingallocate_generator::get_default_values();
$test_module = new mod_ratingallocate_generated_module($this,$record);
$ratingallocate = mod_ratingallocate_generator::get_ratingallocate_for_user($this, $test_module->mod_db, $test_module->teacher);
$this->assertCount(1,$ratingallocate->get_rateable_choices());
}

/**
* Test if option titles are returned according to the default values
*/
public function test_get_option_titles_default(){
$expected_result = array(1 => 'Yes',0 => 'No'); //Depends on language file
$ratings = array(0,1,1,1,0);

$record = mod_ratingallocate_generator::get_default_values();
$test_module = new mod_ratingallocate_generated_module($this,$record);
$ratingallocate = mod_ratingallocate_generator::get_ratingallocate_for_user($this, $test_module->mod_db, $test_module->teacher);

$result = $ratingallocate->get_options_titles($ratings);
$this->assertEquals($expected_result,$result);
}

/**
* Test if option titles are returned according to defined custom values
*/
public function test_get_option_titles_custom(){
$expected_result = array(1 => 'Ja1234', 0 => 'Nein1234'); //Test data
$ratings = array(1,1,1,0,1,1);

$record = mod_ratingallocate_generator::get_default_values();
$record['strategyopt']['strategy_yesno'] = $expected_result;
$test_module = new mod_ratingallocate_generated_module($this,$record);
$ratingallocate = mod_ratingallocate_generator::get_ratingallocate_for_user($this, $test_module->mod_db, $test_module->teacher);

$result = $ratingallocate->get_options_titles($ratings);
$this->assertEquals($expected_result,$result);
}

/**
* Test if option titles are returned according to defined custom values, if ratings consist of just one rating
*/
public function test_get_option_titles_custom1(){
$expected_result = array(1 => 'Ja1234'); //Test data
$ratings = array(1,1,1,1,1);

$record = mod_ratingallocate_generator::get_default_values();
$record['strategyopt']['strategy_yesno'] = $expected_result;
$test_module = new mod_ratingallocate_generated_module($this,$record);
$ratingallocate = mod_ratingallocate_generator::get_ratingallocate_for_user($this, $test_module->mod_db, $test_module->teacher);

$result = $ratingallocate->get_options_titles($ratings);
$this->assertEquals($expected_result,$result);
}

/**
* Test if option titles are returned according to a mixture of defined and custom values,
*/
public function test_get_option_titles_mixed(){
$settings = array(1 => 'Ja1234'); //Test data
$ratings = array(0,1,1,1,1);
$expected_result = $settings;
$expected_result [0] = 'No'; //Depends on language file

$record = mod_ratingallocate_generator::get_default_values();
$record['strategyopt']['strategy_yesno'] = $settings;
$test_module = new mod_ratingallocate_generated_module($this,$record);
$ratingallocate = mod_ratingallocate_generator::get_ratingallocate_for_user($this, $test_module->mod_db, $test_module->teacher);

$result = $ratingallocate->get_options_titles($ratings);
$this->assertEquals($expected_result,$result);
}
}
89 changes: 89 additions & 0 deletions tests/mod_ratingallocate_processor_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?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/>.

/**
* mod_ratingallocate processor tests
*
* @package mod_ratingallocate
* @category test
* @group mod_ratingallocate
* @copyright reischmann
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_ratingallocate_processor_testcase extends advanced_testcase {

function setUp() {
global $PAGE;
$PAGE->set_url('/');
}

/**
* Tests if process_publish_allocations is not working before time runs out
* Assert, that the ratingallocate can not be published
*/
public function test_publishing_before_accesstimestop(){
$ratingallocate = mod_ratingallocate_generator::get_open_ratingallocate_for_teacher($this);
$this->assertEquals(0,$ratingallocate->ratingallocate->published);
$this->call_private_ratingallocate_method($ratingallocate,'process_publish_allocations');
$this->assertEquals(0,$ratingallocate->ratingallocate->published);
}
/**
* Tests if process_publish_allocations is working after time runs out
* Assert, that the ratingallocate can be published
*/
public function test_publishing_after_accesstimestop(){
$ratingallocate = mod_ratingallocate_generator::get_closed_ratingallocate_for_teacher($this);
$this->assertEquals(0,$ratingallocate->ratingallocate->published);
$this->call_private_ratingallocate_method($ratingallocate,'process_publish_allocations');
$this->assertEquals(1,$ratingallocate->ratingallocate->published);
}
/**
* Tests if process_action_allocation_to_grouping is working before time runs out
* Assert, that the number of groupings does not change
*/
public function test_grouping_before_accesstimestop(){
global $DB;
$ratingallocate = mod_ratingallocate_generator::get_open_ratingallocate_for_teacher($this);
$this->assertEquals(0, $DB->count_records('groupings'));
$this->call_private_ratingallocate_method($ratingallocate,'process_action_allocation_to_grouping');
$this->assertEquals(0, $DB->count_records('groupings'));
}
/**
* Tests if process_action_allocation_to_grouping is working after time runs out
* Assert, that the number of groupings changes as expected (1 Grouping should be created)
*/
public function test_grouping_after_accesstimestop(){
global $DB;
$ratingallocate = mod_ratingallocate_generator::get_closed_ratingallocate_for_teacher($this);
$this->assertEquals(0, $DB->count_records('groupings'));
$this->call_private_ratingallocate_method($ratingallocate,'process_action_allocation_to_grouping');
$this->assertEquals(1, $DB->count_records('groupings'));
}

/**
* Enables for calling the private processing functions of the ratingallocate
* @param ratingallocate $ratingallocate
* @param unknown $method_name name of private or protected method
* @param unknown $args arguments the method should be called with
*/
private function call_private_ratingallocate_method(ratingallocate $ratingallocate, $method_name, $args=[]){
$class = new ReflectionClass('ratingallocate');
$method = $class->getMethod($method_name);
$method->setAccessible(true);
$method->invokeArgs($ratingallocate, $args);
}

}
Loading

0 comments on commit b4a0273

Please sign in to comment.