Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  // This mongodb 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_mongodb;
  18  
  19  use cache_store;
  20  use cache_definition;
  21  use cachestore_mongodb;
  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/mongodb/lib.php');
  29  
  30  /**
  31   * MongoDB unit test class.
  32   *
  33   * If you wish to use these unit tests all you need to do is add the following definition to
  34   * your config.php file.
  35   *
  36   * define('TEST_CACHESTORE_MONGODB_TESTSERVER', 'mongodb://localhost:27017');
  37   *
  38   * @package    cachestore_mongodb
  39   * @copyright  2013 Sam Hemelryk
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class store_test extends \cachestore_tests {
  43      /**
  44       * Returns the MongoDB class name
  45       * @return string
  46       */
  47      protected function get_class_name() {
  48          return 'cachestore_mongodb';
  49      }
  50  
  51      /**
  52       * A small additional test to make sure definitions that hash a hash starting with a number work OK
  53       */
  54      public function test_collection_name() {
  55          // This generates a definition that has a hash starting with a number. MDL-46208.
  56          $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_mongodb', 'abc');
  57          $instance = new cachestore_mongodb('MongoDB_Test', cachestore_mongodb::unit_test_configuration());
  58  
  59          if (!$instance->is_ready()) {
  60              $this->markTestSkipped();
  61          }
  62          $instance->initialise($definition);
  63  
  64          $this->assertTrue($instance->set(1, 'alpha'));
  65          $this->assertTrue($instance->set(2, 'beta'));
  66          $this->assertEquals('alpha', $instance->get(1));
  67          $this->assertEquals('beta', $instance->get(2));
  68          $this->assertEquals(array(
  69              1 => 'alpha',
  70              2 => 'beta'
  71          ), $instance->get_many(array(1, 2)));
  72      }
  73  }