mirror of
https://github.com/Icinga/icingaweb2-module-businessprocess.git
synced 2025-12-24 00:29:47 -05:00
LegacyStorageTest: add a test targeting metadata
This commit is contained in:
parent
36e624c448
commit
d24d0237f4
3 changed files with 34 additions and 30 deletions
|
|
@ -68,7 +68,7 @@ class LegacyStorage extends Storage
|
||||||
|
|
||||||
foreach ($this->listAllProcessNames() as $name) {
|
foreach ($this->listAllProcessNames() as $name) {
|
||||||
$meta = $this->loadMetadata($name);
|
$meta = $this->loadMetadata($name);
|
||||||
if (! $meta->permissionsAreSatisfied()) {
|
if (! $meta->canRead()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -103,28 +103,26 @@ class LegacyStorage extends Storage
|
||||||
return preg_split('/\s*,\s*/', $string, -1, PREG_SPLIT_NO_EMPTY);
|
return preg_split('/\s*,\s*/', $string, -1, PREG_SPLIT_NO_EMPTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function readHeader($file)
|
protected function readHeader($file, Metadata $metadata)
|
||||||
{
|
{
|
||||||
$fh = fopen($file, 'r');
|
$fh = fopen($file, 'r');
|
||||||
$cnt = 0;
|
$cnt = 0;
|
||||||
$meta = new Metadata();
|
|
||||||
while ($cnt < 15 && false !== ($line = fgets($fh))) {
|
while ($cnt < 15 && false !== ($line = fgets($fh))) {
|
||||||
$cnt++;
|
$cnt++;
|
||||||
$this->parseHeaderLine($line, $meta);
|
$this->parseHeaderLine($line, $metadata);
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose($fh);
|
fclose($fh);
|
||||||
return $meta;
|
return $metadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function readHeaderString($string)
|
protected function readHeaderString($string, Metadata $metadata)
|
||||||
{
|
{
|
||||||
$meta = new Metadata();
|
|
||||||
foreach (preg_split('/\n/', $string) as $line) {
|
foreach (preg_split('/\n/', $string) as $line) {
|
||||||
$this->parseHeaderLine($line, $meta);
|
$this->parseHeaderLine($line, $metadata);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $meta;
|
return $metadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function emptyHeader()
|
protected function emptyHeader()
|
||||||
|
|
@ -142,11 +140,11 @@ class LegacyStorage extends Storage
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function parseHeaderLine($line, Metadata $meta)
|
protected function parseHeaderLine($line, Metadata $metadata)
|
||||||
{
|
{
|
||||||
if (preg_match('/^\s*#\s+(.+?)\s*:\s*(.+)$/', $line, $m)) {
|
if (preg_match('/^\s*#\s+(.+?)\s*:\s*(.+)$/', $line, $m)) {
|
||||||
if ($meta->hasKey($m[1])) {
|
if ($metadata->hasKey($m[1])) {
|
||||||
$meta->set($m[1], $m[2]);
|
$metadata->set($m[1], $m[2]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -221,7 +219,7 @@ class LegacyStorage extends Storage
|
||||||
$bp = new BusinessProcess();
|
$bp = new BusinessProcess();
|
||||||
$bp->setName($name);
|
$bp->setName($name);
|
||||||
$this->parseString($string, $bp);
|
$this->parseString($string, $bp);
|
||||||
$bp->setMetadata($this->readHeaderString($string));
|
$this->readHeaderString($string, $bp->getMetadata());
|
||||||
return $bp;
|
return $bp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -258,22 +256,13 @@ class LegacyStorage extends Storage
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->loadMetaFromFile($file)->permissionsAreSatisfied();
|
return $this->loadMetadata($name)->canRead();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function loadMetadata($name)
|
public function loadMetadata($name)
|
||||||
{
|
{
|
||||||
return $this->loadMetaFromFile($this->getFilename($name));
|
$metadata = new Metadata($name);
|
||||||
}
|
return $this->readHeader($this->getFilename($name), $metadata);
|
||||||
|
|
||||||
public function loadMetadataFromString($string)
|
|
||||||
{
|
|
||||||
return $this->readHeaderString($string);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function loadMetaFromFile($filename)
|
|
||||||
{
|
|
||||||
return $this->readHeader($filename);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -284,7 +273,7 @@ class LegacyStorage extends Storage
|
||||||
{
|
{
|
||||||
// TODO: do not open twice, this is quick and dirty based on existing code
|
// TODO: do not open twice, this is quick and dirty based on existing code
|
||||||
$file = $this->currentFilename = $this->getFilename($name);
|
$file = $this->currentFilename = $this->getFilename($name);
|
||||||
$bp->setMetadata($this->readHeader($file));
|
$this->readHeader($file, $bp->getMetadata());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function parseFile($name, $bp)
|
protected function parseFile($name, $bp)
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ namespace Icinga\Module\Businessprocess\Storage;
|
||||||
|
|
||||||
use Icinga\Data\ConfigObject;
|
use Icinga\Data\ConfigObject;
|
||||||
use Icinga\Module\Businessprocess\BusinessProcess;
|
use Icinga\Module\Businessprocess\BusinessProcess;
|
||||||
|
use Icinga\Module\Businessprocess\Metadata;
|
||||||
|
|
||||||
abstract class Storage
|
abstract class Storage
|
||||||
{
|
{
|
||||||
|
|
@ -48,4 +49,10 @@ abstract class Storage
|
||||||
* @return bool Whether the process has been deleted
|
* @return bool Whether the process has been deleted
|
||||||
*/
|
*/
|
||||||
abstract public function deleteProcess($name);
|
abstract public function deleteProcess($name);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return Metadata
|
||||||
|
*/
|
||||||
|
abstract public function loadMetadata($name);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,12 +31,12 @@ class LegacyStorageTest extends BaseTestCase
|
||||||
$keys = array_keys($this->makeInstance()->listProcesses());
|
$keys = array_keys($this->makeInstance()->listProcesses());
|
||||||
sort($keys);
|
sort($keys);
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$keys,
|
|
||||||
array(
|
array(
|
||||||
'broken_wrong-operator',
|
'broken_wrong-operator',
|
||||||
'simple_with-header',
|
'simple_with-header',
|
||||||
'simple_without-header',
|
'simple_without-header',
|
||||||
)
|
),
|
||||||
|
$keys
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -45,12 +45,12 @@ class LegacyStorageTest extends BaseTestCase
|
||||||
$keys = array_values($this->makeInstance()->listProcesses());
|
$keys = array_values($this->makeInstance()->listProcesses());
|
||||||
sort($keys);
|
sort($keys);
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$keys,
|
|
||||||
array(
|
array(
|
||||||
'Simple with header (simple_with-header)',
|
'Simple with header (simple_with-header)',
|
||||||
'broken_wrong-operator',
|
'broken_wrong-operator',
|
||||||
'simple_without-header',
|
'simple_without-header',
|
||||||
)
|
),
|
||||||
|
$keys
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -104,6 +104,14 @@ class LegacyStorageTest extends BaseTestCase
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testTitleCanBeReadFromConfig()
|
||||||
|
{
|
||||||
|
$this->assertEquals(
|
||||||
|
'Simple with header',
|
||||||
|
$this->makeInstance()->loadProcess('simple_with-header')->getMetadata()->get('Title')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function testAConfiguredLoopCanBeParsed()
|
public function testAConfiguredLoopCanBeParsed()
|
||||||
{
|
{
|
||||||
$this->assertInstanceOf(
|
$this->assertInstanceOf(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue