MDL-52809 wiki: Add tests for mod_wiki_get_subwikis
authorDani Palou <dani@moodle.com>
Thu, 4 Feb 2016 08:58:28 +0000 (09:58 +0100)
committerDani Palou <dani@moodle.com>
Fri, 26 Feb 2016 13:11:24 +0000 (14:11 +0100)
mod/wiki/tests/externallib_test.php
mod/wiki/tests/lib_test.php

index 978a841..06c3ace 100644 (file)
@@ -315,4 +315,61 @@ class mod_wiki_external_testcase extends externallib_advanced_testcase {
 
     }
 
+    /**
+     * Test get_subwikis.
+     */
+    public function test_get_subwikis() {
+
+        // Test invalid wiki id.
+        try {
+            mod_wiki_external::get_subwikis(0);
+            $this->fail('Exception expected due to invalid get_subwikis wiki id.');
+        } catch (moodle_exception $e) {
+            $this->assertEquals('incorrectwikiid', $e->errorcode);
+        }
+
+        // Test not-enrolled user.
+        $usernotenrolled = self::getDataGenerator()->create_user();
+        $this->setUser($usernotenrolled);
+        try {
+            mod_wiki_external::get_subwikis($this->wiki->id);
+            $this->fail('Exception expected due to not enrolled user.');
+        } catch (moodle_exception $e) {
+            $this->assertEquals('requireloginerror', $e->errorcode);
+        }
+
+        // Test user with full capabilities.
+        $this->setUser($this->student);
+
+        // Create what we expect to be returned. We only test a basic case because deep testing is already done
+        // in the tests for wiki_get_visible_subwikis.
+        $expectedsubwikis = array();
+        $expectedsubwiki = array(
+                'id' => $this->firstpage->subwikiid,
+                'wikiid' => $this->wiki->id,
+                'groupid' => 0,
+                'userid' => 0,
+                'canedit' => true
+            );
+        $expectedsubwikis[] = $expectedsubwiki;
+
+        $result = mod_wiki_external::get_subwikis($this->wiki->id);
+        $result = external_api::clean_returnvalue(mod_wiki_external::get_subwikis_returns(), $result);
+        $this->assertEquals($expectedsubwikis, $result['subwikis']);
+        $this->assertCount(0, $result['warnings']);
+
+        // Test user with no capabilities.
+        // We need a explicit prohibit since this capability is allowed for students by default.
+        assign_capability('mod/wiki:viewpage', CAP_PROHIBIT, $this->studentrole->id, $this->context->id);
+        accesslib_clear_all_caches_for_unit_testing();
+
+        try {
+            mod_wiki_external::get_subwikis($this->wiki->id);
+            $this->fail('Exception expected due to missing capability.');
+        } catch (moodle_exception $e) {
+            $this->assertEquals('nopermissions', $e->errorcode);
+        }
+
+    }
+
 }
index 7fc076c..8f01047 100644 (file)
@@ -28,6 +28,7 @@ defined('MOODLE_INTERNAL') || die();
 
 global $CFG;
 require_once($CFG->dirroot . '/mod/wiki/lib.php');
+require_once($CFG->dirroot . '/mod/wiki/locallib.php');
 require_once($CFG->libdir . '/completionlib.php');
 
 /**
@@ -35,7 +36,7 @@ require_once($CFG->libdir . '/completionlib.php');
  *
  * @package    mod_wiki
  * @category   external
- * @copyright  2015 Dani Palou <juan@moodle.com>
+ * @copyright  2015 Dani Palou <dani@moodle.com>
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  * @since      Moodle 3.1
  */
@@ -391,4 +392,266 @@ class mod_wiki_lib_testcase extends advanced_testcase {
         $this->assertTrue(wiki_user_can_edit($swvisindg2s2));
         $this->assertTrue(wiki_user_can_edit($swvisindteacher));
     }
