MDL-39955 completion: Added unittest for course_completed event
authorRajesh Taneja <rajesh@moodle.com>
Mon, 5 Aug 2013 03:39:12 +0000 (11:39 +0800)
committerRajesh Taneja <rajesh@moodle.com>
Tue, 13 Aug 2013 01:43:38 +0000 (09:43 +0800)
badges/tests/badgeslib_test.php
lib/tests/completionlib_test.php

index bcdaf87..fa7b7cb 100644 (file)
@@ -238,4 +238,26 @@ class core_badgeslib_testcase extends advanced_testcase {
         $this->assertDebuggingCalled('Error baking badge image!');
         $this->assertTrue($badge->is_issued($this->user->id));
     }
+
+    /**
+     * Test badges observer when course_completed event is fired.
+     */
+    public function test_badges_observer_course_criteria_review() {
+        $badge = new badge($this->coursebadge);
+        $this->assertFalse($badge->is_issued($this->user->id));
+
+        $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
+        $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
+        $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_COURSE, 'badgeid' => $badge->id));
+        $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'course_'.$this->course->id => $this->course->id));
+
+        $ccompletion = new completion_completion(array('course' => $this->course->id, 'userid' => $this->user->id));
+
+        // Mark course as complete.
+        $ccompletion->mark_complete();
+
+        // Check if badge is awarded.
+        $this->assertDebuggingCalled('Error baking badge image!');
+        $this->assertTrue($badge->is_issued($this->user->id));
+    }
 }
index b4cfe8c..112b712 100644 (file)
@@ -782,6 +782,33 @@ class core_completionlib_testcase extends advanced_testcase {
         $this->assertEventLegacyData($current, $event);
     }
 
+    /**
+     * Test course completed event.
+     */
+    public function test_course_completed_event() {
+        global $USER;
+
+        $this->setup_data();
+        $this->setAdminUser();
+
+        $completionauto = array('completion' => COMPLETION_TRACKING_AUTOMATIC);
+        $ccompletion = new completion_completion(array('course' => $this->course->id, 'userid' => $this->user->id));
+
+        // Mark course as complete and get triggered event.
+        $sink = $this->redirectEvents();
+        $ccompletion->mark_complete();
+        $events = $sink->get_events();
+        $event = reset($events);
+
+        $this->assertInstanceOf('\core\event\course_completed', $event);
+        $this->assertEquals($this->course->id, $event->get_record_snapshot('course_completions', $event->objectid)->course);
+        $this->assertEquals($this->course->id, $event->courseid);
+        $this->assertEquals($USER->id, $event->userid);
+        $this->assertEquals($this->user->id, $event->other['relateduserid']);
+        $this->assertEquals(context_course::instance($this->course->id), $event->get_context());
+        $data = $ccompletion->get_record_data();
+        $this->assertEventLegacyData($data, $event);
+    }
 }