icingaweb2/test/php/application/forms/Config/AuthenticationFormTest.php

158 lines
5.2 KiB
PHP

<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Tests\Icinga\Form\Config;
use \Mockery;
use \Zend_Config;
use Icinga\Test\BaseTestCase;
/**
* Test for the authentication provider form
*/
class AuthenticationFormTest extends BaseTestCase
{
public function setUp()
{
parent::setUp();
$this->viewMock = Mockery::mock('\Zend_View');
$this->viewMock->shouldReceive('icon')->andReturn('');
}
/**
* Test the ldap provider form population from config
*/
public function testLdapProvider()
{
$form = $this->createForm('Icinga\Form\Config\Authentication\LdapBackendForm');
$config = new Zend_Config(
array(
'backend' => 'ldap',
'target' => 'user',
'user_class' => 'testClass',
'user_name_attribute' => 'testAttribute'
)
);
$form->setBackendName('testldap');
$form->setBackend($config);
$form->create(array('resources' => array()));
// parameters to be hidden
$notShown = array('backend', 'target');
foreach ($config->toArray() as $name => $value) {
if (in_array($name, $notShown)) {
continue;
}
$this->assertEquals(
$value,
$form->getValue('backend_testldap_' . $name),
'Asserting the ' . $name . ' parameter to be correctly populated for a ldap authentication form'
);
}
}
/**
* Test the database provider form population from config
*/
public function testDbProvider()
{
$form = $this->createForm('Icinga\Form\Config\Authentication\DbBackendForm');
$config = new Zend_Config(
array(
'backend' => 'db',
'target' => 'user',
'resource' => 'db_resource'
)
);
$form->setResources(
array(
'db_resource' => array(
'type' => 'db'
)
)
);
$form->setBackendName('test-db');
$form->setBackend($config);
$form->create();
// parameters to be hidden
$notShown = array('backend', 'target');
foreach ($config->toArray() as $name => $value) {
if (in_array($name, $notShown)) {
continue;
}
$this->assertEquals(
$value,
$form->getValue('backend_testdb_' . $name),
'Asserting the ' . $name . ' parameter to be correctly populated for a db authentication form'
);
}
}
/**
* Test whether order modifications via 'priority' are considered
*/
public function testModifyOrder()
{
$form = $this->createForm('Icinga\Form\Config\Authentication\ReorderForm');
$form->setAuthenticationBackend('backend2');
$form->setCurrentOrder(array('backend1', 'backend2', 'backend3', 'backend4'));
$form->setView($this->viewMock);
$form->create();
$this->assertSame(
2,
count($form->getSubForms()),
'Assert that a form for moving backend up and down exists'
);
$this->assertTrue(
$form->upForm->getElement('form_backend_order') !== null,
'Assert that a "move backend up" button exists'
);
$this->assertSame(
array('backend2', 'backend1', 'backend3', 'backend4'),
explode(',', $form->upForm->getElement('form_backend_order')->getValue()),
'Assert the "move backend up" button containing the correct order'
);
$this->assertTrue(
$form->downForm->getElement('form_backend_order') !== null,
'Assert that a "move backend down" button exists'
);
$this->assertSame(
array('backend1', 'backend3', 'backend2', 'backend4'),
explode(',', $form->downForm->getElement('form_backend_order')->getValue()),
'Assert the "move backend up" button containing the correct order'
);
}
/**
* Test whether the reorder form doesn't display senseless ordering (like moving the uppermost element up or
* the lowermost down)
*/
public function testInvalidOrderingNotShown()
{
$form = $this->createForm('Icinga\Form\Config\Authentication\ReorderForm');
$form->setAuthenticationBackend('backend1');
$form->setCurrentOrder(array('backend1', 'backend2', 'backend3', 'backend4'));
$form->setView($this->viewMock);
$form->create();
$this->assertSame(
2,
count($form->getSubForms()),
'Assert that a form for moving backend up and down exists, even when moving up is not possible'
);
$this->assertTrue(
$form->downForm->getElement('form_backend_order') !== null,
'Assert that a "move backend down" button exists when moving up is not possible'
);
$this->assertTrue(
$form->upForm->getElement('form_backend_order') === null,
'Assert that a "move backend up" button does not exist when moving up is not possible'
);
}
}