MDL-51886 wiki: Add unit tests for view_wiki and view_page
[moodle.git] / mod / wiki / tests / lib_test.php
CommitLineData
c1baf89e
DP
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 * Unit tests for mod_wiki lib
19 *
20 * @package mod_wiki
21 * @category external
22 * @copyright 2015 Dani Palou <dani@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @since Moodle 3.1
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30require_once($CFG->dirroot . '/mod/wiki/lib.php');
31require_once($CFG->libdir . '/completionlib.php');
32
33/**
34 * Unit tests for mod_wiki lib
35 *
36 * @package mod_wiki
37 * @category external
38 * @copyright 2015 Dani Palou <juan@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 * @since Moodle 3.1
41 */
42class mod_wiki_lib_testcase extends advanced_testcase {
43
44 /**
45 * Test wiki_view.
46 *
47 * @return void
48 */
49 public function test_wiki_view() {
50 global $CFG;
51
52 $CFG->enablecompletion = COMPLETION_ENABLED;
53 $this->resetAfterTest();
54
55 $this->setAdminUser();
56 // Setup test data.
57 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => COMPLETION_ENABLED));
58 $options = array('completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => COMPLETION_VIEW_REQUIRED);
59 $wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course->id), $options);
60 $context = context_module::instance($wiki->cmid);
61 $cm = get_coursemodule_from_instance('wiki', $wiki->id);
62
63 // Trigger and capture the event.
64 $sink = $this->redirectEvents();
65
66 wiki_view($wiki, $course, $cm, $context);
67
68 $events = $sink->get_events();
69 // 2 additional events thanks to completion.
70 $this->assertCount(3, $events);
71 $event = array_shift($events);
72
73 // Checking that the event contains the expected values.
74 $this->assertInstanceOf('\mod_wiki\event\course_module_viewed', $event);
75 $this->assertEquals($context, $event->get_context());
76 $moodleurl = new \moodle_url('/mod/wiki/view.php', array('id' => $cm->id));
77 $this->assertEquals($moodleurl, $event->get_url());
78 $this->assertEventContextNotUsed($event);
79 $this->assertNotEmpty($event->get_name());
80
81 // Check completion status.
82 $completion = new completion_info($course);
83 $completiondata = $completion->get_data($cm);
84 $this->assertEquals(1, $completiondata->completionstate);
85
86 }
87
88 /**
89 * Test wiki_page_view.
90 *
91 * @return void
92 */
93 public function test_wiki_page_view() {
94 global $CFG;
95
96 $CFG->enablecompletion = COMPLETION_ENABLED;
97 $this->resetAfterTest();
98
99 $this->setAdminUser();
100 // Setup test data.
101 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => COMPLETION_ENABLED));
102 $options = array('completion' => COMPLETION_TRACKING_AUTOMATIC, 'completionview' => COMPLETION_VIEW_REQUIRED);
103 $wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course->id), $options);
104 $context = context_module::instance($wiki->cmid);
105 $cm = get_coursemodule_from_instance('wiki', $wiki->id);
106 $firstpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_first_page($wiki);
107
108 // Trigger and capture the event.
109 $sink = $this->redirectEvents();
110
111 wiki_page_view($wiki, $firstpage, $course, $cm, $context);
112
113 $events = $sink->get_events();
114 // 2 additional events thanks to completion.
115 $this->assertCount(3, $events);
116 $event = array_shift($events);
117
118 // Checking that the event contains the expected values.
119 $this->assertInstanceOf('\mod_wiki\event\page_viewed', $event);
120 $this->assertEquals($context, $event->get_context());
121 $pageurl = new \moodle_url('/mod/wiki/view.php', array('pageid' => $firstpage->id));
122 $this->assertEquals($pageurl, $event->get_url());
123 $this->assertEventContextNotUsed($event);
124 $this->assertNotEmpty($event->get_name());
125
126 // Check completion status.
127 $completion = new completion_info($course);
128 $completiondata = $completion->get_data($cm);
129 $this->assertEquals(1, $completiondata->completionstate);
130
131 }
132}