mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-02-13 11:55:30 +00:00
Add AccompanyingPeriodWorkSubjectConverter and integrate audit logging for accompanying period work
- Implemented `AccompanyingPeriodWorkSubjectConverter` to handle subject conversion for accompanying period work entities. - Added unit tests to ensure proper functionality of the new converter. - Integrated `AuditEvent` dispatching into `AccompanyingCourseWorkController` for viewing and listing accompanying period works.
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\PersonBundle\Audit\SubjectConverter;
|
||||
|
||||
use Chill\MainBundle\Audit\Subject;
|
||||
use Chill\MainBundle\Audit\SubjectConverterInterface;
|
||||
use Chill\MainBundle\Audit\SubjectConverterManagerAwareInterface;
|
||||
use Chill\MainBundle\Audit\SubjectConverterManagerAwareTrait;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork;
|
||||
|
||||
/**
|
||||
* @implements SubjectConverterInterface<AccompanyingPeriodWork>
|
||||
*/
|
||||
class AccompanyingPeriodWorkSubjectConverter implements SubjectConverterInterface, SubjectConverterManagerAwareInterface
|
||||
{
|
||||
use SubjectConverterManagerAwareTrait;
|
||||
|
||||
public function convert(mixed $subject): Subject|array
|
||||
{
|
||||
$data = [new Subject('accompanying_period_work', ['id' => $subject->getId()])];
|
||||
|
||||
foreach ($subject->getPersons() as $person) {
|
||||
$personSubject = $this->subjectConverterManager->getSubjectsForEntity($person);
|
||||
if ($personSubject instanceof Subject) {
|
||||
$data[] = $personSubject;
|
||||
} else {
|
||||
array_push($data, ...$personSubject);
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function supportsConvert(mixed $subject): bool
|
||||
{
|
||||
return $subject instanceof AccompanyingPeriodWork;
|
||||
}
|
||||
|
||||
public static function getDefaultPriority(): int
|
||||
{
|
||||
return 10;
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\PersonBundle\Controller;
|
||||
|
||||
use Chill\MainBundle\Audit\AuditEvent;
|
||||
use Chill\MainBundle\Entity\AuditTrail;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Pagination\PaginatorFactory;
|
||||
use Chill\MainBundle\Templating\Listing\FilterOrderHelper;
|
||||
@@ -21,6 +23,7 @@ use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork;
|
||||
use Chill\PersonBundle\Entity\SocialWork\SocialAction;
|
||||
use Chill\PersonBundle\Repository\AccompanyingPeriod\AccompanyingPeriodWorkRepository;
|
||||
use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodWorkVoter;
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
@@ -29,6 +32,7 @@ use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Component\Translation\TranslatableMessage;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
final class AccompanyingCourseWorkController extends AbstractController
|
||||
@@ -42,6 +46,7 @@ final class AccompanyingCourseWorkController extends AbstractController
|
||||
private readonly TranslatableStringHelperInterface $translatableStringHelper,
|
||||
private readonly FilterOrderHelperFactoryInterface $filterOrderHelperFactory,
|
||||
private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry,
|
||||
private readonly EventDispatcherInterface $eventDispatcher,
|
||||
) {}
|
||||
|
||||
#[Route(path: '{_locale}/person/accompanying-period/{id}/work/new', name: 'chill_person_accompanying_period_work_new', methods: ['GET'])]
|
||||
@@ -154,6 +159,15 @@ final class AccompanyingCourseWorkController extends AbstractController
|
||||
$paginator->getCurrentPageFirstItemNumber()
|
||||
);
|
||||
|
||||
$this->eventDispatcher->dispatch(
|
||||
new AuditEvent(
|
||||
AuditTrail::AUDIT_VIEW,
|
||||
[$period],
|
||||
new TranslatableMessage('accompanying_period.audit.show_list_work'),
|
||||
['action' => 'show_list_work']
|
||||
)
|
||||
);
|
||||
|
||||
return $this->render('@ChillPerson/AccompanyingCourseWork/index.html.twig', [
|
||||
'accompanyingCourse' => $period,
|
||||
'works' => $works,
|
||||
@@ -171,6 +185,13 @@ final class AccompanyingCourseWorkController extends AbstractController
|
||||
|
||||
$this->denyAccessUnlessGranted(AccompanyingPeriodWorkVoter::SEE, $work);
|
||||
|
||||
$this->eventDispatcher->dispatch(
|
||||
new AuditEvent(
|
||||
AuditTrail::AUDIT_VIEW,
|
||||
[$work],
|
||||
)
|
||||
);
|
||||
|
||||
return $this->render('@ChillPerson/AccompanyingCourseWork/show.html.twig', [
|
||||
'accompanyingCourse' => $work->getAccompanyingPeriod(),
|
||||
'work' => $work,
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\PersonBundle\Tests\Audit\SubjectConverter;
|
||||
|
||||
use Chill\MainBundle\Audit\Subject;
|
||||
use Chill\MainBundle\Audit\SubjectConverterManagerInterface;
|
||||
use Chill\PersonBundle\Audit\SubjectConverter\AccompanyingPeriodWorkSubjectConverter;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
class AccompanyingPeriodWorkSubjectConverterTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
private AccompanyingPeriodWorkSubjectConverter $converter;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->converter = new AccompanyingPeriodWorkSubjectConverter();
|
||||
}
|
||||
|
||||
public function testSupportsConvert(): void
|
||||
{
|
||||
$this->assertTrue($this->converter->supportsConvert($this->prophesize(AccompanyingPeriodWork::class)->reveal()));
|
||||
$this->assertFalse($this->converter->supportsConvert(new \stdClass()));
|
||||
}
|
||||
|
||||
public function testConvert(): void
|
||||
{
|
||||
$accompanyingPeriodWork = $this->prophesize(AccompanyingPeriodWork::class);
|
||||
$accompanyingPeriodWork->getId()->willReturn(456);
|
||||
|
||||
$person1 = $this->prophesize(Person::class);
|
||||
$person2 = $this->prophesize(Person::class);
|
||||
|
||||
$accompanyingPeriodWork->getPersons()->willReturn(new ArrayCollection([
|
||||
$person1->reveal(),
|
||||
$person2->reveal(),
|
||||
]));
|
||||
|
||||
$subjectConverterManager = $this->prophesize(SubjectConverterManagerInterface::class);
|
||||
|
||||
$personSubject1 = new Subject('person', ['id' => 1]);
|
||||
$personSubject2 = new Subject('person', ['id' => 2]);
|
||||
|
||||
$subjectConverterManager->getSubjectsForEntity($person1->reveal())->willReturn($personSubject1);
|
||||
$subjectConverterManager->getSubjectsForEntity($person2->reveal())->willReturn([$personSubject2]);
|
||||
|
||||
$this->converter->setSubjectConverterManager($subjectConverterManager->reveal());
|
||||
|
||||
$result = $this->converter->convert($accompanyingPeriodWork->reveal());
|
||||
|
||||
$this->assertCount(3, $result);
|
||||
$this->assertInstanceOf(Subject::class, $result[0]);
|
||||
$this->assertSame('accompanying_period_work', $result[0]->type);
|
||||
$this->assertSame(['id' => 456], $result[0]->identifiers);
|
||||
|
||||
$this->assertSame($personSubject1, $result[1]);
|
||||
$this->assertSame($personSubject2, $result[2]);
|
||||
}
|
||||
|
||||
public function testGetDefaultPriority(): void
|
||||
{
|
||||
$this->assertSame(10, AccompanyingPeriodWorkSubjectConverter::getDefaultPriority());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user