1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ConfigTest.php |
4
|
|
|
* |
5
|
|
|
* Tests for LibreNMS\Config |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
* |
20
|
|
|
* @package LibreNMS |
21
|
|
|
* @link http://librenms.org |
22
|
|
|
* @copyright 2017 Tony Murray |
23
|
|
|
* @author Tony Murray <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace LibreNMS\Tests; |
27
|
|
|
|
28
|
|
|
use LibreNMS\Config; |
29
|
|
|
use LibreNMS\DB\Eloquent; |
30
|
|
|
use ReflectionClass; |
31
|
|
|
|
32
|
|
|
class ConfigTest extends TestCase |
33
|
|
|
{ |
34
|
|
|
private $config; |
35
|
|
|
|
36
|
|
|
public function setUp(): void |
37
|
|
|
{ |
38
|
|
|
parent::setUp(); |
39
|
|
|
$this->config = new \ReflectionProperty(Config::class, 'config'); |
40
|
|
|
$this->config->setAccessible(true); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testGetBasic() |
44
|
|
|
{ |
45
|
|
|
$dir = realpath(__DIR__ . '/..'); |
46
|
|
|
$this->assertEquals($dir, Config::get('install_dir')); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testSetBasic() |
50
|
|
|
{ |
51
|
|
|
Config::set('basics', 'first'); |
52
|
|
|
$this->assertEquals('first', $this->config->getValue()['basics']); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testGet() |
56
|
|
|
{ |
57
|
|
|
$this->setConfig(function (&$config) { |
58
|
|
|
$config['one']['two']['three'] = 'easy'; |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
$this->assertEquals('easy', Config::get('one.two.three')); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testGetDeviceSetting() |
65
|
|
|
{ |
66
|
|
|
$device = array('set' => true, 'null' => null); |
67
|
|
|
$this->setConfig(function (&$config) { |
68
|
|
|
$config['null'] = 'notnull!'; |
69
|
|
|
$config['noprefix'] = true; |
70
|
|
|
$config['prefix']['global'] = true; |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
$this->assertNull(Config::getDeviceSetting($device, 'unset'), 'Non-existing settings should return null'); |
74
|
|
|
$this->assertTrue(Config::getDeviceSetting($device, 'set'), 'Could not get setting from device array'); |
75
|
|
|
$this->assertTrue(Config::getDeviceSetting($device, 'noprefix'), 'Failed to get setting from global config'); |
76
|
|
|
$this->assertEquals( |
77
|
|
|
'notnull!', |
78
|
|
|
Config::getDeviceSetting($device, 'null'), |
79
|
|
|
'Null variables should defer to the global setting' |
80
|
|
|
); |
81
|
|
|
$this->assertTrue( |
82
|
|
|
Config::getDeviceSetting($device, 'global', 'prefix'), |
83
|
|
|
'Failed to get setting from global config with a prefix' |
84
|
|
|
); |
85
|
|
|
$this->assertEquals( |
86
|
|
|
'default', |
87
|
|
|
Config::getDeviceSetting($device, 'something', 'else', 'default'), |
88
|
|
|
'Failed to return the default argument' |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testGetOsSetting() |
93
|
|
|
{ |
94
|
|
|
$this->setConfig(function (&$config) { |
95
|
|
|
$config['os']['nullos']['fancy'] = true; |
96
|
|
|
$config['fallback'] = true; |
97
|
|
|
}); |
98
|
|
|
|
99
|
|
|
$this->assertNull(Config::getOsSetting(null, 'unset'), '$os is null, should return null'); |
100
|
|
|
$this->assertNull(Config::getOsSetting('nullos', 'unset'), 'Non-existing settings should return null'); |
101
|
|
|
$this->assertFalse(Config::getOsSetting('nullos', 'unset', false), 'Non-existing settings should return $default'); |
102
|
|
|
$this->assertTrue(Config::getOsSetting('nullos', 'fancy'), 'Failed to get setting'); |
103
|
|
|
$this->assertTrue(Config::getOsSetting('nullos', 'fallback'), 'Failed to fallback to global setting'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testGetCombined() |
107
|
|
|
{ |
108
|
|
|
$this->setConfig(function (&$config) { |
109
|
|
|
$config['num'] = array('one', 'two'); |
110
|
|
|
$config['os']['nullos']['num'] = array('two', 'three'); |
111
|
|
|
$config['assoc'] = array('a' => 'same', 'b' => 'same'); |
112
|
|
|
$config['os']['nullos']['assoc'] = array('b' => 'different', 'c' => 'still same'); |
113
|
|
|
$config['os']['nullos']['osset'] = true; |
114
|
|
|
$config['gset'] = true; |
115
|
|
|
}); |
116
|
|
|
|
117
|
|
|
$this->assertTrue(Config::getCombined('nullos', 'non-existent', true), 'Did not return default value on non-existent key'); |
|
|
|
|
118
|
|
|
$this->assertTrue(Config::getCombined('nullos', 'osset', false), 'Did not return OS value when global value is not set'); |
|
|
|
|
119
|
|
|
$this->assertTrue(Config::getCombined('nullos', 'gset', false), 'Did not return global value when OS value is not set'); |
120
|
|
|
|
121
|
|
|
$combined = Config::getCombined('nullos', 'num'); |
122
|
|
|
sort($combined); |
123
|
|
|
$this->assertEquals(array('one', 'three', 'two'), $combined); |
124
|
|
|
|
125
|
|
|
$this->assertSame(array('a' => 'same', 'b' => 'different', 'c' => 'still same'), Config::getCombined('nullos', 'assoc')); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testSet() |
129
|
|
|
{ |
130
|
|
|
Config::set('you.and.me', "I'll be there"); |
131
|
|
|
|
132
|
|
|
$this->assertEquals("I'll be there", $this->config->getValue()['you']['and']['me']); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testSetPersist() |
136
|
|
|
{ |
137
|
|
|
$this->dbSetUp(); |
138
|
|
|
|
139
|
|
|
$key = 'testing.persist'; |
140
|
|
|
|
141
|
|
|
$query = \App\Models\Config::query()->where('config_name', $key); |
142
|
|
|
|
143
|
|
|
$query->delete(); |
144
|
|
|
$this->assertFalse($query->exists(), "$key should not be set, clean database"); |
145
|
|
|
Config::set($key, 'one', true); |
146
|
|
|
$this->assertEquals('one', $query->value('config_value')); |
147
|
|
|
Config::set($key, 'two', true); |
148
|
|
|
$this->assertEquals('two', $query->value('config_value')); |
149
|
|
|
|
150
|
|
|
$this->dbTearDown(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testHas() |
154
|
|
|
{ |
155
|
|
|
Config::set('long.key.setting', 'no one cares'); |
156
|
|
|
Config::set('null', null); |
157
|
|
|
|
158
|
|
|
$this->assertFalse(Config::has('null'), 'Keys set to null do not count as existing'); |
159
|
|
|
$this->assertTrue(Config::has('long'), 'Top level key should exist'); |
160
|
|
|
$this->assertTrue(Config::has('long.key.setting'), 'Exact exists on value'); |
161
|
|
|
$this->assertFalse(Config::has('long.key.setting.nothing'), 'Non-existent child setting'); |
162
|
|
|
|
163
|
|
|
$this->assertFalse(Config::has('off.the.wall'), 'Non-existent key'); |
164
|
|
|
$this->assertFalse(Config::has('off.the'), 'Config:has() should not modify the config'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testGetNonExistent() |
168
|
|
|
{ |
169
|
|
|
$this->assertNull(Config::get('There.is.no.way.this.is.a.key')); |
170
|
|
|
$this->assertFalse(Config::has('There.is.no')); // should not add kes when getting |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testGetNonExistentNested() |
174
|
|
|
{ |
175
|
|
|
$this->assertNull(Config::get('cheese.and.bologna')); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
public function testGetSubtree() |
180
|
|
|
{ |
181
|
|
|
Config::set('words.top', 'August'); |
182
|
|
|
Config::set('words.mid', 'And Everything'); |
183
|
|
|
Config::set('words.bot', 'After'); |
184
|
|
|
$expected = array( |
185
|
|
|
'top' => 'August', |
186
|
|
|
'mid' => 'And Everything', |
187
|
|
|
'bot' => 'After' |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
$this->assertEquals($expected, Config::get('words')); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Pass an anonymous function which will be passed the config variable to modify before it is set |
195
|
|
|
* @param callable $function |
196
|
|
|
*/ |
197
|
|
|
private function setConfig($function) |
198
|
|
|
{ |
199
|
|
|
$config = $this->config->getValue(); |
200
|
|
|
$function($config); |
201
|
|
|
$this->config->setValue($config); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|