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