Files
FOSSBilling/tests/modules/Formbuilder/Api/AdminTest.php
T
Belle Aerni 602b537996 Opt-in error reporting via Sentry.io (#1732)
* Began implementing Sentry.io support

* Added to the installer

* Added the ID to the bug report template and UI

* Hook it into our error handler, added the ability to filter by error code

* Attempting to add the DSN via GH actions

* Spelling and making PHPstan happy

* Skip exceptions for various incomplete configurations

Also brings in some of the category suggestions made by @Anuril in #195

* Update tests to account for changes

* Added & renamed exception classes

* Fix tests

* Change a bunch of items to be InformationExceptions

* Use a lighter HTTP client, fixed API error reporting

* I guess that's not needed now?

Arighty then

* Improvements, additional info added

* Update SentryHelper.php

* Improved error logging, added instance ID to error page

Also made it so that errors are correctly sent to Sentry rather than exclusively exceptions

* Make the tests happy

* Added the link, improved wording

Also removed the "performance" part of the reporting as it needs to be added to the front-end and I'm not currently implementing that

* Began implementing UI alerts and info

* Added the ability to toggle, fixed tests, use path constants

* Added some useful info & links to the page

* These should be InformationException

* Fix the automated Sentry DSN entry

* Fix the install number text

* Minor cleanup, converted more to InformationException

* Replaced some direct config references with constants

We have the BB_DEBUG constant to indicate if we have debug mode enabled or not, so we shouldn't be directly referencing it in the config file unless we actually need to do so.

Using the constant means there's less locations to update the reference if the config structure is changed

* Bugfix

* Consistency & fixed a PHPDoc

* Corrections

* We actually do want exceptions thrown here

* Change how we get the instance ID

* Stability improvements

Having support for translatable error pages was proving to be problematic, so I had to remove it.

I also had to update the exception class to handle cases where the trans function wasn't setup yet (needed for the /run-patcher page if an error occurs)

* Missed this change on git

* Also fix the variable names...

* Re-added new deps after rebase

* This should also accept "Error" types

* Added more info to the bug report and removed a dug line

* This wasn't supposed to get renamed

* Updated some missed references to Box_Exception
2023-10-28 18:40:39 -07:00

425 lines
12 KiB
PHP

<?php
namespace Box\Mod\Formbuilder\Api;
class AdminTest extends \BBTestCase {
/**
* @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(): void
{
$this->service = new \Box\Mod\Formbuilder\Service();
$this->api = new \Box\Mod\Formbuilder\Api\Admin();
}
public function testgetDi()
{
$di = new \Pimple\Container();
$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);
$validatorMock = $this->getMockBuilder('\FOSSBilling\Validate')->disableOriginalConstructor()->getMock();
$validatorMock->expects($this->atLeastOnce())
->method('checkRequiredParamsForArray')
->will($this->returnValue(null));
$di = new \Pimple\Container();
$di['validator'] = $validatorMock;
$this->api->setDi($di);
$result = $this->api->create_form($data);
$this->assertIsInt($result);
$this->assertEquals($createdFormId, $result);
}
public function testcreate_formTypeIsNotInList()
{
$data = array(
'name' => 'testName',
'type' => 'custom',
);
$validatorMock = $this->getMockBuilder('\FOSSBilling\Validate')->disableOriginalConstructor()->getMock();
$validatorMock->expects($this->atLeastOnce())
->method('checkRequiredParamsForArray')
->will($this->returnValue(null));
$di = new \Pimple\Container();
$di['validator'] = $validatorMock;
$this->api->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('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->assertIsInt($result);
$this->assertEquals($newFieldId, $result);
}
public function testadd_fieldMissingType()
{
$data = array();
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionCode(2684);
$this->expectExceptionMessage('Form field type is invalid');
$this->api->add_field($data);
}
public function testadd_fieldOptionsNotUnique()
{
$data = array(
'type' => 'text',
'options' => array('sameValue', 'sameValue'),
);
$this->api->setService($this->service);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionCode(3658);
$this->expectExceptionMessage('This input type must have unique values');
$this->api->add_field($data);
}
public function testadd_fieldMissingFormId()
{
$data = array(
'type' => 'text',
'options' => array('sameValue'),
);
$this->api->setService($this->service);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionCode(9846);
$this->expectExceptionMessage('Form id was not passed');
$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()));
$validatorMock = $this->getMockBuilder('\FOSSBilling\Validate')->disableOriginalConstructor()->getMock();
$validatorMock->expects($this->atLeastOnce())
->method('checkRequiredParamsForArray')
->will($this->returnValue(null));
$di = new \Pimple\Container();
$di['validator'] = $validatorMock;
$this->api->setDi($di);
$this->api->setService($serviceMock);
$result = $this->api->get_form($data);
$this->assertIsArray($result);
}
public function testget_form_fields()
{
$data['form_id'] = 1;
$serviceMock = $this->getServiceMock();
$serviceMock->expects($this->atLeastOnce())
->method('getFormFields')
->will($this->returnValue(array()));
$validatorMock = $this->getMockBuilder('\FOSSBilling\Validate')->disableOriginalConstructor()->getMock();
$validatorMock->expects($this->atLeastOnce())
->method('checkRequiredParamsForArray')
->will($this->returnValue(null));
$di = new \Pimple\Container();
$di['validator'] = $validatorMock;
$this->api->setDi($di);
$this->api->setService($serviceMock);
$result = $this->api->get_form_fields($data);
$this->assertIsArray($result);
}
public function testget_field()
{
$data['id'] = 3;
$serviceMock = $this->getServiceMock();
$serviceMock->expects($this->atLeastOnce())
->method('getField')
->will($this->returnValue(array()));
$validatorMock = $this->getMockBuilder('\FOSSBilling\Validate')->disableOriginalConstructor()->getMock();
$validatorMock->expects($this->atLeastOnce())
->method('checkRequiredParamsForArray')
->will($this->returnValue(null));
$di = new \Pimple\Container();
$di['validator'] = $validatorMock;
$this->api->setDi($di);
$this->api->setService($serviceMock);
$result = $this->api->get_field($data);
$this->assertIsArray($result);
}
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->assertIsArray($result);
}
public function testdelete_form()
{
$data['id'] = 1;
$serviceMock = $this->getServiceMock();
$serviceMock->expects($this->atLeastOnce())
->method('removeForm')
->will($this->returnValue(array()));
$validatorMock = $this->getMockBuilder('\FOSSBilling\Validate')->disableOriginalConstructor()->getMock();
$validatorMock->expects($this->atLeastOnce())
->method('checkRequiredParamsForArray')
->will($this->returnValue(null));
$di = new \Pimple\Container();
$di['validator'] = $validatorMock;
$this->api->setDi($di);
$this->api->setService($serviceMock);
$result = $this->api->delete_form($data);
$this->assertTrue($result);
}
public function testdelete_field()
{
$data['id'] = 1;
$serviceMock = $this->getServiceMock();
$serviceMock->expects($this->atLeastOnce())
->method('removeField')
->will($this->returnValue(array()));
$validatorMock = $this->getMockBuilder('\FOSSBilling\Validate')->disableOriginalConstructor()->getMock();
$validatorMock->expects($this->atLeastOnce())
->method('checkRequiredParamsForArray')
->will($this->returnValue(null));
$di = new \Pimple\Container();
$di['validator'] = $validatorMock;
$this->api->setDi($di);
$this->api->setService($serviceMock);
$result = $this->api->delete_field($data);
$this->assertTrue($result);
}
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));
$validatorMock = $this->getMockBuilder('\FOSSBilling\Validate')->disableOriginalConstructor()->getMock();
$validatorMock->expects($this->atLeastOnce())
->method('checkRequiredParamsForArray')
->will($this->returnValue(null));
$di = new \Pimple\Container();
$di['validator'] = $validatorMock;
$this->api->setDi($di);
$this->api->setService($serviceMock);
$result = $this->api->update_field($data);
$this->assertIsInt($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->assertIsArray($result);
}
public function testcopy_form()
{
$newFormId = 2;
$data = array(
'form_id' => 1,
'name' => 'testForm',
);
$serviceMock = $this->getServiceMock();
$serviceMock->expects($this->atLeastOnce())
->method('duplicateForm')
->will($this->returnValue($newFormId));
$this->api->setService($serviceMock);
$result = $this->api->copy_form($data);
$this->assertIsInt($result);
$this->assertEquals($newFormId, $result);
}
public function testcopy_formMissingId()
{
$data = array();
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionCode(9958);
$this->expectExceptionMessage('Form id was not passed');
$this->api->copy_form($data);
}
public function testcopy_formMissingName()
{
$data = array('form_id' => 1);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionCode(9842);
$this->expectExceptionMessage('Form name was not passed');
$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->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage($exceptionMessage, $exceptionCode);
$this->api->update_form_settings($data);
}
}