Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 311 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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  namespace cachestore_redis;
  18  
  19  use cache_store;
  20  use cache_definition;
  21  use cachestore_redis;
  22  
  23  defined('MOODLE_INTERNAL') || die();
  24  
  25  require_once (__DIR__.'/../../../tests/fixtures/stores.php');
  26  require_once (__DIR__.'/../lib.php');
  27  
  28  /**
  29   * Redis cache test.
  30   *
  31   * If you wish to use these unit tests all you need to do is add the following definition to
  32   * your config.php file.
  33   *
  34   * define('TEST_CACHESTORE_REDIS_TESTSERVERS', '127.0.0.1');
  35   *
  36   * @package   cachestore_redis
  37   * @covers    \cachestore_redis
  38   * @copyright Copyright (c) 2015 Moodlerooms Inc. (http://www.moodlerooms.com)
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class store_test extends \cachestore_tests {
  42      /**
  43       * @var cachestore_redis
  44       */
  45      protected $store;
  46  
  47      /**
  48       * Returns the MongoDB class name
  49       *
  50       * @return string
  51       */
  52      protected function get_class_name() {
  53          return 'cachestore_redis';
  54      }
  55  
  56      public function setUp(): void {
  57          if (!cachestore_redis::are_requirements_met() || !defined('TEST_CACHESTORE_REDIS_TESTSERVERS')) {
  58              $this->markTestSkipped('Could not test cachestore_redis. Requirements are not met.');
  59          }
  60          parent::setUp();
  61      }
  62      protected function tearDown(): void {
  63          parent::tearDown();
  64  
  65          if ($this->store instanceof cachestore_redis) {
  66              $this->store->purge();
  67          }
  68      }
  69  
  70      /**
  71       * Creates the required cachestore for the tests to run against Redis.
  72       *
  73       * @param array $extraconfig Extra configuration options for Redis instance, if any
  74       * @param bool $ttl True to use a cache definition with TTL enabled
  75       * @return cachestore_redis
  76       */
  77      protected function create_cachestore_redis(array $extraconfig = [], bool $ttl = false): cachestore_redis {
  78          if ($ttl) {
  79              /** @var cache_definition $definition */
  80              $definition = cache_definition::load('core/wibble', [
  81                  'mode' => 1,
  82                  'simplekeys' => true,
  83                  'simpledata' => true,
  84                  'ttl' => 10,
  85                  'component' => 'core',
  86                  'area' => 'wibble',
  87                  'selectedsharingoption' => 2,
  88                  'userinputsharingkey' => '',
  89                  'sharingoptions' => 15,
  90              ]);
  91          } else {
  92              /** @var cache_definition $definition */
  93              $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_redis', 'phpunit_test');
  94          }
  95          $configuration = array_merge(cachestore_redis::unit_test_configuration(), $extraconfig);
  96          $store = new cachestore_redis('Test', $configuration);
  97          $store->initialise($definition);
  98  
  99          $this->store = $store;
 100  
 101          if (!$store) {
 102              $this->markTestSkipped();
 103          }
 104  
 105          return $store;
 106      }
 107  
 108      public function test_has() {
 109          $store = $this->create_cachestore_redis();
 110  
 111          $this->assertTrue($store->set('foo', 'bar'));
 112          $this->assertTrue($store->has('foo'));
 113          $this->assertFalse($store->has('bat'));
 114      }
 115  
 116      public function test_has_any() {
 117          $store = $this->create_cachestore_redis();
 118  
 119          $this->assertTrue($store->set('foo', 'bar'));
 120          $this->assertTrue($store->has_any(array('bat', 'foo')));
 121          $this->assertFalse($store->has_any(array('bat', 'baz')));
 122      }
 123  
 124      public function test_has_all() {
 125          $store = $this->create_cachestore_redis();
 126  
 127          $this->assertTrue($store->set('foo', 'bar'));
 128          $this->assertTrue($store->set('bat', 'baz'));
 129          $this->assertTrue($store->has_all(array('foo', 'bat')));
 130          $this->assertFalse($store->has_all(array('foo', 'bat', 'this')));
 131      }
 132  
 133      public function test_lock() {
 134          $store = $this->create_cachestore_redis();
 135  
 136          $this->assertTrue($store->acquire_lock('lock', '123'));
 137          $this->assertTrue($store->check_lock_state('lock', '123'));
 138          $this->assertFalse($store->check_lock_state('lock', '321'));
 139          $this->assertNull($store->check_lock_state('notalock', '123'));
 140          $this->assertFalse($store->release_lock('lock', '321'));
 141          $this->assertTrue($store->release_lock('lock', '123'));
 142      }
 143  
 144      /**
 145       * Tests the get_last_io_bytes function when not using compression (just returns unknown).
 146       */
 147      public function test_get_last_io_bytes(): void {
 148          $store = $this->create_cachestore_redis();
 149  
 150          $store->set('foo', [1, 2, 3, 4]);
 151          $this->assertEquals(\cache_store::IO_BYTES_NOT_SUPPORTED, $store->get_last_io_bytes());
 152          $store->get('foo');
 153          $this->assertEquals(\cache_store::IO_BYTES_NOT_SUPPORTED, $store->get_last_io_bytes());
 154      }
 155  
 156      /**
 157       * Tests the get_last_io_bytes byte count when using compression.
 158       */
 159      public function test_get_last_io_bytes_compressed(): void {
 160          $store = $this->create_cachestore_redis(['compressor' => cachestore_redis::COMPRESSOR_PHP_GZIP]);
 161  
 162          $alphabet = 'abcdefghijklmnopqrstuvwxyz';
 163  
 164          $store->set('small', $alphabet);
 165          $store->set('large', str_repeat($alphabet, 10));
 166  
 167          $store->get('small');
 168          // Interesting 'compression'.
 169          $this->assertEquals(54, $store->get_last_io_bytes());
 170          $store->get('large');
 171          // This one is actually smaller than uncompressed value!
 172          $this->assertEquals(57, $store->get_last_io_bytes());
 173          $store->get_many(['small', 'large']);
 174          $this->assertEquals(111, $store->get_last_io_bytes());
 175  
 176          $store->set('small', str_repeat($alphabet, 2));
 177          $this->assertEquals(56, $store->get_last_io_bytes());
 178          $store->set_many([
 179                  ['key' => 'small', 'value' => $alphabet],
 180                  ['key' => 'large', 'value' => str_repeat($alphabet, 10)]
 181          ]);
 182          $this->assertEquals(111, $store->get_last_io_bytes());
 183      }
 184  
 185      /**
 186       * Data provider for whether cache uses TTL or not.
 187       *
 188       * @return array Array with true and false options
 189       */
 190      public static function ttl_or_not(): array {
 191          return [
 192              [false],
 193              [true]
 194          ];
 195      }
 196  
 197      /**
 198       * Tests the delete_many function.
 199       *
 200       * The behaviour is different with TTL enabled so we need to test with that kind of definition
 201       * as well as a 'normal' one.
 202       *
 203       * @param bool $ttl True to test using a TTL definition
 204       * @dataProvider ttl_or_not
 205       */
 206      public function test_delete_many(bool $ttl): void {
 207          $store = $this->create_cachestore_redis([], $ttl);
 208  
 209          // Check it works to delete selected items.
 210          $store->set('foo', 'frog');
 211          $store->set('bar', 'amphibian');
 212          $store->set('hmm', 'undead');
 213          $this->store->delete_many(['foo', 'bar']);
 214          $this->assertFalse($store->get('foo'));
 215          $this->assertFalse($store->get('bar'));
 216          $this->assertEquals('undead', $store->get('hmm'));
 217  
 218          // If called with no keys it should do nothing.
 219          $store->delete_many([]);
 220          $this->assertEquals('undead', $store->get('hmm'));
 221      }
 222  
 223  }