MDL-53703 wiki: Add tests for mod_wiki_get_subwiki_files
authorDani Palou <dani@moodle.com>
Tue, 5 Apr 2016 06:59:43 +0000 (08:59 +0200)
committerDani Palou <dani@moodle.com>
Mon, 11 Apr 2016 09:13:02 +0000 (11:13 +0200)
mod/wiki/tests/externallib_test.php

index 3098ff0..28b7a6a 100644 (file)
@@ -1012,4 +1012,119 @@ class mod_wiki_external_testcase extends externallib_advanced_testcase {
         $this->assertEquals($expectedfpsepg1indstu, $result['page']);
     }
 
+    /**
+     * Test get_subwiki_files using a wiki without files.
+     */
+    public function test_get_subwiki_files_no_files() {
+        $result = mod_wiki_external::get_subwiki_files($this->wiki->id);
+        $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result);
+        $this->assertCount(0, $result['files']);
+        $this->assertCount(0, $result['warnings']);
+    }
+
+    /**
+     * Test get_subwiki_files, check that a student can't get files from another group's subwiki when
+     * using separate groups.
+     */
+    public function test_get_subwiki_files_separate_groups_student_see_other_group() {
+        // Create testing data.
+        $this->create_collaborative_wikis_with_groups();
+
+        $this->setUser($this->student);
+        $this->setExpectedException('moodle_exception');
+        mod_wiki_external::get_subwiki_files($this->wikisep->id, $this->group2->id);
+    }
+
+    /**
+     * Test get_subwiki_files using a collaborative wiki without groups.
+     */
+    public function test_get_subwiki_files_collaborative_no_groups() {
+        $this->setUser($this->student);
+
+        // Add a file as subwiki attachment.
+        $fs = get_file_storage();
+        $file = array('component' => 'mod_wiki', 'filearea' => 'attachments',
+                'contextid' => $this->context->id, 'itemid' => $this->firstpage->subwikiid,
+                'filename' => 'image.jpg', 'filepath' => '/', 'timemodified' => time());
+        $content = 'IMAGE';
+        $fs->create_file_from_string($file, $content);
+
+        $expectedfile = array(
+            'filename' => $file['filename'],
+            'filepath' => $file['filepath'],
+            'mimetype' => 'image/jpeg',
+            'filesize' => strlen($content),
+            'timemodified' => $file['timemodified'],
+            'fileurl' => moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'],
+                            $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']),
+        );
+
+        // Call the WS and check that it returns this file.
+        $result = mod_wiki_external::get_subwiki_files($this->wiki->id);
+        $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result);
+        $this->assertCount(1, $result['files']);
+        $this->assertEquals($expectedfile, $result['files'][0]);
+
+        // Now add another file to the same subwiki.
+        $file['filename'] = 'Another image.jpg';
+        $file['timemodified'] = time();
+        $content = 'ANOTHER IMAGE';
+        $fs->create_file_from_string($file, $content);
+
+        $expectedfile['filename'] = $file['filename'];
+        $expectedfile['timemodified'] = $file['timemodified'];
+        $expectedfile['filesize'] = strlen($content);
+        $expectedfile['fileurl'] = moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'],
+                            $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']);
+
+        // Call the WS and check that it returns both files file.
+        $result = mod_wiki_external::get_subwiki_files($this->wiki->id);
+        $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result);
+        $this->assertCount(2, $result['files']);
+        // The new file is returned first because they're returned in alphabetical order.
+        $this->assertEquals($expectedfile, $result['files'][0]);
+    }
+
+    /**
+     * Test get_subwiki_files using an individual wiki with visible groups.
+     */
+    public function test_get_subwiki_files_visible_groups_individual() {
+        // Create testing data.
+        $this->create_individual_wikis_with_groups();
+
+        $this->setUser($this->student);
+
+        // Add a file as subwiki attachment in the student group 1 subwiki.
+        $fs = get_file_storage();
+        $contextwiki = context_module::instance($this->wikivisind->cmid);
+        $file = array('component' => 'mod_wiki', 'filearea' => 'attachments',
+                'contextid' => $contextwiki->id, 'itemid' => $this->fpvisg1indstu->subwikiid,
+                'filename' => 'image.jpg', 'filepath' => '/', 'timemodified' => time());
+        $content = 'IMAGE';
+        $fs->create_file_from_string($file, $content);
+
+        $expectedfile = array(
+            'filename' => $file['filename'],
+            'filepath' => $file['filepath'],
+            'mimetype' => 'image/jpeg',
+            'filesize' => strlen($content),
+            'timemodified' => $file['timemodified'],
+            'fileurl' => moodle_url::make_webservice_pluginfile_url($file['contextid'], $file['component'],
+                            $file['filearea'], $file['itemid'], $file['filepath'], $file['filename']),
+        );
+
+        // Call the WS and check that it returns this file.
+        $result = mod_wiki_external::get_subwiki_files($this->wikivisind->id, $this->group1->id);
+        $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result);
+        $this->assertCount(1, $result['files']);
+        $this->assertEquals($expectedfile, $result['files'][0]);
+
+        // Now check that a teacher can see it too.
+        $this->setUser($this->teacher);
+        $result = mod_wiki_external::get_subwiki_files($this->wikivisind->id, $this->group1->id, $this->student->id);
+        $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_files_returns(), $result);
+        $this->assertCount(1, $result['files']);
+        $this->assertEquals($expectedfile, $result['files'][0]);
+    }
+
 }