コード例 #1
0
ファイル: externallib_test.php プロジェクト: rsturley/moodle
    /**
     * Test add_discussion_post
     */
    public function test_add_discussion_post() {
        global $CFG;

        $this->resetAfterTest(true);

        $user = self::getDataGenerator()->create_user();
        $otheruser = self::getDataGenerator()->create_user();

        self::setAdminUser();

        // Create course to add the module.
        $course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0));

        // Forum with tracking off.
        $record = new stdClass();
        $record->course = $course->id;
        $forum = self::getDataGenerator()->create_module('forum', $record);
        $cm = get_coursemodule_from_id('forum', $forum->cmid, 0, false, MUST_EXIST);
        $forumcontext = context_module::instance($forum->cmid);

        // Add discussions to the forums.
        $record = new stdClass();
        $record->course = $course->id;
        $record->userid = $user->id;
        $record->forum = $forum->id;
        $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);

        // Try to post (user not enrolled).
        self::setUser($user);
        try {
            mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
            $this->fail('Exception expected due to being unenrolled from the course.');
        } catch (moodle_exception $e) {
            $this->assertEquals('requireloginerror', $e->errorcode);
        }

        $this->getDataGenerator()->enrol_user($user->id, $course->id);
        $this->getDataGenerator()->enrol_user($otheruser->id, $course->id);

        $post = mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
        $post = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $post);

        $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
        $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
        // We receive the discussion and the post.
        $this->assertEquals(2, count($posts['posts']));

        $tested = false;
        foreach ($posts['posts'] as $postel) {
            if ($post['postid'] == $postel['id']) {
                $this->assertEquals('some subject', $postel['subject']);
                $this->assertEquals('some text here...', $postel['message']);
                $tested = true;
            }
        }
        $this->assertTrue($tested);

        // Check not posting in groups the user is not member of.
        $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
        groups_add_member($group->id, $otheruser->id);

        $forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS));
        $record->forum = $forum->id;
        $record->userid = $otheruser->id;
        $record->groupid = $group->id;
        $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);

        try {
            mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
            $this->fail('Exception expected due to invalid permissions for posting.');
        } catch (moodle_exception $e) {
            // Expect debugging since we are switching context, and this is something WS_SERVER mode don't like.
            $this->assertDebuggingCalled();
            $this->assertEquals('nopostforum', $e->errorcode);
        }

    }
コード例 #2
0
 /**
  * Test add_discussion_post
  */
 public function test_add_discussion_post()
 {
     global $CFG;
     $this->resetAfterTest(true);
     $user = self::getDataGenerator()->create_user();
     $otheruser = self::getDataGenerator()->create_user();
     self::setAdminUser();
     // Create course to add the module.
     $course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0));
     // Forum with tracking off.
     $record = new stdClass();
     $record->course = $course->id;
     $forum = self::getDataGenerator()->create_module('forum', $record);
     $cm = get_coursemodule_from_id('forum', $forum->cmid, 0, false, MUST_EXIST);
     $forumcontext = context_module::instance($forum->cmid);
     // Add discussions to the forums.
     $record = new stdClass();
     $record->course = $course->id;
     $record->userid = $user->id;
     $record->forum = $forum->id;
     $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
     // Try to post (user not enrolled).
     self::setUser($user);
     try {
         mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
         $this->fail('Exception expected due to being unenrolled from the course.');
     } catch (moodle_exception $e) {
         $this->assertEquals('requireloginerror', $e->errorcode);
     }
     $this->getDataGenerator()->enrol_user($user->id, $course->id);
     $this->getDataGenerator()->enrol_user($otheruser->id, $course->id);
     $createdpost = mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
     $createdpost = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost);
     $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
     $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
     // We receive the discussion and the post.
     $this->assertEquals(2, count($posts['posts']));
     $tested = false;
     foreach ($posts['posts'] as $thispost) {
         if ($createdpost['postid'] == $thispost['id']) {
             $this->assertEquals('some subject', $thispost['subject']);
             $this->assertEquals('some text here...', $thispost['message']);
             $tested = true;
         }
     }
     $this->assertTrue($tested);
     // Test inline and regular attachment in post
     // Create a file in a draft area for inline attachments.
     $draftidinlineattach = file_get_unused_draft_itemid();
     $draftidattach = file_get_unused_draft_itemid();
     self::setUser($user);
     $usercontext = context_user::instance($user->id);
     $filepath = '/';
     $filearea = 'draft';
     $component = 'user';
     $filenameimg = 'shouldbeanimage.txt';
     $filerecordinline = array('contextid' => $usercontext->id, 'component' => $component, 'filearea' => $filearea, 'itemid' => $draftidinlineattach, 'filepath' => $filepath, 'filename' => $filenameimg);
     $fs = get_file_storage();
     // Create a file in a draft area for regular attachments.
     $filerecordattach = $filerecordinline;
     $attachfilename = 'attachment.txt';
     $filerecordattach['filename'] = $attachfilename;
     $filerecordattach['itemid'] = $draftidattach;
     $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
     $fs->create_file_from_string($filerecordattach, 'simple text attachment');
     $options = array(array('name' => 'inlineattachmentsid', 'value' => $draftidinlineattach), array('name' => 'attachmentsid', 'value' => $draftidattach));
     $dummytext = 'Here is an inline image: <img src="' . $CFG->wwwroot . "/draftfile.php/{$usercontext->id}/user/draft/{$draftidinlineattach}/{$filenameimg}" . '" alt="inlineimage">.';
     $createdpost = mod_forum_external::add_discussion_post($discussion->firstpost, 'new post inline attachment', $dummytext, $options);
     $createdpost = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost);
     $posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
     $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
     // We receive the discussion and the post.
     // Can't guarantee order of posts during tests.
     $postfound = false;
     foreach ($posts['posts'] as $thispost) {
         if ($createdpost['postid'] == $thispost['id']) {
             $this->assertEquals($createdpost['postid'], $thispost['id']);
             $this->assertEquals($thispost['attachment'], 1, "There should be a non-inline attachment");
             $this->assertCount(1, $thispost['attachments'], "There should be 1 attachment");
             $this->assertEquals($thispost['attachments'][0]['filename'], $attachfilename, "There should be 1 attachment");
             $this->assertContains('pluginfile.php', $thispost['message']);
             $postfound = true;
             break;
         }
     }
     $this->assertTrue($postfound);
     // Check not posting in groups the user is not member of.
     $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
     groups_add_member($group->id, $otheruser->id);
     $forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS));
     $record->forum = $forum->id;
     $record->userid = $otheruser->id;
     $record->groupid = $group->id;
     $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
     try {
         mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
         $this->fail('Exception expected due to invalid permissions for posting.');
     } catch (moodle_exception $e) {
         $this->assertEquals('nopostforum', $e->errorcode);
     }
 }