MDL-38564 Adding unit tests for the functions compare_activities_by_time_asc() and compare_activities_by_time_desc()

This commit is contained in:
Ankit Agarwal 2013-03-20 13:17:48 +08:00
parent 373a8e052c
commit bf6b3c7a8a
3 changed files with 139 additions and 36 deletions

View File

@ -3972,3 +3972,54 @@ function update_module($moduleinfo) {
return $moduleinfo;
}
/**
* Compare two objects to find out their correct order based on timestamp (to be used by usort).
* Sorts by descending order of time.
*
* @param stdClass $a First object
* @param stdClass $b Second object
* @return int 0,1,-1 representing the order
*/
function compare_activities_by_time_desc($a, $b) {
// Make sure the activities actually have a timestamp property.
if ((!property_exists($a, 'timestamp')) && (!property_exists($b, 'timestamp'))) {
return 0;
}
// We treat instances without timestamp as if they have a timestamp of 0.
if ((!property_exists($a, 'timestamp')) && (property_exists($b,'timestamp'))) {
return 1;
}
if ((property_exists($a, 'timestamp')) && (!property_exists($b, 'timestamp'))) {
return -1;
}
if ($a->timestamp == $b->timestamp) {
return 0;
}
return ($a->timestamp > $b->timestamp) ? -1 : 1;
}
/**
* Compare two objects to find out their correct order based on timestamp (to be used by usort).
* Sorts by ascending order of time.
*
* @param stdClass $a First object
* @param stdClass $b Second object
* @return int 0,1,-1 representing the order
*/
function compare_activities_by_time_asc($a, $b) {
// Make sure the activities actually have a timestamp property.
if ((!property_exists($a, 'timestamp')) && (!property_exists($b, 'timestamp'))) {
return 0;
}
// We treat instances without timestamp as if they have a timestamp of 0.
if ((!property_exists($a, 'timestamp')) && (property_exists($b, 'timestamp'))) {
return -1;
}
if ((property_exists($a, 'timestamp')) && (!property_exists($b, 'timestamp'))) {
return 1;
}
if ($a->timestamp == $b->timestamp) {
return 0;
}
return ($a->timestamp < $b->timestamp) ? -1 : 1;
}

View File

@ -272,39 +272,3 @@ if (!empty($activities)) {
echo $OUTPUT->footer();
function compare_activities_by_time_desc($a, $b) {
// make sure the activities actually have a timestamp property
if ((!array_key_exists('timestamp', $a)) && (!array_key_exists('timestamp', $b))) {
return 0;
}
// We treat instances without timestamp as if they have a timestamp of 0.
if ((!array_key_exists('timestamp', $a)) && (array_key_exists('timestamp', $b))) {
return 1;
}
if ((array_key_exists('timestamp', $a)) && (!array_key_exists('timestamp', $b))) {
return -1;
}
if ($a->timestamp == $b->timestamp) {
return 0;
}
return ($a->timestamp > $b->timestamp) ? -1 : 1;
}
function compare_activities_by_time_asc($a, $b) {
// make sure the activities actually have a timestamp property
if ((!array_key_exists('timestamp', $a)) && (!array_key_exists('timestamp', $b))) {
return 0;
}
// We treat instances without timestamp as if they have a timestamp of 0.
if ((!array_key_exists('timestamp', $a)) && (array_key_exists('timestamp', $b))) {
return -1;
}
if ((array_key_exists('timestamp', $a)) && (!array_key_exists('timestamp', $b))) {
return 1;
}
if ($a->timestamp == $b->timestamp) {
return 0;
}
return ($a->timestamp < $b->timestamp) ? -1 : 1;
}

View File

@ -1072,4 +1072,92 @@ class courselib_testcase extends advanced_testcase {
$pagetypelist = course_page_type_list($pagetype, null, null);
$this->assertEquals($pagetypelist, $testpagetypelist1);
}
public function test_compare_activities_by_time_desc() {
// Let's create some test data.
$activitiesivities = array();
$x = new stdClass();
$x->timestamp = null;
$activities[] = $x;
$x = new stdClass();
$x->timestamp = 1;
$activities[] = $x;
$x = new stdClass();
$x->timestamp = 3;
$activities[] = $x;
$x = new stdClass();
$x->timestamp = 0;
$activities[] = $x;
$x = new stdClass();
$x->timestamp = 5;
$activities[] = $x;
$x = new stdClass();
$activities[] = $x;
$x = new stdClass();
$x->timestamp = 5;
$activities[] = $x;
// Do the sorting.
usort($activities, 'compare_activities_by_time_desc');
// Let's check the result.
$last = 10;
foreach($activities as $activity) {
if (empty($activity->timestamp)) {
$activity->timestamp = 0;
}
$this->assertLessThanOrEqual($last, $activity->timestamp);
}
}
public function test_compare_activities_by_time_asc() {
// Let's create some test data.
$activities = array();
$x = new stdClass();
$x->timestamp = null;
$activities[] = $x;
$x = new stdClass();
$x->timestamp = 1;
$activities[] = $x;
$x = new stdClass();
$x->timestamp = 3;
$activities[] = $x;
$x = new stdClass();
$x->timestamp = 0;
$activities[] = $x;
$x = new stdClass();
$x->timestamp = 5;
$activities[] = $x;
$x = new stdClass();
$activities[] = $x;
$x = new stdClass();
$x->timestamp = 5;
$activities[] = $x;
// Do the sorting.
usort($activities, 'compare_activities_by_time_asc');
// Let's check the result.
$last = 0;
foreach($activities as $activity) {
if (empty($activity->timestamp)) {
$activity->timestamp = 0;
}
$this->assertGreaterThanOrEqual($last, $activity->timestamp);
}
}
}