2013-08-06 16:28:26 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
/**
|
|
|
|
* This file is part of Icinga 2 Web.
|
|
|
|
*
|
|
|
|
* Icinga 2 Web - Head for multiple monitoring backends.
|
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
|
|
|
*
|
|
|
|
* 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 2
|
|
|
|
* 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
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
|
|
|
*/
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
namespace Tests\Icinga\PreservingIniWriterTest;
|
|
|
|
|
|
|
|
require_once 'Zend/Config.php';
|
|
|
|
require_once 'Zend/Config/Ini.php';
|
|
|
|
require_once 'Zend/Config/Writer/Ini.php';
|
2013-08-06 17:11:44 +02:00
|
|
|
require_once('../../library/Icinga/Config/IniEditor.php');
|
2013-08-06 16:28:26 +02:00
|
|
|
require_once('../../library/Icinga/Config/PreservingIniWriter.php');
|
|
|
|
|
|
|
|
use Icinga\Config\PreservingIniWriter;
|
2013-08-27 11:06:15 +02:00
|
|
|
use Zend_Config;
|
2013-08-06 16:28:26 +02:00
|
|
|
|
|
|
|
class PreservingIniWriterTest extends \PHPUnit_Framework_TestCase {
|
|
|
|
|
|
|
|
private $tmpfiles = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up the test fixture
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$ini =
|
2013-08-09 09:13:27 +02:00
|
|
|
'
|
2013-08-06 16:28:26 +02:00
|
|
|
trailing1="wert"
|
|
|
|
arr[]="0"
|
|
|
|
arr[]="1"
|
|
|
|
arr[]="2"
|
|
|
|
arr[]="3"
|
|
|
|
|
2013-08-09 09:13:27 +02:00
|
|
|
Trailing2=
|
|
|
|
|
|
|
|
;1
|
2013-08-06 16:28:26 +02:00
|
|
|
;2
|
|
|
|
;3
|
2013-08-09 09:13:27 +02:00
|
|
|
[section]
|
|
|
|
property = "some value" ; Some " ; comment"
|
|
|
|
property2 = "some ;value" ; Some comment with " quotes "
|
|
|
|
property3.nest1.nest2 = "value" ; ;
|
|
|
|
|
2013-08-06 16:28:26 +02:00
|
|
|
[parent]
|
|
|
|
;4
|
|
|
|
;5
|
|
|
|
;6
|
|
|
|
;7
|
|
|
|
list[]="zero"
|
|
|
|
list[]="one"
|
|
|
|
|
|
|
|
;8
|
|
|
|
;9
|
|
|
|
many.many.nests="value"
|
|
|
|
propOne="value1"
|
|
|
|
propTwo="2"
|
|
|
|
propThree=
|
|
|
|
propFour="true"
|
|
|
|
|
|
|
|
Prop5="true"
|
|
|
|
|
|
|
|
[child : parent]
|
|
|
|
PropOne="overwritten"
|
|
|
|
;10
|
|
|
|
';
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->writeToTmp('orig', $ini);
|
2013-08-06 16:28:26 +02:00
|
|
|
|
|
|
|
$emptyIni = " ";
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->writeToTmp('empty', $emptyIni);
|
2013-08-06 16:28:26 +02:00
|
|
|
|
|
|
|
$editedIni =
|
|
|
|
';1
|
|
|
|
;2
|
|
|
|
;3
|
|
|
|
;4
|
|
|
|
;5
|
|
|
|
trailing1="1"
|
|
|
|
|
|
|
|
[parent]
|
|
|
|
;6
|
|
|
|
;7
|
|
|
|
;8
|
|
|
|
;9
|
|
|
|
;10
|
|
|
|
propOne="value1"
|
|
|
|
|
|
|
|
[different]
|
|
|
|
prop1="1"
|
|
|
|
prop2="2"
|
|
|
|
|
|
|
|
[nested : different]
|
|
|
|
prop2="5"
|
|
|
|
';
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->writeToTmp('edited', $editedIni);
|
2013-08-06 16:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a string to a temporary file
|
|
|
|
*
|
2013-08-27 18:26:34 +02:00
|
|
|
* @param string $name The name of the temporary file
|
|
|
|
* @param string $content The content
|
2013-08-06 16:28:26 +02:00
|
|
|
*/
|
2013-08-27 11:06:15 +02:00
|
|
|
private function writeToTmp($name, $content)
|
2013-08-06 16:28:26 +02:00
|
|
|
{
|
|
|
|
$this->tmpfiles[$name] =
|
2013-08-27 11:06:15 +02:00
|
|
|
tempnam(dirname(__FILE__) . '/temp', $name);
|
|
|
|
$file = fopen($this->tmpfiles[$name], 'w');
|
|
|
|
fwrite($file, $content);
|
2013-08-06 16:28:26 +02:00
|
|
|
fflush($file);
|
|
|
|
fclose($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tear down the test fixture
|
|
|
|
*/
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
foreach ($this->tmpfiles as $filename) {
|
|
|
|
unlink($filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if the IniWriter works correctly when writing the changes back to
|
|
|
|
* the same ini file
|
|
|
|
*/
|
|
|
|
public function testPropertyChangeSameConfig()
|
|
|
|
{
|
|
|
|
$this->changeConfigAndWriteToFile('orig');
|
|
|
|
$config = new \Zend_Config_Ini(
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->tmpfiles['orig'], null, array('allowModifications' => true)
|
2013-08-06 16:28:26 +02:00
|
|
|
);
|
|
|
|
$this->checkConfigProperties($config);
|
|
|
|
$this->checkConfigComments($this->tmpfiles['orig']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if the IniWriter works correctly when writing to an empty file
|
|
|
|
*/
|
|
|
|
public function testPropertyChangeEmptyConfig()
|
|
|
|
{
|
|
|
|
$this->changeConfigAndWriteToFile('empty');
|
|
|
|
$config = new \Zend_Config_Ini(
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->tmpfiles['empty'], null, array('allowModifications' => true)
|
2013-08-06 16:28:26 +02:00
|
|
|
);
|
|
|
|
$this->checkConfigProperties($config);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if the IniWriter works correctly when writing to a file with changes
|
|
|
|
*/
|
|
|
|
public function testPropertyChangeEditedConfig()
|
|
|
|
{
|
|
|
|
$original = $this->changeConfigAndWriteToFile('edited');
|
|
|
|
$config = new \Zend_Config_Ini(
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->tmpfiles['edited'], null, array('allowModifications' => true)
|
2013-08-06 16:28:26 +02:00
|
|
|
);
|
|
|
|
$this->checkConfigProperties($config);
|
|
|
|
$this->checkConfigComments($this->tmpfiles['edited']);
|
|
|
|
}
|
|
|
|
|
2013-08-27 11:06:15 +02:00
|
|
|
/**
|
|
|
|
* Test if the order of sections is correctly changed in the config.
|
|
|
|
*/
|
|
|
|
public function testSectionOrderChange()
|
|
|
|
{
|
|
|
|
$original = '
|
|
|
|
;1
|
|
|
|
|
|
|
|
[section2]
|
|
|
|
;3
|
|
|
|
|
|
|
|
;4
|
|
|
|
[section3]
|
|
|
|
;5
|
|
|
|
|
|
|
|
;2
|
|
|
|
[section1]
|
|
|
|
property = "something" ; comment
|
|
|
|
|
|
|
|
';
|
|
|
|
$this->writeToTmp('section-order',$original);
|
|
|
|
$config = new Zend_Config(
|
|
|
|
array(
|
|
|
|
'section1' => array(
|
|
|
|
'property' => 'something'
|
|
|
|
),
|
|
|
|
'section2' => array(),
|
|
|
|
'section3' => array()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$writer = new PreservingIniWriter(
|
|
|
|
array('config' => $config, 'filename' => $this->tmpfiles['section-order'])
|
|
|
|
);
|
|
|
|
$writer->write();
|
|
|
|
$changed = new \Zend_Config_Ini(
|
|
|
|
$this->tmpfiles['section-order'],
|
|
|
|
null,
|
|
|
|
array('allowModifications' => true)
|
|
|
|
);
|
|
|
|
$this->assertEquals($config->section1->property, $changed->section1->property);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* IniWriter should move the sections, so that comments
|
|
|
|
* are now in the right order
|
|
|
|
*/
|
|
|
|
$this->checkConfigComments(
|
|
|
|
$this->tmpfiles['section-order'],
|
|
|
|
5,
|
|
|
|
'Sections re-ordered correctly'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-08-06 16:28:26 +02:00
|
|
|
/**
|
2013-08-27 18:26:34 +02:00
|
|
|
* Change the test config, write the changes to the temporary
|
|
|
|
* file $tmpFile and save the path to the file in the array tmpfiles
|
2013-08-06 16:28:26 +02:00
|
|
|
*
|
2013-08-27 18:26:34 +02:00
|
|
|
* @param string $tmpFile The name that should be given to the temporary file
|
2013-08-06 16:28:26 +02:00
|
|
|
*/
|
|
|
|
private function changeConfigAndWriteToFile($tmpFile)
|
|
|
|
{
|
|
|
|
$config = $this->createTestConfig();
|
|
|
|
$this->alterConfig($config);
|
|
|
|
$writer = new PreservingIniWriter(
|
|
|
|
array('config' => $config,'filename' => $this->tmpfiles[$tmpFile])
|
|
|
|
);
|
|
|
|
$writer->write();
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if all comments are present
|
|
|
|
*
|
2013-08-27 11:06:15 +02:00
|
|
|
* @param String $file The file to check
|
|
|
|
* @param Number $count The amount of comments that should be present
|
|
|
|
* @param String $assertion The assertion message that will be displayed on errors
|
2013-08-06 16:28:26 +02:00
|
|
|
*/
|
2013-08-27 11:06:15 +02:00
|
|
|
private function checkConfigComments($file,$count = 10,$assertion = 'Comment unchanged')
|
2013-08-06 16:28:26 +02:00
|
|
|
{
|
|
|
|
$i = 0;
|
|
|
|
foreach (explode("\n",file_get_contents($file)) as $line) {
|
|
|
|
if (preg_match('/^;/',$line)) {
|
|
|
|
$i++;
|
|
|
|
$this->assertEquals(
|
|
|
|
$i,intval(substr($line,1)),
|
2013-08-27 11:06:15 +02:00
|
|
|
$assertion
|
2013-08-06 16:28:26 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->assertEquals($count, $i, 'All comments exist');
|
2013-08-06 16:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if all configuration properties are set correctly
|
|
|
|
*
|
2013-08-27 18:26:34 +02:00
|
|
|
* @param mixed $config The configuration to check
|
2013-08-06 16:28:26 +02:00
|
|
|
*/
|
|
|
|
private function checkConfigProperties($config)
|
|
|
|
{
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->assertEquals('val', $config->Trailing2,
|
2013-08-06 16:28:26 +02:00
|
|
|
'Section-less property updated.');
|
|
|
|
|
|
|
|
$this->assertNull($config->trailing1,
|
|
|
|
'Section-less property deleted.');
|
|
|
|
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->assertEquals('value', $config->new,
|
2013-08-06 16:28:26 +02:00
|
|
|
'Section-less property created.');
|
|
|
|
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->assertEquals('0', $config->arr->{0},
|
2013-08-06 16:28:26 +02:00
|
|
|
'Value persisted in array');
|
|
|
|
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->assertEquals('update', $config->arr->{2},
|
2013-08-06 16:28:26 +02:00
|
|
|
'Value changed in array');
|
|
|
|
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->assertEquals('arrvalue', $config->arr->{4},
|
2013-08-06 16:28:26 +02:00
|
|
|
'Value added to array');
|
|
|
|
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->assertEquals('', $config->parent->propOne,
|
2013-08-06 16:28:26 +02:00
|
|
|
'Section property deleted.');
|
|
|
|
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->assertEquals("2", $config->parent->propTwo,
|
2013-08-06 16:28:26 +02:00
|
|
|
'Section property numerical unchanged.');
|
|
|
|
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->assertEquals('update', $config->parent->propThree,
|
2013-08-06 16:28:26 +02:00
|
|
|
'Section property updated.');
|
|
|
|
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->assertEquals("true", $config->parent->propFour,
|
2013-08-06 16:28:26 +02:00
|
|
|
'Section property boolean unchanged.');
|
|
|
|
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->assertEquals("1", $config->parent->new,
|
2013-08-06 16:28:26 +02:00
|
|
|
'Section property numerical created.');
|
|
|
|
|
|
|
|
$this->assertNull($config->parent->list->{0},
|
2013-08-06 17:11:44 +02:00
|
|
|
'Section array deleted');
|
2013-08-06 16:28:26 +02:00
|
|
|
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->assertEquals('new', $config->parent->list->{1},
|
2013-08-06 16:28:26 +02:00
|
|
|
'Section array changed.');
|
|
|
|
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->assertEquals('changed', $config->parent->many->many->nests,
|
2013-08-09 09:13:27 +02:00
|
|
|
'Change strongly nested value.');
|
2013-08-06 16:28:26 +02:00
|
|
|
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->assertEquals('new', $config->parent->many->many->new,
|
2013-08-06 16:28:26 +02:00
|
|
|
'Ccreate strongy nested value.');
|
|
|
|
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->assertEquals('overwritten', $config->child->PropOne,
|
2013-08-06 16:28:26 +02:00
|
|
|
'Overridden inherited property unchanged.');
|
|
|
|
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->assertEquals('somethingNew', $config->child->propTwo,
|
2013-08-06 16:28:26 +02:00
|
|
|
'Inherited property changed.');
|
|
|
|
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->assertEquals('test', $config->child->create,
|
2013-08-06 16:28:26 +02:00
|
|
|
'Non-inherited property created.');
|
|
|
|
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->assertInstanceOf('Zend_Config', $config->newsection,
|
2013-08-06 16:28:26 +02:00
|
|
|
'New section created.');
|
|
|
|
|
|
|
|
$extends = $config->getExtends();
|
2013-08-27 11:06:15 +02:00
|
|
|
$this->assertEquals('child', $extends['newsection'],
|
2013-08-06 16:28:26 +02:00
|
|
|
'New inheritance created.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-27 18:26:34 +02:00
|
|
|
* Change the content of a Zend_Config for testing purposes
|
2013-08-06 16:28:26 +02:00
|
|
|
*
|
2013-08-27 18:26:34 +02:00
|
|
|
* @param Zend_Config $config The configuration that should be changed
|
2013-08-06 16:28:26 +02:00
|
|
|
*/
|
|
|
|
private function alterConfig(\Zend_Config $config)
|
|
|
|
{
|
|
|
|
$config->Trailing2 = 'val';
|
|
|
|
unset($config->trailing1);
|
|
|
|
$config->new = 'value';
|
|
|
|
$config->arr->{2} = "update";
|
|
|
|
$config->arr->{4} = "arrvalue";
|
|
|
|
|
2013-08-09 09:13:27 +02:00
|
|
|
$config->section->property = "updated";
|
|
|
|
unset($config->section->property3);
|
|
|
|
$config->section->property4 = "created";
|
|
|
|
|
2013-08-06 16:28:26 +02:00
|
|
|
$config->parent->propOne = null;
|
|
|
|
$config->parent->propThree = 'update';
|
|
|
|
$config->parent->new = 1;
|
|
|
|
unset($config->parent->list->{0});
|
|
|
|
$config->parent->list->{1} = 'new';
|
|
|
|
|
|
|
|
$config->parent->many->many->nests = "changed";
|
|
|
|
$config->parent->many->many->new = "new";
|
|
|
|
|
|
|
|
$config->child->propTwo = 'somethingNew';
|
|
|
|
$config->child->create = 'test';
|
|
|
|
|
|
|
|
$config->newsection = array();
|
|
|
|
$config->newsection->p1 = "prop";
|
|
|
|
$config->newsection->P2 = "prop";
|
2013-08-27 11:06:15 +02:00
|
|
|
$config->setExtend('newsection', 'child');
|
2013-08-06 16:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the the configuration that will be used for the tests.
|
|
|
|
*/
|
|
|
|
private function createTestConfig()
|
|
|
|
{
|
|
|
|
return new \Zend_Config_Ini(
|
|
|
|
$this->tmpfiles['orig'],
|
|
|
|
null,
|
|
|
|
array('allowModifications' => true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|