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.

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   * APCu unit tests.
  19   *
  20   * @package    cachestore_apcu
  21   * @copyright  2014 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/apcu/lib.php');
  31  
  32  /**
  33   * APC unit test class.
  34   *
  35   * @package    cachestore_apcu
  36   * @copyright  2014 Sam Hemelryk
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class cachestore_apcu_test extends cachestore_tests {
  40      /**
  41       * Returns the apcu class name
  42       * @return string
  43       */
  44      protected function get_class_name() {
  45          return 'cachestore_apcu';
  46      }
  47  
  48      public function setUp(): void {
  49          if (!cachestore_apcu::are_requirements_met()) {
  50              $this->markTestSkipped('Could not test cachestore_apcu. Requirements are not met.');
  51          }
  52          parent::setUp();
  53      }
  54  
  55      /**
  56       * Test that the Moodle APCu store doesn't cross paths with other code using APCu as well.
  57       */
  58      public function test_cross_application_interaction() {
  59          $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_apcu', 'phpunit_test');
  60          $instance = new cachestore_apcu('Test', cachestore_apcu::unit_test_configuration());
  61          $instance->initialise($definition);
  62  
  63          // Test purge with custom data.
  64          $this->assertTrue($instance->set('test', 'monster'));
  65          $this->assertSame('monster', $instance->get('test'));
  66          $this->assertTrue(apcu_store('test', 'pirate', 180));
  67          $this->assertSame('monster', $instance->get('test'));
  68          $this->assertTrue(apcu_exists('test'));
  69          $this->assertSame('pirate', apcu_fetch('test'));
  70          // Purge and check that our data is gone but the the custom data is still there.
  71          $this->assertTrue($instance->purge());
  72          $this->assertFalse($instance->get('test'));
  73          $this->assertTrue(apcu_exists('test'));
  74          $this->assertSame('pirate', apcu_fetch('test'));
  75      }
  76  
  77      public function test_different_caches_have_different_prefixes() {
  78          $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_apcu', 'phpunit_test');
  79          $instance = new cachestore_apcu('Test', cachestore_apcu::unit_test_configuration());
  80          $instance->initialise($definition);
  81  
  82          $definition2 = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_apcu', 'phpunit_test2');
  83          $instance2 = new cachestore_apcu('Test', cachestore_apcu::unit_test_configuration());
  84          $instance2->initialise($definition2);
  85  
  86          $instance->set('test1', 1);
  87          $this->assertFalse($instance2->get('test1'));
  88          $instance2->purge();
  89          $this->assertSame(1, $instance->get('test1'));
  90      }
  91  }