Merge branch 'MDL-41200' of https://github.com/rwijaya/moodle
[moodle.git] / mod / forum / tests / lib_test.php
CommitLineData
62b3b997
FM
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * The module forums tests
19 *
20 * @package mod_forum
21 * @copyright 2013 Frédéric Massart
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27class mod_forum_lib_testcase extends advanced_testcase {
28
29 public function test_forum_trigger_content_uploaded_event() {
30 $this->resetAfterTest();
31
32 $user = $this->getDataGenerator()->create_user();
33 $course = $this->getDataGenerator()->create_course();
34 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
35 $context = context_module::instance($forum->cmid);
36
37 $this->setUser($user->id);
38 $fakepost = (object) array('id' => 123, 'message' => 'Yay!', 'discussion' => 100);
39 $cm = get_coursemodule_from_instance('forum', $forum->cmid);
40
41 $fs = get_file_storage();
42 $dummy = (object) array(
43 'contextid' => $context->id,
44 'component' => 'mod_forum',
45 'filearea' => 'attachment',
46 'itemid' => $fakepost->id,
47 'filepath' => '/',
48 'filename' => 'myassignmnent.pdf'
49 );
50 $fi = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
51
52 $data = new stdClass();
53 $sink = $this->redirectEvents();
54 forum_trigger_content_uploaded_event($fakepost, $cm, 'some triggered from value');
55 $events = $sink->get_events();
56
57 $this->assertCount(1, $events);
58 $event = reset($events);
59 $this->assertInstanceOf('\mod_forum\event\assessable_uploaded', $event);
60 $this->assertEquals($context->id, $event->contextid);
61 $this->assertEquals($fakepost->id, $event->objectid);
62 $this->assertEquals($fakepost->message, $event->other['content']);
63 $this->assertEquals($fakepost->discussion, $event->other['discussionid']);
64 $this->assertCount(1, $event->other['pathnamehashes']);
65 $this->assertEquals($fi->get_pathnamehash(), $event->other['pathnamehashes'][0]);
66 $expected = new stdClass();
67 $expected->modulename = 'forum';
68 $expected->name = 'some triggered from value';
69 $expected->cmid = $forum->cmid;
70 $expected->itemid = $fakepost->id;
71 $expected->courseid = $course->id;
72 $expected->userid = $user->id;
73 $expected->content = $fakepost->message;
74 $expected->pathnamehashes = array($fi->get_pathnamehash());
75 $this->assertEventLegacyData($expected, $event);
76 }
77
78}