You are here

function SimplenewsAdministrationTest::testSubscriberStatusFilter in Simplenews 8

Test content subscription status filter in subscriber view.

File

src/Tests/SimplenewsAdministrationTest.php, line 850

Class

SimplenewsAdministrationTest
Managing of newsletter categories and content types.

Namespace

Drupal\simplenews\Tests

Code

function testSubscriberStatusFilter() {

  // Make sure subscription overview can't be accessed without permission.
  $this
    ->drupalGet('admin/people/simplenews');
  $this
    ->assertResponse(403);
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer newsletters',
    'create simplenews_issue content',
    'administer nodes',
    'administer simplenews subscriptions',
  ));
  $this
    ->drupalLogin($admin_user);
  $subscribers = array();

  // Create some subscribers.
  for ($i = 0; $i < 3; $i++) {
    $subscribers[] = Subscriber::create(array(
      'mail' => $this
        ->randomEmail(),
    ));
  }
  foreach ($subscribers as $subscriber) {
    $subscriber
      ->setStatus(SubscriberInterface::ACTIVE);
  }

  // Subscribe to the default newsletter and set subscriber status.
  $subscribers[0]
    ->subscribe('default', SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED);
  $subscribers[1]
    ->subscribe('default', SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED);
  $subscribers[2]
    ->subscribe('default', SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED);
  foreach ($subscribers as $subscriber) {
    $subscriber
      ->save();
  }
  $newsletters = simplenews_newsletter_get_all();

  // Filter out subscribers by their subscription status and assert the output.
  $this
    ->drupalGet('admin/people/simplenews', array(
    'query' => array(
      'subscriptions_status' => SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED,
    ),
  ));
  $row = $this
    ->xpath('//tbody/tr');
  $this
    ->assertEqual(1, count($row));
  $this
    ->assertEqual($subscribers[0]
    ->getMail(), trim((string) $row[0]->td[0]));
  $this
    ->drupalGet('admin/people/simplenews', array(
    'query' => array(
      'subscriptions_status' => SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED,
    ),
  ));
  $row = $this
    ->xpath('//tbody/tr');
  $this
    ->assertEqual(1, count($row));
  $this
    ->assertEqual($subscribers[1]
    ->getMail(), trim((string) $row[0]->td[0]));
  $this
    ->assertText($newsletters['default']->name . ' (' . t('Unconfirmed') . ')');
  $this
    ->drupalGet('admin/people/simplenews', array(
    'query' => array(
      'subscriptions_status' => SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED,
    ),
  ));
  $row = $this
    ->xpath('//tbody/tr');
  $this
    ->assertEqual(1, count($row));
  $this
    ->assertEqual($subscribers[2]
    ->getMail(), trim((string) $row[0]->td[0]));
  $this
    ->assertText($newsletters['default']->name . ' (' . t('Unsubscribed') . ')');
}