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(): void
{
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 if ($factory instanceof \core\lock\timing_wrapper_lock_factory
) {
63 $factory = $factory->get_real_factory();
65 $this->assertTrue($factory instanceof \core\lock\file_lock_factory
,
66 'Get an explicit file lock factory');
68 // Test explicit file locks but with file locks prevented.
69 $CFG->preventfilelocking
= 1;
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);
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();
83 $this->assertTrue($factory instanceof \core\lock\db_record_lock_factory
,
84 'Get an explicit db record lock factory');
87 $CFG->lock_factory
= $original;
89 unset($CFG->lock_factory
);
91 if ($originalfilelocking) {
92 $CFG->preventfilelocking
= $originalfilelocking;
94 unset($CFG->preventfilelocking
);