2017-07-21 22:05:07 +00:00
< ? php
/**
* ConfigTest . php
*
* Tests for LibreNMS\Config
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2021-02-08 23:29:04 +00:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2017-07-21 22:05:07 +00:00
*
2021-02-08 23:29:04 +00:00
* @ link https :// www . librenms . org
2021-09-10 18:09:53 +00:00
*
2017-07-21 22:05:07 +00:00
* @ copyright 2017 Tony Murray
* @ author Tony Murray < murraytony @ gmail . com >
*/
namespace LibreNMS\Tests ;
use LibreNMS\Config ;
2019-10-13 13:40:38 +00:00
class ConfigTest extends TestCase
2017-07-21 22:05:07 +00:00
{
2019-10-13 13:40:38 +00:00
private $config ;
2020-05-19 14:31:50 +00:00
protected function setUp () : void
2019-10-13 13:40:38 +00:00
{
parent :: setUp ();
$this -> config = new \ReflectionProperty ( Config :: class , 'config' );
$this -> config -> setAccessible ( true );
}
2023-05-24 20:21:54 +00:00
public function testGetBasic () : void
2017-07-21 22:05:07 +00:00
{
$dir = realpath ( __DIR__ . '/..' );
$this -> assertEquals ( $dir , Config :: get ( 'install_dir' ));
}
2023-05-24 20:21:54 +00:00
public function testSetBasic () : void
2017-07-21 22:05:07 +00:00
{
Config :: set ( 'basics' , 'first' );
2019-10-13 13:40:38 +00:00
$this -> assertEquals ( 'first' , $this -> config -> getValue ()[ 'basics' ]);
2017-07-21 22:05:07 +00:00
}
2023-05-24 20:21:54 +00:00
public function testGet () : void
2017-07-21 22:05:07 +00:00
{
2019-10-13 13:40:38 +00:00
$this -> setConfig ( function ( & $config ) {
$config [ 'one' ][ 'two' ][ 'three' ] = 'easy' ;
});
2017-07-21 22:05:07 +00:00
$this -> assertEquals ( 'easy' , Config :: get ( 'one.two.three' ));
}
2023-05-24 20:21:54 +00:00
public function testGetDeviceSetting () : void
2017-07-21 22:05:07 +00:00
{
$device = [ 'set' => true , 'null' => null ];
2019-10-13 13:40:38 +00:00
$this -> setConfig ( function ( & $config ) {
$config [ 'null' ] = 'notnull!' ;
$config [ 'noprefix' ] = true ;
$config [ 'prefix' ][ 'global' ] = true ;
});
2017-07-21 22:05:07 +00:00
$this -> assertNull ( Config :: getDeviceSetting ( $device , 'unset' ), 'Non-existing settings should return null' );
$this -> assertTrue ( Config :: getDeviceSetting ( $device , 'set' ), 'Could not get setting from device array' );
$this -> assertTrue ( Config :: getDeviceSetting ( $device , 'noprefix' ), 'Failed to get setting from global config' );
$this -> assertEquals (
'notnull!' ,
Config :: getDeviceSetting ( $device , 'null' ),
'Null variables should defer to the global setting'
);
$this -> assertTrue (
Config :: getDeviceSetting ( $device , 'global' , 'prefix' ),
'Failed to get setting from global config with a prefix'
);
$this -> assertEquals (
'default' ,
Config :: getDeviceSetting ( $device , 'something' , 'else' , 'default' ),
'Failed to return the default argument'
);
}
2023-05-24 20:21:54 +00:00
public function testGetOsSetting () : void
2017-07-21 22:05:07 +00:00
{
2019-10-13 13:40:38 +00:00
$this -> setConfig ( function ( & $config ) {
$config [ 'os' ][ 'nullos' ][ 'fancy' ] = true ;
$config [ 'fallback' ] = true ;
});
2017-07-21 22:05:07 +00:00
2017-09-13 18:43:21 +00:00
$this -> assertNull ( Config :: getOsSetting ( null , 'unset' ), '$os is null, should return null' );
$this -> assertNull ( Config :: getOsSetting ( 'nullos' , 'unset' ), 'Non-existing settings should return null' );
$this -> assertFalse ( Config :: getOsSetting ( 'nullos' , 'unset' , false ), 'Non-existing settings should return $default' );
$this -> assertTrue ( Config :: getOsSetting ( 'nullos' , 'fancy' ), 'Failed to get setting' );
2020-05-19 19:35:32 +00:00
$this -> assertNull ( Config :: getOsSetting ( 'nullos' , 'fallback' ), 'Incorrectly loaded global setting' );
2017-09-13 18:43:21 +00:00
}
2022-04-22 02:49:26 +00:00
public function testGetCombined () : void
2017-09-13 18:43:21 +00:00
{
2019-10-13 13:40:38 +00:00
$this -> setConfig ( function ( & $config ) {
$config [ 'num' ] = [ 'one' , 'two' ];
2022-04-22 02:49:26 +00:00
$config [ 'withprefix' ][ 'num' ] = [ 'four' , 'five' ];
2019-10-13 13:40:38 +00:00
$config [ 'os' ][ 'nullos' ][ 'num' ] = [ 'two' , 'three' ];
$config [ 'assoc' ] = [ 'a' => 'same' , 'b' => 'same' ];
2022-04-22 02:49:26 +00:00
$config [ 'withprefix' ][ 'assoc' ] = [ 'a' => 'prefix_same' , 'd' => 'prefix_same' ];
2019-10-13 13:40:38 +00:00
$config [ 'os' ][ 'nullos' ][ 'assoc' ] = [ 'b' => 'different' , 'c' => 'still same' ];
2022-04-22 02:49:26 +00:00
$config [ 'os' ][ 'nullos' ][ 'osset' ] = 'ossetting' ;
$config [ 'gset' ] = 'fallbackone' ;
$config [ 'withprefix' ][ 'gset' ] = 'fallbacktwo' ;
2019-10-13 13:40:38 +00:00
});
2017-09-21 17:02:01 +00:00
2022-04-22 02:49:26 +00:00
$this -> assertSame ([ 'default' ], Config :: getCombined ( 'nullos' , 'non-existent' , '' , [ 'default' ]), 'Did not return default value on non-existent key' );
$this -> assertSame ([ 'ossetting' ], Config :: getCombined ( 'nullos' , 'osset' , '' , [ 'default' ]), 'Did not return OS value when global value is not set' );
$this -> assertSame ([ 'fallbackone' ], Config :: getCombined ( 'nullos' , 'gset' , '' , [ 'default' ]), 'Did not return global value when OS value is not set' );
$this -> assertSame ([ 'default' ], Config :: getCombined ( 'nullos' , 'non-existent' , 'withprefix.' , [ 'default' ]), 'Did not return default value on non-existent key' );
$this -> assertSame ([ 'ossetting' ], Config :: getCombined ( 'nullos' , 'osset' , 'withprefix.' , [ 'default' ]), 'Did not return OS value when global value is not set' );
$this -> assertSame ([ 'fallbacktwo' ], Config :: getCombined ( 'nullos' , 'gset' , 'withprefix.' , [ 'default' ]), 'Did not return global value when OS value is not set' );
2017-09-13 18:43:21 +00:00
2019-10-13 13:40:38 +00:00
$combined = Config :: getCombined ( 'nullos' , 'num' );
2017-09-13 18:43:21 +00:00
sort ( $combined );
$this -> assertEquals ([ 'one' , 'three' , 'two' ], $combined );
2022-04-22 02:49:26 +00:00
$combined = Config :: getCombined ( 'nullos' , 'num' , 'withprefix.' );
sort ( $combined );
$this -> assertEquals ([ 'five' , 'four' , 'three' , 'two' ], $combined );
2017-09-13 18:43:21 +00:00
$this -> assertSame ([ 'a' => 'same' , 'b' => 'different' , 'c' => 'still same' ], Config :: getCombined ( 'nullos' , 'assoc' ));
2022-04-22 02:49:26 +00:00
// should associative not ignore same values (d=>prefix_same)? are associative arrays actually used?
$this -> assertSame ([ 'a' => 'prefix_same' , 'b' => 'different' , 'c' => 'still same' ], Config :: getCombined ( 'nullos' , 'assoc' , 'withprefix.' ));
2017-07-21 22:05:07 +00:00
}
2023-05-24 20:21:54 +00:00
public function testSet () : void
2017-07-21 22:05:07 +00:00
{
Config :: set ( 'you.and.me' , " I'll be there " );
2019-10-13 13:40:38 +00:00
$this -> assertEquals ( " I'll be there " , $this -> config -> getValue ()[ 'you' ][ 'and' ][ 'me' ]);
2017-07-21 22:05:07 +00:00
}
2023-05-24 20:21:54 +00:00
public function testSetPersist () : void
2017-11-01 21:56:47 +00:00
{
2018-08-17 20:29:20 +00:00
$this -> dbSetUp ();
2017-11-01 21:56:47 +00:00
$key = 'testing.persist' ;
2019-10-06 21:51:22 +00:00
$query = \App\Models\Config :: query () -> where ( 'config_name' , $key );
2018-05-09 13:05:17 +00:00
$query -> delete ();
$this -> assertFalse ( $query -> exists (), " $key should not be set, clean database " );
2019-10-16 21:22:05 +00:00
Config :: persist ( $key , 'one' );
2018-05-09 13:05:17 +00:00
$this -> assertEquals ( 'one' , $query -> value ( 'config_value' ));
2019-10-16 21:22:05 +00:00
Config :: persist ( $key , 'two' );
2018-05-09 13:05:17 +00:00
$this -> assertEquals ( 'two' , $query -> value ( 'config_value' ));
2017-11-01 21:56:47 +00:00
2018-08-17 20:29:20 +00:00
$this -> dbTearDown ();
2017-11-01 21:56:47 +00:00
}
2023-05-24 20:21:54 +00:00
public function testHas () : void
2017-07-21 22:05:07 +00:00
{
Config :: set ( 'long.key.setting' , 'no one cares' );
Config :: set ( 'null' , null );
$this -> assertFalse ( Config :: has ( 'null' ), 'Keys set to null do not count as existing' );
$this -> assertTrue ( Config :: has ( 'long' ), 'Top level key should exist' );
$this -> assertTrue ( Config :: has ( 'long.key.setting' ), 'Exact exists on value' );
$this -> assertFalse ( Config :: has ( 'long.key.setting.nothing' ), 'Non-existent child setting' );
$this -> assertFalse ( Config :: has ( 'off.the.wall' ), 'Non-existent key' );
$this -> assertFalse ( Config :: has ( 'off.the' ), 'Config:has() should not modify the config' );
}
2023-05-24 20:21:54 +00:00
public function testGetNonExistent () : void
2017-07-21 22:05:07 +00:00
{
$this -> assertNull ( Config :: get ( 'There.is.no.way.this.is.a.key' ));
$this -> assertFalse ( Config :: has ( 'There.is.no' )); // should not add kes when getting
}
2023-05-24 20:21:54 +00:00
public function testGetNonExistentNested () : void
2017-07-21 22:05:07 +00:00
{
$this -> assertNull ( Config :: get ( 'cheese.and.bologna' ));
}
2023-05-24 20:21:54 +00:00
public function testGetSubtree () : void
2017-07-21 22:05:07 +00:00
{
Config :: set ( 'words.top' , 'August' );
Config :: set ( 'words.mid' , 'And Everything' );
Config :: set ( 'words.bot' , 'After' );
$expected = [
'top' => 'August' ,
'mid' => 'And Everything' ,
'bot' => 'After' ,
];
$this -> assertEquals ( $expected , Config :: get ( 'words' ));
}
2019-10-13 13:40:38 +00:00
/**
* Pass an anonymous function which will be passed the config variable to modify before it is set
2021-09-10 18:09:53 +00:00
*
2021-09-08 21:35:56 +00:00
* @ param callable $function
2019-10-13 13:40:38 +00:00
*/
private function setConfig ( $function )
{
$config = $this -> config -> getValue ();
$function ( $config );
$this -> config -> setValue ( $config );
}
2020-03-16 14:17:58 +00:00
2023-05-24 20:21:54 +00:00
public function testForget () : void
2020-03-16 14:17:58 +00:00
{
Config :: set ( 'forget.me' , 'now' );
$this -> assertTrue ( Config :: has ( 'forget.me' ));
Config :: forget ( 'forget.me' );
$this -> assertFalse ( Config :: has ( 'forget.me' ));
}
2023-05-24 20:21:54 +00:00
public function testForgetSubtree () : void
2020-03-16 14:17:58 +00:00
{
Config :: set ( 'forget.me.sub' , 'yep' );
$this -> assertTrue ( Config :: has ( 'forget.me.sub' ));
Config :: forget ( 'forget.me' );
$this -> assertFalse ( Config :: has ( 'forget.me.sub' ));
}
2017-07-21 22:05:07 +00:00
}