Merge branch 'MDL-72498' of git://github.com/paulholden/moodle
[moodle.git] / course / tests / courserequest_test.php
blobfa76479026cd598236e095ff773fe810ee465c32
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Course request related unit tests
21 * @package core
22 * @category phpunit
23 * @copyright 2012 Frédéric Massart
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
30 require_once($CFG->dirroot.'/course/lib.php');
32 class core_course_courserequest_testcase extends advanced_testcase {
34 public function test_create_request() {
35 global $DB, $USER;
36 $this->resetAfterTest(true);
38 $defaultcategory = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
39 set_config('enablecourserequests', 1);
40 set_config('lockrequestcategory', 1);
41 set_config('defaultrequestcategory', $defaultcategory);
43 // Create some categories.
44 $cat1 = $this->getDataGenerator()->create_category();
45 $cat2 = $this->getDataGenerator()->create_category();
46 $cat3 = $this->getDataGenerator()->create_category();
48 // Basic course request.
49 $data = new stdClass();
50 $data->fullname = 'Həllo World!';
51 $data->shortname = 'Hi th€re!';
52 $data->summary_editor['text'] = 'Lorem Ipsum ©';
53 $data->summary_editor['format'] = FORMAT_HTML;
54 $data->reason = 'Because PHP Unit is cool.';
55 $cr = course_request::create($data);
57 $this->assertEquals($data->fullname, $cr->fullname);
58 $this->assertEquals($data->shortname, $cr->shortname);
59 $this->assertEquals($data->summary_editor['text'], $cr->summary);
60 $this->assertEquals($data->summary_editor['format'], $cr->summaryformat);
61 $this->assertEquals($data->reason, $cr->reason);
62 $this->assertEquals($USER->id, $cr->requester);
63 $this->assertEquals($defaultcategory, $cr->category);
65 // Request with category but category selection not allowed.
66 set_config('defaultrequestcategory', $cat2->id);
67 $data->category = $cat1->id;
68 $cr = course_request::create($data);
69 $this->assertEquals($cat2->id, $cr->category);
71 // Request with category different than default and category selection allowed.
72 set_config('defaultrequestcategory', $cat3->id);
73 set_config('lockrequestcategory', 0);
74 $data->category = $cat1->id;
75 $cr = course_request::create($data);
76 $this->assertEquals($cat1->id, $cr->category);
79 public function test_approve_request() {
80 global $DB;
81 $this->resetAfterTest(true);
82 $this->preventResetByRollback();
84 $defaultcategory = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
85 set_config('enablecourserequests', 1);
86 set_config('lockrequestcategory', 1);
87 set_config('defaultrequestcategory', $defaultcategory);
89 // Create some categories.
90 $cat1 = $this->getDataGenerator()->create_category();
91 $cat2 = $this->getDataGenerator()->create_category();
93 // Create a user and allow course requests for him.
94 $requester = $this->getDataGenerator()->create_user();
95 $roleid = create_role('Course requestor role', 'courserequestor', '');
96 assign_capability('moodle/course:request', CAP_ALLOW, $roleid,
97 context_system::instance()->id);
98 role_assign($roleid, $requester->id, context_system::instance()->id);
99 accesslib_clear_all_caches_for_unit_testing();
101 $data = new stdClass();
102 $data->fullname = 'Həllo World!';
103 $data->shortname = 'Hi th€re!';
104 $data->summary_editor['text'] = 'Lorem Ipsum ©';
105 $data->summary_editor['format'] = FORMAT_HTML;
106 $data->reason = 'Because PHP Unit is cool.';
108 // Test without category.
109 $this->setUser($requester);
110 $cr = course_request::create($data);
111 $this->setAdminUser();
112 $sink = $this->redirectMessages();
113 $id = $cr->approve();
114 $this->assertCount(1, $sink->get_messages());
115 $sink->close();
116 $course = $DB->get_record('course', array('id' => $id));
117 $this->assertEquals($data->fullname, $course->fullname);
118 $this->assertEquals($data->shortname, $course->shortname);
119 $this->assertEquals($data->summary_editor['text'], $course->summary);
120 $this->assertEquals($data->summary_editor['format'], $course->summaryformat);
121 $this->assertEquals(1, $course->requested);
122 $this->assertEquals($defaultcategory, $course->category);
124 // Test with category.
125 set_config('lockrequestcategory', 0);
126 set_config('defaultrequestcategory', $cat2->id);
127 $data->shortname .= ' 2nd';
128 $data->category = $cat1->id;
129 $this->setUser($requester);
130 $cr = course_request::create($data);
131 $this->setAdminUser();
132 $sink = $this->redirectMessages();
133 $id = $cr->approve();
134 $this->assertCount(1, $sink->get_messages());
135 $sink->close();
136 $course = $DB->get_record('course', array('id' => $id));
137 $this->assertEquals($data->category, $course->category);
140 public function test_reject_request() {
141 global $DB;
142 $this->resetAfterTest(true);
143 $this->preventResetByRollback();
145 $this->setAdminUser();
146 set_config('enablecourserequests', 1);
147 set_config('lockrequestcategory', 1);
148 set_config('defaultrequestcategory', $DB->get_field_select('course_categories', "MIN(id)", "parent=0"));
150 // Create a user and allow course requests for him.
151 $requester = $this->getDataGenerator()->create_user();
152 $roleid = create_role('Course requestor role', 'courserequestor', '');
153 assign_capability('moodle/course:request', CAP_ALLOW, $roleid,
154 context_system::instance()->id);
155 role_assign($roleid, $requester->id, context_system::instance()->id);
156 accesslib_clear_all_caches_for_unit_testing();
158 $data = new stdClass();
159 $data->fullname = 'Həllo World!';
160 $data->shortname = 'Hi th€re!';
161 $data->summary_editor['text'] = 'Lorem Ipsum ©';
162 $data->summary_editor['format'] = FORMAT_HTML;
163 $data->reason = 'Because PHP Unit is cool.';
165 $this->setUser($requester);
166 $cr = course_request::create($data);
167 $this->assertTrue($DB->record_exists('course_request', array('id' => $cr->id)));
169 $this->setAdminUser();
170 $sink = $this->redirectMessages();
171 $cr->reject('Sorry!');
172 $this->assertFalse($DB->record_exists('course_request', array('id' => $cr->id)));
173 $this->assertCount(1, $sink->get_messages());
174 $sink->close();