Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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.
   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   * Static unit tests
  19   *
  20   * @package    cachestore_static
  21   * @copyright  2013 Sam Hemelryk
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  // Include the necessary evils.
  28  global $CFG;
  29  require_once($CFG->dirroot.'/cache/tests/fixtures/stores.php');
  30  require_once($CFG->dirroot.'/cache/stores/static/lib.php');
  31  
  32  /**
  33   * Static unit test class.
  34   *
  35   * @package    cachestore_static
  36   * @copyright  2013 Sam Hemelryk
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class cachestore_static_test extends cachestore_tests {
  40      /**
  41       * Returns the static class name
  42       * @return string
  43       */
  44      protected function get_class_name() {
  45          return 'cachestore_static';
  46      }
  47  
  48      /**
  49       * Test the maxsize option.
  50       */
  51      public function test_maxsize() {
  52          $defid = 'phpunit/testmaxsize';
  53          $config = cache_config_testing::instance();
  54          $config->phpunit_add_definition($defid, array(
  55              'mode' => cache_store::MODE_REQUEST,
  56              'component' => 'phpunit',
  57              'area' => 'testmaxsize',
  58              'maxsize' => 3
  59          ));
  60          $definition = cache_definition::load($defid, $config->get_definition_by_id($defid));
  61          $instance = cachestore_static::initialise_test_instance($definition);
  62  
  63          $this->assertTrue($instance->set('key1', 'value1'));
  64          $this->assertTrue($instance->set('key2', 'value2'));
  65          $this->assertTrue($instance->set('key3', 'value3'));
  66  
  67          $this->assertTrue($instance->has('key1'));
  68          $this->assertTrue($instance->has('key2'));
  69          $this->assertTrue($instance->has('key3'));
  70  
  71          $this->assertTrue($instance->set('key4', 'value4'));
  72          $this->assertTrue($instance->set('key5', 'value5'));
  73  
  74          $this->assertFalse($instance->has('key1'));
  75          $this->assertFalse($instance->has('key2'));
  76          $this->assertTrue($instance->has('key3'));
  77          $this->assertTrue($instance->has('key4'));
  78          $this->assertTrue($instance->has('key5'));
  79  
  80          $this->assertFalse($instance->get('key1'));
  81          $this->assertFalse($instance->get('key2'));
  82          $this->assertEquals('value3', $instance->get('key3'));
  83          $this->assertEquals('value4', $instance->get('key4'));
  84          $this->assertEquals('value5', $instance->get('key5'));
  85  
  86          // Test adding one more.
  87          $this->assertTrue($instance->set('key6', 'value6'));
  88          $this->assertFalse($instance->get('key3'));
  89  
  90          // Test reducing and then adding to make sure we don't lost one.
  91          $this->assertTrue($instance->delete('key6'));
  92          $this->assertTrue($instance->set('key7', 'value7'));
  93          $this->assertEquals('value4', $instance->get('key4'));
  94  
  95          // Set the same key three times to make sure it doesn't count overrides.
  96          for ($i = 0; $i < 3; $i++) {
  97              $this->assertTrue($instance->set('key8', 'value8'));
  98          }
  99  
 100          $this->assertEquals('value7', $instance->get('key7'), 'Overrides are incorrectly incrementing size');
 101  
 102          // Test adding many.
 103          $this->assertEquals(3, $instance->set_many(array(
 104              array('key' => 'keyA', 'value' => 'valueA'),
 105              array('key' => 'keyB', 'value' => 'valueB'),
 106              array('key' => 'keyC', 'value' => 'valueC')
 107          )));
 108          $this->assertEquals(array(
 109              'key4' => false,
 110              'key5' => false,
 111              'key6' => false,
 112              'key7' => false,
 113              'keyA' => 'valueA',
 114              'keyB' => 'valueB',
 115              'keyC' => 'valueC'
 116          ), $instance->get_many(array(
 117              'key4', 'key5', 'key6', 'key7', 'keyA', 'keyB', 'keyC'
 118          )));
 119      }
 120  
 121      /**
 122       * Simple test to verify igbinary availability and check basic serialization is working ok.
 123       */
 124      public function test_igbinary_serializer() {
 125          // Skip if igbinary is not available.
 126          if (!extension_loaded('igbinary')) {
 127              $this->markTestSkipped('Cannot test igbinary serializer. Extension missing');
 128          }
 129          // Prepare the static instance.
 130          $defid = 'phpunit/igbinary';
 131          $config = cache_config_testing::instance();
 132          $config->phpunit_add_definition($defid, array(
 133              'mode' => cache_store::MODE_REQUEST,
 134              'component' => 'phpunit',
 135              'area' => 'testigbinary'
 136          ));
 137          $definition = cache_definition::load($defid, $config->get_definition_by_id($defid));
 138          $instance = cachestore_static::initialise_test_instance($definition);
 139          // Prepare an object.
 140          $obj = new stdClass();
 141          $obj->someint = 9;
 142          $obj->somestring = '99';
 143          $obj->somearray = [9 => 999, '99' => '9999'];
 144          // Serialize and set.
 145          $objser = igbinary_serialize($obj);
 146          $instance->set('testigbinary', $objser);
 147          // Get and unserialize.
 148          $res = $instance->get('testigbinary');
 149          $resunser = igbinary_unserialize($res);
 150          // Check expectations.
 151          $this->assertSame($objser, $res);     // Ok from cache (ig-serialized, 100% same string).
 152          $this->assertEquals($obj, $resunser); // Ok ig-unserialized (equal
 153          $this->assertNotSame($obj, $resunser);// but different objects, obviously).
 154      }
 155  }