mirror of
https://github.com/FOSSBilling/FOSSBilling.git
synced 2026-11-09 13:36:07 +09:00
389 lines
10 KiB
PHP
389 lines
10 KiB
PHP
<?php
|
|
|
|
|
|
namespace Box\Mod\Formbuilder\Api;
|
|
|
|
|
|
class AdminTest extends \PHPUnit_Framework_TestCase {
|
|
|
|
/**
|
|
* @var \Box\Mod\Formbuilder\Service
|
|
*/
|
|
protected $service = null;
|
|
|
|
/**
|
|
* @var \Box\Mod\Formbuilder\Api\Admin
|
|
*/
|
|
protected $api = null;
|
|
|
|
public function getServiceMock()
|
|
{
|
|
return $this->getMockBuilder('\Box\Mod\Formbuilder\Service')->getMock();
|
|
}
|
|
|
|
public function setup()
|
|
{
|
|
$this->service = new \Box\Mod\Formbuilder\Service();
|
|
$this->api = new \Box\Mod\Formbuilder\Api\Admin();
|
|
}
|
|
|
|
|
|
public function testgetDi()
|
|
{
|
|
$di = new \Box_Di();
|
|
$this->api->setDi($di);
|
|
$getDi = $this->api->getDi();
|
|
$this->assertEquals($di, $getDi);
|
|
}
|
|
|
|
public function testcreate_form()
|
|
{
|
|
$data = array('name' => 'testForm');
|
|
$createdFormId = 1;
|
|
|
|
$serviceMock = $this->getServiceMock();
|
|
$serviceMock->expects($this->atLeastOnce())
|
|
->method('addNewForm')
|
|
->will($this->returnValue($createdFormId));
|
|
|
|
$this->api->setService($serviceMock);
|
|
|
|
$result = $this->api->create_form($data);
|
|
$this->assertInternalType('int', $result);
|
|
$this->assertEquals($createdFormId, $result);
|
|
}
|
|
|
|
public function testcreate_formMissingName()
|
|
{
|
|
$data = array();
|
|
$this->setExpectedException('\Box_Exception', 'Form name was not provided');
|
|
$this->api->create_form($data);
|
|
}
|
|
public function testcreate_formTypeIsNotInList()
|
|
{
|
|
$data = array(
|
|
'name' => 'testName',
|
|
'type' => 'custom',
|
|
);
|
|
$this->setExpectedException('\Box_Exception', 'Form style was not found in predefined list', 3657);
|
|
$this->api->create_form($data);
|
|
}
|
|
|
|
public function testadd_field()
|
|
{
|
|
$data = array(
|
|
'type' => 'text',
|
|
'options' => array('sameValue'),
|
|
'form_id' => 1,
|
|
);
|
|
$newFieldId = 2;
|
|
|
|
$serviceMock = $this->getServiceMock();
|
|
$serviceMock->expects($this->atLeastOnce())
|
|
->method('typeValidation')
|
|
->will($this->returnValue(true));
|
|
|
|
$serviceMock->expects($this->atLeastOnce())
|
|
->method('isArrayUnique')
|
|
->will($this->returnValue(true));
|
|
|
|
$serviceMock->expects($this->atLeastOnce())
|
|
->method('addNewField')
|
|
->will($this->returnValue($newFieldId));
|
|
|
|
$this->api->setService($serviceMock);
|
|
|
|
$result = $this->api->add_field($data);
|
|
$this->assertInternalType('int', $result);
|
|
$this->assertEquals($newFieldId, $result);
|
|
}
|
|
|
|
public function testadd_fieldMissingType()
|
|
{
|
|
$data = array();
|
|
$this->setExpectedException('\Box_Exception', 'Form field type is not valid', 2684);
|
|
$this->api->add_field($data);
|
|
}
|
|
|
|
public function testadd_fieldOptionsNotUnique()
|
|
{
|
|
$data = array(
|
|
'type' => 'text',
|
|
'options' => array('sameValue', 'sameValue'),
|
|
);
|
|
|
|
$this->api->setService($this->service);
|
|
$this->setExpectedException('\Box_Exception', 'This input type must have unique values', 3658);
|
|
$this->api->add_field($data);
|
|
}
|
|
|
|
public function testadd_fieldMissingFormId()
|
|
{
|
|
$data = array(
|
|
'type' => 'text',
|
|
'options' => array('sameValue'),
|
|
);
|
|
|
|
$this->api->setService($this->service);
|
|
|
|
$this->setExpectedException('\Box_Exception', 'Form id was not passed', 9846);
|
|
$this->api->add_field($data);
|
|
}
|
|
|
|
public function testget_form()
|
|
{
|
|
$data['id'] = 1;
|
|
|
|
$serviceMock = $this->getServiceMock();
|
|
$serviceMock->expects($this->atLeastOnce())
|
|
->method('getForm')
|
|
->will($this->returnValue(array()));
|
|
|
|
$this->api->setService($serviceMock);
|
|
$result = $this->api->get_form($data);
|
|
$this->assertInternalType('array', $result);
|
|
}
|
|
|
|
public function testget_formMissingFormId()
|
|
{
|
|
$data = array();
|
|
|
|
$this->setExpectedException('\Box_Exception', 'Form id was not passed', 2391);
|
|
$this->api->get_form($data);
|
|
}
|
|
|
|
public function testget_form_fields()
|
|
{
|
|
$data['form_id'] = 1;
|
|
|
|
$serviceMock = $this->getServiceMock();
|
|
$serviceMock->expects($this->atLeastOnce())
|
|
->method('getFormFields')
|
|
->will($this->returnValue(array()));
|
|
|
|
$this->api->setService($serviceMock);
|
|
$result = $this->api->get_form_fields($data);
|
|
$this->assertInternalType('array', $result);
|
|
}
|
|
|
|
public function testget_form_fieldsMissingFormId()
|
|
{
|
|
$data = array();
|
|
|
|
$this->setExpectedException('\Box_Exception', 'Form id was not passed', 1822);
|
|
$this->api->get_form_fields($data);
|
|
}
|
|
|
|
|
|
public function testget_field()
|
|
{
|
|
$data['id'] = 3;
|
|
|
|
$serviceMock = $this->getServiceMock();
|
|
$serviceMock->expects($this->atLeastOnce())
|
|
->method('getField')
|
|
->will($this->returnValue(array()));
|
|
|
|
$this->api->setService($serviceMock);
|
|
|
|
$result = $this->api->get_field($data);
|
|
$this->assertInternalType('array', $result);
|
|
}
|
|
|
|
public function testget_fieldMissingId()
|
|
{
|
|
$data = array();
|
|
|
|
$this->setExpectedException('\Box_Exception', 'Field id was not passed', 3547);
|
|
$this->api->get_field($data);
|
|
}
|
|
|
|
public function testget_forms()
|
|
{
|
|
$serviceMock = $this->getServiceMock();
|
|
$serviceMock->expects($this->atLeastOnce())
|
|
->method('getForms')
|
|
->will($this->returnValue(array()));
|
|
|
|
$this->api->setService($serviceMock);
|
|
|
|
$result = $this->api->get_forms();
|
|
$this->assertInternalType('array', $result);
|
|
}
|
|
|
|
public function testdelete_form()
|
|
{
|
|
$data['id'] = 1;
|
|
|
|
$serviceMock = $this->getServiceMock();
|
|
$serviceMock->expects($this->atLeastOnce())
|
|
->method('removeForm')
|
|
->will($this->returnValue(array()));
|
|
|
|
$this->api->setService($serviceMock);
|
|
|
|
$result = $this->api->delete_form($data);
|
|
$this->assertTrue($result);
|
|
}
|
|
|
|
public function testdelete_formMissingId()
|
|
{
|
|
$data = array();
|
|
|
|
$this->setExpectedException('\Box_Exception', 'Form id was not passed', 9958);
|
|
$this->api->delete_form($data);
|
|
}
|
|
|
|
|
|
public function testdelete_field()
|
|
{
|
|
$data['id'] = 1;
|
|
|
|
$serviceMock = $this->getServiceMock();
|
|
$serviceMock->expects($this->atLeastOnce())
|
|
->method('removeField')
|
|
->will($this->returnValue(array()));
|
|
|
|
$this->api->setService($serviceMock);
|
|
|
|
$result = $this->api->delete_field($data);
|
|
$this->assertTrue($result);
|
|
}
|
|
|
|
public function testdelete_fieldMissingId()
|
|
{
|
|
$data = array();
|
|
|
|
$this->setExpectedException('\Box_Exception', 'Field id was not passed', 9959);
|
|
$this->api->delete_field($data);
|
|
}
|
|
|
|
public function testupdate_field()
|
|
{
|
|
$updatedFieldId = 1;
|
|
$data = array (
|
|
'id' => $updatedFieldId,
|
|
'options' => array('sameValue'),
|
|
);
|
|
|
|
$serviceMock = $this->getServiceMock();
|
|
$serviceMock->expects($this->atLeastOnce())
|
|
->method('updateField')
|
|
->will($this->returnValue($updatedFieldId));
|
|
$serviceMock->expects($this->atLeastOnce())
|
|
->method('isArrayUnique')
|
|
->will($this->returnValue(true));
|
|
|
|
$this->api->setService($serviceMock);
|
|
|
|
$result = $this->api->update_field($data);
|
|
$this->assertInternalType('int', $result);
|
|
$this->assertEquals($updatedFieldId, $result);
|
|
}
|
|
|
|
public function testget_pairs()
|
|
{
|
|
$data = array();
|
|
$serviceMock = $this->getServiceMock();
|
|
$serviceMock->expects($this->atLeastOnce())
|
|
->method('getFormPairs')
|
|
->will($this->returnValue(array()));
|
|
|
|
$this->api->setService($serviceMock);
|
|
|
|
$result = $this->api->get_pairs($data);
|
|
$this->assertInternalType('array', $result);
|
|
}
|
|
|
|
public function testcopy_form()
|
|
{
|
|
$newFormId = 2;
|
|
$data = array(
|
|
'form_id' => 1,
|
|
'name' => 'testForm',
|
|
);
|
|
|
|
$serviceMock = $this->getServiceMock();
|
|
$serviceMock->expects($this->atLeastOnce())
|
|
->method('dublicateForm')
|
|
->will($this->returnValue($newFormId));
|
|
|
|
$this->api->setService($serviceMock);
|
|
$result = $this->api->copy_form($data);
|
|
$this->assertInternalType('int', $result);
|
|
$this->assertEquals($newFormId, $result);
|
|
}
|
|
|
|
public function testcopy_formMisssingId()
|
|
{
|
|
$data = array();
|
|
|
|
$this->setExpectedException('\Box_Exception', 'Form id was not passed', 9958);
|
|
$this->api->copy_form($data);
|
|
}
|
|
|
|
public function testcopy_formMisssingName()
|
|
{
|
|
$data = array('form_id' => 1);
|
|
|
|
$this->setExpectedException('\Box_Exception', 'Form name was not passed', 9842);
|
|
$this->api->copy_form($data);
|
|
}
|
|
|
|
public function testupdate_form_settings()
|
|
{
|
|
$data = array(
|
|
'form_id' => 1,
|
|
'form_name' => 'testForm',
|
|
'type' => 'default',
|
|
|
|
);
|
|
|
|
$serviceMock = $this->getServiceMock();
|
|
$serviceMock->expects($this->atLeastOnce())
|
|
->method('updateFormSettings')
|
|
->will($this->returnValue(true));
|
|
|
|
$this->api->setService($serviceMock);
|
|
$result = $this->api->update_form_settings($data);
|
|
$this->assertTrue($result);
|
|
}
|
|
|
|
|
|
public function form_settings_data()
|
|
{
|
|
return array(
|
|
array('form_id', 'Form id was not passed', 1654),
|
|
array('form_name', 'Form name was not passed', 9241),
|
|
array('type', 'Form type was not passed', 3794),
|
|
array('', 'Field type not supported', 3207),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider form_settings_data
|
|
*/
|
|
public function testupdate_form_settingsExceptions($missingField, $exceptionMessage, $exceptionCode)
|
|
{
|
|
$data = array(
|
|
'form_id' => 1,
|
|
'form_name' => 'testForm',
|
|
'type' => 'customType',
|
|
|
|
);
|
|
unset($data[ $missingField ]);
|
|
|
|
$this->setExpectedException('\Box_Exception', $exceptionMessage, $exceptionCode);
|
|
$this->api->update_form_settings($data);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|