Add tests, move BaseFormTest away from modules/monitoring

refs #3777
This commit is contained in:
Jannis Moßhammer 2013-08-15 10:56:17 +02:00 committed by Eric Lippmann
parent 9ddc03d571
commit 17c1e5a449
13 changed files with 203 additions and 48 deletions

View File

@ -64,7 +64,7 @@ class GeneralForm extends Form
*
* @param IcingaConfig $cfg
*/
public function setConfiguration(IcingaConfig $cfg)
public function setConfiguration($cfg)
{
$this->config = $cfg;
}

View File

@ -1,6 +1,6 @@
<?php
namespace Monitoring\Backend\Livestatus\Query;
namespace Icinga\Monitoring\Backend\Livestatus\Query;
use Icinga\Data\AbstractQuery;
@ -23,10 +23,6 @@ class StatusQuery extends AbstractQuery
'host_accepts_passive_checks' => 'host_accept_passive_checks',
'host_last_state_change',
'host_problems' => 'is_flapping',
'service_in_downtime' => 'is_flapping',
'service_handled' => 'is_flapping',
// Service config
'service_description' => 'description',
'service_display_name' => 'display_name',
@ -41,34 +37,12 @@ class StatusQuery extends AbstractQuery
'service_last_state_change' => 'last_state_change',
// Service comments
//'comments_with_info',
//'downtimes_with_info',
'comments_with_info',
'downtimes_with_info',
);
public function init()
{
$this->query = $this->createQuery();
//print_r($this->ds->getConnection()->fetchAll($this->query));
//die('asdf');
}
public function count()
{
return $this->ds->getConnection()->count($this->query);
}
public function fetchAll()
{
return $this->ds->getConnection()->fetchAll($this->query);
}
public function fetchRow()
{
return array_shift($this->ds->getConnection()->fetchAll($this->query));
}
protected function createQuery()
{
return $this->ds->getConnection()->select()->from('services', $this->available_columns);
return $this->connection->getConnection()->select()->from('services', $this->available_columns);
}
}

View File

