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  /**
  18   * External function test for get_h5pactivities_by_courses.
  19   *
  20   * @package    mod_h5pactivity
  21   * @category   external
  22   * @since      Moodle 3.9
  23   * @copyright  2020 Carlos Escobedo <carlos@moodle.com>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  namespace mod_h5pactivity\external;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  global $CFG;
  32  
  33  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  34  
  35  use core_external\external_api;
  36  use externallib_advanced_testcase;
  37  use context_module;
  38  
  39  /**
  40   * External function test for get_h5pactivities_by_courses.
  41   *
  42   * @package    mod_h5pactivity
  43   * @copyright  2020 Carlos Escobedo <carlos@moodle.com>
  44   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45   */
  46  class get_h5pactivities_by_courses_test extends externallib_advanced_testcase {
  47  
  48      /**
  49       * Test test_get_h5pactivities_by_courses user student.
  50       */
  51      public function test_get_h5pactivities_by_courses() {
  52          global $CFG, $DB;
  53  
  54          $this->resetAfterTest();
  55          $this->setAdminUser();
  56  
  57          // Create 2 courses.
  58          // Course 1 -> 2 activities with H5P files package without deploy.
  59          // Course 2 -> 1 activity with H5P file package deployed.
  60          $course1 = $this->getDataGenerator()->create_course();
  61          $params = [
  62              'course' => $course1->id,
  63              'packagefilepath' => $CFG->dirroot.'/h5p/tests/fixtures/filltheblanks.h5p',
  64              'introformat' => 1
  65          ];
  66          $activities[] = $this->getDataGenerator()->create_module('h5pactivity', $params);
  67          // Add filename and contextid to make easier the asserts.
  68          $activities[0]->filename = 'filltheblanks.h5p';
  69          $context = context_module::instance($activities[0]->cmid);
  70          $activities[0]->contextid = $context->id;
  71  
  72          $params = [
  73              'course' => $course1->id,
  74              'packagefilepath' => $CFG->dirroot.'/h5p/tests/fixtures/greeting-card.h5p',
  75              'introformat' => 1
  76          ];
  77          $activities[] = $this->getDataGenerator()->create_module('h5pactivity', $params);
  78          // Add filename and contextid to make easier the asserts.
  79          $activities[1]->filename = 'greeting-card.h5p';
  80          $context = context_module::instance($activities[1]->cmid);
  81          $activities[1]->contextid = $context->id;
  82  
  83          $course2 = $this->getDataGenerator()->create_course();
  84          $params = [
  85              'course' => $course2->id,
  86              'packagefilepath' => $CFG->dirroot.'/h5p/tests/fixtures/guess-the-answer.h5p',
  87              'introformat' => 1
  88          ];
  89          $activities[] = $this->getDataGenerator()->create_module('h5pactivity', $params);
  90          $activities[2]->filename = 'guess-the-answer.h5p';
  91          $context = context_module::instance($activities[2]->cmid);
  92          $activities[2]->contextid = $context->id;
  93  
  94          // Create a fake deploy H5P file.
  95          $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
  96          $deployedfile = $generator->create_export_file($activities[2]->filename, $context->id, 'mod_h5pactivity', 'package');
  97  
  98          // Create a user and enrol as student in both courses.
  99          $user = $this->getDataGenerator()->create_user();
 100          $studentrole = $DB->get_record('role', ['shortname' => 'student']);
 101          $maninstance1 = $DB->get_record('enrol', ['courseid' => $course1->id, 'enrol' => 'manual'], '*', MUST_EXIST);
 102          $maninstance2 = $DB->get_record('enrol', ['courseid' => $course2->id, 'enrol' => 'manual'], '*', MUST_EXIST);
 103          $manual = enrol_get_plugin('manual');
 104          $manual->enrol_user($maninstance1, $user->id, $studentrole->id);
 105          $manual->enrol_user($maninstance2, $user->id, $studentrole->id);
 106  
 107          // Set admin settings.
 108          set_config('enablesavestate', 1, 'mod_h5pactivity');
 109          set_config('savestatefreq', 120, 'mod_h5pactivity');
 110  
 111          // Check the activities returned by the first course.
 112          $this->setUser($user);
 113          $courseids = [$course1->id];
 114          $result = get_h5pactivities_by_courses::execute($courseids);
 115          $result = external_api::clean_returnvalue(get_h5pactivities_by_courses::execute_returns(), $result);
 116          $this->assertCount(0, $result['warnings']);
 117          $this->assertCount(2, $result['h5pactivities']);
 118          $this->assert_activities($activities, $result);
 119          $this->assertNotContains('deployedfile', $result['h5pactivities'][0]);
 120          $this->assertNotContains('deployedfile', $result['h5pactivities'][1]);
 121          $this->assertEquals(1, $result['h5pglobalsettings']['enablesavestate']);
 122          $this->assertEquals(120, $result['h5pglobalsettings']['savestatefreq']);
 123  
 124          // Call the external function without passing course id.
 125          // Expected result, all the courses, course1 and course2.
 126          $result = get_h5pactivities_by_courses::execute([]);
 127          $result = external_api::clean_returnvalue(get_h5pactivities_by_courses::execute_returns(), $result);
 128          $this->assertCount(0, $result['warnings']);
 129          $this->assertCount(3, $result['h5pactivities']);
 130          // We need to sort the $result by id.
 131          // Because we are not sure how it is ordered with more than one course.
 132          array_multisort(array_map(function($element) {
 133              return $element['id'];
 134          }, $result['h5pactivities']), SORT_ASC, $result['h5pactivities']);
 135          $this->assert_activities($activities, $result);
 136          $this->assertNotContains('deployedfile', $result['h5pactivities'][0]);
 137          $this->assertNotContains('deployedfile', $result['h5pactivities'][1]);
 138          // Only the activity from the second course has been deployed.
 139          $this->assertEquals($deployedfile['filename'], $result['h5pactivities'][2]['deployedfile']['filename']);
 140          $this->assertEquals($deployedfile['filepath'], $result['h5pactivities'][2]['deployedfile']['filepath']);
 141          $this->assertEquals($deployedfile['filesize'], $result['h5pactivities'][2]['deployedfile']['filesize']);
 142          $this->assertEquals($deployedfile['timemodified'], $result['h5pactivities'][2]['deployedfile']['timemodified']);
 143          $this->assertEquals($deployedfile['mimetype'], $result['h5pactivities'][2]['deployedfile']['mimetype']);
 144          $this->assertEquals($deployedfile['fileurl'], $result['h5pactivities'][2]['deployedfile']['fileurl']);
 145          $this->assertEquals(1, $result['h5pglobalsettings']['enablesavestate']);
 146          $this->assertEquals(120, $result['h5pglobalsettings']['savestatefreq']);
 147  
 148          // Unenrol user from second course.
 149          $manual->unenrol_user($maninstance2, $user->id);
 150          // Remove the last activity from the array.
 151          array_pop($activities);
 152  
 153          // Disable save state.
 154          set_config('enablesavestate', 0, 'mod_h5pactivity');
 155  
 156          // Call the external function without passing course id.
 157          $result = get_h5pactivities_by_courses::execute([]);
 158          $result = external_api::clean_returnvalue(get_h5pactivities_by_courses::execute_returns(), $result);
 159          $this->assertCount(0, $result['warnings']);
 160          $this->assertCount(2, $result['h5pactivities']);
 161          $this->assert_activities($activities, $result);
 162          $this->assertEquals(0, $result['h5pglobalsettings']['enablesavestate']);
 163          $this->assertNotContains('savestatefreq', $result['h5pglobalsettings']);
 164  
 165          // Call for the second course we unenrolled the user from, expected warning.
 166          $result = get_h5pactivities_by_courses::execute([$course2->id]);
 167          $result = external_api::clean_returnvalue(get_h5pactivities_by_courses::execute_returns(), $result);
 168          $this->assertCount(1, $result['warnings']);
 169          $this->assertEquals('1', $result['warnings'][0]['warningcode']);
 170          $this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
 171      }
 172  
 173      /**
 174       * Create a scenario to use into the tests.
 175       *
 176       * @param  array $activities list of H5P activities.
 177       * @param  array $result list of H5P activities by WS.
 178       * @return void
 179       */
 180      protected function assert_activities(array $activities, array $result): void {
 181  
 182          $total = count($result['h5pactivities']);
 183          for ($i = 0; $i < $total; $i++) {
 184              $this->assertEquals($activities[$i]->id, $result['h5pactivities'][$i]['id']);
 185              $this->assertEquals($activities[$i]->course, $result['h5pactivities'][$i]['course']);
 186              $this->assertEquals($activities[$i]->name, $result['h5pactivities'][$i]['name']);
 187              $this->assertEquals($activities[$i]->timecreated, $result['h5pactivities'][$i]['timecreated']);
 188              $this->assertEquals($activities[$i]->timemodified, $result['h5pactivities'][$i]['timemodified']);
 189              $this->assertEquals($activities[$i]->intro, $result['h5pactivities'][$i]['intro']);
 190              $this->assertEquals($activities[$i]->introformat, $result['h5pactivities'][$i]['introformat']);
 191              $this->assertEquals([], $result['h5pactivities'][$i]['introfiles']);
 192              $this->assertEquals($activities[$i]->grade, $result['h5pactivities'][$i]['grade']);
 193              $this->assertEquals($activities[$i]->displayoptions, $result['h5pactivities'][$i]['displayoptions']);
 194              $this->assertEquals($activities[$i]->enabletracking, $result['h5pactivities'][$i]['enabletracking']);
 195              $this->assertEquals($activities[$i]->grademethod, $result['h5pactivities'][$i]['grademethod']);
 196              $this->assertEquals($activities[$i]->cmid, $result['h5pactivities'][$i]['coursemodule']);
 197              $this->assertEquals($activities[$i]->contextid, $result['h5pactivities'][$i]['context']);
 198              $this->assertEquals($activities[$i]->filename, $result['h5pactivities'][$i]['package'][0]['filename']);
 199          }
 200      }
 201  }