mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 05:44:24 +00:00
Send a notification to all User which are members of UserGroups, when a workflow is sent to them
This commit is contained in:
parent
86ec6f82da
commit
87599425df
@ -13,6 +13,7 @@ namespace Chill\MainBundle\Tests\Workflow\EventSubscriber;
|
|||||||
|
|
||||||
use Chill\MainBundle\Entity\Notification;
|
use Chill\MainBundle\Entity\Notification;
|
||||||
use Chill\MainBundle\Entity\User;
|
use Chill\MainBundle\Entity\User;
|
||||||
|
use Chill\MainBundle\Entity\UserGroup;
|
||||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
||||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep;
|
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep;
|
||||||
use Chill\MainBundle\Workflow\EventSubscriber\NotificationOnTransition;
|
use Chill\MainBundle\Workflow\EventSubscriber\NotificationOnTransition;
|
||||||
@ -20,8 +21,6 @@ use Chill\MainBundle\Workflow\Helper\MetadataExtractor;
|
|||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\Argument;
|
use Prophecy\Argument;
|
||||||
use Prophecy\Call\Call;
|
|
||||||
use Prophecy\Exception\Prediction\FailedPredictionException;
|
|
||||||
use Prophecy\PhpUnit\ProphecyTrait;
|
use Prophecy\PhpUnit\ProphecyTrait;
|
||||||
use Symfony\Component\Security\Core\Security;
|
use Symfony\Component\Security\Core\Security;
|
||||||
use Symfony\Component\Workflow\Event\Event;
|
use Symfony\Component\Workflow\Event\Event;
|
||||||
@ -57,29 +56,23 @@ final class NotificationOnTransitionTest extends TestCase
|
|||||||
$id->setValue($entityWorkflow, 1);
|
$id->setValue($entityWorkflow, 1);
|
||||||
|
|
||||||
$step = new EntityWorkflowStep();
|
$step = new EntityWorkflowStep();
|
||||||
|
$userGroup = (new UserGroup())->addUser($userInGroup = new User())->addUser($dest);
|
||||||
$entityWorkflow->addStep($step);
|
$entityWorkflow->addStep($step);
|
||||||
$step->addDestUser($dest)
|
$step
|
||||||
|
->addDestUser($dest)
|
||||||
|
->addDestUserGroup($userGroup)
|
||||||
->setCurrentStep('to_state');
|
->setCurrentStep('to_state');
|
||||||
|
|
||||||
$em = $this->prophesize(EntityManagerInterface::class);
|
$em = $this->prophesize(EntityManagerInterface::class);
|
||||||
$em->persist(Argument::type(Notification::class))->should(
|
|
||||||
static function ($args) use ($dest) {
|
|
||||||
/** @var Call[] $args */
|
|
||||||
if (1 !== \count($args)) {
|
|
||||||
throw new FailedPredictionException('no notification sent');
|
|
||||||
}
|
|
||||||
|
|
||||||
$notification = $args[0]->getArguments()[0];
|
// we check that both notification has been persisted once,
|
||||||
|
// eliminating doublons
|
||||||
if (!$notification instanceof Notification) {
|
$em->persist(Argument::that(
|
||||||
throw new FailedPredictionException('persist is not a notification');
|
fn ($notificationCandidate) => $notificationCandidate instanceof Notification && $notificationCandidate->getAddressees()->contains($dest)
|
||||||
}
|
))->shouldBeCalledOnce();
|
||||||
|
$em->persist(Argument::that(
|
||||||
if (!$notification->getAddressees()->contains($dest)) {
|
fn ($notificationCandidate) => $notificationCandidate instanceof Notification && $notificationCandidate->getAddressees()->contains($userInGroup)
|
||||||
throw new FailedPredictionException('the dest is not notified');
|
))->shouldBeCalledOnce();
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
$engine = $this->prophesize(\Twig\Environment::class);
|
$engine = $this->prophesize(\Twig\Environment::class);
|
||||||
$engine->render(Argument::type('string'), Argument::type('array'))
|
$engine->render(Argument::type('string'), Argument::type('array'))
|
||||||
|
@ -13,6 +13,7 @@ namespace Chill\MainBundle\Workflow\EventSubscriber;
|
|||||||
|
|
||||||
use Chill\MainBundle\Entity\Notification;
|
use Chill\MainBundle\Entity\Notification;
|
||||||
use Chill\MainBundle\Entity\User;
|
use Chill\MainBundle\Entity\User;
|
||||||
|
use Chill\MainBundle\Entity\UserGroup;
|
||||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
||||||
use Chill\MainBundle\Workflow\Helper\MetadataExtractor;
|
use Chill\MainBundle\Workflow\Helper\MetadataExtractor;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
@ -69,7 +70,18 @@ class NotificationOnTransition implements EventSubscriberInterface
|
|||||||
// the dests for the current step
|
// the dests for the current step
|
||||||
$entityWorkflow->getCurrentStep()->getDestUser()->toArray(),
|
$entityWorkflow->getCurrentStep()->getDestUser()->toArray(),
|
||||||
// the cc users for the current step
|
// the cc users for the current step
|
||||||
$entityWorkflow->getCurrentStep()->getCcUser()->toArray()
|
$entityWorkflow->getCurrentStep()->getCcUser()->toArray(),
|
||||||
|
// the users within groups
|
||||||
|
$entityWorkflow->getCurrentStep()->getDestUserGroups()->reduce(
|
||||||
|
function (array $accumulator, UserGroup $userGroup) {
|
||||||
|
foreach ($userGroup->getUsers() as $user) {
|
||||||
|
$accumulator[] = $user;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $accumulator;
|
||||||
|
},
|
||||||
|
[]
|
||||||
|
),
|
||||||
) as $dest) {
|
) as $dest) {
|
||||||
$dests[spl_object_hash($dest)] = $dest;
|
$dests[spl_object_hash($dest)] = $dest;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user