Merge branch 'wip-MDL-57456-master' of git://github.com/marinaglancy/moodle
authorDavid Monllao <davidm@moodle.com>
Wed, 19 Apr 2017 07:42:29 +0000 (15:42 +0800)
committerDavid Monllao <davidm@moodle.com>
Wed, 19 Apr 2017 07:42:29 +0000 (15:42 +0800)
1  2 
mod/glossary/lang/en/glossary.php
mod/glossary/lib.php
mod/glossary/tests/lib_test.php

Simple merge
Simple merge
@@@ -195,41 -196,93 +196,131 @@@ class mod_glossary_lib_testcase extend
          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;
+         $this->resetAfterTest();
+         $this->setAdminUser();
+         // Setup test data.
+         $glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
+         $course3 = $this->getDataGenerator()->create_course();
+         $course2 = $this->getDataGenerator()->create_course();
+         $course1 = $this->getDataGenerator()->create_course();
+         $glossary1 = $this->getDataGenerator()->create_module('glossary', array('course' => $course1->id));
+         $glossary2 = $this->getDataGenerator()->create_module('glossary', array('course' => $course2->id));
+         $glossary3 = $this->getDataGenerator()->create_module('glossary', array('course' => $course3->id));
+         $entry11 = $glossarygenerator->create_content($glossary1, array('tags' => array('Cats', 'Dogs')));
+         $entry12 = $glossarygenerator->create_content($glossary1, array('tags' => array('Cats', 'mice')));
+         $entry13 = $glossarygenerator->create_content($glossary1, array('tags' => array('Cats')));
+         $entry14 = $glossarygenerator->create_content($glossary1);
+         $entry15 = $glossarygenerator->create_content($glossary1, array('tags' => array('Cats')));
+         $entry16 = $glossarygenerator->create_content($glossary1, array('tags' => array('Cats'), 'approved' => false));
+         $entry21 = $glossarygenerator->create_content($glossary2, array('tags' => array('Cats')));
+         $entry22 = $glossarygenerator->create_content($glossary2, array('tags' => array('Cats', 'Dogs')));
+         $entry23 = $glossarygenerator->create_content($glossary2, array('tags' => array('mice', 'Cats')));
+         $entry31 = $glossarygenerator->create_content($glossary3, array('tags' => array('mice', 'Cats')));
+         $tag = core_tag_tag::get_by_name(0, 'Cats');
+         // Admin can see everything.
+         $res = mod_glossary_get_tagged_entries($tag, /*$exclusivemode = */false,
+             /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$entry = */0);
+         $this->assertRegExp('/'.$entry11->concept.'</', $res->content);
+         $this->assertRegExp('/'.$entry12->concept.'</', $res->content);
+         $this->assertRegExp('/'.$entry13->concept.'</', $res->content);
+         $this->assertNotRegExp('/'.$entry14->concept.'</', $res->content);
+         $this->assertRegExp('/'.$entry15->concept.'</', $res->content);
+         $this->assertRegExp('/'.$entry16->concept.'</', $res->content);
+         $this->assertNotRegExp('/'.$entry21->concept.'</', $res->content);
+         $this->assertNotRegExp('/'.$entry22->concept.'</', $res->content);
+         $this->assertNotRegExp('/'.$entry23->concept.'</', $res->content);
+         $this->assertNotRegExp('/'.$entry31->concept.'</', $res->content);
+         $this->assertEmpty($res->prevpageurl);
+         $this->assertNotEmpty($res->nextpageurl);
+         $res = mod_glossary_get_tagged_entries($tag, /*$exclusivemode = */false,
+             /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$entry = */1);
+         $this->assertNotRegExp('/'.$entry11->concept.'</', $res->content);
+         $this->assertNotRegExp('/'.$entry12->concept.'</', $res->content);
+         $this->assertNotRegExp('/'.$entry13->concept.'</', $res->content);
+         $this->assertNotRegExp('/'.$entry14->concept.'</', $res->content);
+         $this->assertNotRegExp('/'.$entry15->concept.'</', $res->content);
+         $this->assertNotRegExp('/'.$entry16->concept.'</', $res->content);
+         $this->assertRegExp('/'.$entry21->concept.'</', $res->content);
+         $this->assertRegExp('/'.$entry22->concept.'</', $res->content);
+         $this->assertRegExp('/'.$entry23->concept.'</', $res->content);
+         $this->assertRegExp('/'.$entry31->concept.'</', $res->content);
+         $this->assertNotEmpty($res->prevpageurl);
+         $this->assertEmpty($res->nextpageurl);
+         // Create and enrol a user.
+         $student = self::getDataGenerator()->create_user();
+         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
+         $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id, 'manual');
+         $this->getDataGenerator()->enrol_user($student->id, $course2->id, $studentrole->id, 'manual');
+         $this->setUser($student);
+         core_tag_index_builder::reset_caches();
+         // User can not see entries in course 3 because he is not enrolled.
+         $res = mod_glossary_get_tagged_entries($tag, /*$exclusivemode = */false,
+             /*$fromctx = */0, /*$ctx = */0, /*$rec = */1, /*$entry = */1);
+         $this->assertRegExp('/'.$entry22->concept.'/', $res->content);
+         $this->assertRegExp('/'.$entry23->concept.'/', $res->content);
+         $this->assertNotRegExp('/'.$entry31->concept.'/', $res->content);
+         // User can search glossary entries inside a course.
+         $coursecontext = context_course::instance($course1->id);
+         $res = mod_glossary_get_tagged_entries($tag, /*$exclusivemode = */false,
+             /*$fromctx = */0, /*$ctx = */$coursecontext->id, /*$rec = */1, /*$entry = */0);
+         $this->assertRegExp('/'.$entry11->concept.'/', $res->content);
+         $this->assertRegExp('/'.$entry12->concept.'/', $res->content);
+         $this->assertRegExp('/'.$entry13->concept.'/', $res->content);
+         $this->assertNotRegExp('/'.$entry14->concept.'/', $res->content);
+         $this->assertRegExp('/'.$entry15->concept.'/', $res->content);
+         $this->assertNotRegExp('/'.$entry21->concept.'/', $res->content);
+         $this->assertNotRegExp('/'.$entry22->concept.'/', $res->content);
+         $this->assertNotRegExp('/'.$entry23->concept.'/', $res->content);
+         $this->assertEmpty($res->nextpageurl);
+         // User cannot see hidden entries.
+         $this->assertNotRegExp('/'.$entry16->concept.'/', $res->content);
+     }
  }