Merge branch 'wip-MDL-57456-master' of git://github.com/marinaglancy/moodle
[moodle.git] / mod / glossary / tests / lib_test.php
index aa5baf5..28b0fcb 100644 (file)
@@ -196,6 +196,44 @@ class mod_glossary_lib_testcase extends advanced_testcase {
         return calendar_event::create($event);
     }
 
+    /**
+     * Test the callback responsible for returning the completion rule descriptions.
+     * This function should work given either an instance of the module (cm_info), such as when checking the active rules,
+     * or if passed a stdClass of similar structure, such as when checking the the default completion settings for a mod type.
+     */
+    public function test_mod_glossary_completion_get_active_rule_descriptions() {
+        $this->resetAfterTest();
+        $this->setAdminUser();
+
+        // Two activities, both with automatic completion. One has the 'completionsubmit' rule, one doesn't.
+        $course = $this->getDataGenerator()->create_course(['enablecompletion' => 2]);
+        $glossary1 = $this->getDataGenerator()->create_module('glossary', [
+            'course' => $course->id,
+            'completion' => 2,
+            'completionentries' => 3
+        ]);
+        $glossary2 = $this->getDataGenerator()->create_module('glossary', [
+            'course' => $course->id,
+            'completion' => 2,
+            'completionentries' => 0
+        ]);
+        $cm1 = cm_info::create(get_coursemodule_from_instance('glossary', $glossary1->id));
+        $cm2 = cm_info::create(get_coursemodule_from_instance('glossary', $glossary2->id));
+
+        // Data for the stdClass input type.
+        // This type of input would occur when checking the default completion rules for an activity type, where we don't have
+        // any access to cm_info, rather the input is a stdClass containing completion and customdata attributes, just like cm_info.
+        $moddefaults = new stdClass();
+        $moddefaults->customdata = ['customcompletionrules' => ['completionentries' => 3]];
+        $moddefaults->completion = 2;
+
+        $activeruledescriptions = [get_string('completionentriesdesc', 'glossary', $glossary1->completionentries)];
+        $this->assertEquals(mod_glossary_get_completion_active_rule_descriptions($cm1), $activeruledescriptions);
+        $this->assertEquals(mod_glossary_get_completion_active_rule_descriptions($cm2), []);
+        $this->assertEquals(mod_glossary_get_completion_active_rule_descriptions($moddefaults), $activeruledescriptions);
+        $this->assertEquals(mod_glossary_get_completion_active_rule_descriptions(new stdClass()), []);
+    }
+
     public function test_mod_glossary_get_tagged_entries() {
         global $DB;