Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

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