Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   1  <?php
   2  
   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.
  14  //
  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/>.
  17  
  18  /**
  19   * Course request related unit tests
  20   *
  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
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once($CFG->dirroot.'/course/lib.php');
  31  
  32  class core_course_courserequest_testcase extends advanced_testcase {
  33  
  34      public function test_create_request() {
  35          global $DB, $USER;
  36          $this->resetAfterTest(true);
  37  
  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);
  42  
  43          // Create some categories.
  44          $cat1 = $this->getDataGenerator()->create_category();
  45          $cat2 = $this->getDataGenerator()->create_category();
  46          $cat3 = $this->getDataGenerator()->create_category();
  47  
  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);
  56  
  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);
  64  
  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);
  70  
  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);
  77      }
  78  
  79      public function test_approve_request() {
  80          global $DB;
  81          $this->resetAfterTest(true);
  82          $this->preventResetByRollback();
  83  
  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);
  88  
  89          // Create some categories.
  90          $cat1 = $this->getDataGenerator()->create_category();
  91          $cat2 = $this->getDataGenerator()->create_category();
  92  
  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();
 100  
 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.';
 107  
 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);
 123  
 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);
 138      }
 139  
 140      public function test_reject_request() {
 141          global $DB;
 142          $this->resetAfterTest(true);
 143          $this->preventResetByRollback();
 144  
 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"));
 149  
 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();
 157  
 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.';
 164  
 165          $this->setUser($requester);
 166          $cr = course_request::create($data);
 167          $this->assertTrue($DB->record_exists('course_request', array('id' => $cr->id)));
 168  
 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();
 175      }
 176  }