Skip to content

Commit

Permalink
fix: [DPMMA-2550] point groups api improvements and extra tests
Browse files Browse the repository at this point in the history
  • Loading branch information
patrykgruszka committed Apr 26, 2024
1 parent deb1c41 commit c201aa1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/bundles/PointBundle/Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
'standard_entity' => true,
'name' => 'pointGroups',
'path' => '/points/groups',
'controller' => \Mautic\PointBundle\Controller\Api\PointGroupsApiController::class,
'controller' => Mautic\PointBundle\Controller\Api\PointGroupsApiController::class,
],
'mautic_api_getcontactpointgroups' => [
'path' => '/contacts/{contactId}/points/groups',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Mautic\PointBundle\Controller\Api;

use Doctrine\Persistence\ManagerRegistry;
Expand Down Expand Up @@ -33,7 +35,7 @@ class PointGroupsApiController extends CommonApiController
*/
protected $model;

/** @phpstan-ignore-next-line */
/** @phpstan-ignore-next-line the parent class uses the deprecated MauticFactory */
public function __construct(CorePermissions $security, Translator $translator, EntityResultHelper $entityResultHelper, RouterInterface $router, FormFactoryInterface $formFactory, AppVersion $appVersion, RequestStack $requestStack, ManagerRegistry $doctrine, ModelFactory $modelFactory, EventDispatcherInterface $dispatcher, CoreParametersHelper $coreParametersHelper, MauticFactory $factory, PointGroupModel $pointGroupModel, private LeadModel $leadModel)
{
$this->model = $pointGroupModel;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

declare(strict_types=1);

namespace Mautic\PointBundle\Tests\Controller\Api;

use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\CoreBundle\Translation\Translator;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Entity\PointsChangeLog;
use Mautic\PointBundle\Entity\Group;
Expand All @@ -12,6 +15,9 @@ final class PointGroupsApiControllerTest extends MauticMysqlTestCase
{
public function testPointGroupCRUDActions(): void
{
/** @var Translator $translator */
$translator = static::getContainer()->get('translator');

// Create a new point group
$this->client->request('POST', '/api/points/groups/new', [
'name' => 'New Point Group',
Expand Down Expand Up @@ -61,16 +67,29 @@ public function testPointGroupCRUDActions(): void
$this->client->request('DELETE', "/api/points/groups/{$createdData['id']}/delete");
$deleteResponse = $this->client->getResponse();

$this->assertSame(200, $deleteResponse->getStatusCode());
$this->assertSame(Response::HTTP_OK, $deleteResponse->getStatusCode());
$responseData = json_decode($deleteResponse->getContent(), true);
$this->assertArrayHasKey('pointGroup', $responseData);
$deleteData = $responseData['pointGroup'];
$this->assertEquals('Updated Point Group Name', $deleteData['name']);
$this->assertEquals('Updated description of the point group', $deleteData['description']);

// Try to GET the group that should no longer exist
$this->client->request('GET', "/api/points/groups/{$createdData['id']}");
$getResponse = $this->client->getResponse();
$this->assertSame(Response::HTTP_NOT_FOUND, $getResponse->getStatusCode());
$responseData = json_decode($getResponse->getContent(), true);
$this->assertArrayHasKey('errors', $responseData);
$this->assertCount(1, $responseData['errors']);
$this->assertSame(Response::HTTP_NOT_FOUND, $responseData['errors'][0]['code']);
$this->assertSame($translator->trans('mautic.core.error.notfound', [], 'flashes'), $responseData['errors'][0]['message']);
}

public function testContactGroupPointsActions(): void
{
/** @var Translator $translator */
$translator = static::getContainer()->get('translator');

// Arrange
$contact = $this->createContact('test@example.com');
$pointGroupA = $this->createGroup('Group A');
Expand Down Expand Up @@ -115,6 +134,26 @@ public function testContactGroupPointsActions(): void
['delta' => 12, 'groupId' => $pointGroupA->getId()],
['delta' => 21, 'groupId' => $pointGroupB->getId()],
]);

// Try to GET the group points that should not exist
$this->client->request('GET', "/api/contacts/{$contact->getId()}/points/groups/0");
$response = $this->client->getResponse();
$this->assertSame(Response::HTTP_NOT_FOUND, $response->getStatusCode());
$responseData = json_decode($response->getContent(), true);
$this->assertArrayHasKey('errors', $responseData);
$this->assertCount(1, $responseData['errors']);
$this->assertSame(Response::HTTP_NOT_FOUND, $responseData['errors'][0]['code']);
$this->assertSame($translator->trans('mautic.lead.event.api.point.group.not.found'), $responseData['errors'][0]['message']);

// Try to GET the group points for a contact that should not exist
$this->client->request('GET', '/api/contacts/0/points/groups/0');
$response = $this->client->getResponse();
$this->assertSame(Response::HTTP_NOT_FOUND, $response->getStatusCode());
$responseData = json_decode($response->getContent(), true);
$this->assertArrayHasKey('errors', $responseData);
$this->assertCount(1, $responseData['errors']);
$this->assertSame(Response::HTTP_NOT_FOUND, $responseData['errors'][0]['code']);
$this->assertSame($translator->trans('mautic.lead.event.api.lead.not.found'), $responseData['errors'][0]['message']);
}

private function adjustPointsAndAssert(Lead $contact, Group $pointGroup, string $operator, int $value, int $expectedScore): void
Expand Down

0 comments on commit c201aa1

Please sign in to comment.