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.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

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