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 311 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   * Class containing unit tests for the task to clean orphaned h5p records.
  19   *
  20   * @package   core
  21   * @copyright  2021 Ilya Tregubov <ilya@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   *
  24   */
  25  class h5p_clean_orphaned_records_task_test extends advanced_testcase {
  26  
  27      /**
  28       * Test task execution
  29       *
  30       * return void
  31       */
  32      public function test_task_execution(): void {
  33          global $CFG, $DB;
  34  
  35          $this->resetAfterTest();
  36          $this->setAdminUser();
  37  
  38          // Create a course.
  39          $course = $this->getDataGenerator()->create_course();
  40          $params = [
  41              'course' => $course->id,
  42              'packagefilepath' => $CFG->dirroot.'/h5p/tests/fixtures/greeting-card.h5p',
  43              'introformat' => 1
  44          ];
  45  
  46          // Create h5pactivity.
  47          $activity = $this->getDataGenerator()->create_module('h5pactivity', $params);
  48          $activity->filename = 'greeting-card.h5p';
  49          $context = context_module::instance($activity->cmid);
  50  
  51          // Create a fake deploy H5P file.
  52          /** @var \core_h5p_generator $generator */
  53          $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
  54          $generator->create_export_file($activity->filename, $context->id,
  55              'mod_h5pactivity', 'package');
  56  
  57          // Delete activity.
  58          course_delete_module($activity->cmid);
  59  
  60          $orphanedh5psql = "SELECT h5p.id, h5p.pathnamehash
  61                               FROM {h5p} h5p
  62                          LEFT JOIN {files} f ON f.pathnamehash = h5p.pathnamehash
  63                              WHERE f.pathnamehash IS NULL";
  64          $orphanedh5p = $DB->get_records_sql($orphanedh5psql);
  65          $this->assertEquals(1, count($orphanedh5p));
  66  
  67          $h5pid = end($orphanedh5p)->id;
  68          $orphanedfilessql = "SELECT id
  69                                 FROM {files}
  70                                WHERE itemid = :h5pid
  71                                  AND filearea = 'content'
  72                                  AND component = 'core_h5p'";
  73          $orphanedfiles = $DB->get_records_sql($orphanedfilessql, ['h5pid' => $h5pid]);
  74          $this->assertEquals(2, count($orphanedfiles));
  75  
  76          // Execute task.
  77          $task = new \core\task\h5p_clean_orphaned_records_task();
  78          $task->execute();
  79  
  80          $orphanedh5p = $DB->get_record_sql($orphanedh5psql);
  81          $this->assertFalse($orphanedh5p);
  82  
  83          $orphanedfiles = $DB->get_record_sql($orphanedfilessql, ['h5pid' => $h5pid]);
  84          $this->assertFalse($orphanedfiles);
  85      }
  86  }