2013-08-06 10:50:52 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Icinga\Web;
|
|
|
|
|
|
|
|
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'));
|
2013-08-29 14:16:26 +02:00
|
|
|
$this->assertArrayHasKey('data-icinga-form-autosubmit', $form->getElement('example1')->getAttribs(),
|
2013-08-06 10:50:52 +02:00
|
|
|
'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'));
|
2013-08-29 14:16:26 +02:00
|
|
|
$this->assertArrayHasKey('data-icinga-form-autosubmit', $form->getElement('example1')->getAttribs(),
|
2013-08-06 10:50:52 +02:00
|
|
|
'Asserting that auto-submit got enabled for multiple elements');
|
2013-08-29 14:16:26 +02:00
|
|
|
$this->assertArrayHasKey('data-icinga-form-autosubmit', $form->getElement('example2')->getAttribs(),
|
2013-08-06 10:50:52 +02:00
|
|
|
'Asserting that auto-submit got enabled for multiple elements');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests whether automatic form submission can only be enabled for existing elements
|
2013-09-04 18:27:16 +02:00
|
|
|
*
|
2013-08-06 10:50:52 +02:00
|
|
|
* @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');
|
|
|
|
}
|
2013-09-04 18:28:35 +02:00
|
|
|
}
|