Skip to content

Commit 5a8ace4

Browse files
oliverkleeSam Tuke
authored andcommitted
[FEATURE] REST action for getting all subscribers for a list (#83)
1 parent 3462b99 commit 5a8ace4

File tree

4 files changed

+139
-3
lines changed

4 files changed

+139
-3
lines changed

Classes/Controller/ListController.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use FOS\RestBundle\Controller\FOSRestController;
77
use FOS\RestBundle\Routing\ClassResourceInterface;
88
use FOS\RestBundle\View\View;
9+
use PhpList\PhpList4\Domain\Model\Messaging\SubscriberList;
910
use PhpList\PhpList4\Domain\Repository\Messaging\SubscriberListRepository;
1011
use PhpList\PhpList4\Security\Authentication;
1112
use PhpList\RestBundle\Controller\Traits\AuthenticationTrait;
@@ -48,4 +49,19 @@ public function cgetAction(Request $request): View
4849

4950
return View::create()->setData($this->subscriberListRepository->findAll());
5051
}
52+
53+
/**
54+
* Gets a list of all subscribers (members) of a subscriber list.
55+
*
56+
* @param Request $request
57+
* @param SubscriberList $list
58+
*
59+
* @return View
60+
*/
61+
public function getMembersAction(Request $request, SubscriberList $list): View
62+
{
63+
$this->requireAuthentication($request);
64+
65+
return View::create()->setData($list->getSubscribers());
66+
}
5167
}

Tests/Integration/Controller/AbstractControllerTest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ protected function assertHttpOkay()
273273
}
274274

275275
/**
276-
* Asserts that the current client response has a HTTP CREATED status (and the application/json content type).
276+
* Asserts that the current client response has a HTTP CREATED status (and the application/json content type).
277277
*
278278
* @return void
279279
*/
@@ -283,7 +283,7 @@ protected function assertHttpCreated()
283283
}
284284

285285
/**
286-
* Asserts that the current client response has a HTTP BAD REQUEST status (and the application/json content type).
286+
* Asserts that the current client response has a HTTP BAD REQUEST status (and the application/json content type).
287287
*
288288
* @return void
289289
*/
@@ -293,7 +293,7 @@ protected function assertHttpBadRequest()
293293
}
294294

295295
/**
296-
* Asserts that the current client response has a HTTP UNAUTHORIZED status (and the application/json content type).
296+
* Asserts that the current client response has a HTTP UNAUTHORIZED status (and the application/json content type).
297297
*
298298
* @return void
299299
*/
@@ -302,6 +302,16 @@ protected function assertHttpUnauthorized()
302302
$this->assertHttpStatusWithJsonContentType(Response::HTTP_UNAUTHORIZED);
303303
}
304304

305+
/**
306+
* Asserts that the current client response has a HTTP NOT FOUND status (and the application/json content type).
307+
*
308+
* @return void
309+
*/
310+
protected function assertHttpNotFound()
311+
{
312+
$this->assertHttpStatusWithJsonContentType(Response::HTTP_NOT_FOUND);
313+
}
314+
305315
/**
306316
* Asserts that the current client response has a HTTP FORBIDDEN status and the corresponding error message
307317
* provided in the JSON response.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
userid,listid,entered,modified
2+
1,2,"2016-07-22 15:01:17","2016-08-23 19:50:43"
3+
2,2,"2016-08-22 15:01:17","2016-09-23 19:50:43"
4+
2,1,"2016-09-22 15:01:17","2016-10-23 19:50:43"
5+
3,1,"2017-09-22 15:01:17","2017-10-23 19:50:43"

Tests/Integration/Controller/ListControllerTest.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ class ListControllerTest extends AbstractControllerTest
1717
*/
1818
const LISTS_TABLE_NAME = 'phplist_list';
1919

