Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310]

   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   * Redis cache test.
  19   *
  20   * If you wish to use these unit tests all you need to do is add the following definition to
  21   * your config.php file.
  22   *
  23   * define('TEST_CACHESTORE_REDIS_TESTSERVERS', '127.0.0.1');
  24   *
  25   * @package   cachestore_redis
  26   * @copyright Copyright (c) 2015 Moodlerooms Inc. (http://www.moodlerooms.com)
  27   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  require_once (__DIR__.'/../../../tests/fixtures/stores.php');
  33  require_once (__DIR__.'/../lib.php');
  34  
  35  /**
  36   * Redis cache test.
  37   *
  38   * @package   cachestore_redis
  39   * @copyright Copyright (c) 2015 Moodlerooms Inc. (http://www.moodlerooms.com)
  40   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class cachestore_redis_test extends cachestore_tests {
  43      /**
  44       * @var cachestore_redis
  45       */
  46      protected $store;
  47  
  48      /**
  49       * Returns the MongoDB class name
  50       *
  51       * @return string
  52       */
  53      protected function get_class_name() {
  54          return 'cachestore_redis';
  55      }
  56  
  57      public function setUp() {
  58          if (!cachestore_redis::are_requirements_met() || !defined('TEST_CACHESTORE_REDIS_TESTSERVERS')) {
  59              $this->markTestSkipped('Could not test cachestore_redis. Requirements are not met.');
  60          }
  61          parent::setUp();
  62      }
  63      protected function tearDown() {
  64          parent::tearDown();
  65  
  66          if ($this->store instanceof cachestore_redis) {
  67              $this->store->purge();
  68          }
  69      }
  70  
  71      /**
  72       * Creates the required cachestore for the tests to run against Redis.
  73       *
  74       * @return cachestore_redis
  75       */
  76      protected function create_cachestore_redis() {
  77          /** @var cache_definition $definition */
  78          $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_redis', 'phpunit_test');
  79          $store = new cachestore_redis('Test', cachestore_redis::unit_test_configuration());
  80          $store->initialise($definition);
  81  
  82          $this->store = $store;
  83  
  84          if (!$store) {
  85              $this->markTestSkipped();
  86          }
  87  
  88          return $store;
  89      }
  90  
  91      public function test_has() {
  92          $store = $this->create_cachestore_redis();
  93  
  94          $this->assertTrue($store->set('foo', 'bar'));
  95          $this->assertTrue($store->has('foo'));
  96          $this->assertFalse($store->has('bat'));
  97      }
  98  
  99      public function test_has_any() {
 100          $store = $this->create_cachestore_redis();
 101  
 102          $this->assertTrue($store->set('foo', 'bar'));
 103          $this->assertTrue($store->has_any(array('bat', 'foo')));
 104          $this->assertFalse($store->has_any(array('bat', 'baz')));
 105      }
 106  
 107      public function test_has_all() {
 108          $store = $this->create_cachestore_redis();
 109  
 110          $this->assertTrue($store->set('foo', 'bar'));
 111          $this->assertTrue($store->set('bat', 'baz'));
 112          $this->assertTrue($store->has_all(array('foo', 'bat')));
 113          $this->assertFalse($store->has_all(array('foo', 'bat', 'this')));
 114      }
 115  
 116      public function test_lock() {
 117          $store = $this->create_cachestore_redis();
 118  
 119          $this->assertTrue($store->acquire_lock('lock', '123'));
 120          $this->assertTrue($store->check_lock_state('lock', '123'));
 121          $this->assertFalse($store->check_lock_state('lock', '321'));
 122          $this->assertNull($store->check_lock_state('notalock', '123'));
 123          $this->assertFalse($store->release_lock('lock', '321'));
 124          $this->assertTrue($store->release_lock('lock', '123'));
 125      }
 126  }