@ -5,7 +5,7 @@
namespace Test\Monitoring\Forms\Command;
require_once realpath(__DIR__ . '/BaseFormTest.php');
require_once realpath('library/Icinga/Web/Form/BaseFormTest.php');
require_once realpath(__DIR__ . '/../../../../../../../modules/monitoring/application/forms/Command/AcknowledgeForm.php');
require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/ConfigAwareFactory.php');
require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/DateTimeFactory.php');
@ -13,6 +13,7 @@ require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/DateT
use \DateTimeZone;
use \Monitoring\Form\Command\AcknowledgeForm; // Used by constant FORMCLASS
use \Icinga\Util\DateTimeFactory;
use \Test\Icinga\Web\Form\BaseFormTest;
class AcknowledgeFormTest extends BaseFormTest
{

View File

@ -5,10 +5,11 @@
namespace Test\Monitoring\Forms\Command;
require_once realpath(__DIR__ . '/BaseFormTest.php');
require_once realpath('library/Icinga/Web/Form/BaseFormTest.php');
require_once realpath(__DIR__ . '/../../../../../../../modules/monitoring/application/forms/Command/CommentForm.php');
use \Monitoring\Form\Command\CommentForm; // Used by constant FORMCLASS
use \Test\Icinga\Web\Form\BaseFormTest;
class CommentFormTest extends BaseFormTest
{

View File

@ -1,7 +1,7 @@
<?php
namespace Test\Monitoring\Forms\Command;
require_once __DIR__.'/BaseFormTest.php';
require_once realpath('library/Icinga/Web/Form/BaseFormTest.php');
require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form.php';
require_once __DIR__. '/../../../../../../../library/Icinga/Web/Form/Element/Note.php';
require_once __DIR__. '/../../../../../application/forms/Command/CommandForm.php';
@ -9,6 +9,8 @@ require_once __DIR__. '/../../../../../application/forms/Command/CommandForm.php
use \Zend_View;
use \Zend_Test_PHPUnit_ControllerTestCase;
use \Test\Icinga\Web\Form\BaseFormTest;
use Monitoring\Form\Command\CommandForm;
class CommandFormTest extends BaseFormTest

View File

@ -3,7 +3,7 @@
namespace Test\Monitoring\Forms\Command;
require_once __DIR__. '/BaseFormTest.php';
require_once realpath('library/Icinga/Web/Form/BaseFormTest.php');
require_once __DIR__. '/../../../../../application/forms/Command/CommandForm.php';
require_once __DIR__. '/../../../../../application/forms/Command/WithChildrenCommandForm.php';
require_once __DIR__. '/../../../../../application/forms/Command/CommandWithIdentifierForm.php';
@ -11,6 +11,7 @@ require_once __DIR__. '/../../../../../application/forms/Command/CommandWithIden
use Monitoring\Form\Command\CommandWithIdentifierForm;
use \Zend_View;
use \Zend_Test_PHPUnit_ControllerTestCase;
use \Test\Icinga\Web\Form\BaseFormTest;
class CommandWithIdentifierFormTest extends BaseFormTest
{

View File

@ -5,10 +5,11 @@
namespace Test\Monitoring\Forms\Command;
require_once realpath(__DIR__ . '/BaseFormTest.php');
require_once realpath('library/Icinga/Web/Form/BaseFormTest.php');
require_once realpath(__DIR__ . '/../../../../../application/forms/Command/CustomNotificationForm.php');
use \Monitoring\Form\Command\CustomNotificationForm; // Used by constant FORM_CLASS
use \Test\Icinga\Web\Form\BaseFormTest;
class CustomNotificationFormTest extends BaseFormTest
{

View File

@ -5,10 +5,11 @@
namespace Test\Monitoring\Forms\Command;
require_once realpath(__DIR__ . '/BaseFormTest.php');
require_once realpath('library/Icinga/Web/Form/BaseFormTest.php');
require_once realpath(__DIR__ . '/../../../../../application/forms/Command/DelayNotificationForm.php');
use \Monitoring\Form\Command\DelayNotificationForm; // Used by constant FORM_CLASS
use \Test\Icinga\Web\Form\BaseFormTest;
class DelayNotificationFormTest extends BaseFormTest
{

View File

@ -5,7 +5,7 @@
namespace Test\Monitoring\Forms\Command;
require_once realpath(__DIR__ . '/BaseFormTest.php');
require_once realpath('library/Icinga/Web/Form/BaseFormTest.php');
require_once realpath(__DIR__ . '/../../../../../application/forms/Command/RescheduleNextCheckForm.php');
require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/ConfigAwareFactory.php');
require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/DateTimeFactory.php');
@ -13,6 +13,7 @@ require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/DateT
use \Monitoring\Form\Command\RescheduleNextCheckForm; // Used by constant FORM_CLASS
use \DateTimeZone;
use \Icinga\Util\DateTimeFactory;
use \Test\Icinga\Web\Form\BaseFormTest;
class RescheduleNextCheckFormTest extends BaseFormTest
{

View File

@ -5,7 +5,7 @@
namespace Test\Monitoring\Forms\Command;
require_once realpath(__DIR__ . '/BaseFormTest.php');
require_once realpath('library/Icinga/Web/Form/BaseFormTest.php');
require_once realpath(__DIR__ . '/../../../../../../../modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php');
require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/ConfigAwareFactory.php');
require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/DateTimeFactory.php');
@ -13,6 +13,7 @@ require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Util/DateT
use \Monitoring\Form\Command\ScheduleDowntimeForm; // Used by constant FORM_CLASS
use \DateTimeZone;
use \Icinga\Util\DateTimeFactory;
use \Test\Icinga\Web\Form\BaseFormTest;
class ScheduleDowntimeFormTest extends BaseFormTest
{

View File

@ -5,10 +5,11 @@
namespace Test\Monitoring\Forms\Command;
require_once realpath(__DIR__ . '/BaseFormTest.php');
require_once realpath('library/Icinga/Web/Form/BaseFormTest.php');
require_once realpath(__DIR__ . '/../../../../../application/forms/Command/SubmitPassiveCheckResultForm.php');
use \Monitoring\Form\Command\SubmitPassiveCheckResultForm; // Used by constant FORM_CLASS
use \Test\Icinga\Web\Form\BaseFormTest;
class SubmitPassiveCheckResultFormTest extends BaseFormTest
{

View File

@ -0,0 +1,161 @@
<?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 Test\Icinga\Form\Config;
require_once('Zend/Config.php');
require_once('Zend/Config/Ini.php');
require_once(realpath('library/Icinga/Web/Form/BaseFormTest.php'));
require_once(realpath('../../application/forms/Config/GeneralForm.php'));
use Test\Icinga\Web\Form\BaseFormTest;
use \Icinga\Web\Form;
use \DOMDocument;
use \Zend_Config;
use \Zend_View;
class GeneralFormTest extends \Test\Icinga\Web\Form\BaseFormTest
{
private function isHiddenElement($value, $htmlString)
{
$html = new DOMDocument();
$html->loadHTML($htmlString);
$hidden = $html->getElementsByTagName('noscript');
foreach($hidden as $node) {
foreach($node->childNodes as $child) {
if ($child->hasAttributes() === false) {
continue;
}
if (strpos($child->attributes->item(0)->value, $value) !== false) {
return true;
}
}
}
return false;
}
/**
*
*/
public function testCorrectFieldPopulation()
{
date_default_timezone_set('UTC');
$form = $this->getRequestForm(array(), 'Icinga\Form\Config\GeneralForm');
$form->setConfiguration(
new Zend_Config(
array(
'global' => array(
'environment' => 'development',
'timezone' => 'Europe/Berlin',
'indexModule' => 'monitoring',
'indexController' => 'dashboard',
'moduleFolder' => '/my/module/path',
'dateFormat' => 'd-m/Y',
'timeFormat' => 'A:i'
),
'preferences' => array(
'type' => 'ini',
'configPath' => './my/path'
)
)
)
);
$form->setConfigDir('/tmp');
$view = new Zend_View();
$form->create();
$this->assertEquals(1, $form->getValue('environment'), 'Asserting the checkbox for devlopment being set to true');
$this->assertEquals('Europe/Berlin', $form->getValue('timezone'), 'Asserting the correct timezone to be displayed');
$this->assertEquals('/my/module/path', $form->getValue('module_folder'), 'Asserting the correct module folder to be set');
$this->assertEquals('d-m/Y', $form->getValue('date_format'), 'Asserting the correct data format to be set');
$this->assertEquals('A:i', $form->getValue('time_format'), 'Asserting the correct time to be set');
$this->assertEquals('ini', $form->getValue('preferences_type'), 'Asserting the correct preference type to be set');
$this->assertEquals('./my/path', $form->getValue('preferences_ini_path'), 'Asserting the correct ini path to be set');
$this->assertEquals('', $form->getValue('preferences_db_resource'), 'Asserting the database resource not to be set');
}
public function testCorrectConditionalIniFieldRendering()
{
date_default_timezone_set('UTC');
$form = $this->getRequestForm(array(), 'Icinga\Form\Config\GeneralForm');
$form->setConfiguration(
new Zend_Config(
array(
'preferences' => array(
'type' => 'ini',
'configPath' => './my/path'
)
)
)
);
$form->setConfigDir('/tmp');
$form->create();
$view = new Zend_View();
$this->assertFalse(
$this->isHiddenElement('preferences_ini_path', $form->render($view)),
"Asserting the ini path field to be displayed when an ini preference is set"
);
$this->assertTrue(
$this->isHiddenElement('preferences_db_resource', $form->render($view)),
"Asserting the db resource to be hidden when an ini preference is set"
);
}
public function testCorrectConditionalDbFieldRendering()
{
date_default_timezone_set('UTC');
$form = $this->getRequestForm(array(), 'Icinga\Form\Config\GeneralForm');
$form->setConfiguration(
new Zend_Config(
array(
'preferences' => array(
'type' => 'db',
'configPath' => './my/path',
'resource' => 'my_resource'
)
)
)
);
$form->setConfigDir('/tmp');
$form->create();
$view = new Zend_View();
$this->assertTrue(
$this->isHiddenElement('preferences_ini_path', $form->render($view)),
"Asserting the ini path field to be hidden when db preference is set"
);
$this->assertFalse(
$this->isHiddenElement('preferences_ini_resource', $form->render($view)),
"Asserting the db resource to be displayed when db preference is set"
);
}
}

View File

@ -1,4 +1,8 @@
<?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace {
if (!function_exists('t')) {
@ -14,7 +18,7 @@ namespace {
}
}
namespace Test\Monitoring\Forms\Command {
namespace Test\Icinga\Web\Form {
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
require_once 'Zend/Form.php';
@ -23,16 +27,17 @@ namespace Test\Monitoring\Forms\Command {
require_once 'Zend/Form/Element/Reset.php';
require_once 'Zend/Form/Element/Checkbox.php';
require_once 'Zend/Form/Element/Hidden.php';
require_once 'Zend/Form/Decorator/Abstract.php';
require_once 'Zend/Validate/Date.php';
$base = __DIR__.'/../../../../../../../';
require_once realpath($base.'library/Icinga/Exception/ProgrammingError.php');
require_once realpath($base.'library/Icinga/Web/Form.php');
require_once realpath($base.'library/Icinga/Web/Form/InvalidCSRFTokenException.php');
require_once realpath($base.'library/Icinga/Web/Form/Element/Note.php');
require_once realpath($base.'library/Icinga/Web/Form/Element/DateTimePicker.php');
$base = '../../';
require_once realpath($base . 'library/Icinga/Exception/ProgrammingError.php');
require_once realpath($base . 'library/Icinga/Web/Form.php');
require_once realpath($base . 'library/Icinga/Web/Form/InvalidCSRFTokenException.php');
require_once realpath($base . 'library/Icinga/Web/Form/Element/Note.php');
require_once realpath($base . 'library/Icinga/Web/Form/Element/DateTimePicker.php');
require_once realpath($base . 'modules/monitoring/application/forms/Command/CommandForm.php');
require_once realpath($base . 'modules/monitoring/application/forms/Command/WithChildrenCommandForm.php');
require_once realpath('../../library/Icinga/Web/Form/Decorator/ConditionalHidden.php');
use \Zend_View;
use \Zend_Form;
use \Zend_View_Interface;
@ -58,6 +63,11 @@ namespace Test\Monitoring\Forms\Command {
return $form;
}
public function testForRemovingWarnings()
{
$this->assertTrue(true);
}
}
}