mirror of
https://github.com/FOSSBilling/FOSSBilling.git
synced 2026-11-30 12:18:43 +09:00
* Allow admins to edit ticket replies, with edit history (#2317) Admins previously had no way to fix a typo or correct a reply without editing the database directly. Adds an Edit action to admin-authored messages in the admin ticket view; client-authored messages remain uneditable so an admin can never alter a client's own words. Every edit snapshots the message's prior content into a new support_ticket_message_history table before overwriting it, so the original wording sent to a client is never silently lost. Admins can view a message's revision history via a lazy-loaded modal. Also fixes ticket_message_update, which existed but was never wired into any UI and skipped the markdown sanitization that ticket_reply already applies. * Bump fresh-install last_patch to 83 Fresh installs seed last_patch from content.sql so the patcher doesn't try to reapply migrations already baked into structure.sql. Missed bumping this when patch83 (support_ticket_message_history) was added. * Fix message history modal getting stuck on Loading... after a failed fetch dataset.loaded was set before the API call resolved, so a failed request left the modal permanently stuck on "Loading..." with no way to retry short of a page reload. Move the flag to the success callback and add an error handler that shows a retryable message. * Make support_ticket_message_history.support_ticket_message_id required A history row is meaningless without the message it snapshots; allowing NULL let orphaned rows exist. Tightened the column in patch84, structure.sql, and the entity's JoinColumn to NOT NULL. * Delete message edit history when a ticket is removed Service::rm() explicitly removes a ticket's notes and messages rather than relying on the entity mapping's onDelete: CASCADE, because this schema has no real FK constraints anywhere (referential integrity is handled at the application layer throughout this codebase). History rows were missed by that pattern and were left orphaned after a ticket delete. Now removed alongside their message, following the same explicit-cleanup convention as notes/messages. Also cache SupportTicketMessageHistoryRepository via setDi(), matching every other repository on this service, and add direct coverage for getMessageHistory().
323 lines
15 KiB
PHP
323 lines
15 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Copyright 2022-2026 FOSSBilling
|
|
* SPDX-License-Identifier: Apache-2.0.
|
|
*
|
|
* @copyright FOSSBilling (https://www.fossbilling.org)
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Helpers;
|
|
|
|
use Pimple\Container;
|
|
use Psr\Log\AbstractLogger;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
/**
|
|
* Create a minimal DI container for testing.
|
|
*/
|
|
function container(): Container
|
|
{
|
|
$di = new Container();
|
|
$di['config'] = [
|
|
'salt' => 'test_salt_' . uniqid(),
|
|
'url' => 'http://localhost/',
|
|
];
|
|
$di['validator'] = fn (): \FOSSBilling\Validate => new \FOSSBilling\Validate();
|
|
$di['tools'] = fn (): \FOSSBilling\Tools => new \FOSSBilling\Tools();
|
|
$di['filesystem'] = fn (): \Symfony\Component\Filesystem\Filesystem => new \Symfony\Component\Filesystem\Filesystem();
|
|
$di['logger'] = fn (): \Psr\Log\LoggerInterface => new class extends AbstractLogger {
|
|
public array $calls = [];
|
|
|
|
public function log($level, string|\Stringable $message, array $context = []): void
|
|
{
|
|
$this->calls[] = ['method' => $level, 'params' => [$message, $context]];
|
|
}
|
|
|
|
public function setChannel(string $channel): self
|
|
{
|
|
$this->calls[] = ['method' => 'setChannel', 'params' => [$channel]];
|
|
|
|
return $this;
|
|
}
|
|
};
|
|
$di['request'] = fn (): Request => Request::create('http://localhost/');
|
|
$di['filesystem'] = fn (): \Symfony\Component\Filesystem\Filesystem => new \Symfony\Component\Filesystem\Filesystem();
|
|
$di['session'] = static function (): object {
|
|
$session = \Mockery::mock(\FOSSBilling\Session::class)->shouldIgnoreMissing();
|
|
$session->shouldReceive('regenerateId')->byDefault()->andReturnNull();
|
|
$session->shouldReceive('set')->byDefault()->andReturnNull();
|
|
$session->shouldReceive('get')->byDefault()->andReturnNull();
|
|
$session->shouldReceive('delete')->byDefault()->andReturnNull();
|
|
|
|
return $session;
|
|
};
|
|
$di['db'] = static function (): object {
|
|
$db = \Mockery::mock(\Box_Database::class)->shouldIgnoreMissing();
|
|
$db->shouldReceive('find')->byDefault()->andReturn([]);
|
|
$db->shouldReceive('getAll')->byDefault()->andReturn([]);
|
|
$db->shouldReceive('getAssoc')->byDefault()->andReturn([]);
|
|
$db->shouldReceive('toArray')->byDefault()->andReturn([]);
|
|
$db->shouldReceive('exec')->byDefault()->andReturn(1);
|
|
$db->shouldReceive('transaction')->byDefault()->andReturnUsing(static fn (callable $callback): mixed => $callback());
|
|
|
|
return $db;
|
|
};
|
|
$di['dbal'] = static function (): object {
|
|
$dbal = \Mockery::mock(\Doctrine\DBAL\Connection::class)->shouldIgnoreMissing();
|
|
$result = \Mockery::mock(\Doctrine\DBAL\Result::class)->shouldIgnoreMissing();
|
|
$result->shouldReceive('fetchAssociative')->byDefault()->andReturn(false);
|
|
$result->shouldReceive('fetchAllAssociative')->byDefault()->andReturn([]);
|
|
$result->shouldReceive('fetchOne')->byDefault()->andReturn(false);
|
|
|
|
$queryBuilder = \Mockery::mock(\Doctrine\DBAL\Query\QueryBuilder::class)->shouldIgnoreMissing();
|
|
foreach ([
|
|
'select',
|
|
'addSelect',
|
|
'from',
|
|
'leftJoin',
|
|
'innerJoin',
|
|
'where',
|
|
'andWhere',
|
|
'orWhere',
|
|
'orderBy',
|
|
'addOrderBy',
|
|
'setFirstResult',
|
|
'setMaxResults',
|
|
'setParameter',
|
|
'setParameters',
|
|
'insert',
|
|
'update',
|
|
'delete',
|
|
'values',
|
|
'setValue',
|
|
'set',
|
|
] as $method) {
|
|
$queryBuilder->shouldReceive($method)->byDefault()->andReturn($queryBuilder);
|
|
}
|
|
$queryBuilder->shouldReceive('executeQuery')->byDefault()->andReturn($result);
|
|
$queryBuilder->shouldReceive('executeStatement')->byDefault()->andReturn(1);
|
|
$queryBuilder->shouldReceive('fetchAssociative')->byDefault()->andReturn(false);
|
|
$queryBuilder->shouldReceive('fetchAllAssociative')->byDefault()->andReturn([]);
|
|
$queryBuilder->shouldReceive('fetchOne')->byDefault()->andReturn(false);
|
|
|
|
$dbal->shouldReceive('fetchAssociative')->byDefault()->andReturn([]);
|
|
$dbal->shouldReceive('fetchAllAssociative')->byDefault()->andReturn([]);
|
|
$dbal->shouldReceive('fetchOne')->byDefault()->andReturn(null);
|
|
$dbal->shouldReceive('executeQuery')->byDefault()->andReturn($result);
|
|
$dbal->shouldReceive('executeStatement')->byDefault()->andReturn(1);
|
|
$dbal->shouldReceive('insert')->byDefault()->andReturn(1);
|
|
$dbal->shouldReceive('update')->byDefault()->andReturn(1);
|
|
$dbal->shouldReceive('lastInsertId')->byDefault()->andReturn(1);
|
|
$dbal->shouldReceive('createQueryBuilder')->byDefault()->andReturn($queryBuilder);
|
|
|
|
return $dbal;
|
|
};
|
|
$di['events_manager'] = fn (): object => new class {
|
|
public array $events = [];
|
|
|
|
public function fire(array|string $event): void
|
|
{
|
|
$this->events[] = $event;
|
|
}
|
|
};
|
|
$di['auth'] = fn (): object => \Mockery::mock()->shouldIgnoreMissing();
|
|
$di['pager'] = fn (): object => new class {
|
|
public function getDefaultPerPage(): int
|
|
{
|
|
return 20;
|
|
}
|
|
|
|
public function getAdvancedSearch(array $data, array $fields = [], string $table = ''): array
|
|
{
|
|
return ['', []];
|
|
}
|
|
|
|
public function getSimpleResultSet(string $query, array $params = [], int $perPage = 20): array
|
|
{
|
|
return ['list' => [], 'total' => 0, 'pages' => 0, 'page' => 1, 'per_page' => $perPage];
|
|
}
|
|
|
|
public function getPaginatedResultSet(string $query, array $params = [], mixed $pagination = null): array
|
|
{
|
|
return ['list' => [], 'total' => 0, 'pages' => 0, 'page' => 1, 'per_page' => 20];
|
|
}
|
|
};
|
|
$di['rate_limiter'] = fn (): object => new class {
|
|
public function consume(string $policyName, string $subject, int $tokens = 1): \FOSSBilling\Security\RateLimitResult
|
|
{
|
|
return new \FOSSBilling\Security\RateLimitResult($policyName, false, null, null);
|
|
}
|
|
|
|
public function consumeOrThrow(string $policyName, string $subject, int $tokens = 1): \FOSSBilling\Security\RateLimitResult
|
|
{
|
|
return $this->consume($policyName, $subject, $tokens);
|
|
}
|
|
};
|
|
$di['mod_config'] = $di->protect(fn (string $name): array => []);
|
|
$di['em'] = static function (): object {
|
|
$adminGroupRepository = \Mockery::mock(\Box\Mod\Staff\Repository\AdminGroupRepository::class)->shouldIgnoreMissing();
|
|
$adminGroupMemberRepository = \Mockery::mock(\Box\Mod\Staff\Repository\AdminGroupMemberRepository::class)->shouldIgnoreMissing();
|
|
|
|
$extensionMetaRepository = \Mockery::mock(\Box\Mod\Extension\Repository\ExtensionMetaRepository::class)->shouldIgnoreMissing();
|
|
$extensionMetaRepository->shouldReceive('findOneByExtensionAndScope')->byDefault()->andReturn(null);
|
|
$extensionMetaRepository->shouldReceive('findByExtensionAndScope')->byDefault()->andReturn([]);
|
|
$extensionMetaRepository->shouldReceive('deleteByExtensionAndScope')->byDefault()->andReturn(0);
|
|
|
|
$emailTemplateRepository = \Mockery::mock(\Box\Mod\Email\Repository\EmailTemplateRepository::class)->shouldIgnoreMissing();
|
|
$templateQueryBuilder = \Mockery::mock(\Doctrine\ORM\QueryBuilder::class)->shouldIgnoreMissing();
|
|
$emailTemplateRepository->shouldReceive('find')->byDefault()->andReturn(null);
|
|
$emailTemplateRepository->shouldReceive('findOneByActionCode')->byDefault()->andReturn(null);
|
|
$emailTemplateRepository->shouldReceive('getSearchQueryBuilder')->byDefault()->andReturn($templateQueryBuilder);
|
|
|
|
$emailTemplateGroupRepository = \Mockery::mock(\Box\Mod\Email\Repository\EmailTemplateGroupRepository::class)->shouldIgnoreMissing();
|
|
$emailTemplateGroupRepository->shouldReceive('getGroupIdsForTemplate')->byDefault()->andReturn([]);
|
|
$emailTemplateGroupRepository->shouldReceive('countTemplatesUsingGroup')->byDefault()->andReturn(0);
|
|
|
|
$activityClientEmailRepository = \Mockery::mock(\Box\Mod\Email\Repository\ActivityClientEmailRepository::class)->shouldIgnoreMissing();
|
|
$queuedEmailRepository = \Mockery::mock(\Box\Mod\Email\Repository\QueuedEmailRepository::class)->shouldIgnoreMissing();
|
|
|
|
$kbArticleRepository = \Mockery::mock(\Box\Mod\Support\Repository\KbArticleRepository::class)->shouldIgnoreMissing();
|
|
$kbArticleCategoryRepository = \Mockery::mock(\Box\Mod\Support\Repository\KbArticleCategoryRepository::class)->shouldIgnoreMissing();
|
|
$cannedResponseRepository = \Mockery::mock(\Box\Mod\Support\Repository\CannedResponseRepository::class)->shouldIgnoreMissing();
|
|
$cannedResponseCategoryRepository = \Mockery::mock(\Box\Mod\Support\Repository\CannedResponseCategoryRepository::class)->shouldIgnoreMissing();
|
|
$helpdeskRepository = \Mockery::mock(\Box\Mod\Support\Repository\HelpdeskRepository::class)->shouldIgnoreMissing();
|
|
$supportTicketRepository = \Mockery::mock(\Box\Mod\Support\Repository\SupportTicketRepository::class)->shouldIgnoreMissing();
|
|
$supportTicketMessageRepository = \Mockery::mock(\Box\Mod\Support\Repository\SupportTicketMessageRepository::class)->shouldIgnoreMissing();
|
|
$supportTicketNoteRepository = \Mockery::mock(\Box\Mod\Support\Repository\SupportTicketNoteRepository::class)->shouldIgnoreMissing();
|
|
$supportTicketMessageHistoryRepository = \Mockery::mock(\Box\Mod\Support\Repository\SupportTicketMessageHistoryRepository::class)->shouldIgnoreMissing();
|
|
|
|
$em = \Mockery::mock(\Doctrine\ORM\EntityManagerInterface::class)->shouldIgnoreMissing();
|
|
$em->shouldReceive('getRepository')->byDefault()->andReturnUsing(static fn (string $class): object => match ($class) {
|
|
\Box\Mod\Staff\Entity\AdminGroup::class => $adminGroupRepository,
|
|
\Box\Mod\Staff\Entity\AdminGroupMember::class => $adminGroupMemberRepository,
|
|
\Box\Mod\Email\Entity\EmailTemplate::class => $emailTemplateRepository,
|
|
\Box\Mod\Email\Entity\EmailTemplateGroup::class => $emailTemplateGroupRepository,
|
|
\Box\Mod\Email\Entity\ActivityClientEmail::class => $activityClientEmailRepository,
|
|
\Box\Mod\Email\Entity\QueuedEmail::class => $queuedEmailRepository,
|
|
\Box\Mod\Support\Entity\KbArticle::class => $kbArticleRepository,
|
|
\Box\Mod\Support\Entity\KbArticleCategory::class => $kbArticleCategoryRepository,
|
|
\Box\Mod\Support\Entity\CannedResponse::class => $cannedResponseRepository,
|
|
\Box\Mod\Support\Entity\CannedResponseCategory::class => $cannedResponseCategoryRepository,
|
|
\Box\Mod\Support\Entity\Helpdesk::class => $helpdeskRepository,
|
|
\Box\Mod\Support\Entity\SupportTicket::class => $supportTicketRepository,
|
|
\Box\Mod\Support\Entity\SupportTicketMessage::class => $supportTicketMessageRepository,
|
|
\Box\Mod\Support\Entity\SupportTicketNote::class => $supportTicketNoteRepository,
|
|
\Box\Mod\Support\Entity\SupportTicketMessageHistory::class => $supportTicketMessageHistoryRepository,
|
|
\Box\Mod\Extension\Entity\Extension::class => \Mockery::mock(\Box\Mod\Extension\Repository\ExtensionRepository::class)->shouldIgnoreMissing(),
|
|
default => $extensionMetaRepository,
|
|
});
|
|
|
|
return $em;
|
|
};
|
|
|
|
$staffService = \Mockery::mock(\Box\Mod\Staff\Service::class)->shouldIgnoreMissing();
|
|
$staffService->shouldReceive('hasPermission')->byDefault()->andReturn(true);
|
|
$staffService->shouldReceive('checkPermissionsAndThrowException')->byDefault()->andReturn(true);
|
|
|
|
$emailService = \Mockery::mock(\Box\Mod\Email\Service::class)->shouldIgnoreMissing();
|
|
$emailTemplateGroupRepositoryDefault = \Mockery::mock(\Box\Mod\Email\Repository\EmailTemplateGroupRepository::class)->shouldIgnoreMissing();
|
|
$emailTemplateGroupRepositoryDefault->shouldReceive('countTemplatesUsingGroup')->byDefault()->andReturn(0);
|
|
$emailService->shouldReceive('getTemplateGroupRepository')->byDefault()->andReturn($emailTemplateGroupRepositoryDefault);
|
|
|
|
$di['mod_service'] = $di->protect(static function (string $name = '', string $sub = '') use ($staffService, $emailService): object {
|
|
if (strtolower($name) === 'staff') {
|
|
return $staffService;
|
|
}
|
|
|
|
if (strtolower($name) === 'email') {
|
|
return $emailService;
|
|
}
|
|
|
|
return \Mockery::mock()->shouldIgnoreMissing();
|
|
});
|
|
|
|
return $di;
|
|
}
|
|
|
|
/**
|
|
* Create a module service resolver with staff permissions enabled by default.
|
|
*
|
|
* @param array<string, object> $services
|
|
*/
|
|
function moduleService(array $services = []): \Closure
|
|
{
|
|
$staffService = $services['staff'] ?? \Mockery::mock(\Box\Mod\Staff\Service::class)->shouldIgnoreMissing();
|
|
$staffService->shouldReceive('hasPermission')->byDefault()->andReturn(true);
|
|
$staffService->shouldReceive('checkPermissionsAndThrowException')->byDefault()->andReturn(true);
|
|
|
|
if (!isset($services['email'])) {
|
|
$emailTemplateGroupRepositoryDefault = \Mockery::mock(\Box\Mod\Email\Repository\EmailTemplateGroupRepository::class)->shouldIgnoreMissing();
|
|
$emailTemplateGroupRepositoryDefault->shouldReceive('countTemplatesUsingGroup')->byDefault()->andReturn(0);
|
|
$services['email'] = \Mockery::mock(\Box\Mod\Email\Service::class)->shouldIgnoreMissing();
|
|
$services['email']->shouldReceive('getTemplateGroupRepository')->byDefault()->andReturn($emailTemplateGroupRepositoryDefault);
|
|
}
|
|
|
|
return static function (string $name = '', string $sub = '') use ($services, $staffService): object {
|
|
if (strtolower($name) === 'staff') {
|
|
return $staffService;
|
|
}
|
|
|
|
$module = strtolower($name);
|
|
$moduleWithSub = $sub === '' ? $module : $module . ':' . strtolower($sub);
|
|
|
|
return $services[$moduleWithSub] ?? $services[$module] ?? \Mockery::mock()->shouldIgnoreMissing();
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Access a private property on an object using reflection.
|
|
*
|
|
* @return mixed The property value when getting, null when setting
|
|
*/
|
|
function accessPrivate(object $instance, string $property, mixed $value = null): mixed
|
|
{
|
|
$refl = new \ReflectionClass($instance);
|
|
$prop = null;
|
|
|
|
if ($refl->hasProperty($property)) {
|
|
$prop = $refl->getProperty($property);
|
|
} else {
|
|
$parentClass = $refl->getParentClass();
|
|
if ($parentClass && $parentClass->hasProperty($property)) {
|
|
$prop = $parentClass->getProperty($property);
|
|
}
|
|
}
|
|
|
|
if ($prop === null || $prop->isStatic()) {
|
|
return null;
|
|
}
|
|
|
|
// If value is provided, set the property
|
|
if (func_num_args() > 2) {
|
|
$prop->setValue($instance, $value);
|
|
|
|
return null;
|
|
}
|
|
|
|
// Otherwise, get the property value
|
|
return $prop->getValue($instance);
|
|
}
|
|
|
|
/**
|
|
* Inject a mock filesystem into a service that has a private filesystem property.
|
|
*/
|
|
function injectMockFilesystem(object $service, \Mockery\MockInterface $filesystemMock): void
|
|
{
|
|
$refl = new \ReflectionClass($service);
|
|
while (!$refl->hasProperty('filesystem')) {
|
|
$parent = $refl->getParentClass();
|
|
if ($parent === false) {
|
|
throw new \ReflectionException(sprintf('Property %s::$filesystem does not exist', $service::class));
|
|
}
|
|
|
|
$refl = $parent;
|
|
}
|
|
|
|
$prop = $refl->getProperty('filesystem');
|
|
$prop->setValue($service, $filesystemMock);
|
|
}
|