Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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   * Test for H5P content bank plugin.
  19   *
  20   * @package    contenttype_h5p
  21   * @category   test
  22   * @copyright  2020 Amaia Anabitarte <amaia@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  /**
  27   * Test for H5P content bank plugin.
  28   *
  29   * @package    contenttype_h5p
  30   * @category   test
  31   * @copyright  2020 Amaia Anabitarte <amaia@moodle.com>
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   * @coversDefaultClass \contenttype_h5p\contenttype
  34   */
  35  class contenttype_h5p_contenttype_plugin_testcase extends advanced_testcase {
  36  
  37      /**
  38       * Test the behaviour of delete_content().
  39       */
  40      public function test_delete_content() {
  41          global $CFG, $USER, $DB;
  42  
  43          $this->resetAfterTest();
  44          $systemcontext = context_system::instance();
  45  
  46          // Create users.
  47          $roleid = $DB->get_field('role', 'id', array('shortname' => 'manager'));
  48          $manager = $this->getDataGenerator()->create_user();
  49          $this->getDataGenerator()->role_assign($roleid, $manager->id);
  50          $this->setUser($manager);
  51  
  52          // Add an H5P file to the content bank.
  53          $filepath = $CFG->dirroot . '/h5p/tests/fixtures/filltheblanks.h5p';
  54          $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
  55          $contents = $generator->generate_contentbank_data('contenttype_h5p', 2, $USER->id, $systemcontext, true, $filepath);
  56          $content1 = array_shift($contents);
  57          $content2 = array_shift($contents);
  58  
  59          // Load this H5P file though the player to create the H5P DB entries.
  60          $h5pplayer = new \core_h5p\player($content1->get_file_url(), new \stdClass(), true);
  61          $h5pplayer->add_assets_to_page();
  62          $h5pplayer->output();
  63          $h5pplayer = new \core_h5p\player($content2->get_file_url(), new \stdClass(), true);
  64          $h5pplayer->add_assets_to_page();
  65          $h5pplayer->output();
  66  
  67          // Check the H5P content has been created.
  68          $this->assertEquals(2, $DB->count_records('h5p'));
  69          $this->assertEquals(2, $DB->count_records('contentbank_content'));
  70  
  71          // Check the H5P content is removed after calling this method.
  72          $contenttype = new \contenttype_h5p\contenttype($systemcontext);
  73          $contenttype->delete_content($content1);
  74          $this->assertEquals(1, $DB->count_records('h5p'));
  75          $this->assertEquals(1, $DB->count_records('contentbank_content'));
  76      }
  77  
  78      /**
  79       * Tests can_upload behavior.
  80       *
  81       * @covers ::can_upload
  82       */
  83      public function test_can_upload() {
  84          $this->resetAfterTest();
  85  
  86          $systemcontext = \context_system::instance();
  87          $systemtype = new \contenttype_h5p\contenttype($systemcontext);
  88  
  89          // Admins can upload.
  90          $this->setAdminUser();
  91          $this->assertTrue($systemtype->can_upload());
  92  
  93          // Teacher can upload in the course but not at system level.
  94          $course = $this->getDataGenerator()->create_course();
  95          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
  96          $coursecontext = \context_course::instance($course->id);
  97          $coursetype = new \contenttype_h5p\contenttype($coursecontext);
  98          $this->setUser($teacher);
  99          $this->assertTrue($coursetype->can_upload());
 100          $this->assertFalse($systemtype->can_upload());
 101  
 102          // Users can't upload.
 103          $user = $this->getDataGenerator()->create_user();
 104          $this->setUser($user);
 105          $this->assertFalse($coursetype->can_upload());
 106          $this->assertFalse($systemtype->can_upload());
 107      }
 108  
 109      /**
 110       * Tests get_icon result.
 111       *
 112       * @covers ::get_icon
 113       */
 114      public function test_get_icon() {
 115          global $CFG;
 116  
 117          $this->resetAfterTest();
 118          $systemcontext = context_system::instance();
 119          $this->setAdminUser();
 120          $contenttype = new contenttype_h5p\contenttype($systemcontext);
 121  
 122          // Add an H5P fill the blanks file to the content bank.
 123          $filepath = $CFG->dirroot . '/h5p/tests/fixtures/filltheblanks.h5p';
 124          $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
 125          $contents = $generator->generate_contentbank_data('contenttype_h5p', 1, 0, $systemcontext, true, $filepath);
 126          $filltheblanks = array_shift($contents);
 127  
 128          // Add an H5P find the words file to the content bank.
 129          $filepath = $CFG->dirroot . '/h5p/tests/fixtures/find-the-words.h5p';
 130          $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
 131          $contents = $generator->generate_contentbank_data('contenttype_h5p', 1, 0, $systemcontext, true, $filepath);
 132          $findethewords = array_shift($contents);
 133  
 134          // Check before deploying the icon for both contents is the same: default one.
 135          // Because we don't know specific H5P content type yet.
 136          $defaulticon = $contenttype->get_icon($filltheblanks);
 137          $this->assertEquals($defaulticon, $contenttype->get_icon($findethewords));
 138          $this->assertStringContainsString('h5p', $defaulticon);
 139  
 140          // Deploy one of the contents though the player to create the H5P DB entries and know specific content type.
 141          $h5pplayer = new \core_h5p\player($findethewords->get_file_url(), new \stdClass(), true);
 142          $h5pplayer->add_assets_to_page();
 143          $h5pplayer->output();
 144  
 145          // Once the H5P has been deployed, we know the specific H5P content type, so the icon returned is not default one.
 146          $findicon = $contenttype->get_icon($findethewords);
 147          $this->assertNotEquals($defaulticon, $findicon);
 148          $this->assertStringContainsStringIgnoringCase('find', $findicon);
 149      }
 150  
 151      /**
 152       * Tests get_download_url result.
 153       *
 154       * @covers ::get_download_url
 155       */
 156      public function test_get_download_url() {
 157          global $CFG;
 158  
 159          $this->resetAfterTest();
 160          $systemcontext = context_system::instance();
 161          $this->setAdminUser();
 162          $contenttype = new contenttype_h5p\contenttype($systemcontext);
 163  
 164          // Add an H5P fill the blanks file to the content bank.
 165          $filename = 'filltheblanks.h5p';
 166          $filepath = $CFG->dirroot . '/h5p/tests/fixtures/' . $filename;
 167          $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
 168          $contents = $generator->generate_contentbank_data('contenttype_h5p', 1, 0, $systemcontext, true, $filepath);
 169          $filltheblanks = array_shift($contents);
 170  
 171          // Check before deploying the URL is returned OK.
 172          $url1 = $contenttype->get_download_url($filltheblanks);
 173          $this->assertNotEmpty($url1);
 174          $this->assertStringContainsString($filename, $url1);
 175  
 176          // Deploy the contents though the player to create the H5P DB entries and know specific content type.
 177          $h5pplayer = new \core_h5p\player($filltheblanks->get_file_url(), new \stdClass(), true);
 178          $h5pplayer->add_assets_to_page();
 179          $h5pplayer->output();
 180  
 181          // Once the H5P has been deployed, the URL is still the same.
 182          $url2 = $contenttype->get_download_url($filltheblanks);
 183          $this->assertNotEmpty($url2);
 184          $this->assertEquals($url1, $url2);
 185      }
 186  }