Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/Subscription/Controller/SubscriberController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Attribute\Route;

/**
Expand Down Expand Up @@ -281,4 +282,53 @@ public function deleteSubscriber(

return $this->json(null, Response::HTTP_NO_CONTENT);
}

#[Route('/confirm', name: 'confirm', methods: ['GET'])]
#[OA\Get(
path: '/api/v2/subscribers/confirm',
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production.',
summary: 'Confirm a subscriber by uniqueId.',
requestBody: new OA\RequestBody(
required: true,
content: new OA\JsonContent(
properties: [
new OA\Property(property: 'uniqueId', type: 'string', example: 'e9d8c9b2e6')
]
)
),
tags: ['subscribers'],
responses: [
new OA\Response(
response: 200,
description: 'Subscriber confirmed',
content: new OA\MediaType(
mediaType: 'text/html'
)
),
new OA\Response(
response: 400,
description: 'Missing or invalid uniqueId'
),
new OA\Response(
response: 404,
description: 'Subscriber not found'
)
]
)]
public function setSubscriberAsConfirmed(Request $request): Response
{
$uniqueId = $request->query->get('uniqueId');

if (!$uniqueId) {
return new Response('<h1>Missing confirmation code.</h1>', 400);
}

try {
$this->subscriberManager->markAsConfirmedByUniqueId($uniqueId);
} catch (NotFoundHttpException) {
return new Response('<h1>Subscriber isn\'t found or already confirmed.</h1>', 404);
}

return new Response('<h1>Thank you, your subscription is confirmed!</h1>');
}
}
2 changes: 1 addition & 1 deletion src/Subscription/Controller/SubscriberImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function __construct(
description: 'List id to add imported subscribers to',
type: 'string',
default: null,
pattern: "^\\d+$"
pattern: '^\\d+$'
),
new OA\Property(
property: 'update_existing',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpList\RestBundle\Subscription\Controller\SubscriberController;
use PhpList\RestBundle\Tests\Integration\Common\AbstractTestController;
use PhpList\RestBundle\Tests\Integration\Subscription\Fixtures\SubscriberFixture;
use Symfony\Component\HttpFoundation\Response;

/**
* Testcase.
Expand Down Expand Up @@ -165,4 +166,44 @@ public function testPostSubscribersWithValidSessionKeyAssignsProvidedSubscriberD
static::assertTrue($responseContent['html_email']);
static::assertFalse($responseContent['disabled']);
}
public function testSetSubscriberAsConfirmedWithMissingUniqueIdReturnsBadRequestStatus()
{
self::getClient()->request('get', '/api/v2/subscribers/confirm');

$response = self::getClient()->getResponse();
self::assertSame(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
self::assertStringContainsString('Missing confirmation code', $response->getContent());
}

public function testSetSubscriberAsConfirmedWithInvalidUniqueIdReturnsNotFoundStatus()
{
self::getClient()->request('get', '/api/v2/subscribers/confirm', ['uniqueId' => 'invalid-unique-id']);

$response = self::getClient()->getResponse();
self::assertSame(Response::HTTP_NOT_FOUND, $response->getStatusCode());
self::assertStringContainsString('Subscriber isn\'t found', $response->getContent());
}

public function testSetSubscriberAsConfirmedWithValidUniqueIdConfirmsSubscriber()
{
$email = 'unconfirmed1@example.com';
$jsonData = ['email' => $email, 'requestConfirmation' => true];

$this->authenticatedJsonRequest('post', '/api/v2/subscribers', [], [], [], json_encode($jsonData));
$responseContent = $this->getDecodedJsonResponseContent();
$uniqueId = $responseContent['unique_id'];

$subscriberId = $responseContent['id'];
$subscriber = $this->subscriberRepository->find($subscriberId);
self::assertFalse($subscriber->isConfirmed());

self::getClient()->request('get', '/api/v2/subscribers/confirm', ['uniqueId' => $uniqueId]);

$response = self::getClient()->getResponse();
self::assertSame(Response::HTTP_OK, $response->getStatusCode());
self::assertStringContainsString('Thank you, your subscription is confirmed', $response->getContent());

$subscriber = $this->subscriberRepository->findOneByUniqueId($uniqueId);
self::assertTrue($subscriber->isConfirmed());
}
}
Loading