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 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]

   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 mod_label;
  18  
  19  use externallib_advanced_testcase;
  20  use mod_label_external;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  global $CFG;
  25  
  26  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  27  
  28  /**
  29   * External mod_label functions unit tests
  30   *
  31   * @package    mod_label
  32   * @category   external
  33   * @copyright  2017 Juan Leyva <juan@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   * @since      Moodle 3.3
  36   */
  37  class externallib_test extends externallib_advanced_testcase {
  38  
  39      /**
  40       * Test test_mod_label_get_labels_by_courses
  41       */
  42      public function test_mod_label_get_labels_by_courses() {
  43          global $DB;
  44  
  45          $this->resetAfterTest(true);
  46  
  47          $course1 = self::getDataGenerator()->create_course();
  48          $course2 = self::getDataGenerator()->create_course();
  49  
  50          $student = self::getDataGenerator()->create_user();
  51          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  52          $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id);
  53  
  54          // First label.
  55          $record = new \stdClass();
  56          $record->course = $course1->id;
  57          $label1 = self::getDataGenerator()->create_module('label', $record);
  58  
  59          // Second label.
  60          $record = new \stdClass();
  61          $record->course = $course2->id;
  62          $label2 = self::getDataGenerator()->create_module('label', $record);
  63  
  64          // Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
  65          $enrol = enrol_get_plugin('manual');
  66          $enrolinstances = enrol_get_instances($course2->id, true);
  67          foreach ($enrolinstances as $courseenrolinstance) {
  68              if ($courseenrolinstance->enrol == "manual") {
  69                  $instance2 = $courseenrolinstance;
  70                  break;
  71              }
  72          }
  73          $enrol->enrol_user($instance2, $student->id, $studentrole->id);
  74  
  75          self::setUser($student);
  76  
  77          $returndescription = mod_label_external::get_labels_by_courses_returns();
  78  
  79          // Create what we expect to be returned when querying the two courses.
  80          $expectedfields = array('id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'introfiles', 'timemodified',
  81                                  'section', 'visible', 'groupmode', 'groupingid', 'lang');
  82  
  83          // Add expected coursemodule and data.
  84          $label1->coursemodule = $label1->cmid;
  85          $label1->introformat = 1;
  86          $label1->section = 0;
  87          $label1->visible = true;
  88          $label1->groupmode = 0;
  89          $label1->groupingid = 0;
  90          $label1->introfiles = [];
  91          $label1->lang = '';
  92  
  93          $label2->coursemodule = $label2->cmid;
  94          $label2->introformat = 1;
  95          $label2->section = 0;
  96          $label2->visible = true;
  97          $label2->groupmode = 0;
  98          $label2->groupingid = 0;
  99          $label2->introfiles = [];
 100          $label2->lang = '';
 101  
 102          foreach ($expectedfields as $field) {
 103              $expected1[$field] = $label1->{$field};
 104              $expected2[$field] = $label2->{$field};
 105          }
 106  
 107          $expectedlabels = array($expected2, $expected1);
 108  
 109          // Call the external function passing course ids.
 110          $result = mod_label_external::get_labels_by_courses(array($course2->id, $course1->id));
 111          $result = \external_api::clean_returnvalue($returndescription, $result);
 112  
 113          $this->assertEquals($expectedlabels, $result['labels']);
 114          $this->assertCount(0, $result['warnings']);
 115  
 116          // Call the external function without passing course id.
 117          $result = mod_label_external::get_labels_by_courses();
 118          $result = \external_api::clean_returnvalue($returndescription, $result);
 119          $this->assertEquals($expectedlabels, $result['labels']);
 120          $this->assertCount(0, $result['warnings']);
 121  
 122          // Add a file to the intro.
 123          $filename = "file.txt";
 124          $filerecordinline = array(
 125              'contextid' => \context_module::instance($label2->cmid)->id,
 126              'component' => 'mod_label',
 127              'filearea'  => 'intro',
 128              'itemid'    => 0,
 129              'filepath'  => '/',
 130              'filename'  => $filename,
 131          );
 132          $fs = get_file_storage();
 133          $timepost = time();
 134          $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
 135  
 136          $result = mod_label_external::get_labels_by_courses(array($course2->id, $course1->id));
 137          $result = \external_api::clean_returnvalue($returndescription, $result);
 138  
 139          $this->assertCount(1, $result['labels'][0]['introfiles']);
 140          $this->assertEquals($filename, $result['labels'][0]['introfiles'][0]['filename']);
 141  
 142          // Unenrol user from second course.
 143          $enrol->unenrol_user($instance2, $student->id);
 144          array_shift($expectedlabels);
 145  
 146          // Call the external function without passing course id.
 147          $result = mod_label_external::get_labels_by_courses();
 148          $result = \external_api::clean_returnvalue($returndescription, $result);
 149          $this->assertEquals($expectedlabels, $result['labels']);
 150  
 151          // Call for the second course we unenrolled the user from, expected warning.
 152          $result = mod_label_external::get_labels_by_courses(array($course2->id));
 153          $this->assertCount(1, $result['warnings']);
 154          $this->assertEquals('1', $result['warnings'][0]['warningcode']);
 155          $this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
 156      }
 157  }