Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-38564 Adding unit tests for the functions compare_activities_by_t…

…ime_asc() and compare_activities_by_time_desc()
  • Loading branch information...
commit bf6b3c7a8aff02104fe49e541f2b9490d5e7ba3c 1 parent 373a8e0
Ankit Agarwal ankitagarwal authored
Showing with 139 additions and 36 deletions.
  1. +51 −0 course/lib.php
  2. +0 −36 course/recent.php
  3. +88 −0 course/tests/courselib_test.php
51 course/lib.php
View
@@ -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;
+}
36 course/recent.php
View
@@ -272,39 +272,3 @@
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;
-}
-
88 course/tests/courselib_test.php
View
@@ -1072,4 +1072,92 @@ public function test_course_page_type_list() {
$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);
+ }
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.