Files
FOSSBilling/tests/modules/System/ServiceTest.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

419 lines
13 KiB
PHP

<?php
namespace Box\Mod\System;
use Twig\Environment;
class ServiceTest extends \BBTestCase
{
/**
* @var \Box\Mod\System\Service
*/
protected $service = null;
public function setup(): void
{
$this->service = new \Box\Mod\System\Service();
}
public function testgetParamValueMissingKeyParam()
{
$param = array();
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Parameter key is missing');
$this->service->getParamValue($param);
}
public function testgetCompany()
{
$config = array('url' => 'www.fossbilling.org');
$expected = array(
'www' => $config['url'],
'name' => 'Inc. Test',
'email' => 'work@example.eu',
'tel' => NULL,
'signature' => NULL,
'logo_url' => NULL,
'logo_url_dark' => NULL,
'favicon_url' => NULL,
'address_1' => NULL,
'address_2' => NULL,
'address_3' => NULL,
'account_number' => NULL,
'bank_name' => NULL,
'bic' => NULL,
'display_bank_info' => NULL,
'bank_info_pagebottom' => NULL,
'number' => NULL,
'note' => NULL,
'privacy_policy' => NULL,
'tos' => NULL,
'vat_number' => NULL,
);
$multParamsResults = array(
array(
'param' => 'company_name',
'value' => 'Inc. Test',
),
array(
'param' => 'company_email',
'value' => 'work@example.eu',
),
);
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('getAll')
->will($this->returnValue($multParamsResults));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['config'] = $config;
$this->service->setDi($di);
$result = $this->service->getCompany();
$this->assertIsArray($result);
$this->assertEquals($expected, $result);
}
public function testgetLanguages()
{
$result = $this->service->getLanguages(true);
$this->assertIsArray($result);
}
public function testgetParams()
{
$expected = array(
'company_name' => 'Inc. Test',
'company_email' => 'work@example.eu',
);
$multParamsResults = array(
array(
'param' => 'company_name',
'value' => 'Inc. Test',
),
array(
'param' => 'company_email',
'value' => 'work@example.eu',
),
);
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('getAll')
->will($this->returnValue($multParamsResults));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$this->service->setDi($di);
$result = $this->service->getParams(array());
$this->assertIsArray($result);
$this->assertEquals($expected, $result);
}
public function testupdateParams()
{
$data = array(
'company_name' => 'newValue'
);
$eventMock = $this->getMockBuilder('\Box_EventManager')->getMock();
$eventMock->expects($this->atLeastOnce())
->method('fire');
$logMock = $this->getMockBuilder('\Box_Log')->getMock();
$systemServiceMock = $this->getMockBuilder('\Box\Mod\System\Service')->setMethods(array('setParamValue'))->getMock();
$systemServiceMock->expects($this->atLeastOnce())
->method('setParamValue')
->will($this->returnValue(true));
$di = new \Pimple\Container();
$di['events_manager'] = $eventMock;
$di['logger'] = $logMock;
$systemServiceMock->setDi($di);
$result = $systemServiceMock->updateParams($data);
$this->assertIsBool($result);
$this->assertTrue($result);
}
public function testgetMessages()
{
$latestVersion = '1.0.0';
$type = 'info';
$systemServiceMock = $this->getMockBuilder('\Box\Mod\System\Service')->setMethods(array('getParamValue'))->getMock();
$systemServiceMock->expects($this->atLeastOnce())
->method('getParamValue')
->will($this->returnValue(false));
$updaterMock = $this->getMockBuilder('\FOSSBilling\Update')->getMock();
$updaterMock->expects($this->atLeastOnce())
->method('isUpdateAvailable')
->will($this->returnValue(true));
$updaterMock->expects($this->atLeastOnce())
->method('getLatestVersion')
->will($this->returnValue($latestVersion));
$di = new \Pimple\Container();
$di['updater'] = $updaterMock;
$di['mod_service'] = $di->protect(function () use ($systemServiceMock) {
return $systemServiceMock;
});
$di['tools'] = $toolsMock;
$systemServiceMock->setDi($di);
$result = $systemServiceMock->getMessages($type);
$this->assertIsArray($result);
}
public function testtemplateExistsEmptyPaths()
{
$getThemeResults = array('paths' => array());
$systemServiceMock = $this->getMockBuilder('\Box\Mod\System\Service')->setMethods(array('getThemeConfig'))->getMock();
$systemServiceMock->expects($this->atLeastOnce())
->method('getThemeConfig')
->will($this->returnValue($getThemeResults));
$di = new \Pimple\Container();
$di['mod_service'] = $di->protect(function () use ($systemServiceMock) {
return $systemServiceMock;
});
$this->service->setDi($di);
$result = $this->service->templateExists('defaultFile.cp');
$this->assertIsBool($result);
$this->assertFalse($result);
}
public function testrenderStringTemplateException()
{
$vars = array(
'_client_id' => 1
);
$this
->getMockBuilder('Drupal\Core\Template\TwigEnvironment')
->disableOriginalConstructor()
->getMock();
$twigMock = $this->getMockBuilder('\Twig\Environment')->disableOriginalConstructor()->getMock();
$twigMock->expects($this->atLeastOnce())
->method('addGlobal');
$twigMock->method('createTemplate')
->will($this->throwException(new \Error('Error')));
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('load')
->will($this->returnValue(new \Model_Client()));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['twig'] = $twigMock;
$di['api_client'] = new \Model_Client();
$this->service->setDi($di);
$this->expectException(\Error::class);
$this->service->renderString('test', false, $vars);
}
public function testrenderStringTemplate()
{
$vars = array(
'_client_id' => 1
);
$twigMock = $this->getMockBuilder(Environment::class)
->disableOriginalConstructor()
->getMock();
$twigMock->expects($this->atLeastOnce())
->method('addGlobal');
// $twigMock->method('createTemplate')
// ->willReturn(new \FakeTemplateWrapper('test'));
// $twigMock->method('load')
// ->willReturn(new \FakeTemplateWrapper('test'));
$twigMock->method('render')
->willReturn('');
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('load')
->will($this->returnValue(new \Model_Client()));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['twig'] = $twigMock;
$di['api_client'] = new \Model_Client();
$this->service->setDi($di);
$string = $this->service->renderString('test', true, $vars);
$this->assertEquals($string, 'test');
}
public function testclearCache()
{
$toolsMock = $this->getMockBuilder('\FOSSBilling\Tools')->getMock();
$toolsMock->expects($this->atLeastOnce())
->method('emptyFolder')
->will($this->returnValue(true));
$di = new \Pimple\Container();
$di['tools'] = $toolsMock;
$this->service->setDi($di);
$result = $this->service->clearCache();
$this->assertIsBool($result);
$this->assertTrue($result);
}
public function testgetPeriod()
{
$code = '1W';
$expexted = 'Every week';
$result = $this->service->getPeriod($code);
$this->assertIsString($result);
$this->assertEquals($expexted, $result);
}
public function testgetCountries()
{
$modMock = $this->getMockBuilder('\Box_Mod')->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getConfig')
->will($this->returnValue(array('countries' => 'US')));
$di = new \Pimple\Container();
$di['mod'] = $di->protect(function () use ($modMock) {
return $modMock;
});
$this->service->setDi($di);
$result = $this->service->getCountries();
$this->assertIsArray($result);
}
public function testgetEuCountries()
{
$modMock = $this->getMockBuilder('\Box_Mod')->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getConfig')
->will($this->returnValue(array('countries' => 'US')));
$di = new \Pimple\Container();
$di['mod'] = $di->protect(function () use ($modMock) {
return $modMock;
});
$this->service->setDi($di);
$result = $this->service->getEuCountries();
$this->assertIsArray($result);
}
public function testgetStates()
{
$result = $this->service->getStates();
$this->assertIsArray($result);
}
public function testgetPhoneCodes()
{
$data = array();
$result = $this->service->getPhoneCodes($data);
$this->assertIsArray($result);
}
public function testgetVersion()
{
$result = $this->service->getVersion();
$this->assertIsString($result);
$this->assertEquals(\FOSSBilling\Version::VERSION, $result);
}
public function testgetPendingMessages()
{
$di = new \Pimple\Container();
$sessionMock = $this->getMockBuilder('\FOSSBilling\Session')->disableOriginalConstructor()->getMock();
$sessionMock->expects($this->atLeastOnce())
->method('get')
->with('pending_messages')
->willReturn(array());
$di['session'] = $sessionMock;
$this->service->setDi($di);
$result = $this->service->getPendingMessages();
$this->assertIsArray($result);
}
public function testgetPendingMessages_GetReturnsNotArray()
{
$di = new \Pimple\Container();
$sessionMock = $this->getMockBuilder('\FOSSBilling\Session')->disableOriginalConstructor()->getMock();
$sessionMock->expects($this->atLeastOnce())
->method('get')
->with('pending_messages')
->willReturn(null);
$di['session'] = $sessionMock;
$this->service->setDi($di);
$result = $this->service->getPendingMessages();
$this->assertIsArray($result);
}
public function testsetPendingMessage()
{
$serviceMock = $this->getMockBuilder('\Box\Mod\System\Service')
->setMethods(array('getPendingMessages'))
->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getPendingMessages')
->willReturn(array());
$di = new \Pimple\Container();
$sessionMock = $this->getMockBuilder('\FOSSBilling\Session')->disableOriginalConstructor()->getMock();
$sessionMock->expects($this->atLeastOnce())
->method('set')
->with('pending_messages');
$di['session'] = $sessionMock;
$serviceMock->setDi($di);
$message = 'Important Message';
$result = $serviceMock->setPendingMessage($message);
$this->assertTrue($result);
}
public function testclearPendingMessages()
{
$di = new \Pimple\Container();
$sessionMock = $this->getMockBuilder('\FOSSBilling\Session')->disableOriginalConstructor()->getMock();
$sessionMock->expects($this->atLeastOnce())
->method('delete')
->with('pending_messages');
$di['session'] = $sessionMock;
$this->service->setDi($di);
$result = $this->service->clearPendingMessages();
$this->assertTrue($result);
}
}