Merge branch 'master_MDL-57324' of git://github.com/danmarsden/moodle
[moodle.git] / course / tests / courserequest_test.php
blob1e96d21e29c8fc6b25e9f077d515e2c45d69bb84
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('requestcategoryselection', 0);
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('requestcategoryselection', 1);
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('requestcategoryselection', 0);
87 set_config('defaultrequestcategory', $defaultcategory);
89 // Create some categories.
90 $cat1 = $this->getDataGenerator()->create_category();
91 $cat2 = $this->getDataGenerator()->create_category();
93 $requester = $this->getDataGenerator()->create_user();
95 $data = new stdClass();
96 $data->fullname = 'Həllo World!';
97 $data->shortname = 'Hi th€re!';
98 $data->summary_editor['text'] = 'Lorem Ipsum ©';
99 $data->summary_editor['format'] = FORMAT_HTML;
100 $data->reason = 'Because PHP Unit is cool.';
102 // Test without category.
103 $this->setUser($requester);
104 $cr = course_request::create($data);
105 $this->setAdminUser();
106 $sink = $this->redirectMessages();
107 $id = $cr->approve();
108 $this->assertCount(1, $sink->get_messages());
109 $sink->close();
110 $course = $DB->get_record('course', array('id' => $id));
111 $this->assertEquals($data->fullname, $course->fullname);
112 $this->assertEquals($data->shortname, $course->shortname);
113 $this->assertEquals($data->summary_editor['text'], $course->summary);
114 $this->assertEquals($data->summary_editor['format'], $course->summaryformat);
115 $this->assertEquals(1, $course->requested);
116 $this->assertEquals($defaultcategory, $course->category);
118 // Test with category.
119 set_config('requestcategoryselection', 1);
120 set_config('defaultrequestcategory', $cat2->id);
121 $data->shortname .= ' 2nd';
122 $data->category = $cat1->id;
123 $this->setUser($requester);
124 $cr = course_request::create($data);
125 $this->setAdminUser();
126 $sink = $this->redirectMessages();
127 $id = $cr->approve();
128 $this->assertCount(1, $sink->get_messages());
129 $sink->close();
130 $course = $DB->get_record('course', array('id' => $id));
131 $this->assertEquals($data->category, $course->category);
134 public function test_reject_request() {
135 global $DB;
136 $this->resetAfterTest(true);
137 $this->preventResetByRollback();
139 $this->setAdminUser();
140 set_config('enablecourserequests', 1);
141 set_config('requestcategoryselection', 0);
142 set_config('defaultrequestcategory', $DB->get_field_select('course_categories', "MIN(id)", "parent=0"));
144 $requester = $this->getDataGenerator()->create_user();
146 $data = new stdClass();
147 $data->fullname = 'Həllo World!';
148 $data->shortname = 'Hi th€re!';
149 $data->summary_editor['text'] = 'Lorem Ipsum ©';
150 $data->summary_editor['format'] = FORMAT_HTML;
151 $data->reason = 'Because PHP Unit is cool.';
153 $this->setUser($requester);
154 $cr = course_request::create($data);
155 $this->assertTrue($DB->record_exists('course_request', array('id' => $cr->id)));
157 $this->setAdminUser();
158 $sink = $this->redirectMessages();
159 $cr->reject('Sorry!');
160 $this->assertFalse($DB->record_exists('course_request', array('id' => $cr->id)));
161 $this->assertCount(1, $sink->get_messages());
162 $sink->close();