Files
FOSSBilling/tests-legacy/modules/Extension/ServiceTest.php
T
Adam Daley 53eb4f2fca Refactor Twig environments and extensions (#2752)
* Replace Custom Twig components

Replace custom Twig components with native Twig components, remove unused custom Twig components, and move functionality to macros where appropriate.

m

Update partial_extensions.html.twig

* Update Twig components and migrate to FOSSBilling namespace

* Update Currency module...

* Refactor Twig environment config and loading

* Switch CSRF to cookie-based double-submit

* Add Api Twig extension and restore widget/svg helpers

* Cast language flags; improve JSON parsing

* Add markdown DI service and use in Admin API

* Refactor Twig extensions and markdown, simplify APIs

* Add sandboxed email Twig env, update render APIs

* Fix pagination URL usage and menu class

* Fix HTML markup in email templates

* Use macro for avatars and status badges

* Fix Twig spacing and pagination URL filter order

* Replace Custom Twig components

Replace custom Twig components with native Twig components, remove unused custom Twig components, and move functionality to macros where appropriate.

m

Update partial_extensions.html.twig

* Update Twig components and migrate to FOSSBilling namespace

* Refactor Twig environment config and loading

* Add Api Twig extension and restore widget/svg helpers

* Cast language flags; improve JSON parsing

* Add markdown DI service and use in Admin API

* Refactor Twig extensions and markdown, simplify APIs

* Fix pagination URL usage and menu class

* Fix Twig spacing and pagination URL filter order

* Use `current_theme` global and refactor `period_title`

* Allow markdown and date in email Twig sandbox

* Update email template examples

* Fix Twig URLs, quoting, and logo alt text

* Fix Twig templates: URLs, i18n, search params

* Use `templates/*` directories for module templates

* Tighten Twig sandbox and add tests

* Add Twig environment and template docs to `AGENTS.md`

* Use Symfony Filesystem/Finder and assorted fixes

* Fix ticket reply link fragment concatenation

* Close missing divs in dashboard template

* Use detailed `toApiArray` for client

* Fix module name resolution for email templates

* Regenerate `composer.lock`

* Use `markdown_to_html` filter in admin macro

* Handle null price/quantity and rename test method

* Use `Tools::isHTTPS()` for HTTPS checks

* Add 'area' param to `url` filter and update templates

* Use client area URLs in admin templates

* Use client area and guest API URLs in templates

* Remove client-side invoice delete feature

* Create Twig env via `TwigFactory` for PDF rendering

* Add embedded orderbutton JS and update render

* Add 'srclink' to cspell allowlist

* Use `renderEmailTplString` for email rendering

* Bump twig/markdown-extra to v3.24

* Use `markdown_to_html` for privacy policy

* Escape API JSON, handle null IP, client URLs

* Remove initial admin creation flow

* Handle HTTP failures and make tests robust

* Use `api_url` helper in admin templates

* Use `url` filter for footer link 5

* Add patch53 to remove legacy html templates

* Bump setting last_patch to 53

* Add patch54 to restore legacy email templates

* Use renderEmailTplString for email templating

* Remove redundant else/endif in order template

* Add file-hash cache buster for asset URLs

* Remove raw Twig filter and handle missing modules dir

* Return '-' for future timestamps

* Register and reuse TwigFactory and add CSRF cookie

* Adjust API URL helper and add Twig error handling

* Update composer.lock and package-lock.json

* Add twig_factory to ServiceTest DI

* Use url filter for link_page in template

* Composer - update deps and add markdown-extra

* Remove docblocks and inline comments in `TwigFactory`

* Add patches 53-55 and update `last_patch`

* Add `has_permission` Twig function; exclude `href`

* Remove duplicate network interface form

* Guard removal of legacy dirs; add network input

* Handle null category descriptions in template

* Add Twig adapter env and sandbox policy

* Add 'striptags' to cspell word list

* Update composer.lock (deps updated/removed)

* Sanitize custom payment adapter templates

* Remove redundant legacy API tests

* Bump `last_patch` setting to 56

* Coalesce null before `markdown_to_html`

* Move update patcher to system CLI command

* Remove stray `endif` from recaptcha macro

* Update composer.lock

* Fix extra closing brace in Api ServiceTest

* Fix test errors from main merge

- Remove testGetRequestCount tests (method moved to RateLimiter)
- Remove testGetDi from Staff and System GuestTest (protected method)
- Fix IP error message in testLoginCheckIpException
- Add rate_limiter mock to Staff GuestTest tests that need it

* Fix remaining Staff GuestTest issues

- Add extension service mock for captcha check
- Fix error message from 'User not found' to 'Admin not found'

* Fix Staff GuestTest: update query and add logger mock

* Fix Staff GuestTest: set admin status to active for password reset flow

* Fix stale API calls and inverted required condition

- Notification: replace removed system_string_render with direct output
- Antispam settings: replace |alink/|link with |url/|api_url filters
- build_form macro: fix inverted required condition for non-textarea inputs

* Sanitize JS/data URIs and disable buttons by permission

* Use client login/profile links in support emails

* Encode fb-api JSON and tighten URL sanitization

* Prefer session auth for API key methods

* Remove stray endif and fix template formatting

* Add CSRF token to CSV exports; fix template

* Remove stray endif in currency template

* Make invoice PDF generation return Response

* Fix Twig filters, templates, and form handling

* Remove readonly from anonymous class in test

* Inline DebugBar CSS/JS in renderHead

* Add Bootstrap Tab to global bootstrap

* Capitalize template UI strings

* Use CKEditor API for reply quotes

* Remove WYSIWYG module and move CKEditor to themes

* Bump `last_patch` in install SQL to 59

* Remove Spamchecker module

* Run domain management script only for active orders

* Standardize UI text capitalization & minor fixes

* Remove Spamchecker legacy tests; fix period test

* Rename Spamchecker tests to Antispam

* Restore antispam config in tests; remove fixture

* Refactor Antispam Guest tests for config checks

* Update theme build docs; remove Wysiwyg module

* Remove spamchecker from cspell and tweak comment

* Add spamchecker to cspell word list

* Capitalize 'Update Graphs' button label
2026-05-06 21:02:16 +01:00

823 lines
27 KiB
PHP

<?php
declare(strict_types=1);
namespace Box\Mod\Extension;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use Symfony\Component\Filesystem\Filesystem;
class PdoMock extends \PDO
{
public function __construct()
{
}
}
class PdoStatmentsMock extends \PDOStatement
{
public function __construct()
{
}
}
#[Group('Core')]
final class ServiceTest extends \BBTestCase
{
protected ?Service $service;
protected $filesystemMock;
public function setUp(): void
{
$this->filesystemMock = $this->createMock(Filesystem::class);
$this->service = new Service($this->filesystemMock);
}
public function testIsCoreModule(): void
{
$coreModules = ['extension', 'cron', 'staff'];
$modMock = $this->getMockBuilder(\FOSSBilling\Module::class)->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getCoreModules')
->willReturn($coreModules);
$di = $this->getDi();
$di['mod'] = $di->protect(fn ($name): \PHPUnit\Framework\MockObject\MockObject => $modMock);
$this->service->setDi($di);
$result = $this->service->isCoreModule('extension');
$this->assertIsBool($result);
$this->assertTrue($result);
}
public function testIsExtensionActiveModNotFound(): void
{
$coreModules = ['extension', 'cron', 'staff'];
$modMock = $this->getMockBuilder(\FOSSBilling\Module::class)->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getCoreModules')
->willReturn($coreModules);
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('getCell')
->willReturn(null);
$di = $this->getDi();
$di['db'] = $dbMock;
$di['mod'] = $di->protect(fn ($name): \PHPUnit\Framework\MockObject\MockObject => $modMock);
$this->service->setDi($di);
$result = $this->service->isExtensionActive('mod', 'ModDoesNotExists');
$this->assertIsBool($result);
$this->assertFalse($result);
}
public function testRemoveNotExistingModules(): void
{
$model = new \Model_Extension();
$model->loadBean(new \DummyBean());
$model->name = 'extensionName';
$modMock = $this->getMockBuilder(\FOSSBilling\Module::class)->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getManifest')->willThrowException(new \Exception());
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('find')
->willReturn([$model]);
$di = $this->getDi();
$di['db'] = $dbMock;
$di['mod'] = $di->protect(fn ($name): \PHPUnit\Framework\MockObject\MockObject => $modMock);
$this->service->setDi($di);
$result = $this->service->removeNotExistingModules();
$this->assertIsInt($result);
$this->assertTrue($result > 0);
}
public static function searchQueryData(): array
{
return [
[[], 'SELECT * FROM extension', []],
[['type' => 'mod'], 'AND type = :type', [':type' => 'mod']],
[['search' => 'FindUp'], 'AND name LIKE :search', [':search' => 'FindUp']],
];
}
#[DataProvider('searchQueryData')]
public function testGetSearchQuery(array $data, string $expectedStr, array $expectedParams): void
{
$di = $this->getDi();
$this->service->setDi($di);
[$sql, $params] = $this->service->getSearchQuery($data);
$this->assertIsString($sql);
$this->assertIsArray($params);
$this->assertTrue(str_contains($sql, $expectedStr), $sql);
$this->assertEquals([], array_diff_key($params, $expectedParams));
}
public function testGetExtensionsList(): void
{
$data = [
'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->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('getAll')
->willReturn([$model]);
$dbMock->expects($this->atLeastOnce())
->method('find')
->willReturn([$modelFind]);
$coreModules = ['extension', 'cron', 'staff'];
$modMock = $this->getMockBuilder(\FOSSBilling\Module::class)->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getCoreModules')
->willReturn($coreModules);
$modMock->expects($this->atLeastOnce())
->method('getManifest')
->willReturn([]);
$modMock->expects($this->atLeastOnce())
->method('hasSettingsPage')
->willReturn(true);
$di = $this->getDi();
$di['db'] = $dbMock;
$di['mod'] = $di->protect(fn ($name): \PHPUnit\Framework\MockObject\MockObject => $modMock);
$this->service->setDi($di);
$result = $this->service->getExtensionsList($data);
$this->assertIsArray($result);
}
public function testGetExtensionsListOnlyInstalled(): void
{
$data = [
'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->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('getAll')
->willReturn([$model]);
$dbMock->expects($this->atLeastOnce())
->method('find')
->willReturn([$modelFind]);
$modMock = $this->getMockBuilder(\FOSSBilling\Module::class)->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getManifest')
->willReturn([]);
$modMock->expects($this->atLeastOnce())
->method('hasSettingsPage')
->willReturn(true);
$di = $this->getDi();
$di['db'] = $dbMock;
$di['mod'] = $di->protect(fn ($name): \PHPUnit\Framework\MockObject\MockObject => $modMock);
$this->service->setDi($di);
$result = $this->service->getExtensionsList($data);
$this->assertIsArray($result);
}
public function testGetAdminNavigation(): void
{
$extensionServiceMock = $this->getMockBuilder(Service::class)->onlyMethods(['getConfig'])->getMock();
$extensionServiceMock->expects($this->atLeastOnce())
->method('getConfig')
->willReturn([]);
$staffServiceMock = $this->createMock(\Box\Mod\Staff\Service::class);
$staffServiceMock->expects($this->atLeastOnce())
->method('hasPermission')
->willReturn(true);
$dbalMock = new class {
public function createQueryBuilder(): object
{
return new class {
public function select($field)
{
return $this;
}
public function from($table)
{
return $this;
}
public function where($cond)
{
return $this;
}
public function andWhere($cond)
{
return $this;
}
public function setParameter($key, $val)
{
return $this;
}
public function executeQuery()
{
return $this;
}
public function fetchFirstColumn(): array
{
return [];
}
};
}
};
$link = 'extension';
$urlMock = $this->createMock('Box_Url');
$urlMock->expects($this->atLeastOnce())
->method('adminLink')
->willReturn('http://fossbilling.org/index.php?_url=/' . $link);
$di = $this->getDi();
$di['mod'] = $di->protect(function ($name) use ($di) {
$mod = new \FOSSBilling\Module($name);
$mod->setDi($di);
return $mod;
});
$di['tools'] = new \FOSSBilling\Tools();
$di['mod_service'] = $di->protect(function ($mod) use ($extensionServiceMock, $staffServiceMock) {
if ($mod == 'staff') {
return $staffServiceMock;
}
return $extensionServiceMock;
});
$di['url'] = $urlMock;
$di['dbal'] = $dbalMock;
$this->service->setDi($di);
$result = $this->service->getAdminNavigation(new \Model_Admin());
$this->assertIsArray($result);
}
public function testFindExtension(): void
{
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->willReturn(new \Model_Extension());
$di = $this->getDi();
$di['db'] = $dbMock;
$this->service->setDi($di);
$result = $this->service->findExtension('mod', 'id');
$this->assertInstanceOf('\Model_Extension', $result);
}
public function testUpdate(): void
{
$model = new \Model_Extension();
$model->loadBean(new \DummyBean());
$model->type = 'mod';
$model->name = 'testExtension';
$model->version = '2';
$extensionMock = $this->createMock(\FOSSBilling\ExtensionManager::class);
$staffService = $this->createMock(\Box\Mod\Staff\Service::class);
$staffService->expects($this->atLeastOnce())->method('checkPermissionsAndThrowException');
$di = $this->getDi();
$di['extension_manager'] = $extensionMock;
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $staffService);
$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(): void
{
$ext = new \Model_Extension();
$ext->loadBean(new \DummyBean());
$ext->type = 'mod';
$ext->name = 'testExtension';
$expectedResult = [
'id' => $ext->name,
'type' => $ext->type,
'redirect' => true,
'has_settings' => true,
];
$staffService = $this->createMock(\Box\Mod\Staff\Service::class);
$staffService->expects($this->atLeastOnce())->method('checkPermissionsAndThrowException');
$modMock = $this->getMockBuilder(\FOSSBilling\Module::class)->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getManifest')
->willReturn(['version' => 1]);
$modMock->expects($this->atLeastOnce())
->method('hasAdminController')
->willReturn(true);
$modMock->expects($this->atLeastOnce())
->method('hasSettingsPage')
->willReturn(true);
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('store')
->willReturn(1);
$di = $this->getDi();
$di['db'] = $dbMock;
$di['mod'] = $di->protect(fn ($name): \PHPUnit\Framework\MockObject\MockObject => $modMock);
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $staffService);
$this->service->setDi($di);
$result = $this->service->activate($ext);
$this->assertIsArray($result);
$this->assertEquals($expectedResult, $result);
}
public function testDeactivate(): void
{
$ext = new \Model_Extension();
$ext->loadBean(new \DummyBean());
$ext->type = 'mod';
$ext->name = 'extensionTest';
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('trash');
$modMock = $this->getMockBuilder(\FOSSBilling\Module::class)->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getCoreModules')
->willReturn([]);
$staffService = $this->createMock(\Box\Mod\Staff\Service::class);
$staffService->expects($this->atLeastOnce())->method('checkPermissionsAndThrowException');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['mod'] = $di->protect(fn ($name): \PHPUnit\Framework\MockObject\MockObject => $modMock);
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $staffService);
$this->service->setDi($di);
$result = $this->service->deactivate($ext);
$this->assertTrue($result);
}
public function testDeactivateCoreModuleException(): void
{
$ext = new \Model_Extension();
$ext->loadBean(new \DummyBean());
$ext->type = 'mod';
$ext->name = 'extensionTest';
$modMock = $this->getMockBuilder(\FOSSBilling\Module::class)->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getCoreModules')
->willReturn([$ext->name]);
$staffService = $this->createMock(\Box\Mod\Staff\Service::class);
$staffService->expects($this->atLeastOnce())->method('checkPermissionsAndThrowException');
$dbMock = $this->createMock('\Box_Database');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['mod'] = $di->protect(fn ($name): \PHPUnit\Framework\MockObject\MockObject => $modMock);
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $staffService);
$this->service->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Core modules are an integral part of the FOSSBilling system and cannot be deactivated.');
$this->service->deactivate($ext);
}
public function testDeactivateHookExtension(): void
{
$ext = new \Model_Extension();
$ext->loadBean(new \DummyBean());
$ext->type = 'hook';
$ext->name = 'extensionTest';
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('trash');
$staffService = $this->createMock(\Box\Mod\Staff\Service::class);
$staffService->expects($this->atLeastOnce())->method('checkPermissionsAndThrowException');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $staffService);
$this->service->setDi($di);
$result = $this->service->deactivate($ext);
$this->assertTrue($result);
}
public function testDeactivateModule(): void
{
$ext = new \Model_Extension();
$ext->loadBean(new \DummyBean());
$ext->type = 'mod';
$ext->name = 'extensionTest';
$modMock = $this->getMockBuilder(\FOSSBilling\Module::class)->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getCoreModules')
->willReturn([]);
$staffService = $this->createMock(\Box\Mod\Staff\Service::class);
$staffService->expects($this->atLeastOnce())->method('checkPermissionsAndThrowException');
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('trash');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['mod'] = $di->protect(fn ($name): \PHPUnit\Framework\MockObject\MockObject => $modMock);
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $staffService);
$this->service->setDi($di);
$result = $this->service->deactivate($ext);
$this->assertTrue($result);
}
public function testUninstall(): void
{
$dbMock = $this->createMock('\Box_Database');
$modMock = $this->getMockBuilder(\FOSSBilling\Module::class)->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getCoreModules')
->willReturn([]);
$modMock->expects($this->atLeastOnce())
->method('uninstall')
->willReturn(true);
$staffService = $this->createMock(\Box\Mod\Staff\Service::class);
$staffService->expects($this->atLeastOnce())->method('checkPermissionsAndThrowException');
$di = $this->getDi();
// Only mock getExtensionPath to return our temp dir, let other methods work normally
$serviceMock = $this->getMockBuilder(Service::class)
->onlyMethods(['getExtensionPath'])
->setConstructorArgs([$this->filesystemMock])
->getMock();
// Create temp directory that actually exists for the first test
$tmpDir = sys_get_temp_dir() . '/fb_test_ext_' . uniqid();
mkdir($tmpDir, 0o755, true);
// Configure getExtensionPath to return the temp directory
$serviceMock->expects($this->atLeastOnce())
->method('getExtensionPath')
->willReturn($tmpDir);
$di['db'] = $dbMock;
$di['logger'] = new \Box_Log();
$di['mod'] = $di->protect(fn ($name): \PHPUnit\Framework\MockObject\MockObject => $modMock);
$di['mod_service'] = $di->protect(function ($name) use ($staffService) {
if ($name === 'Staff') {
return $staffService;
}
return null;
});
$serviceMock->setDi($di);
// Set up filesystem mock to return true for exists() before calling uninstall
$this->filesystemMock->expects($this->atLeastOnce())
->method('exists')
->willReturn(true);
$result = $serviceMock->uninstall('mod', 'TestExtension');
$this->assertTrue($result);
// Clean up temp directory
if (is_dir($tmpDir)) {
rmdir($tmpDir);
}
$result = $serviceMock->uninstall('mod', 'Branding');
$this->assertTrue($result);
}
public function testDownloadAndExtractDownloadUrlMissing(): void
{
$extensionMock = $this->createMock(\FOSSBilling\ExtensionManager::class);
$extensionMock->expects($this->atLeastOnce())
->method('getLatestExtensionRelease')
->willReturn([]);
$staffService = $this->createMock(\Box\Mod\Staff\Service::class);
$staffService->expects($this->atLeastOnce())->method('checkPermissionsAndThrowException');
$di = $this->getDi();
$di['extension_manager'] = $extensionMock;
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $staffService);
$this->service->setDi($di);
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Couldn\'t find a valid download URL for the extension.');
$this->service->downloadAndExtract('mod', 'extensionId');
}
public function testGetInstalledMods(): void
{
$dbalMock = new class {
public function createQueryBuilder(): object
{
return new class {
public function select($field)
{
return $this;
}
public function from($table)
{
return $this;
}
public function where($cond)
{
return $this;
}
public function andWhere($cond)
{
return $this;
}
public function setParameter($key, $val)
{
return $this;
}
public function executeQuery()
{
return $this;
}
public function fetchFirstColumn(): array
{
return [];
}
};
}
};
$di = new \Pimple\Container();
$di['dbal'] = $dbalMock;
$this->service->setDi($di);
$result = $this->service->getInstalledMods();
$this->assertSame([], $result);
}
public function testActivateExistingExtension(): void
{
$data = [
'id' => 'extensionId',
'type' => 'extensionType',
];
$model = new \Model_Extension();
$model->loadBean(new \DummyBean());
$serviceMock = $this->getMockBuilder(Service::class)
->onlyMethods(['findExtension', 'activate'])
->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('findExtension')
->willReturnOnConsecutiveCalls(null, $model);
$serviceMock->expects($this->atLeastOnce())
->method('activate')
->willReturn([]);
$dbMock = $this->createMock(\Box_Database::class);
$dbMock->expects($this->atLeastOnce())
->method('dispense')
->willReturn($model);
$dbMock->expects($this->atLeastOnce())
->method('store')
->willReturn(1);
$eventMock = $this->createMock(\Box_EventManager::class);
$eventMock->expects($this->atLeastOnce())->method('fire');
$di = $this->getDi();
$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(): void
{
$data = [
'id' => 'extensionId',
'type' => 'extensionType',
];
$model = new \Model_Extension();
$model->loadBean(new \DummyBean());
$serviceMock = $this->getMockBuilder(Service::class)
->onlyMethods(['findExtension', 'activate'])
->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('findExtension')
->willReturn($model);
$serviceMock->expects($this->atLeastOnce())
->method('activate')
->will($this->throwException(new \Exception()));
$eventMock = $this->createMock(\Box_EventManager::class);
$eventMock->expects($this->atLeastOnce())->method('fire');
$di = $this->getDi();
$di['events_manager'] = $eventMock;
$serviceMock->setDi($di);
$this->expectException(\Exception::class);
$serviceMock->activateExistingExtension($data);
}
public function testGetConfig(): void
{
$data = [
'ext' => 'extensionName',
];
$model = new \Model_ExtensionMeta();
$model->loadBean(new \DummyBean());
$dbMock = $this->createMock(\Box_Database::class);
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->willReturn($model);
$cryptMock = $this->createMock(\Box_Crypt::class);
$cryptMock->expects($this->atLeastOnce())
->method('decrypt');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['crypt'] = $cryptMock;
$di['cache'] = new \Symfony\Component\Cache\Adapter\ArrayAdapter();
$this->service->setDi($di);
$result = $this->service->getConfig($data['ext']);
$this->assertIsArray($result);
}
public function testGetConfigExtensionMetaNotFound(): void
{
$data = [
'ext' => 'extensionName',
];
$model = new \Model_ExtensionMeta();
$model->loadBean(new \DummyBean());
$dbMock = $this->createMock(\Box_Database::class);
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->willReturn(null);
$dbMock->expects($this->atLeastOnce())
->method('dispense')
->willReturn($model);
$dbMock->expects($this->atLeastOnce())
->method('store')
->willReturn(1);
$di = $this->getDi();
$di['db'] = $dbMock;
$di['cache'] = new \Symfony\Component\Cache\Adapter\ArrayAdapter();
$this->service->setDi($di);
$result = $this->service->getConfig($data['ext']);
$this->assertIsArray($result);
$this->assertEquals(['ext' => 'extensionName'], $result);
}
public function testSetConfig(): void
{
$data = [
'ext' => 'extensionName',
];
$serviceMock = $this->getMockBuilder(Service::class)->onlyMethods(['getConfig', 'getCoreAndActiveModulesAndPermissions'])->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getConfig')
->willReturn([]);
$toolsMock = $this->createMock(\FOSSBilling\Tools::class);
$cryptMock = $this->createMock(\Box_Crypt::class);
$cryptMock->expects($this->atLeastOnce())
->method('encrypt')
->willReturn('encryptedConfig');
$model = new \Model_ExtensionMeta();
$model->loadBean(new \DummyBean());
$dbMock = $this->createMock(\Box_Database::class);
$dbMock->expects($this->atLeastOnce())
->method('exec')
->willReturn([]);
$eventMock = $this->createMock(\Box_EventManager::class);
$eventMock->expects($this->atLeastOnce())->method('fire');
$staffMock = $this->createMock(\Box\Mod\Staff\Service::class);
$modMock = $this->getMockBuilder(\FOSSBilling\Module::class)->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())->method('getCoreModules')->willReturn([]);
$di = $this->getDi();
$di['db'] = $dbMock;
$di['tools'] = $toolsMock;
$di['crypt'] = $cryptMock;
$di['events_manager'] = $eventMock;
$di['logger'] = new \Box_Log();
$di['mod'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $modMock);
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $staffMock);
$di['cache'] = new \Symfony\Component\Cache\Adapter\ArrayAdapter();
$serviceMock->setDi($di);
$result = $serviceMock->setConfig($data);
$this->assertTrue($result);
}
}