Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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 cachestore_file;
  18  
  19  use cache_definition;
  20  use cache_store;
  21  use cachestore_file;
  22  
  23  /**
  24   * Async purge support test for File cache.
  25   *
  26   * @package   cachestore_file
  27   * @copyright Catalyst IT Europe Ltd 2021
  28   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   * @author    Jackson D'Souza <jackson.dsouza@catalyst-eu.net>
  30   * @coversDefaultClass \cachestore_file
  31   */
  32  class asyncpurge_test extends \advanced_testcase {
  33  
  34      /**
  35       * Testing Asynchronous file store cache purge
  36       *
  37       * @covers ::initialise
  38       * @covers ::set
  39       * @covers ::get
  40       * @covers ::purge
  41       */
  42      public function test_cache_async_purge() {
  43          $this->resetAfterTest(true);
  44  
  45          // Cache definition.
  46          $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_file', 'phpunit_test');
  47  
  48          // Extra config, set async purge = true.
  49          $extraconfig = ['asyncpurge' => true, 'filecacherev' => time()];
  50          $configuration = array_merge(cachestore_file::unit_test_configuration(), $extraconfig);
  51          $name = 'File async test';
  52  
  53          // Create file cache store.
  54          $cache = new cachestore_file($name, $configuration);
  55  
  56          // Initialise file cache store.
  57          $cache->initialise($definition);
  58          $cache->set('foo', 'bar');
  59          $this->assertSame('bar', $cache->get('foo'));
  60  
  61          // Purge this file cache store.
  62          $cache->purge();
  63  
  64          // Purging file cache store shouldn't purge the data but create a new cache revision directory.
  65          $this->assertSame('bar', $cache->get('foo'));
  66          $cache->set('foo', 'bar 2');
  67          $this->assertSame('bar 2', $cache->get('foo'));
  68      }
  69  
  70      /**
  71       * Testing Adhoc Cron - deletes old cache revision directory
  72       *
  73       * @covers \cachestore_file\task
  74       */
  75      public function test_cache_async_purge_cron() {
  76          global $CFG, $USER;
  77  
  78          $this->resetAfterTest(true);
  79  
  80          $tmpdir = realpath($CFG->tempdir);
  81          $directorypath = '/cachefile_store';
  82          $cacherevdir = $tmpdir . $directorypath;
  83  
  84          // Create cache revision directory.
  85          mkdir($cacherevdir, $CFG->directorypermissions, true);
  86  
  87          // Create / execute adhoc task to delete cache revision directory.
  88          $asynctask = new cachestore_file\task\asyncpurge();
  89          $asynctask->set_blocking(false);
  90          $asynctask->set_custom_data(['path' => $cacherevdir]);
  91          $asynctask->set_userid($USER->id);
  92          \core\task\manager::queue_adhoc_task($asynctask);
  93          $asynctask->execute();
  94  
  95          // Check if cache revision directory has been deleted.
  96          $this->assertDirectoryDoesNotExist($cacherevdir);
  97      }
  98  }