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 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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-887.h5p',
  43              'introformat' => 1
  44          ];
  45  
  46          // Create h5pactivity.
  47          $activity = $this->getDataGenerator()->create_module('h5pactivity', $params);
  48          $activity->filename = 'greeting-card-887.h5p';
  49          $context = context_module::instance($activity->cmid);
  50  
  51          // Create a fake deploy H5P file.
  52          $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
  53          $generator->create_export_file($activity->filename, $context->id,
  54              'mod_h5pactivity', 'package');
  55  
  56          // Delete activity.
  57          course_delete_module($activity->cmid);
  58  
  59          $orphanedh5psql = "SELECT h5p.id, h5p.pathnamehash
  60                               FROM {h5p} h5p
  61                          LEFT JOIN {files} f ON f.pathnamehash = h5p.pathnamehash
  62                              WHERE f.pathnamehash IS NULL";
  63          $orphanedh5p = $DB->get_records_sql($orphanedh5psql);
  64          $this->assertEquals(1, count($orphanedh5p));
  65  
  66          $h5pid = end($orphanedh5p)->id;
  67          $orphanedfilessql = "SELECT id
  68                                 FROM {files}
  69                                WHERE itemid = :h5pid
  70                                  AND filearea = 'content'
  71                                  AND component = 'core_h5p'";
  72          $orphanedfiles = $DB->get_records_sql($orphanedfilessql, ['h5pid' => $h5pid]);
  73          $this->assertEquals(3, count($orphanedfiles));
  74  
  75          // Execute task.
  76          $task = new \core\task\h5p_clean_orphaned_records_task();
  77          $task->execute();
  78  
  79          $orphanedh5p = $DB->get_record_sql($orphanedh5psql);
  80          $this->assertFalse($orphanedh5p);
  81  
  82          $orphanedfiles = $DB->get_record_sql($orphanedfilessql, ['h5pid' => $h5pid]);
  83          $this->assertFalse($orphanedfiles);
  84      }
  85  }