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