Files
FOSSBilling/tests/modules/Extension/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

726 lines
24 KiB
PHP

<?php
namespace Box\Mod\Extension;
use \Symfony\Component\HttpClient\MockHttpClient;
class PdoMock extends \PDO
{
public function __construct (){}
}
class PdoStatmentsMock extends \PDOStatement
{
public function __construct (){}
}
class ServiceTest extends \BBTestCase {
/**
* @var \Box\Mod\Extension\Service
*/
protected $service = null;
public function setup(): void
{
$this->service = new \Box\Mod\Extension\Service();
}
public function testgetDi()
{
$di = new \Pimple\Container();
$this->service->setDi($di);
$getDi = $this->service->getDi();
$this->assertEquals($di, $getDi);
}
public function testisCoreModule()
{
$coreModules = array('extension', 'cron', 'staff');
$modMock = $this->getMockBuilder('\Box_Mod')->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getCoreModules')
->will($this->returnValue($coreModules));
$di = new \Pimple\Container();
$di['mod'] = $di->protect(function ($name) use($modMock) { return $modMock;});
$this->service->setDi($di);
$result = $this->service->isCoreModule('extension');
$this->assertIsBool($result);
$this->assertTrue($result);
}
public function testisExtensionActiveModAndCoreModule()
{
$coreModules = array('extension', 'cron', 'staff');
$modMock = $this->getMockBuilder('\Box_Mod')->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getCoreModules')
->will($this->returnValue($coreModules));
$di = new \Pimple\Container();
$di['mod'] = $di->protect(function ($name) use($modMock) { return $modMock;});
$this->service->setDi($di);
$result = $this->service->isExtensionActive('mod', 'extension');
$this->assertIsBool($result);
$this->assertTrue($result);
}
public function testisExtensionActiveModNotFound()
{
$coreModules = array('extension', 'cron', 'staff');
$modMock = $this->getMockBuilder('\Box_Mod')->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getCoreModules')
->will($this->returnValue($coreModules));
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('getCell')
->will($this->returnValue(null));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['mod'] = $di->protect(function ($name) use($modMock) { return $modMock;});
$this->service->setDi($di);
$result = $this->service->isExtensionActive('mod', 'ModDoesNotExists');
$this->assertIsBool($result);
$this->assertFalse($result);
}
public function testonBeforeAdminCronRun()
{
//@TODO later create test
}
public function testremoveNotExistingModules()
{
$model = new \Model_Extension();
$model->loadBean(new \DummyBean());
$model->name = 'extensionName';
$modMock = $this->getMockBuilder('\Box_Mod')->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getManifest')->willThrowException(new \Exception());
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('find')
->will($this->returnValue(array($model)));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['mod'] = $di->protect(function ($name) use($modMock) { return $modMock;});
$this->service->setDi($di);
$result = $this->service->removeNotExistingModules();
$this->assertIsInt($result);
$this->assertTrue($result > 0);
}
public function searchQueryData()
{
return array(
array(array(), 'SELECT * FROM extension', array()),
array(array('type' => 'mod'), 'AND type = :type', array(':type' => 'mod')),
array(array('search' => 'FindUp'), 'AND name LIKE :search', array(':search' => 'FindUp')),
);
}
/**
* @dataProvider searchQueryData
*/
public function testgetSearchQuery($data, $expectedStr, $expectedParams)
{
$di = new \Pimple\Container();
$this->service->setDi($di);
list($sql, $params) = $this->service->getSearchQuery($data);
$this->assertIsString($sql);
$this->assertIsArray($params);
$this->assertTrue(strpos($sql, $expectedStr) !== false, $sql);
$this->assertTrue(array_diff_key($params, $expectedParams) == array());
}
public function testgetExtensionsList()
{
$data = array(
'has_settings' => true,
'active' => true,
);
$model['manifest'] = '{"J":5,"0":"N"}';
$model['type'] = 'mod';
$model['status'] = 'installed';
$model['name'] = 'extensionName';
$model['version'] = '1';
$modelFind = new \Model_Extension();
$modelFind->loadBean(new \DummyBean());
$modelFind->name = 'extensionName';
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('getAll')
->will($this->returnValue(array($model)));
$dbMock->expects($this->atLeastOnce())
->method('find')
->will($this->returnValue(array($modelFind)));
$coreModules = array('extension', 'cron', 'staff');
$modMock = $this->getMockBuilder('\Box_Mod')->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getCoreModules')
->will($this->returnValue($coreModules));
$modMock->expects($this->atLeastOnce())
->method('getManifest')
->will($this->returnValue(array()));
$modMock->expects($this->atLeastOnce())
->method('hasSettingsPage')
->will($this->returnValue(true));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['mod'] = $di->protect(function ($name) use($modMock) { return $modMock;});
$this->service->setDi($di);
$result = $this->service->getExtensionsList($data);
$this->assertIsArray($result);
}
public function testgetExtensionsListOnlyInstalled()
{
$data = array(
'installed' => true,
);
$model['manifest'] = '{"J":5,"0":"N"}';
$model['type'] = 'mod';
$model['status'] = 'installed';
$model['name'] = 'extensionName';
$model['version'] = '1';
$modelFind = new \Model_Extension();
$modelFind->loadBean(new \DummyBean());
$modelFind->name = 'extensionName';
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('getAll')
->will($this->returnValue(array($model)));
$dbMock->expects($this->atLeastOnce())
->method('find')
->will($this->returnValue(array($modelFind)));
$modMock = $this->getMockBuilder('\Box_Mod')->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getManifest')
->will($this->returnValue(array()));
$modMock->expects($this->atLeastOnce())
->method('hasSettingsPage')
->will($this->returnValue(true));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['mod'] = $di->protect(function ($name) use($modMock) { return $modMock;});
$this->service->setDi($di);
$result = $this->service->getExtensionsList($data);
$this->assertIsArray($result);
}
public function testgetAdminNavigation()
{
$staffServiceMock = $this->getMockBuilder('\Box\Mod\Staff\Service')->getMock();
$staffServiceMock->expects($this->atLeastOnce())
->method('hasPermission')
->will($this->returnValue(true));
$pdoStatment = $this->getMockBuilder('\Box\Mod\Extension\PdoStatmentsMock')->getMock();
$pdoStatment->expects($this->atLeastOnce())
->method('execute');
$pdoStatment->expects($this->atLeastOnce())
->method('fetchAll')
->will($this->returnValue(array()));
$pdoMock = $this->getMockBuilder('\Box\Mod\Extension\PdoMock')->getMock();
$pdoMock->expects($this->atLeastOnce())
->method('prepare')
->will($this->returnValue($pdoStatment));
$link = 'extension';
$urlMock = $this->getMockBuilder('Box_Url')->getMock();
$urlMock->expects($this->atLeastOnce())
->method('adminLink')
->willReturn('http://fossbilling.org/index.php?_url=/' . $link);
$di['url'] = $urlMock;
$di = new \Pimple\Container();
$di['mod'] = $di->protect(function ($name) use($di) {
$mod = new \Box_Mod($name);
$mod->setDi($di);
return $mod;
});
$di['tools'] = new \FOSSBilling\Tools();
$di['mod_service'] = $di->protect(function () use ($staffServiceMock) {return $staffServiceMock; });
$di['pdo'] = $pdoMock;
$di['url'] = $urlMock;
$this->service->setDi($di);
$result = $this->service->getAdminNavigation(new \Model_Admin());
$this->assertIsArray($result);
}
public function testfindExtension()
{
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->will($this->returnValue(new \Model_Extension()));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$this->service->setDi($di);
$result = $this->service->findExtension('mod', 'id');
$this->assertInstanceOf('\Model_Extension', $result);
}
public function testupdate()
{
$model = new \Model_Extension();
$model->loadBean(new \DummyBean());
$model->type = 'mod';
$model->name = 'testExtension';
$model->version = '2';
$newversion = '3';
$extensionMock = $this->getMockBuilder('\FOSSBilling\ExtensionManager')->getMock();
$di = new \Pimple\Container();
$di['extension_manager'] = $extensionMock;
$this->service->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionCode(252);
$this->expectExceptionMessage('Visit the extension directory for more information on updating this extension.');
$this->service->update($model);
}
public function testactivate()
{
$ext = new \Model_Extension();
$ext->loadBean(new \DummyBean());
$ext->type = 'mod';
$ext->name = 'testExtension';
$expectedResult = array(
'id' => $ext->name,
'type' => $ext->type,
'redirect' => true,
'has_settings' => true,
);
$modMock = $this->getMockBuilder('\Box_Mod')->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getManifest')
->will($this->returnValue(array('version' => 1)));
$modMock->expects($this->atLeastOnce())
->method('hasAdminController')
->will($this->returnValue(true));
$modMock->expects($this->atLeastOnce())
->method('hasSettingsPage')
->will($this->returnValue(true));
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('store')
->will($this->returnValue(1));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['mod'] = $di->protect(function ($name) use($modMock) { return $modMock;});
$this->service->setDi($di);
$result = $this->service->activate($ext);
$this->assertIsArray($result);
$this->assertEquals($expectedResult, $result);
}
public function testdeactivate()
{
$ext = new \Model_Extension();
$ext->loadBean(new \DummyBean());
$ext->type = 'mod';
$ext->name = 'extensionTest';
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('trash');
$modMock = $this->getMockBuilder('\Box_Mod')->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getCoreModules')
->will($this->returnValue(array()));
$modMock->expects($this->atLeastOnce())
->method('uninstall')
->will($this->returnValue(true));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['mod'] = $di->protect(function ($name) use($modMock) { return $modMock;});
$this->service->setDi($di);
$result = $this->service->deactivate($ext);
$this->assertTrue($result);
}
public function testdeactivateCoreModuleException()
{
$ext = new \Model_Extension();
$ext->loadBean(new \DummyBean());
$ext->type = 'mod';
$ext->name = 'extensionTest';
$modMock = $this->getMockBuilder('\Box_Mod')->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getCoreModules')
->will($this->returnValue(array($ext->name)));
$di = new \Pimple\Container();
$di['mod'] = $di->protect(function ($name) use($modMock) { return $modMock;});
$this->service->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('FOSSBilling core modules can not be managed');
$this->service->deactivate($ext);
}
public function testdeactivateUninstallException()
{
$ext = new \Model_Extension();
$ext->loadBean(new \DummyBean());
$ext->type = 'mod';
$ext->name = 'extensionTest';
$exceptionMessage = 'testException';
$modMock = $this->getMockBuilder('\Box_Mod')->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getCoreModules')
->will($this->returnValue(array()));
$modMock->expects($this->atLeastOnce())
->method('uninstall')
->willThrowException(new \FOSSBilling\Exception($exceptionMessage));
$di = new \Pimple\Container();
$di['mod'] = $di->protect(function ($name) use($modMock) { return $modMock;});
$this->service->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage($exceptionMessage);
$this->service->deactivate($ext);
}
public function testdeactivateExtensionHook()
{
$ext = new \Model_Extension();
$ext->loadBean(new \DummyBean());
$ext->type = 'hook';
$ext->name = 'extensionTest';
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('trash');
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$this->service->setDi($di);
$result = $this->service->deactivate($ext);
$this->assertTrue($result);
}
public function testuninstall()
{
$ext = new \Model_Extension();
$ext->loadBean(new \DummyBean());
$ext->type = 'mod';
$ext->name = 'extensionTest';
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('trash');
$modMock = $this->getMockBuilder('\Box_Mod')->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getCoreModules')
->will($this->returnValue(array()));
$modMock->expects($this->atLeastOnce())
->method('uninstall')
->will($this->returnValue(true));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['mod'] = $di->protect(function ($name) use($modMock) { return $modMock;});
$this->service->setDi($di);
$result = $this->service->deactivate($ext);
$this->assertTrue($result);
}
public function testdownloadAndExtractDownloadUrlMissing()
{
$extensionMock = $this->getMockBuilder(\FOSSBilling\ExtensionManager::class)->getMock();
$extensionMock->expects($this->atLeastOnce())
->method('getLatestExtensionRelease')
->will($this->returnValue(array()));
$di = new \Pimple\Container();
$di['extension_manager'] = $extensionMock;
$this->service->setDi($di);
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Coudn\'t find a valid download URL for the extension.');
$this->service->downloadAndExtract('mod', 'extensionId');
}
public function testgetInstalledMods()
{
$pdoStatment = $this->getMockBuilder(\Box\Mod\Extension\PdoStatmentsMock::class)->getMock();
$pdoStatment->expects($this->atLeastOnce())
->method('execute');
$pdoStatment->expects($this->atLeastOnce())
->method('fetchAll')
->will($this->returnValue(array()));
$pdoMock = $this->getMockBuilder('\Box\Mod\Extension\PdoMock')->getMock();
$pdoMock->expects($this->atLeastOnce())
->method('prepare')
->will($this->returnValue($pdoStatment));
$di = new \Pimple\Container();
$di['pdo'] = $pdoMock;
$this->service->setDi($di);
$result = $this->service->getInstalledMods();
$this->assertEquals(array(), $result);
}
public function testactivateExistingExtension()
{
$data = array(
'id' => 'extensionId',
'type' => 'extensionType',
);
$model = new \Model_Extension();
$model->loadBean(new \DummyBean());
$serviceMock = $this->getMockBuilder('\Box\Mod\Extension\Service')
->setMethods(array('findExtension', 'activate'))
->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('findExtension')
->will($this->onConsecutiveCalls(null, $model));
$serviceMock->expects($this->atLeastOnce())
->method('activate')
->will($this->returnValue(array()));
$dbMock = $this->getMockBuilder(\Box_Database::class)->getMock();
$dbMock->expects($this->atLeastOnce())
->method('dispense')
->will($this->returnValue($model));
$dbMock->expects($this->atLeastOnce())
->method('store')
->will($this->returnValue(1));
$eventMock = $this->getMockBuilder(\Box_EventManager::class)->getMock();
$eventMock->expects($this->atLeastOnce())->
method('fire');
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['events_manager'] = $eventMock;
$di['logger'] = new \Box_Log();
$serviceMock->setDi($di);
$result = $serviceMock->activateExistingExtension($data);
$this->assertIsArray($result);
}
public function testactivateException()
{
$data = array(
'id' => 'extensionId',
'type' => 'extensionType',
);
$model = new \Model_Extension();
$model->loadBean(new \DummyBean());
$serviceMock = $this->getMockBuilder('\Box\Mod\Extension\Service')
->setMethods(array('findExtension', 'activate'))
->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('findExtension')
->will($this->returnValue($model));
$serviceMock->expects($this->atLeastOnce())
->method('activate')
->will($this->throwException(new \Exception()));
$eventMock = $this->getMockBuilder(\Box_EventManager::class)->getMock();
$eventMock->expects($this->atLeastOnce())->
method('fire');
$di = new \Pimple\Container();
$di['events_manager'] = $eventMock;
$serviceMock->setDi($di);
$this->expectException(\Exception::class);
$serviceMock->activateExistingExtension($data);
}
public function testgetConfig()
{
$data = array(
'ext' => 'extensionName',
);
$model = new \Model_ExtensionMeta();
$model->loadBean(new \DummyBean());
$dbMock = $this->getMockBuilder(\Box_Database::class)->getMock();
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->will($this->returnValue($model));
$cryptMock = $this->getMockBuilder(\Box_Crypt::class)->getMock();
$cryptMock->expects($this->atLeastOnce())
->method('decrypt');
$toolsMock = $this->getMockBuilder(\FOSSBilling\Tools::class)->getMock();
$toolsMock->expects($this->atLeastOnce())
->method('decodeJ')
->will($this->returnValue(array()));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['crypt'] = $cryptMock;
$di['tools'] = $toolsMock;
$di['config'] = array('salt' => '');
$this->service->setDi($di);
$result = $this->service->getConfig($data['ext']);
$this->assertIsArray($result);
}
public function testgetConfigExtensionMetaNotFound()
{
$data = array(
'ext' => 'extensionName',
);
$model = new \Model_ExtensionMeta();
$model->loadBean(new \DummyBean());
$dbMock = $this->getMockBuilder(\Box_Database::class)->getMock();
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->will($this->returnValue(null));
$dbMock->expects($this->atLeastOnce())
->method('dispense')
->will($this->returnValue($model));
$dbMock->expects($this->atLeastOnce())
->method('store')
->will($this->returnValue(1));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$this->service->setDi($di);
$result = $this->service->getConfig($data['ext']);
$this->assertIsArray($result);
$this->assertEquals(array(), $result);
}
public function testsetConfig()
{
$data = array(
'ext' => 'extensionName',
);
$serviceMock = $this->getMockBuilder(\Box\Mod\Extension\Service::class)->setMethods(['getConfig'])->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getConfig')
->will($this->returnValue(array()));
$toolsMock = $this->getMockBuilder(\FOSSBilling\Tools::class)->getMock();
$cryptMock = $this->getMockBuilder(\Box_Crypt::class)->getMock();
$cryptMock->expects($this->atLeastOnce())
->method('encrypt')
->will($this->returnValue('encryptedConfig'));
$model = new \Model_ExtensionMeta();
$model->loadBean(new \DummyBean());
$dbMock = $this->getMockBuilder(\Box_Database::class)->getMock();
$dbMock->expects($this->atLeastOnce())
->method('exec')
->will($this->returnValue(array()));
$eventMock = $this->getMockBuilder(\Box_EventManager::class)->getMock();
$eventMock->expects($this->atLeastOnce())->
method('fire');
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$di['tools'] = $toolsMock;
$di['crypt'] = $cryptMock;
$di['events_manager'] = $eventMock;
$di['logger'] = new \Box_Log();
$di['config'] = array('salt' => '');
$serviceMock->setDi($di);
$result = $serviceMock->setConfig($data);
$this->assertTrue($result);
}
}