MDL-65328 phpunit: make test immune to (lack of) ordering
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Sat, 13 Apr 2019 10:43:31 +0000 (12:43 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Sat, 13 Apr 2019 10:43:31 +0000 (12:43 +0200)
lib/tests/targets_test.php

index 0250731..4c55dc7 100644 (file)
@@ -396,12 +396,22 @@ class core_analytics_targets_testcase extends advanced_testcase {
         $dg->enrol_user($student2->id, $course1->id, $studentrole->id);
         $dg->enrol_user($student3->id, $course1->id, $studentrole->id);
 
+        // get_all_samples() does not guarantee any order, so let's
+        // explicitly define the expectations here for later comparing.
+        // Expectations format being array($userid => expectation, ...)
+        $expectations = [];
+
         $courseitem = grade_item::fetch_course_item($course1->id);
-        // Student1 fails.
+        // Student1 (< gradepass) fails, so it's non achieved sample.
         $courseitem->update_final_grade($student1->id, 30);
-        // Student2 pass.
+        $expectations[$student1->id] = 1;
+
+        // Student2 (> gradepass) passes, so it's achieved sample.
         $courseitem->update_final_grade($student2->id, 60);
-        // Student 3 has no grade.
+        $expectations[$student2->id] = 0;
+
+        // Student 3 (has no grade) fails, so it's non achieved sample.
+        $expectations[$student3->id] = 1;
 
         $courseitem->gradepass = 50;
         $DB->update_record('grade_items', $courseitem);
@@ -417,26 +427,13 @@ class core_analytics_targets_testcase extends advanced_testcase {
         list($sampleids, $samplesdata) = $method->invoke($analyser, $analysable);
         $target->add_sample_data($samplesdata);
 
-        // Users in array $sampleids are sorted by user id, so student1 is the first sample.
-        $sampleid = reset($sampleids);
-
         $class = new ReflectionClass('\core\analytics\target\course_gradetopass');
         $method = $class->getMethod('calculate_sample');
         $method->setAccessible(true);
 
-        // Method calculate_sample() returns 1 when the user has not successfully graded to pass the course.
-        $this->assertEquals(1, $method->invoke($target, $sampleid, $analysable));
-
-        // Student2.
-        $sampleid = next($sampleids);
-
-        // Method calculate_sample() returns 0 when the user has successfully graded to pass the course.
-        $this->assertEquals(0, $method->invoke($target, $sampleid, $analysable));
-
-        // Student3.
-        $sampleid = next($sampleids);
-
-        // Method calculate_sample() returns 1 when the user has not been graded.
-        $this->assertEquals(1, $method->invoke($target, $sampleid, $analysable));
+        // Verify all the expectations are fulfilled.
+        foreach ($sampleids as $sampleid => $key) {
+            $this->assertEquals($expectations[$samplesdata[$key]['user']->id], $method->invoke($target, $sampleid, $analysable));
+        }
     }
 }