20+
/**
21+
* @var string
22+
*/
23+
const SUBSCRIBER_TABLE_NAME = 'phplist_user_user';
24+
25+
/**
26+
* @var string
27+
*/
28+
const SUBSCRIPTION_TABLE_NAME = 'phplist_listuser';
29+
2030
/**
2131
* @test
2232
*/
@@ -100,4 +110,99 @@ public function getListsWithCurrentSessionKeyReturnsListData()
100110
]
101111
);
102112
}
113+
114+
/**
115+
* @test
116+
*/
117+
public function getListMembersWithoutSessionKeyReturnsForbiddenStatus()
118+
{
119+
$this->client->request('get', '/api/v2/lists/1/members');
120+
121+
$this->assertHttpForbidden();
122+
}
123+
124+
/**
125+
* @test
126+
*/
127+
public function getListMembersWithExpiredSessionKeyReturnsForbiddenStatus()
128+
{
129+
$this->getDataSet()->addTable(self::ADMINISTRATOR_TABLE_NAME, __DIR__ . '/Fixtures/Administrator.csv');
130+
$this->getDataSet()->addTable(self::TOKEN_TABLE_NAME, __DIR__ . '/Fixtures/AdministratorToken.csv');
131+
$this->applyDatabaseChanges();
132+
133+
$this->client->request(
134+
'get',
135+
'/api/v2/lists/1/members',
136+
[],
137+
[],
138+
['PHP_AUTH_USER' => 'unused', 'PHP_AUTH_PW' => 'cfdf64eecbbf336628b0f3071adba763']
139+
);
140+
141+
$this->assertHttpForbidden();
142+
}
143+
144+
/**
145+
* @test
146+
*/
147+
public function getListMembersWithCurrentSessionKeyForInexistentListReturnsNotFoundStatus()
148+
{
149+
$this->authenticatedJsonRequest('get', '/api/v2/lists/999/members');
150+
151+
$this->assertHttpNotFound();
152+
}
153+
154+
/**
155+
* @test
156+
*/
157+
public function getListMembersWithCurrentSessionKeyForExistingListReturnsOkayStatus()
158+
{
159+
$this->getDataSet()->addTable(self::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv');
160+
$this->applyDatabaseChanges();
161+
162+
$this->authenticatedJsonRequest('get', '/api/v2/lists/1/members');
163+
164+
$this->assertHttpOkay();
165+
}
166+
167+
/**
168+
* @test
169+
*/
170+
public function getListMembersWithCurrentSessionKeyForExistingListWithoutSubscribersReturnsEmptyArray()
171+
{
172+
$this->getDataSet()->addTable(self::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv');
173+
$this->applyDatabaseChanges();
174+
175+
$this->authenticatedJsonRequest('get', '/api/v2/lists/1/members');
176+
177+
$this->assertJsonResponseContentEquals([]);
178+
}
179+
180+
/**
181+
* @test
182+
*/
183+
public function getListMembersWithCurrentSessionKeyForExistingListWithSubscribersReturnsSubscribers()
184+
{
185+
$this->getDataSet()->addTable(self::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv');
186+
$this->getDataSet()->addTable(self::SUBSCRIBER_TABLE_NAME, __DIR__ . '/Fixtures/Subscriber.csv');
187+
$this->getDataSet()->addTable(self::SUBSCRIPTION_TABLE_NAME, __DIR__ . '/Fixtures/Subscription.csv');
188+
$this->applyDatabaseChanges();
189+
190+
$this->authenticatedJsonRequest('get', '/api/v2/lists/2/members');
191+
192+
$this->assertJsonResponseContentEquals(
193+
[
194+
[
195+
'creation_date' => '2016-07-22T15:01:17+00:00',
196+
'email' => '[email protected]',
197+
'confirmed' => true,
198+
'blacklisted' => true,
199+
'bounce_count' => 17,
200+
'unique_id' => '95feb7fe7e06e6c11ca8d0c48cb46e89',
201+
'html_email' => true,
202+
'disabled' => true,
203+
'id' => 1,
204+
]
205+
]
206+
);
207+
}
103208
}

0 commit comments

Comments
 (0)