mirror of
https://github.com/FOSSBilling/FOSSBilling.git
synced 2026-12-04 22:24:42 +09:00
* Apply suggested fix to tests-legacy/modules/Servicedownloadable/ServiceTest.php from Copilot Autofix Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> * Apply suggested fix to tests-legacy/modules/Servicedownloadable/ServiceTest.php from Copilot Autofix Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> * Apply suggested fix to tests-legacy/modules/Servicedownloadable/ServiceTest.php from Copilot Autofix Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> --------- Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
232 lines
7.7 KiB
PHP
232 lines
7.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Box\Mod\Servicedownloadable;
|
|
|
|
use PHPUnit\Framework\Attributes\Group;
|
|
|
|
#[Group('Core')]
|
|
final class ServiceTest extends \BBTestCase
|
|
{
|
|
protected ?Service $service;
|
|
|
|
public function setUp(): void
|
|
{
|
|
$this->service = new Service();
|
|
}
|
|
|
|
public function testGetDi(): void
|
|
{
|
|
$di = $this->getDi();
|
|
$this->service->setDi($di);
|
|
$getDi = $this->service->getDi();
|
|
$this->assertEquals($di, $getDi);
|
|
}
|
|
|
|
public function testAttachOrderConfig(): void
|
|
{
|
|
$productModel = new \Model_Product();
|
|
$productModel->loadBean(new \DummyBean());
|
|
$productModel->config = '{"filename" : "temp/asdcxTest.txt"}';
|
|
|
|
$data = [];
|
|
|
|
$expected = array_merge(json_decode($productModel->config ?? '', true), $data);
|
|
|
|
$validatorMock = $this->getMockBuilder(\FOSSBilling\Validate::class)->disableOriginalConstructor()->getMock();
|
|
$validatorMock->expects($this->any())->method('checkRequiredParamsForArray')
|
|
;
|
|
|
|
$di = $this->getDi();
|
|
$di['validator'] = $validatorMock;
|
|
$this->service->setDi($di);
|
|
$result = $this->service->attachOrderConfig($productModel, $data);
|
|
$this->assertIsArray($result);
|
|
$this->assertEquals($expected, $result);
|
|
}
|
|
|
|
public function testActionCreate(): void
|
|
{
|
|
$clientOrderModel = new \Model_ClientOrder();
|
|
$clientOrderModel->loadBean(new \DummyBean());
|
|
$clientOrderModel->config = '{"filename" : "temp/asdcxTest.txt"}';
|
|
|
|
$model = new \Model_ServiceDownloadable();
|
|
$model->loadBean(new \DummyBean());
|
|
|
|
$dbMock = $this->createMock(\Box_Database::class);
|
|
$dbMock->expects($this->atLeastOnce())
|
|
->method('dispense')
|
|
->willReturn($model);
|
|
|
|
$dbMock->expects($this->atLeastOnce())
|
|
->method('store')
|
|
->willReturn(1);
|
|
$validatorMock = $this->getMockBuilder(\FOSSBilling\Validate::class)->disableOriginalConstructor()->getMock();
|
|
$validatorMock->expects($this->any())->method('checkRequiredParamsForArray')
|
|
;
|
|
|
|
$di = $this->getDi();
|
|
$di['db'] = $dbMock;
|
|
$di['validator'] = $validatorMock;
|
|
|
|
$this->service->setDi($di);
|
|
$result = $this->service->action_create($clientOrderModel);
|
|
$this->assertInstanceOf('\Model_ServiceDownloadable', $result);
|
|
}
|
|
|
|
public function testActionDelete(): void
|
|
{
|
|
$clientOrderModel = new \Model_ClientOrder();
|
|
|
|
$orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class);
|
|
$orderServiceMock->expects($this->atLeastOnce())
|
|
->method('getOrderService')
|
|
->willReturn(new \Model_ServiceDownloadable());
|
|
|
|
$dbMock = $this->createMock(\Box_Database::class);
|
|
$dbMock->expects($this->atLeastOnce())
|
|
->method('trash');
|
|
|
|
$di = $this->getDi();
|
|
$di['db'] = $dbMock;
|
|
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $orderServiceMock);
|
|
|
|
$this->service->setDi($di);
|
|
$this->service->action_delete($clientOrderModel);
|
|
}
|
|
|
|
public function testSaveProductConfig(): void
|
|
{
|
|
$data = [
|
|
'update_orders' => true,
|
|
];
|
|
|
|
$productModel = new \Model_Product();
|
|
$productModel->loadBean(new \DummyBean());
|
|
$productModel->config = '{"filename": "test.txt"}';
|
|
|
|
$dbMock = $this->getMockBuilder(\Box_Database::class)->getMock();
|
|
$dbMock->expects($this->atLeastOnce())
|
|
->method('store')
|
|
->with($productModel)
|
|
->willReturn(1);
|
|
|
|
$di = new \Pimple\Container();
|
|
$di['db'] = $dbMock;
|
|
|
|
$this->service->setDi($di);
|
|
$result = $this->service->saveProductConfig($productModel, $data);
|
|
|
|
$this->assertIsBool($result);
|
|
$this->assertTrue($result);
|
|
|
|
// Verify the config was updated correctly
|
|
$updatedConfig = json_decode($productModel->config, true);
|
|
$this->assertIsArray($updatedConfig);
|
|
$this->assertEquals('test.txt', $updatedConfig['filename']);
|
|
$this->assertTrue($updatedConfig['update_orders']);
|
|
$this->assertNotNull($productModel->updated_at);
|
|
}
|
|
|
|
public function testSaveProductConfigWithExistingConfig(): void
|
|
{
|
|
$data = [
|
|
'update_orders' => false,
|
|
];
|
|
|
|
$productModel = new \Model_Product();
|
|
$productModel->loadBean(new \DummyBean());
|
|
$productModel->config = '{"filename": "existing.txt", "update_orders": true}';
|
|
|
|
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
|
|
$dbMock->expects($this->atLeastOnce())
|
|
->method('store')
|
|
->with($productModel)
|
|
->willReturn(1);
|
|
|
|
$di = new \Pimple\Container();
|
|
$di['db'] = $dbMock;
|
|
|
|
$this->service->setDi($di);
|
|
$result = $this->service->saveProductConfig($productModel, $data);
|
|
|
|
$this->assertIsBool($result);
|
|
$this->assertTrue($result);
|
|
|
|
// Verify the config was updated correctly
|
|
$updatedConfig = json_decode($productModel->config, true);
|
|
$this->assertIsArray($updatedConfig);
|
|
$this->assertEquals('existing.txt', $updatedConfig['filename']);
|
|
$this->assertFalse($updatedConfig['update_orders']);
|
|
$this->assertNotNull($productModel->updated_at);
|
|
}
|
|
|
|
public function testSaveProductConfigWithNoExistingConfig(): void
|
|
{
|
|
$data = [
|
|
'update_orders' => true,
|
|
];
|
|
|
|
$productModel = new \Model_Product();
|
|
$productModel->loadBean(new \DummyBean());
|
|
$productModel->config = null;
|
|
|
|
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
|
|
$dbMock->expects($this->atLeastOnce())
|
|
->method('store')
|
|
->with($productModel)
|
|
->willReturn(1);
|
|
|
|
$di = new \Pimple\Container();
|
|
$di['db'] = $dbMock;
|
|
|
|
$this->service->setDi($di);
|
|
$result = $this->service->saveProductConfig($productModel, $data);
|
|
|
|
$this->assertIsBool($result);
|
|
$this->assertTrue($result);
|
|
|
|
// Verify the config was created correctly
|
|
$updatedConfig = json_decode((string) $productModel->config, true);
|
|
$this->assertIsArray($updatedConfig);
|
|
$this->assertTrue($updatedConfig['update_orders']);
|
|
$this->assertNotNull($productModel->updated_at);
|
|
}
|
|
|
|
public function testValidateFileUploadAllowsKnownExtensionWithOctetStreamMime(): void
|
|
{
|
|
$this->service->setDi($this->getDi());
|
|
|
|
$file = $this->createMock(\Symfony\Component\HttpFoundation\File\UploadedFile::class);
|
|
$file->method('getClientOriginalExtension')->willReturn('exe');
|
|
$file->method('getClientOriginalName')->willReturn('installer.exe');
|
|
$file->method('getMimeType')->willReturn('application/octet-stream');
|
|
|
|
$this->invokeValidateFileUpload($file);
|
|
|
|
$this->addToAssertionCount(1);
|
|
}
|
|
|
|
public function testValidateFileUploadRejectsUnknownExtension(): void
|
|
{
|
|
$this->service->setDi($this->getDi());
|
|
|
|
$file = $this->createMock(\Symfony\Component\HttpFoundation\File\UploadedFile::class);
|
|
$file->method('getClientOriginalExtension')->willReturn('php');
|
|
$file->method('getClientOriginalName')->willReturn('shell.php');
|
|
$file->method('getMimeType')->willReturn('application/x-httpd-php');
|
|
|
|
$this->expectException(\FOSSBilling\Exception::class);
|
|
|
|
$this->invokeValidateFileUpload($file);
|
|
}
|
|
|
|
private function invokeValidateFileUpload(\Symfony\Component\HttpFoundation\File\UploadedFile $file): void
|
|
{
|
|
$reflection = new \ReflectionMethod(Service::class, 'validateFileUpload');
|
|
$reflection->invoke($this->service, $file);
|
|
}
|
|
}
|