Merge branch 'MDL-76985-MOODLE_400_STABLE' of https://github.com/sh-csg/moodle into...
[moodle.git] / lib / tests / lock_config_test.php
blob39c0dab3326ce80aeefd4c81c421aacac6beae0f
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.
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/>.
17 namespace core;
19 /**
20 * Unit tests for our locking configuration.
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
27 class lock_config_test extends \advanced_testcase {
29 /**
30 * Tests the static parse charset method
31 * @return void
33 public function test_lock_config() {
34 global $CFG;
35 $original = null;
36 if (isset($CFG->lock_factory)) {
37 $original = $CFG->lock_factory;
39 $originalfilelocking = null;
40 if (isset($CFG->preventfilelocking)) {
41 $originalfilelocking = $CFG->preventfilelocking;
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');
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);
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 $this->assertTrue($factory instanceof \core\lock\file_lock_factory,
63 'Get an explicit file lock factory');
65 // Test explicit file locks but with file locks prevented.
66 $CFG->preventfilelocking = 1;
67 try {
68 $factory = \core\lock\lock_config::get_lock_factory('cache');
69 $this->fail('Exception expected');
70 } catch (\moodle_exception $ex) {
71 $this->assertInstanceOf('coding_exception', $ex);
74 // Test explicit db locks.
75 $CFG->lock_factory = '\core\lock\db_record_lock_factory';
76 $factory = \core\lock\lock_config::get_lock_factory('cache');
77 $this->assertTrue($factory instanceof \core\lock\db_record_lock_factory,
78 'Get an explicit db record lock factory');
80 if ($original) {
81 $CFG->lock_factory = $original;
82 } else {
83 unset($CFG->lock_factory);
85 if ($originalfilelocking) {
86 $CFG->preventfilelocking = $originalfilelocking;
87 } else {
88 unset($CFG->preventfilelocking);