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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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   * lock unit tests
  19   *
  20   * @package    core
  21   * @category   lock
  22   * @copyright  2013 Damyon Wiese
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  
  29  /**
  30   * Unit tests for our locking configuration.
  31   *
  32   * @package    core
  33   * @category   lock
  34   * @copyright  2013 Damyon Wiese
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class lock_config_testcase extends advanced_testcase {
  38  
  39      /**
  40       * Tests the static parse charset method
  41       * @return void
  42       */
  43      public function test_lock_config() {
  44          global $CFG;
  45          $original = null;
  46          if (isset($CFG->lock_factory)) {
  47              $original = $CFG->lock_factory;
  48          }
  49          $originalfilelocking = null;
  50          if (isset($CFG->preventfilelocking)) {
  51              $originalfilelocking = $CFG->preventfilelocking;
  52          }
  53  
  54          // Test no configuration.
  55          unset($CFG->lock_factory);
  56          $CFG->preventfilelocking = 0;
  57          $factory = \core\lock\lock_config::get_lock_factory('cache');
  58          $this->assertNotEmpty($factory, 'Get a default factory with no configuration');
  59  
  60          // Test explicit broken lock.
  61          $CFG->lock_factory = '\core\lock\not_a_lock_factory';
  62          try {
  63              $factory = \core\lock\lock_config::get_lock_factory('cache');
  64              $this->fail('Exception expected');
  65          } catch (moodle_exception $ex) {
  66              $this->assertInstanceOf('coding_exception', $ex);
  67          }
  68  
  69          // Test explicit file locks.
  70          $CFG->lock_factory = '\core\lock\file_lock_factory';
  71          $factory = \core\lock\lock_config::get_lock_factory('cache');
  72          $this->assertTrue($factory instanceof \core\lock\file_lock_factory,
  73                  'Get an explicit file lock factory');
  74  
  75          // Test explicit file locks but with file locks prevented.
  76          $CFG->preventfilelocking = 1;
  77          try {
  78              $factory = \core\lock\lock_config::get_lock_factory('cache');
  79              $this->fail('Exception expected');
  80          } catch (moodle_exception $ex) {
  81              $this->assertInstanceOf('coding_exception', $ex);
  82          }
  83  
  84          // Test explicit db locks.
  85          $CFG->lock_factory = '\core\lock\db_record_lock_factory';
  86          $factory = \core\lock\lock_config::get_lock_factory('cache');
  87          $this->assertTrue($factory instanceof \core\lock\db_record_lock_factory,
  88                  'Get an explicit db record lock factory');
  89  
  90          if ($original) {
  91              $CFG->lock_factory = $original;
  92          } else {
  93              unset($CFG->lock_factory);
  94          }
  95          if ($originalfilelocking) {
  96              $CFG->preventfilelocking = $originalfilelocking;
  97          } else {
  98              unset($CFG->preventfilelocking);
  99          }
 100      }
 101  }
 102