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]

   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  defined('MOODLE_INTERNAL') || die();
  24  
  25  // Include the necessary evils.
  26  global $CFG;
  27  require_once($CFG->dirroot.'/cache/tests/fixtures/stores.php');
  28  require_once($CFG->dirroot.'/cache/stores/file/lib.php');
  29  
  30  /**
  31   * File unit test class.
  32   *
  33   * @package    cachestore_file
  34   * @copyright  2013 Sam Hemelryk
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   * @covers \cachestore_file
  37   */
  38  class store_test extends \cachestore_tests {
  39      /**
  40       * Returns the file class name
  41       * @return string
  42       */
  43      protected function get_class_name() {
  44          return 'cachestore_file';
  45      }
  46  
  47      /**
  48       * Testing cachestore_file::get with prescan enabled and with
  49       * deleting the cache between the prescan and the call to get.
  50       *
  51       * The deleting of cache simulates some other process purging
  52       * the cache.
  53       */
  54      public function test_cache_get_with_prescan_and_purge() {
  55          global $CFG;
  56  
  57          $definition = cache_definition::load_adhoc(cache_store::MODE_REQUEST, 'cachestore_file', 'phpunit_test');
  58          $name = 'File test';
  59  
  60          $path = make_cache_directory('cachestore_file_test');
  61          $cache = new cachestore_file($name, array('path' => $path, 'prescan' => true));
  62          $cache->initialise($definition);
  63  
  64          $cache->set('testing', 'value');
  65  
  66          $path  = make_cache_directory('cachestore_file_test');
  67          $cache = new cachestore_file($name, array('path' => $path, 'prescan' => true));
  68          $cache->initialise($definition);
  69  
  70          // Let's pretend that some other process purged caches.
  71          remove_dir($CFG->cachedir.'/cachestore_file_test', true);
  72          make_cache_directory('cachestore_file_test');
  73  
  74          $cache->get('testing');
  75      }
  76  
  77      /**
  78       * Tests the get_last_read byte count.
  79       */
  80      public function test_get_last_io_bytes(): void {
  81          $definition = cache_definition::load_adhoc(cache_store::MODE_REQUEST, 'cachestore_file', 'phpunit_test');
  82          $store = new \cachestore_file('Test');
  83          $store->initialise($definition);
  84  
  85          $store->set('foo', 'bar');
  86          $store->set('frog', 'ribbit');
  87          $store->get('foo');
  88          // It's not 3 bytes, because the data is stored serialized.
  89          $this->assertEquals(10, $store->get_last_io_bytes());
  90          $store->get('frog');
  91          $this->assertEquals(13, $store->get_last_io_bytes());
  92          $store->get_many(['foo', 'frog']);
  93          $this->assertEquals(23, $store->get_last_io_bytes());
  94  
  95          $store->set('foo', 'goo');
  96          $this->assertEquals(10, $store->get_last_io_bytes());
  97          $store->set_many([
  98                  ['key' => 'foo', 'value' => 'bar'],
  99                  ['key' => 'frog', 'value' => 'jump']
 100          ]);
 101          $this->assertEquals(21, $store->get_last_io_bytes());
 102      }
 103  
 104      public function test_lock() {
 105          $store = new \cachestore_file('Test');
 106  
 107          $this->assertTrue($store->acquire_lock('lock', '123'));
 108          $this->assertTrue($store->check_lock_state('lock', '123'));
 109          $this->assertFalse($store->check_lock_state('lock', '321'));
 110          $this->assertNull($store->check_lock_state('notalock', '123'));
 111          $this->assertFalse($store->release_lock('lock', '321'));
 112          $this->assertTrue($store->release_lock('lock', '123'));
 113      }
 114  }