Skip to content

Commit

Permalink
get the number of subscribers of list, Fix #115
Browse files Browse the repository at this point in the history
Signed-off-by: Xheni Myrtaj <myrtajxheni@gmail.com>
  • Loading branch information
xh3n1 committed Feb 14, 2019
1 parent c8e3636 commit 79781fa
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Controller/ListController.php
Expand Up @@ -16,6 +16,7 @@
* This controller provides REST API access to subscriber lists.
*
* @author Oliver Klee <oliver@phplist.com>
* @author Xheni Myrtaj <xheni@phplist.com>
*/
class ListController extends FOSRestController implements ClassResourceInterface
{
Expand Down Expand Up @@ -96,4 +97,18 @@ public function getMembersAction(Request $request, SubscriberList $list): View

return View::create()->setData($list->getSubscribers());
}

/**
* Gets the total number of subscribers of a list.
* @param Request $request
* @param SubscriberList $list
*
* @return View
*/
public function getCountAction(Request $request, SubscriberList $list): View
{
$this->requireAuthentication($request);

return View::create()->setData(count($list->getSubscribers()));
}
}
1 change: 1 addition & 0 deletions tests/Integration/Controller/Fixtures/SubscriberList.csv
@@ -1,3 +1,4 @@
id,name,description,entered,modified,listorder,prefix,active,category,owner
1,"News","News (and some fun stuff)","2016-06-22 15:01:17","2016-06-23 19:50:43",12,"phpList",1,"news",1
2,"More news","","2016-06-22 15:01:17","2016-06-23 19:50:43",12,"",1,"",1
3,"Tech news","","2019-02-11 15:01:15","2019-02-11 19:50:43",12,"",1,"",1
90 changes: 90 additions & 0 deletions tests/Integration/Controller/ListControllerTest.php
Expand Up @@ -10,6 +10,7 @@
* Testcase.
*
* @author Oliver Klee <oliver@phplist.com>
* @author Xheni Myrtaj <xheni@phplist.com>
*/
class ListControllerTest extends AbstractControllerTest
{
Expand Down Expand Up @@ -107,6 +108,16 @@ public function getListsWithCurrentSessionKeyReturnsListData()
'public' => true,
'category' => '',
'id' => 2,
],
[
'name' => 'Tech news',
'description' => '',
'creation_date' => '2019-02-11T15:01:15+00:00',
'list_position' => 12,
'subject_prefix' => '',
'public' => true,
'category' => '',
'id' => 3,
]
]
);
Expand Down Expand Up @@ -320,4 +331,83 @@ public function getListMembersWithCurrentSessionKeyForExistingListWithSubscriber
]
);
}

/**
* @test
*/
public function getListCountForExistingListWithoutSessionKeyReturnsForbiddenStatus()
{
$this->getDataSet()->addTable(static::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv');
$this->applyDatabaseChanges();

$this->client->request('get', '/api/v2/lists/1/count');

$this->assertHttpForbidden();
}

/**
* @test
*/
public function getListCountForExistingListWithExpiredSessionKeyReturnsForbiddenStatus()
{
$this->getDataSet()->addTable(static::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv');
$this->getDataSet()->addTable(static::ADMINISTRATOR_TABLE_NAME, __DIR__ . '/Fixtures/Administrator.csv');
$this->getDataSet()->addTable(static::TOKEN_TABLE_NAME, __DIR__ . '/Fixtures/AdministratorToken.csv');
$this->applyDatabaseChanges();

$this->client->request(
'get',
'/api/v2/lists/1/count',
[],
[],
['PHP_AUTH_USER' => 'unused', 'PHP_AUTH_PW' => 'cfdf64eecbbf336628b0f3071adba763']
);

$this->assertHttpForbidden();
}

/**
* @test
*/
public function getListCountWithCurrentSessionKeyForExistingListReturnsOkayStatus()
{
$this->getDataSet()->addTable(static::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv');
$this->applyDatabaseChanges();

$this->authenticatedJsonRequest('get', '/api/v2/lists/1/count');

$this->assertHttpOkay();
}

/**
* @test
*/
public function getListCountWithCurrentSessionKeyForExistingListWithSubscribersReturnsSubscribersCount()
{
$this->getDataSet()->addTable(static::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv');
$this->getDataSet()->addTable(static::SUBSCRIBER_TABLE_NAME, __DIR__ . '/Fixtures/Subscriber.csv');
$this->getDataSet()->addTable(static::SUBSCRIPTION_TABLE_NAME, __DIR__ . '/Fixtures/Subscription.csv');
$this->applyDatabaseChanges();

$this->authenticatedJsonRequest('get', '/api/v2/lists/2/count');
$response = $this->getDecodedJsonResponseContent();

static::assertSame(1, $response);
}

/**
* @test
*/
public function getListCountWithCurrentSessionKeyForExistingListWithNoSubscribersReturnsZero()
{
$this->getDataSet()->addTable(static::LISTS_TABLE_NAME, __DIR__ . '/Fixtures/SubscriberList.csv');
$this->getDataSet()->addTable(static::SUBSCRIBER_TABLE_NAME, __DIR__ . '/Fixtures/Subscriber.csv');
$this->getDataSet()->addTable(static::SUBSCRIPTION_TABLE_NAME, __DIR__ . '/Fixtures/Subscription.csv');
$this->applyDatabaseChanges();

$this->authenticatedJsonRequest('get', '/api/v2/lists/3/count');
$response = $this->getDecodedJsonResponseContent();

static::assertSame(0, $response);
}
}

0 comments on commit 79781fa

Please sign in to comment.