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 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  /**
  18   * MongoDB unit tests.
  19   *
  20   * If you wish to use these unit tests all you need to do is add the following definition to
  21   * your config.php file.
  22   *
  23   * define('TEST_CACHESTORE_MONGODB_TESTSERVER', 'mongodb://localhost:27017');
  24   *
  25   * @package    cachestore_mongodb
  26   * @copyright  2013 Sam Hemelryk
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  // Include the necessary evils.
  33  global $CFG;
  34  require_once($CFG->dirroot.'/cache/tests/fixtures/stores.php');
  35  require_once($CFG->dirroot.'/cache/stores/mongodb/lib.php');
  36  
  37  /**
  38   * MongoDB unit test class.
  39   *
  40   * @package    cachestore_mongodb
  41   * @copyright  2013 Sam Hemelryk
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class cachestore_mongodb_test extends cachestore_tests {
  45      /**
  46       * Returns the MongoDB class name
  47       * @return string
  48       */
  49      protected function get_class_name() {
  50          return 'cachestore_mongodb';
  51      }
  52  
  53      /**
  54       * A small additional test to make sure definitions that hash a hash starting with a number work OK
  55       */
  56      public function test_collection_name() {
  57          // This generates a definition that has a hash starting with a number. MDL-46208.
  58          $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_mongodb', 'abc');
  59          $instance = new cachestore_mongodb('MongoDB_Test', cachestore_mongodb::unit_test_configuration());
  60  
  61          if (!$instance->is_ready()) {
  62              $this->markTestSkipped();
  63          }
  64          $instance->initialise($definition);
  65  
  66          $this->assertTrue($instance->set(1, 'alpha'));
  67          $this->assertTrue($instance->set(2, 'beta'));
  68          $this->assertEquals('alpha', $instance->get(1));
  69          $this->assertEquals('beta', $instance->get(2));
  70          $this->assertEquals(array(
  71              1 => 'alpha',
  72              2 => 'beta'
  73          ), $instance->get_many(array(1, 2)));
  74      }
  75  }