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 session 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_session;
  18  
  19  use cache_store;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  // Include the necessary evils.
  24  global $CFG;
  25  require_once($CFG->dirroot.'/cache/tests/fixtures/stores.php');
  26  require_once($CFG->dirroot.'/cache/stores/session/lib.php');
  27  
  28  /**
  29   * Session unit test class.
  30   *
  31   * @package    cachestore_session
  32   * @copyright  2013 Sam Hemelryk
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class store_test extends \cachestore_tests {
  36      /**
  37       * Returns the session class name
  38       * @return string
  39       */
  40      protected function get_class_name() {
  41          return 'cachestore_session';
  42      }
  43  
  44      /**
  45       * Test the maxsize option.
  46       */
  47      public function test_maxsize() {
  48          $config = \cache_config_testing::instance();
  49          $config->phpunit_add_definition('phpunit/one', array(
  50              'mode' => cache_store::MODE_SESSION,
  51              'component' => 'phpunit',
  52              'area' => 'one',
  53              'maxsize' => 3
  54          ));
  55  
  56          $config->phpunit_add_definition('phpunit/two', array(
  57              'mode' => cache_store::MODE_SESSION,
  58              'component' => 'phpunit',
  59              'area' => 'two',
  60              'maxsize' => 3
  61          ));
  62  
  63          $cacheone = \cache::make('phpunit', 'one');
  64  
  65          $this->assertTrue($cacheone->set('key1', 'value1'));
  66          $this->assertTrue($cacheone->set('key2', 'value2'));
  67          $this->assertTrue($cacheone->set('key3', 'value3'));
  68  
  69          $this->assertTrue($cacheone->has('key1'));
  70          $this->assertTrue($cacheone->has('key2'));
  71          $this->assertTrue($cacheone->has('key3'));
  72  
  73          $this->assertTrue($cacheone->set('key4', 'value4'));
  74          $this->assertTrue($cacheone->set('key5', 'value5'));
  75  
  76          $this->assertFalse($cacheone->has('key1'));
  77          $this->assertFalse($cacheone->has('key2'));
  78          $this->assertTrue($cacheone->has('key3'));
  79          $this->assertTrue($cacheone->has('key4'));
  80          $this->assertTrue($cacheone->has('key5'));
  81  
  82          $this->assertFalse($cacheone->get('key1'));
  83          $this->assertFalse($cacheone->get('key2'));
  84          $this->assertEquals('value3', $cacheone->get('key3'));
  85          $this->assertEquals('value4', $cacheone->get('key4'));
  86          $this->assertEquals('value5', $cacheone->get('key5'));
  87  
  88          // Test adding one more.
  89          $this->assertTrue($cacheone->set('key6', 'value6'));
  90          $this->assertFalse($cacheone->get('key3'));
  91  
  92          // Test reducing and then adding to make sure we don't lost one.
  93          $this->assertTrue($cacheone->delete('key6'));
  94          $this->assertTrue($cacheone->set('key7', 'value7'));
  95          $this->assertEquals('value4', $cacheone->get('key4'));
  96  
  97          // Set the same key three times to make sure it doesn't count overrides.
  98          for ($i = 0; $i < 3; $i++) {
  99              $this->assertTrue($cacheone->set('key8', 'value8'));
 100          }
 101          $this->assertEquals('value7', $cacheone->get('key7'), 'Overrides are incorrectly incrementing size');
 102  
 103          // Test adding many.
 104          $this->assertEquals(3, $cacheone->set_many(array(
 105              'keyA' => 'valueA',
 106              'keyB' => 'valueB',
 107              'keyC' => 'valueC'
 108          )));
 109          $this->assertEquals(array(
 110              'key4' => false,
 111              'key5' => false,
 112              'key6' => false,
 113              'key7' => false,
 114              'keyA' => 'valueA',
 115              'keyB' => 'valueB',
 116              'keyC' => 'valueC'
 117          ), $cacheone->get_many(array(
 118              'key4', 'key5', 'key6', 'key7', 'keyA', 'keyB', 'keyC'
 119          )));
 120  
 121          $cachetwo = \cache::make('phpunit', 'two');
 122  
 123          // Test adding many.
 124          $this->assertEquals(3, $cacheone->set_many(array(
 125              'keyA' => 'valueA',
 126              'keyB' => 'valueB',
 127              'keyC' => 'valueC'
 128          )));
 129  
 130          $this->assertEquals(3, $cachetwo->set_many(array(
 131              'key1' => 'value1',
 132              'key2' => 'value2',
 133              'key3' => 'value3'
 134          )));
 135  
 136          $this->assertEquals(array(
 137              'keyA' => 'valueA',
 138              'keyB' => 'valueB',
 139              'keyC' => 'valueC'
 140          ), $cacheone->get_many(array(
 141              'keyA', 'keyB', 'keyC'
 142          )));
 143  
 144          $this->assertEquals(array(
 145              'key1' => 'value1',
 146              'key2' => 'value2',
 147              'key3' => 'value3'
 148          ), $cachetwo->get_many(array(
 149              'key1', 'key2', 'key3'
 150          )));
 151  
 152          // Test that that cache deletes element that was least recently accessed.
 153          $this->assertEquals('valueA', $cacheone->get('keyA'));
 154          $cacheone->set('keyD', 'valueD');
 155          $this->assertEquals('valueA', $cacheone->get('keyA'));
 156          $this->assertFalse($cacheone->get('keyB'));
 157          $this->assertEquals(array('keyD' => 'valueD', 'keyC' => 'valueC'), $cacheone->get_many(array('keyD', 'keyC')));
 158          $cacheone->set('keyE', 'valueE');
 159          $this->assertFalse($cacheone->get('keyB'));
 160          $this->assertFalse($cacheone->get('keyA'));
 161          $this->assertEquals(array('keyA' => false, 'keyE' => 'valueE', 'keyD' => 'valueD', 'keyC' => 'valueC'),
 162                  $cacheone->get_many(array('keyA', 'keyE', 'keyD', 'keyC')));
 163          // Overwrite keyE (moves it to the end of array), and set keyF.
 164          $cacheone->set_many(array('keyE' => 'valueE', 'keyF' => 'valueF'));
 165          $this->assertEquals(array('keyC' => 'valueC', 'keyE' => 'valueE', 'keyD' => false, 'keyF' => 'valueF'),
 166                  $cacheone->get_many(array('keyC', 'keyE', 'keyD', 'keyF')));
 167      }
 168  
 169      public function test_ttl() {
 170          $config = \cache_config_testing::instance();
 171          $config->phpunit_add_definition('phpunit/three', array(
 172              'mode' => cache_store::MODE_SESSION,
 173              'component' => 'phpunit',
 174              'area' => 'three',
 175              'maxsize' => 3,
 176              'ttl' => 3
 177          ));
 178  
 179          $cachethree = \cache::make('phpunit', 'three');
 180  
 181          // Make sure that when cache with ttl is full the elements that were added first are deleted first regardless of access time.
 182          $cachethree->set('key1', 'value1');
 183          $cachethree->set('key2', 'value2');
 184          $cachethree->set('key3', 'value3');
 185          $cachethree->set('key4', 'value4');
 186          $this->assertFalse($cachethree->get('key1'));
 187          $this->assertEquals('value4', $cachethree->get('key4'));
 188          $cachethree->set('key5', 'value5');
 189          $this->assertFalse($cachethree->get('key2'));
 190          $this->assertEquals('value4', $cachethree->get('key4'));
 191          $cachethree->set_many(array('key6' => 'value6', 'key7' => 'value7'));
 192          $this->assertEquals(array('key3' => false, 'key4' => false, 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7'),
 193                  $cachethree->get_many(array('key3', 'key4', 'key5', 'key6', 'key7')));
 194      }
 195  }