Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   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  namespace core_course;
  18  
  19  use course_request;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  require_once($CFG->dirroot.'/course/lib.php');
  25  
  26  /**
  27   * Course request related unit tests
  28   *
  29   * @package    core_course
  30   * @category   test
  31   * @copyright  2012 Frédéric Massart
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class courserequest_test extends \advanced_testcase {
  35  
  36      public function test_create_request() {
  37          global $DB, $USER;
  38          $this->resetAfterTest(true);
  39  
  40          $defaultcategory = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
  41          set_config('enablecourserequests', 1);
  42          set_config('lockrequestcategory', 1);
  43          set_config('defaultrequestcategory', $defaultcategory);
  44  
  45          // Create some categories.
  46          $cat1 = $this->getDataGenerator()->create_category();
  47          $cat2 = $this->getDataGenerator()->create_category();
  48          $cat3 = $this->getDataGenerator()->create_category();
  49  
  50          // Basic course request.
  51          $data = new \stdClass();
  52          $data->fullname = 'Həllo World!';
  53          $data->shortname = 'Hi th€re!';
  54          $data->summary_editor['text'] = 'Lorem Ipsum ©';
  55          $data->summary_editor['format'] = FORMAT_HTML;
  56          $data->reason = 'Because PHP Unit is cool.';
  57          $cr = course_request::create($data);
  58  
  59          $this->assertEquals($data->fullname, $cr->fullname);
  60          $this->assertEquals($data->shortname, $cr->shortname);
  61          $this->assertEquals($data->summary_editor['text'], $cr->summary);
  62          $this->assertEquals($data->summary_editor['format'], $cr->summaryformat);
  63          $this->assertEquals($data->reason, $cr->reason);
  64          $this->assertEquals($USER->id, $cr->requester);
  65          $this->assertEquals($defaultcategory, $cr->category);
  66  
  67          // Request with category but category selection not allowed.
  68          set_config('defaultrequestcategory', $cat2->id);
  69          $data->category = $cat1->id;
  70          $cr = course_request::create($data);
  71          $this->assertEquals($cat2->id, $cr->category);
  72  
  73          // Request with category different than default and category selection allowed.
  74          set_config('defaultrequestcategory', $cat3->id);
  75          set_config('lockrequestcategory', 0);
  76          $data->category = $cat1->id;
  77          $cr = course_request::create($data);
  78          $this->assertEquals($cat1->id, $cr->category);
  79      }
  80  
  81      public function test_approve_request() {
  82          global $DB;
  83          $this->resetAfterTest(true);
  84          $this->preventResetByRollback();
  85  
  86          $defaultcategory = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
  87          set_config('enablecourserequests', 1);
  88          set_config('lockrequestcategory', 1);
  89          set_config('defaultrequestcategory', $defaultcategory);
  90  
  91          // Create some categories.
  92          $cat1 = $this->getDataGenerator()->create_category();
  93          $cat2 = $this->getDataGenerator()->create_category();
  94  
  95          // Create a user and allow course requests for him.
  96          $requester = $this->getDataGenerator()->create_user();
  97          $roleid = create_role('Course requestor role', 'courserequestor', '');
  98          assign_capability('moodle/course:request', CAP_ALLOW, $roleid,
  99              \context_system::instance()->id);
 100          role_assign($roleid, $requester->id, \context_system::instance()->id);
 101          accesslib_clear_all_caches_for_unit_testing();
 102  
 103          $data = new \stdClass();
 104          $data->fullname = 'Həllo World!';
 105          $data->shortname = 'Hi th€re!';
 106          $data->summary_editor['text'] = 'Lorem Ipsum ©';
 107          $data->summary_editor['format'] = FORMAT_HTML;
 108          $data->reason = 'Because PHP Unit is cool.';
 109  
 110          // Test without category.
 111          $this->setUser($requester);
 112          $cr = course_request::create($data);
 113          $this->setAdminUser();
 114          $sink = $this->redirectMessages();
 115          $id = $cr->approve();
 116          $this->assertCount(1, $sink->get_messages());
 117          $sink->close();
 118          $course = $DB->get_record('course', array('id' => $id));
 119          $this->assertEquals($data->fullname, $course->fullname);
 120          $this->assertEquals($data->shortname, $course->shortname);
 121          $this->assertEquals($data->summary_editor['text'], $course->summary);
 122          $this->assertEquals($data->summary_editor['format'], $course->summaryformat);
 123          $this->assertEquals(1, $course->requested);
 124          $this->assertEquals($defaultcategory, $course->category);
 125  
 126          // Test with category.
 127          set_config('lockrequestcategory', 0);
 128          set_config('defaultrequestcategory', $cat2->id);
 129          $data->shortname .= ' 2nd';
 130          $data->category = $cat1->id;
 131          $this->setUser($requester);
 132          $cr = course_request::create($data);
 133          $this->setAdminUser();
 134          $sink = $this->redirectMessages();
 135          $id = $cr->approve();
 136          $this->assertCount(1, $sink->get_messages());
 137          $sink->close();
 138          $course = $DB->get_record('course', array('id' => $id));
 139          $this->assertEquals($data->category, $course->category);
 140      }
 141  
 142      public function test_reject_request() {
 143          global $DB;
 144          $this->resetAfterTest(true);
 145          $this->preventResetByRollback();
 146  
 147          $this->setAdminUser();
 148          set_config('enablecourserequests', 1);
 149          set_config('lockrequestcategory', 1);
 150          set_config('defaultrequestcategory', $DB->get_field_select('course_categories', "MIN(id)", "parent=0"));
 151  
 152          // Create a user and allow course requests for him.
 153          $requester = $this->getDataGenerator()->create_user();
 154          $roleid = create_role('Course requestor role', 'courserequestor', '');
 155          assign_capability('moodle/course:request', CAP_ALLOW, $roleid,
 156              \context_system::instance()->id);
 157          role_assign($roleid, $requester->id, \context_system::instance()->id);
 158          accesslib_clear_all_caches_for_unit_testing();
 159  
 160          $data = new \stdClass();
 161          $data->fullname = 'Həllo World!';
 162          $data->shortname = 'Hi th€re!';
 163          $data->summary_editor['text'] = 'Lorem Ipsum ©';
 164          $data->summary_editor['format'] = FORMAT_HTML;
 165          $data->reason = 'Because PHP Unit is cool.';
 166  
 167          $this->setUser($requester);
 168          $cr = course_request::create($data);
 169          $this->assertTrue($DB->record_exists('course_request', array('id' => $cr->id)));
 170  
 171          $this->setAdminUser();
 172          $sink = $this->redirectMessages();
 173          $cr->reject('Sorry!');
 174          $this->assertFalse($DB->record_exists('course_request', array('id' => $cr->id)));
 175          $this->assertCount(1, $sink->get_messages());
 176          $sink->close();
 177      }
 178  }