2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
20 * Unit tests for our locking configuration.
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
{
30 * Tests the static parse charset method
33 public function test_lock_config() {
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';
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;
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');
81 $CFG->lock_factory
= $original;
83 unset($CFG->lock_factory
);
85 if ($originalfilelocking) {
86 $CFG->preventfilelocking
= $originalfilelocking;
88 unset($CFG->preventfilelocking
);