+
+    /**
+     * Test wiki_get_visible_subwikis without groups.
+     *
+     * @return void
+     */
+    public function test_wiki_get_visible_subwikis_without_groups() {
+        global $DB;
+
+        $this->resetAfterTest();
+        $this->setAdminUser();
+
+        // Setup test data.
+        $course = $this->getDataGenerator()->create_course();
+        $indwiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course->id, 'wikimode' => 'individual'));
+        $colwiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course->id, 'wikimode' => 'collaborative'));
+
+        // Create users.
+        $student = self::getDataGenerator()->create_user();
+        $teacher = self::getDataGenerator()->create_user();
+
+        // Users enrolments.
+        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
+        $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
+        $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
+        $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id, 'manual');
+
+        $this->setUser($student);
+
+        // Check that not passing a wiki returns empty array.
+        $result = wiki_get_visible_subwikis(null);
+        $this->assertEquals(array(), $result);
+
+        // Check that the student can get the only subwiki from the collaborative wiki.
+        $expectedsubwikis = array();
+        $expectedsubwiki = new stdClass();
+        $expectedsubwiki->id = -1; // We haven't created any page so the subwiki hasn't been created.
+        $expectedsubwiki->wikiid = $colwiki->id;
+        $expectedsubwiki->groupid = 0;
+        $expectedsubwiki->userid = 0;
+        $expectedsubwikis[] = $expectedsubwiki;
+
+        $result = wiki_get_visible_subwikis($colwiki);
+        $this->assertEquals($expectedsubwikis, $result);
+
+        // Create a page now so the subwiki is created.
+        $colfirstpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_first_page($colwiki);
+
+        // Call the function again, now we expect to have a subwiki ID.
+        $expectedsubwikis[0]->id = $colfirstpage->subwikiid;
+        $result = wiki_get_visible_subwikis($colwiki);
+        $this->assertEquals($expectedsubwikis, $result);
+
+        // Check that the teacher can see it too.
+        $this->setUser($teacher);
+        $result = wiki_get_visible_subwikis($colwiki);
+        $this->assertEquals($expectedsubwikis, $result);
+
+        // Check that the student can only see his subwiki in the individual wiki.
+        $this->setUser($student);
+        $expectedsubwikis[0]->id = -1;
+        $expectedsubwikis[0]->wikiid = $indwiki->id;
+        $expectedsubwikis[0]->userid = $student->id;
+        $result = wiki_get_visible_subwikis($indwiki);
+        $this->assertEquals($expectedsubwikis, $result);
+
+        // Check that the teacher can see his subwiki and the student subwiki in the individual wiki.
+        $this->setUser($teacher);
+        $teachersubwiki = new stdClass();
+        $teachersubwiki->id = -1;
+        $teachersubwiki->wikiid = $indwiki->id;
+        $teachersubwiki->groupid = 0;
+        $teachersubwiki->userid = $teacher->id;
+        $expectedsubwikis[] = $teachersubwiki;
+
+        $result = wiki_get_visible_subwikis($indwiki);
+        $this->assertEquals($expectedsubwikis, $result, '', 0, 10, true); // Compare without order.
+    }
+
+    /**
+     * Test wiki_get_visible_subwikis using collaborative wikis with groups.
+     *
+     * @return void
+     */
+    public function test_wiki_get_visible_subwikis_with_groups_collaborative() {
+        global $DB;
+
+        $this->resetAfterTest();
+        $this->setAdminUser();
+
+        // Setup test data.
+        $course = $this->getDataGenerator()->create_course();
+        $wikisepcol = $this->getDataGenerator()->create_module('wiki', array('course' => $course->id,
+                                                        'groupmode' => SEPARATEGROUPS, 'wikimode' => 'collaborative'));
+        $wikiviscol = $this->getDataGenerator()->create_module('wiki', array('course' => $course->id,
+                                                        'groupmode' => VISIBLEGROUPS, 'wikimode' => 'collaborative'));
+
+        // Create users.
+        $student = self::getDataGenerator()->create_user();
+        $student2 = self::getDataGenerator()->create_user();
+        $student3 = self::getDataGenerator()->create_user();
+        $teacher = self::getDataGenerator()->create_user();
+
+        // Users enrolments.
+        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
+        $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
+        $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
+        $this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id, 'manual');
+        $this->getDataGenerator()->enrol_user($student3->id, $course->id, $studentrole->id, 'manual');
+        $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id, 'manual');
+
+        // Create groups.
+        $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
+        $this->getDataGenerator()->create_group_member(array('userid' => $student->id, 'groupid' => $group1->id));
+        $this->getDataGenerator()->create_group_member(array('userid' => $student2->id, 'groupid' => $group1->id));
+        $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
+        $this->getDataGenerator()->create_group_member(array('userid' => $student2->id, 'groupid' => $group2->id));
+        $this->getDataGenerator()->create_group_member(array('userid' => $student3->id, 'groupid' => $group2->id));
+
+        $this->setUser($student);
+
+        // Create all the possible subwikis. We haven't created any page so ids will be -1.
+        // Subwikis in collaborative wikis: 1 subwiki per group + 1 subwiki for all participants.
+        $swsepcolg1 = new stdClass();
+        $swsepcolg1->id = -1;
+        $swsepcolg1->wikiid = $wikisepcol->id;
+        $swsepcolg1->groupid = $group1->id;
+        $swsepcolg1->userid = 0;
+
+        $swsepcolg2 = clone($swsepcolg1);
+        $swsepcolg2->groupid = $group2->id;
+
+        $swsepcolallparts = clone($swsepcolg1); // All participants.
+        $swsepcolallparts->groupid = 0;
+
+        $swviscolg1 = clone($swsepcolg1);
+        $swviscolg1->wikiid = $wikiviscol->id;
+
+        $swviscolg2 = clone($swviscolg1);
+        $swviscolg2->groupid = $group2->id;
+
+        $swviscolallparts = clone($swviscolg1); // All participants.
+        $swviscolallparts->groupid = 0;
+
+        // Check that the student can get only the subwiki from his group in collaborative wiki with separate groups.
+        $expectedsubwikis = array($swsepcolg1);
+        $result = wiki_get_visible_subwikis($wikisepcol);
+        $this->assertEquals($expectedsubwikis, $result);
+
+        // Check that he can get subwikis from both groups in collaborative wiki with visible groups, and also all participants.
+        $expectedsubwikis = array($swviscolallparts, $swviscolg1, $swviscolg2);
+        $result = wiki_get_visible_subwikis($wikiviscol);
+        $this->assertEquals($expectedsubwikis, $result, '', 0, 10, true);
+
+        // Now test it as a teacher. No need to check visible groups wikis because the result is the same as student.
+        $this->setUser($teacher);
+
+        // Check that he can get the subwikis from all the groups in collaborative wiki with separate groups.
+        $expectedsubwikis = array($swsepcolg1, $swsepcolg2, $swsepcolallparts);
+        $result = wiki_get_visible_subwikis($wikisepcol);
+        $this->assertEquals($expectedsubwikis, $result, '', 0, 10, true);
+    }
+
+    /**
+     * Test wiki_get_visible_subwikis using individual wikis with groups.
+     *
+     * @return void
+     */
+    public function test_wiki_get_visible_subwikis_with_groups_individual() {
+        global $DB;
+
+        $this->resetAfterTest();
+        $this->setAdminUser();
+
+        // Setup test data.
+        $course = $this->getDataGenerator()->create_course();
+        $wikisepind = $this->getDataGenerator()->create_module('wiki', array('course' => $course->id,
+                                                        'groupmode' => SEPARATEGROUPS, 'wikimode' => 'individual'));
+        $wikivisind = $this->getDataGenerator()->create_module('wiki', array('course' => $course->id,
+                                                        'groupmode' => VISIBLEGROUPS, 'wikimode' => 'individual'));
+
+        // Create users.
+        $student = self::getDataGenerator()->create_user();
+        $student2 = self::getDataGenerator()->create_user();
+        $student3 = self::getDataGenerator()->create_user();
+        $teacher = self::getDataGenerator()->create_user();
+
+        // Users enrolments.
+        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
+        $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
+        $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
+        $this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id, 'manual');
+        $this->getDataGenerator()->enrol_user($student3->id, $course->id, $studentrole->id, 'manual');
+        $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id, 'manual');
+
+        // Create groups.
+        $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
+        $this->getDataGenerator()->create_group_member(array('userid' => $student->id, 'groupid' => $group1->id));
+        $this->getDataGenerator()->create_group_member(array('userid' => $student2->id, 'groupid' => $group1->id));
+        $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
+        $this->getDataGenerator()->create_group_member(array('userid' => $student2->id, 'groupid' => $group2->id));
+        $this->getDataGenerator()->create_group_member(array('userid' => $student3->id, 'groupid' => $group2->id));
+
+        $this->setUser($student);
+
+        // Create all the possible subwikis to be returned. We haven't created any page so ids will be -1.
+        // Subwikis in individual wikis: 1 subwiki per user and group. If user doesn't belong to any group then groupid is 0.
+        $swsepindg1s1 = new stdClass();
+        $swsepindg1s1->id = -1;
+        $swsepindg1s1->wikiid = $wikisepind->id;
+        $swsepindg1s1->groupid = $group1->id;
+        $swsepindg1s1->userid = $student->id;
+
+        $swsepindg1s2 = clone($swsepindg1s1);
+        $swsepindg1s2->userid = $student2->id;
+
+        $swsepindg2s2 = clone($swsepindg1s2);
+        $swsepindg2s2->groupid = $group2->id;
+
+        $swsepindg2s3 = clone($swsepindg1s1);
+        $swsepindg2s3->userid = $student3->id;
+        $swsepindg2s3->groupid = $group2->id;
+
+        $swsepindteacher = clone($swsepindg1s1);
+        $swsepindteacher->userid = $teacher->id;
+        $swsepindteacher->groupid = 0;
+
+        $swvisindg1s1 = clone($swsepindg1s1);
+        $swvisindg1s1->wikiid = $wikivisind->id;
+
+        $swvisindg1s2 = clone($swvisindg1s1);
+        $swvisindg1s2->userid = $student2->id;
+
+        $swvisindg2s2 = clone($swvisindg1s2);
+        $swvisindg2s2->groupid = $group2->id;
+
+        $swvisindg2s3 = clone($swvisindg1s1);
+        $swvisindg2s3->userid = $student3->id;
+        $swvisindg2s3->groupid = $group2->id;
+
+        $swvisindteacher = clone($swvisindg1s1);
+        $swvisindteacher->userid = $teacher->id;
+        $swvisindteacher->groupid = 0;
+
+        // Check that student can get the subwikis from his group in individual wiki with separate groups.
+        $expectedsubwikis = array($swsepindg1s1, $swsepindg1s2);
+        $result = wiki_get_visible_subwikis($wikisepind);
+        $this->assertEquals($expectedsubwikis, $result, '', 0, 10, true);
+
+        // Check that he can get subwikis from all users and groups in individual wiki with visible groups.
+        $expectedsubwikis = array($swvisindg1s1, $swvisindg1s2, $swvisindg2s2, $swvisindg2s3, $swvisindteacher);
+        $result = wiki_get_visible_subwikis($wikivisind);
+        $this->assertEquals($expectedsubwikis, $result, '', 0, 10, true);
+
+        // Now test it as a teacher. No need to check visible groups wikis because the result is the same as student.
+        $this->setUser($teacher);
+
+        // Check that teacher can get the subwikis from all the groups in individual wiki with separate groups.
+        $expectedsubwikis = array($swsepindg1s1, $swsepindg1s2, $swsepindg2s2, $swsepindg2s3, $swsepindteacher);
+        $result = wiki_get_visible_subwikis($wikisepind);
+        $this->assertEquals($expectedsubwikis, $result, '', 0, 10, true);
+    }
 }