mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-29 16:54:04 +02:00
parent
5533b632ed
commit
3ad4a4281d
26
doc/form.md
26
doc/form.md
@ -162,6 +162,32 @@ interface methods:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
## Testing forms
|
||||||
|
|
||||||
|
When testing forms it is a good idea to use Zend_Test_PHPUnit_ControllerTestCase
|
||||||
|
instead of others like PHPUnit_Framework_TestCase as this enables you to use a
|
||||||
|
request dummy which can be passed to your form.
|
||||||
|
|
||||||
|
### Example:
|
||||||
|
|
||||||
|
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
|
||||||
|
|
||||||
|
class YourTestCase extends Zend_Test_PHPUnit_ControllerTestCase
|
||||||
|
{
|
||||||
|
function exampleTest()
|
||||||
|
{
|
||||||
|
$request = $this->getRequest();
|
||||||
|
$request->setMethod('POST')->setPost(array(
|
||||||
|
'key' => 'value'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$form = new SomeForm();
|
||||||
|
$form->setRequest($request);
|
||||||
|
|
||||||
|
...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
## Additional resources
|
## Additional resources
|
||||||
|
|
||||||
* [API documentation](http://build.icinga.org/jenkins/view/icinga2-web/job/icinga2web-development/javadoc/?)
|
* [API documentation](http://build.icinga.org/jenkins/view/icinga2-web/job/icinga2web-development/javadoc/?)
|
||||||
|
109
test/php/library/Icinga/Web/FormTest.php
Normal file
109
test/php/library/Icinga/Web/FormTest.php
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Icinga\Web;
|
||||||
|
|
||||||
|
require_once('Zend/Form.php');
|
||||||
|
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
|
||||||
|
|
||||||
|
require_once('../../library/Icinga/Web/Form.php');
|
||||||
|
require_once('../../library/Icinga/Exception/ProgrammingError.php');
|
||||||
|
require_once('../../library/Icinga/Web/Form/InvalidCSRFTokenException.php');
|
||||||
|
|
||||||
|
use Icinga\Web\Form;
|
||||||
|
use \Zend_Test_PHPUnit_ControllerTestCase;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dummy extension class as Icinga\Web\Form is an abstract one
|
||||||
|
*/
|
||||||
|
class TestForm extends Form
|
||||||
|
{
|
||||||
|
public function create()
|
||||||
|
{
|
||||||
|
// pass
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for the Icinga\Web\Form class (Base class for all other forms)
|
||||||
|
*/
|
||||||
|
class FormTest extends Zend_Test_PHPUnit_ControllerTestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Tests whether the cancel label will be added to the form
|
||||||
|
*/
|
||||||
|
function testCancelLabel()
|
||||||
|
{
|
||||||
|
$form = new TestForm();
|
||||||
|
$form->setCancelLabel('Cancel');
|
||||||
|
$form->buildForm();
|
||||||
|
$this->assertCount(2, $form->getElements(), 'Asserting that the cancel label is present');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests whether the submit button will be added to the form
|
||||||
|
*/
|
||||||
|
function testSubmitButton()
|
||||||
|
{
|
||||||
|
$form = new TestForm();
|
||||||
|
$form->setSubmitLabel('Submit');
|
||||||
|
$form->buildForm();
|
||||||
|
$this->assertCount(2, $form->getElements(), 'Asserting that the submit button is present');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests whether automatic form submission will be enabled for a single field
|
||||||
|
*/
|
||||||
|
function testEnableAutoSubmitSingle()
|
||||||
|
{
|
||||||
|
$form = new TestForm();
|
||||||
|
$form->addElement('checkbox', 'example1', array());
|
||||||
|
$form->enableAutoSubmit(array('example1'));
|
||||||
|
$this->assertArrayHasKey('onchange', $form->getElement('example1')->getAttribs(),
|
||||||
|
'Asserting that auto-submit got enabled for one element');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests whether automatic form submission will be enabled for multiple fields
|
||||||
|
*/
|
||||||
|
function testEnableAutoSubmitMultiple()
|
||||||
|
{
|
||||||
|
$form = new TestForm();
|
||||||
|
$form->addElement('checkbox', 'example1', array());
|
||||||
|
$form->addElement('checkbox', 'example2', array());
|
||||||
|
$form->enableAutoSubmit(array('example1', 'example2'));
|
||||||
|
$this->assertArrayHasKey('onchange', $form->getElement('example1')->getAttribs(),
|
||||||
|
'Asserting that auto-submit got enabled for multiple elements');
|
||||||
|
$this->assertArrayHasKey('onchange', $form->getElement('example2')->getAttribs(),
|
||||||
|
'Asserting that auto-submit got enabled for multiple elements');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests whether automatic form submission can only be enabled for existing elements
|
||||||
|
*
|
||||||
|
* @expectedException Icinga\Exception\ProgrammingError
|
||||||
|
*/
|
||||||
|
function testEnableAutoSubmitExisting()
|
||||||
|
{
|
||||||
|
$form = new TestForm();
|
||||||
|
$form->enableAutoSubmit(array('not_existing'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests whether a form will be detected as properly submitted
|
||||||
|
*/
|
||||||
|
function testFormSubmission()
|
||||||
|
{
|
||||||
|
$form = new TestForm();
|
||||||
|
$form->setTokenDisabled();
|
||||||
|
$form->setSubmitLabel('foo');
|
||||||
|
$request = $this->getRequest();
|
||||||
|
$form->setRequest($request->setMethod('GET'));
|
||||||
|
$this->assertFalse($form->isSubmittedAndValid(),
|
||||||
|
'Asserting that it is not possible to submit a form not using POST');
|
||||||
|
$request->setMethod('POST')->setPost(array('btn_submit' => 'foo'));
|
||||||
|
$this->assertTrue($form->isSubmittedAndValid(),
|
||||||
|
'Asserting that it is possible to detect a form as submitted');
|
||||||
|
}
|
||||||
|
}
|
@ -9,7 +9,7 @@ from optparse import OptionParser, BadOptionError, AmbiguousOptionError
|
|||||||
|
|
||||||
|
|
||||||
APPLICATION = 'phpunit'
|
APPLICATION = 'phpunit'
|
||||||
DEFAULT_ARGS = ['--strict']
|
DEFAULT_ARGS = []
|
||||||
|
|
||||||
VAGRANT_SCRIPT = '/vagrant/test/php/runtests'
|
VAGRANT_SCRIPT = '/vagrant/test/php/runtests'
|
||||||
REPORT_DIRECTORY = '../../build/log'
|
REPORT_DIRECTORY = '../../build/log'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user