Refactor ResourceForm tests

refs #6011
This commit is contained in:
Johannes Meyer 2014-04-16 16:41:23 +02:00
parent 3d44c45eb8
commit 21b949758f
7 changed files with 509 additions and 159 deletions

View File

@ -89,7 +89,7 @@ class LdapBackendForm extends BaseBackendForm
array( array(
'required' => true, 'required' => true,
'allowEmpty' => false, 'allowEmpty' => false,
'label' => t('LDAP resource'), 'label' => t('LDAP Resource'),
'helptext' => t('The resource to use for authenticating with this provider'), 'helptext' => t('The resource to use for authenticating with this provider'),
'value' => $this->getBackend()->get('resource'), 'value' => $this->getBackend()->get('resource'),
'multiOptions' => $this->getResources() 'multiOptions' => $this->getResources()

View File

@ -29,40 +29,41 @@
namespace Icinga\Form\Config\Resource; namespace Icinga\Form\Config\Resource;
use \Zend_Config; use Exception;
use Zend_Config;
use Zend_Form_Element_Checkbox;
use Icinga\Web\Form; use Icinga\Web\Form;
use Icinga\Logger\Logger;
use Icinga\Web\Form\Decorator\HelpText;
use Icinga\Data\ResourceFactory; use Icinga\Data\ResourceFactory;
use Icinga\Web\Form\Element\Number;
use Icinga\Web\Form\Decorator\HelpText;
/**
* Form for modifying a monitoring backend
*/
class ResourceForm extends Form class ResourceForm extends Form
{ {
/** /**
* The currently edited resource. * The resource
* *
* @var Zend_Config * @var Zend_Config
*/ */
private $resource; protected $resource;
/** /**
* The (new) name of the resource
*
* @var string * @var string
*/ */
private $name = ''; protected $name;
/** /**
* The old name of the resource
*
* @var string * @var string
*/ */
private $oldName = ''; protected $oldName;
/** /**
* Return the current resource name. * Set the current resource name
* *
* @param string $name * @param string $name The name to set
*
* @return void|\Zend_Form
*/ */
public function setName($name) public function setName($name)
{ {
@ -70,18 +71,24 @@ class ResourceForm extends Form
} }
/** /**
* Get the current resource name
*
* @return null|string * @return null|string
*/ */
public function getName() public function getName()
{ {
return $this->getValue('resource_all_name'); $name = $this->getValue('resource_all_name');
if (!$name) {
return $this->name;
}
return $name;
} }
/** /**
* Set the original name of the resource. This value is persisted using * Set the original name of the resource
* a hidden field.
* *
* @param $name * @param string $name The name to set
*/ */
public function setOldName($name) public function setOldName($name)
{ {
@ -89,24 +96,55 @@ class ResourceForm extends Form
} }
/** /**
* Get the resource name that was initially set. * Get the resource name that was initially set
*
* @return null|string
*/ */
public function getOldName() public function getOldName()
{ {
return $this->getValue('resource_all_name_old'); $oldName = $this->getValue('resource_all_name_old');
if (!$oldName) {
return $this->oldName;
}
return $oldName;
} }
private function addDbForm() /**
* Set the resource configuration to edit.
*
* @param Zend_Config $resource The config to set
*/
public function setResource(Zend_Config $resource)
{
$this->resource = $resource;
}
/**
* Get the current resource configuration.
*
* @return Zend_Config
*/
public function getResource()
{
if (!isset($this->resource)) {
$this->resource = new Zend_Config(array('type' => 'db'));
}
return $this->resource;
}
protected function addDbForm()
{ {
$this->addElement( $this->addElement(
'select', 'select',
'resource_db_db', 'resource_db_db',
array( array(
'label' => 'Database Type', 'required' => true,
'value' => $this->getResource()->get('db', 'mysql'), 'label' => t('Database Type'),
'required' => true, 'helptext' => t('The type of SQL database you want to create.'),
'helptext' => 'The type of SQL database you want to create.', 'value' => $this->getResource()->get('db', 'mysql'),
'multiOptions' => array( 'multiOptions' => array(
'mysql' => 'MySQL', 'mysql' => 'MySQL',
'pgsql' => 'PostgreSQL' 'pgsql' => 'PostgreSQL'
//'oracle' => 'Oracle' //'oracle' => 'Oracle'
@ -118,24 +156,22 @@ class ResourceForm extends Form
'text', 'text',
'resource_db_host', 'resource_db_host',
array ( array (
'label' => 'Host', 'required' => true,
'value' => $this->getResource()->get('host', 'localhost'), 'label' => t('Host'),
'required' => true, 'helptext' => t('The hostname of the database.'),
'helptext' => 'The hostname of the database.' 'value' => $this->getResource()->get('host', 'localhost')
) )
); );
$this->addElement( $this->addElement(
'text', new Number(
'resource_db_port', array(
array( 'name' => 'resource_db_port',
'label' => 'Port', 'required' => true,
'value' => $this->getResource()->get('port', 3306), 'label' => t('Port'),
'required' => true, 'helptext' => t('The port to use.'),
'validators' => array( 'value' => $this->getResource()->get('port', 3306)
array('regex', false, '/^[0-9]+$/') )
),
'helptext' => 'The port number to use.'
) )
); );
@ -143,10 +179,10 @@ class ResourceForm extends Form
'text', 'text',
'resource_db_dbname', 'resource_db_dbname',
array( array(
'label' => 'Database Name',
'value' => $this->getResource()->get('dbname', ''),
'required' => true, 'required' => true,
'helptext' => 'The name of the database to use' 'label' => t('Database Name'),
'helptext' => t('The name of the database to use'),
'value' => $this->getResource()->get('dbname', '')
) )
); );
@ -154,10 +190,10 @@ class ResourceForm extends Form
'text', 'text',
'resource_db_username', 'resource_db_username',
array ( array (
'label' => 'Username', 'required' => true,
'value' => $this->getResource()->get('username', ''), 'label' => t('Username'),
'required' => true, 'helptext' => t('The user name to use for authentication.'),
'helptext' => 'The user name to use for authentication.' 'value' => $this->getResource()->get('username', '')
) )
); );
@ -165,64 +201,65 @@ class ResourceForm extends Form
'password', 'password',
'resource_db_password', 'resource_db_password',
array( array(
'label' => 'Password', 'required' => true,
'renderPassword' => true, 'renderPassword' => true,
'value' => $this->getResource()->get('password', ''), 'label' => t('Password'),
'helptext' => 'The password to use for authentication', 'helptext' => t('The password to use for authentication'),
'required' => true 'value' => $this->getResource()->get('password', '')
) )
); );
} }
private function addStatusdatForm() protected function addStatusdatForm()
{ {
$this->addElement( $this->addElement(
'text', 'text',
'resource_statusdat_status_file', 'resource_statusdat_status_file',
array( array(
'label' => 'Status.dat File', 'required' => true,
'value' => $this->getResource()->get('status_file', '/usr/local/icinga/var/status.dat'), 'label' => t('Filepath'),
'required' => true, 'helptext' => t('Location of your icinga status.dat file'),
'helptext' => 'Location of your icinga status.dat file' 'value' => $this->getResource()->get('status_file', '/usr/local/icinga/var/status.dat')
) )
); );
$this->addElement( $this->addElement(
'text', 'text',
'resource_statusdat_object_file', 'resource_statusdat_object_file',
array( array(
'label' => 'Objects.cache File', 'required' => true,
'value' => $this->getResource()->get('status_file', '/usr/local/icinga/var/objects.cache'), 'label' => t('Filepath'),
'required' => true, 'helptext' => t('Location of your icinga objects.cache file'),
'helptext' => 'Location of your icinga objects.cache file' 'value' => $this->getResource()->get('status_file', '/usr/local/icinga/var/objects.cache')
) )
); );
} }
private function addLivestatusForm() protected function addLivestatusForm()
{ {
$this->addElement( $this->addElement(
'text', 'text',
'resource_livestatus_socket', 'resource_livestatus_socket',
array( array(
'label' => 'Livestatus Socket Location', 'required' => true,
'required' => true, 'label' => t('Socket'),
'helptext' => 'The path to your livestatus socket used for querying monitoring data', 'helptext' => t('The path to your livestatus socket used for querying monitoring data'),
'value' => $this->getResource()->socket, 'value' => $this->getResource()->get('socket', '/usr/local/icinga/var/rw/livestatus')
) )
); );
} }
private function addLdapForm() protected function addLdapForm()
{ {
$this->addElement( $this->addElement(
'text', 'text',
'resource_ldap_hostname', 'resource_ldap_hostname',
array( array(
'label' => 'LDAP Server Host', 'required' => true,
'allowEmpty' => false, 'allowEmpty' => false,
'value' => $this->getResource()->get('hostname', 'localhost'), 'label' => t('Host'),
'helptext' => 'The hostname or address of the LDAP server to use for authentication', 'helptext' => t('The hostname or address of the LDAP server to use for authentication'),
'required' => true 'value' => $this->getResource()->get('hostname', 'localhost')
) )
); );
@ -230,10 +267,10 @@ class ResourceForm extends Form
'text', 'text',
'resource_ldap_root_dn', 'resource_ldap_root_dn',
array( array(
'label' => 'LDAP Root DN', 'required' => true,
'value' => $this->getResource()->get('root_dn', 'ou=people,dc=icinga,dc=org'), 'label' => t('Root DN'),
'helptext' => 'The path where users can be found on the ldap server', 'helptext' => t('The path where users can be found on the ldap server'),
'required' => true 'value' => $this->getResource()->get('root_dn', 'ou=people,dc=icinga,dc=org')
) )
); );
@ -241,10 +278,10 @@ class ResourceForm extends Form
'text', 'text',
'resource_ldap_bind_dn', 'resource_ldap_bind_dn',
array( array(
'label' => 'LDAP Bind DN', 'required' => true,
'value' => $this->getResource()->get('bind_dn', 'cn=admin,cn=config'), 'label' => t('Bind DN'),
'helptext' => 'The user dn to use for querying the ldap server', 'helptext' => t('The user dn to use for querying the ldap server'),
'required' => true 'value' => $this->getResource()->get('bind_dn', 'cn=admin,cn=config')
) )
); );
@ -252,77 +289,73 @@ class ResourceForm extends Form
'password', 'password',
'resource_ldap_bind_pw', 'resource_ldap_bind_pw',
array( array(
'label' => 'LDAP Bind Password', 'required' => true,
'renderPassword' => true, 'renderPassword' => true,
'value' => $this->getResource()->get('bind_pw', ''), 'label' => t('Bind Password'),
'helptext' => 'The password to use for querying the ldap server', 'helptext' => t('The password to use for querying the ldap server'),
'required' => true 'value' => $this->getResource()->get('bind_pw', '')
) )
); );
} }
/** protected function addFileForm()
* Set the resource configuration to edit.
*
* @param Zend_Config $resource
*/
public function setResource(Zend_Config $resource)
{ {
$this->resource = $resource; $this->addElement(
'text',
'resource_file_filename',
array(
'required' => true,
'label' => t('Filepath'),
'helptext' => t('The filename to fetch information from'),
'value' => $this->getResource()->get('filename', '')
)
);
$this->addElement(
'text',
'resource_file_fields',
array(
'required' => true,
'label' => t('Pattern'),
'helptext' => t('The regular expression by which to identify columns'),
'value' => $this->getResource()->get('fields', '')
)
);
} }
/** protected function addNameFields()
* Get the current resource configuration.
*
* @return Zend_Config
*/
public function getResource()
{
if (!isset($this->resource)) {
// Init empty resource
$this->resource = new Zend_Config(
array('type' => 'db')
);
}
return $this->resource;
}
/**
* Add a field to change the resource name and one hidden field
* to save the previous resource name.
*/
private function addNameFields()
{ {
$this->addElement( $this->addElement(
'text', 'text',
'resource_all_name', 'resource_all_name',
array( array(
'label' => 'Resource Name', 'required' => true,
'value' => $this->name, 'label' => t('Resource Name'),
'helptext' => 'The unique name of this resource', 'helptext' => t('The unique name of this resource'),
'required' => true 'value' => $this->getName()
) )
); );
$this->addElement( $this->addElement(
'hidden', 'hidden',
'resource_all_name_old', 'resource_all_name_old',
array( array(
'value' => $this->oldName 'value' => $this->getOldName()
) )
); );
} }
/** /**
* Add checkbox at the beginning of the form which allows to skip logic connection validation * Add checkbox at the beginning of the form which allows to skip connection validation
*/ */
private function addForceCreationCheckbox() protected function addForceCreationCheckbox()
{ {
$checkbox = new \Zend_Form_Element_Checkbox( $checkbox = new Zend_Form_Element_Checkbox(
array( array(
'name' => 'backend_force_creation', 'order' => 0,
'label' => 'Force Changes', 'name' => 'resource_force_creation',
'helptext' => 'Check this box to enforce changes without connectivity validation', 'label' => t('Force Changes'),
'order' => 0 'helptext' => t('Check this box to enforce changes without connectivity validation')
) )
); );
$checkbox->addDecorator(new HelpText()); $checkbox->addDecorator(new HelpText());
@ -332,21 +365,22 @@ class ResourceForm extends Form
/** /**
* Add a select box for choosing the type to use for this backend * Add a select box for choosing the type to use for this backend
*/ */
private function addTypeSelectionBox() protected function addTypeSelectionBox()
{ {
$this->addElement( $this->addElement(
'select', 'select',
'resource_type', 'resource_type',
array( array(
'label' => 'Resource Type', 'required' => true,
'value' => $this->getResource()->type, 'label' => t('Resource Type'),
'required' => true, 'helptext' => t('The type of resource'),
'helptext' => 'The type of resource.', 'value' => $this->getResource()->type,
'multiOptions' => array( 'multiOptions' => array(
'db' => 'SQL Database', 'db' => t('SQL Database'),
'ldap' => 'Ldap', 'ldap' => 'LDAP',
'statusdat' => 'Status.dat', 'statusdat' => 'Status.dat',
'livestatus' => 'Livestatus' 'livestatus' => 'Livestatus',
'file' => t('File')
) )
) )
); );
@ -354,21 +388,21 @@ class ResourceForm extends Form
} }
/** /**
* Validate this form with the Zend validation mechanism and perform a validation of the connection. * Validate this form with the Zend validation mechanism and perform a validation of the connection
* *
* If validation fails, the 'backend_force_creation' checkbox is prepended to the form to allow users to * If validation fails, the 'resource_force_creation' checkbox is prepended to the form to allow users to
* skip the logic connection validation. * skip the connection validation
* *
* @param array $data The form input to validate * @param array $data The form input to validate
* *
* @return bool True when validation succeeded, false if not * @return bool True when validation succeeded, false if not
*/ */
public function isValid($data) public function isValid($data)
{ {
if (!parent::isValid($data)) { if (!parent::isValid($data)) {
return false; return false;
} }
if ($this->getRequest()->getPost('backend_force_creation')) { if (isset($data['resource_force_creation']) && $data['resource_force_creation']) {
return true; return true;
} }
if (!$this->isValidResource()) { if (!$this->isValidResource()) {
@ -380,14 +414,15 @@ class ResourceForm extends Form
/** /**
* Test if the changed resource is a valid resource, by instantiating it and * Test if the changed resource is a valid resource, by instantiating it and
* checking if connection is possible. * checking if a connection is possible
* *
* @return bool True when connection to the resource is possible. * @return bool True when a connection to the resource is possible
*/ */
private function isValidResource() public function isValidResource()
{ {
$config = $this->getConfig();
try { try {
$config = $this->getConfig();
switch ($config->type) { switch ($config->type) {
case 'db': case 'db':
$resource = ResourceFactory::createResource($config); $resource = ResourceFactory::createResource($config);
@ -396,23 +431,33 @@ class ResourceForm extends Form
case 'statusdat': case 'statusdat':
if (!file_exists($config->object_file) || !file_exists($config->status_file)) { if (!file_exists($config->object_file) || !file_exists($config->status_file)) {
$this->addErrorMessage( $this->addErrorMessage(
'Connectivity validation failed, the provided file or socket does not exist.' t('Connectivity validation failed, the provided file does not exist.')
); );
return false; return false;
} }
break; break;
case 'livestatus': case 'livestatus':
// TODO: Implement check $resource = ResourceFactory::createResource($config);
$resource->connect()->disconnect();
break; break;
case 'ldap': case 'ldap':
$resource = ResourceFactory::createResource($config); $resource = ResourceFactory::createResource($config);
$resource->connect(); $resource->connect();
break; break;
case 'file':
if (!file_exists($config->filename)) {
$this->addErrorMessage(
t('Connectivity validation failed, the provided file does not exist.')
);
return false;
}
break;
} }
} catch (\Exception $exc) { } catch (Exception $e) {
$this->addErrorMessage('Connectivity validation failed, connection to the given resource not possible.'); $this->addErrorMessage(t('Connectivity validation failed, connection to the given resource not possible.'));
return false; return false;
} }
return true; return true;
} }
@ -420,6 +465,7 @@ class ResourceForm extends Form
{ {
$this->addNameFields(); $this->addNameFields();
$this->addTypeSelectionBox(); $this->addTypeSelectionBox();
switch ($this->getRequest()->getParam('resource_type', $this->getResource()->type)) { switch ($this->getRequest()->getParam('resource_type', $this->getResource()->type)) {
case 'db': case 'db':
$this->addDbForm(); $this->addDbForm();
@ -433,30 +479,33 @@ class ResourceForm extends Form
case 'ldap': case 'ldap':
$this->addLdapForm(); $this->addLdapForm();
break; break;
case 'file':
$this->addFileForm();
break;
} }
$this->setSubmitLabel('{{SAVE_ICON}} Save Changes'); $this->setSubmitLabel('{{SAVE_ICON}} Save Changes');
} }
/** /**
* Return a configuration containing the backend settings entered in this form * Return a configuration containing the backend settings entered in this form
* *
* @return Zend_Config The updated configuration for this backend * @return Zend_Config The updated configuration for this backend
*/ */
public function getConfig() public function getConfig()
{ {
$values = $this->getValues(); $values = $this->getValues();
$type = $values['resource_type'];
$result = array('type' => $type); $result = array('type' => $values['resource_type']);
foreach ($values as $key => $value) { foreach ($values as $key => $value) {
if ($key !== 'resource_type' && $key !== 'resource_all_name' && $key !== 'resource_all_name_old') { if ($key !== 'resource_type' && $key !== 'resource_all_name' && $key !== 'resource_all_name_old') {
$configKey = explode('_', $key, 3); $configKey = explode('_', $key, 3);
if (sizeof($configKey) < 3) { if (count($configKey) === 3) {
Logger::warning('EditResourceForm: invalid form key "' . $key . '" was ignored.'); $result[$configKey[2]] = $value;
continue;
} }
$result[$configKey[2]] = $value;
} }
} }
return new Zend_Config($result); return new Zend_Config($result);
} }
} }

View File

@ -273,6 +273,15 @@ class Connection
} }
} }
public function connect()
{
if (!$this->connection) {
$this->getConnection();
}
return $this;
}
public function disconnect() public function disconnect()
{ {
if ($this->connection) { if ($this->connection) {

View File

@ -29,16 +29,38 @@
namespace Icinga\Web\Form\Element; namespace Icinga\Web\Form\Element;
use Zend_Form_Element_Xhtml;
/** /**
* Number form element * Number form element
*
* @TODO: The given label for this element is not displayed. (Reason unknown)
*/ */
class Number extends \Zend_Form_Element_Xhtml class Number extends Zend_Form_Element_Xhtml
{ {
/** /**
* Default form view helper to use for rendering * Default form view helper to use for rendering
*
* @var string * @var string
*/ */
public $helper = "formNumber"; public $helper = "formNumber";
/**
* Check whether $value is of type integer
*
* @param string $value The value to check
* @param mixed $context Context to use
*
* @return bool
*/
public function isValid($value, $context = null)
{
if (parent::isValid($value, $context)) {
if (is_numeric($value)) {
return true;
}
$this->addError(t('Please enter a number.'));
}
return false;
}
} }

View File

@ -4,12 +4,18 @@
namespace Tests\Icinga\Form\Config\Authentication; namespace Tests\Icinga\Form\Config\Authentication;
use \Mockery; use Mockery;
use Icinga\Test\BaseTestCase; use Icinga\Test\BaseTestCase;
use Icinga\Form\Config\Authentication\DbBackendForm; use Icinga\Form\Config\Authentication\DbBackendForm;
class DbBackendFormTest extends BaseTestCase class DbBackendFormTest extends BaseTestCase
{ {
public function tearDown()
{
parent::tearDown();
Mockery::close(); // Necessary because some tests run in a separate process
}
/** /**
* @runInSeparateProcess * @runInSeparateProcess
*/ */

View File

@ -4,12 +4,18 @@
namespace Tests\Icinga\Form\Config\Authentication; namespace Tests\Icinga\Form\Config\Authentication;
use \Mockery; use Mockery;
use Icinga\Test\BaseTestCase; use Icinga\Test\BaseTestCase;
use Icinga\Form\Config\Authentication\LdapBackendForm; use Icinga\Form\Config\Authentication\LdapBackendForm;
class LdapBackendFormTest extends BaseTestCase class LdapBackendFormTest extends BaseTestCase
{ {
public function tearDown()
{
parent::tearDown();
Mockery::close(); // Necessary because some tests run in a separate process
}
/** /**
* @runInSeparateProcess * @runInSeparateProcess
*/ */

View File

@ -0,0 +1,258 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Tests\Icinga\Form\Config\Resource;
use Mockery;
use Zend_Config;
use Icinga\Test\BaseTestCase;
use Icinga\Form\Config\Resource\ResourceForm;
class TestResourceForm extends ResourceForm
{
public $is_valid;
public function isValidResource()
{
return $this->is_valid;
}
}
class ResourceFormTest extends BaseTestCase
{
public function tearDown()
{
parent::tearDown();
Mockery::close(); // Necessary because some tests run in a separate process
}
public function testIsForceCreationCheckboxBeingAdded()
{
$form = new TestResourceForm();
$form->is_valid = false;
$this->assertFalse($form->isValid(array()));
$this->assertNotNull(
$form->getElement('resource_force_creation'),
'Checkbox to force the creation of a resource is not being added though the resource is invalid'
);
}
public function testIsForceCreationCheckboxNotBeingAdded()
{
$form = new TestResourceForm();
$form->is_valid = true;
$this->assertTrue($form->isValid(array()));
$this->assertNull(
$form->getElement('resource_force_creation'),
'Checkbox to force the creation of a resource is being added though the resource is valid'
);
}
public function testIsTheFormValidIfForceCreationTrue()
{
$form = new TestResourceForm();
$form->is_valid = false;
$this->assertTrue(
$form->isValid(array('resource_force_creation' => 1)),
'ResourceForm with invalid resource is not valid though force creation is set'
);
}
/**
* @runInSeparateProcess
*/
public function testValidDbResourceIsValid()
{
$this->setUpResourceFactoryMock(
Mockery::mock()->shouldReceive('getConnection')->atMost()->twice()->andReturn(Mockery::self())->getMock()
);
$form = $this->buildResourceForm(new Zend_Config(array('type' => 'db')));
$this->assertTrue(
$form->isValidResource(),
'ResourceForm claims that a valid db resource is not valid'
);
}
/**
* @runInSeparateProcess
*/
public function testInvalidDbResourceIsNotValid()
{
$this->setUpResourceFactoryMock(
Mockery::mock()->shouldReceive('getConnection')->once()->andThrow('\Exception')->getMock()
);
$form = $this->buildResourceForm(new Zend_Config(array('type' => 'db')));
$this->assertFalse(
$form->isValidResource(),
'ResourceForm claims that an invalid db resource is valid'
);
}
/**
* @runInSeparateProcess
*/
public function testValidLdapResourceIsValid()
{
$this->setUpResourceFactoryMock(
Mockery::mock()->shouldReceive('connect')->getMock()
);
$form = $this->buildResourceForm(new Zend_Config(array('type' => 'ldap')));
$this->assertTrue(
$form->isValidResource(),
'ResourceForm claims that a valid ldap resource is not valid'
);
}
/**
* @runInSeparateProcess
*/
public function testInvalidLdapResourceIsNotValid()
{
$this->setUpResourceFactoryMock(
Mockery::mock()->shouldReceive('connect')->once()->andThrow('\Exception')->getMock()
);
$form = $this->buildResourceForm(new Zend_Config(array('type' => 'ldap')));
$this->assertFalse(
$form->isValidResource(),
'ResourceForm claims that an invalid ldap resource is valid'
);
}
/**
* @runInSeparateProcess
*/
public function testValidLivestatusResourceIsValid()
{
$this->setUpResourceFactoryMock(
Mockery::mock()->shouldReceive('connect')->andReturn(Mockery::self())
->shouldReceive('disconnect')->getMock()
);
$form = $this->buildResourceForm(new Zend_Config(array('type' => 'livestatus')));
$this->assertTrue(
$form->isValidResource(),
'ResourceForm claims that a valid livestatus resource is not valid'
);
}
/**
* @runInSeparateProcess
*/
public function testInvalidLivestatusResourceIsNotValid()
{
$this->setUpResourceFactoryMock(
Mockery::mock()->shouldReceive('connect')->once()->andThrow('\Exception')->getMock()
);
$form = $this->buildResourceForm(new Zend_Config(array('type' => 'livestatus')));
$this->assertFalse(
$form->isValidResource(),
'ResourceForm claims that an invalid livestatus resource is valid'
);
}
public function testValidFileResourceIsValid()
{
$form = $this->buildResourceForm(
new Zend_Config(
array(
'type' => 'file',
'filename' => BaseTestCase::$testDir . '/res/status/icinga.status.dat'
)
)
);
$this->assertTrue(
$form->isValidResource(),
'ResourceForm claims that a valid file resource is not valid'
);
}
public function testInvalidFileResourceIsNotValid()
{
$form = $this->buildResourceForm(
new Zend_Config(
array(
'type' => 'file',
'filename' => 'not_existing'
)
)
);
$this->assertFalse(
$form->isValidResource(),
'ResourceForm claims that an invalid file resource is valid'
);
}
public function testValidStatusdatResourceIsValid()
{
$form = $this->buildResourceForm(
new Zend_Config(
array(
'type' => 'statusdat',
'status_file' => BaseTestCase::$testDir . '/res/status/icinga.status.dat',
'object_file' => BaseTestCase::$testDir . '/res/status/icinga.objects.cache',
)
)
);
$this->assertTrue(
$form->isValidResource(),
'ResourceForm claims that a valid statusdat resource is not valid'
);
}
public function testInvalidStatusdatResourceIsNotValid()
{
$form = $this->buildResourceForm(
new Zend_Config(
array(
'type' => 'statusdat',
'status_file' => 'not_existing',
'object_file' => 'not_existing'
)
)
);
$this->assertFalse(
$form->isValidResource(),
'ResourceForm claims that an invalid statusdat resource is valid'
);
}
protected function buildResourceForm($resourceConfig)
{
$form = new ResourceForm();
$form->setRequest($this->getRequestMock());
$form->setResource($resourceConfig);
$form->create();
return $form;
}
protected function getRequestMock()
{
return Mockery::mock('\Zend_Controller_Request_Abstract')
->shouldReceive('getParam')
->with(Mockery::type('string'), Mockery::type('string'))
->andReturnUsing(function ($name, $default) { return $default; })
->getMock();
}
protected function setUpResourceFactoryMock($resourceMock)
{
Mockery::mock('alias:Icinga\Data\ResourceFactory')
->shouldReceive('createResource')
->with(Mockery::type('\Zend_Config'))
->andReturn($resourceMock);
}
}