Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

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