You are here

public function SimplenewsAdministrationTest::testSubscriberStatusFilter in Simplenews 8.2

Same name and namespace in other branches
  1. 3.x tests/src/Functional/SimplenewsAdministrationTest.php \Drupal\Tests\simplenews\Functional\SimplenewsAdministrationTest::testSubscriberStatusFilter()

Test content subscription status filter in subscriber view.

File

tests/src/Functional/SimplenewsAdministrationTest.php, line 867

Class

SimplenewsAdministrationTest
Managing of newsletter categories and content types.

Namespace

Drupal\Tests\simplenews\Functional

Code

public function testSubscriberStatusFilter() {

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

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

  // 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', [
    'query' => [
      'subscriptions_status' => SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED,
    ],
  ]);
  $row = $this
    ->xpath('//tbody/tr');
  $this
    ->assertEqual(1, count($row));
  $this
    ->assertEqual($subscribers[0]
    ->getMail(), trim($row[0]
    ->find('xpath', '/td')
    ->getText()));
  $this
    ->drupalGet('admin/people/simplenews', [
    'query' => [
      'subscriptions_status' => SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED,
    ],
  ]);
  $row = $this
    ->xpath('//tbody/tr');
  $this
    ->assertEqual(1, count($row));
  $this
    ->assertEqual($subscribers[1]
    ->getMail(), trim($row[0]
    ->find('xpath', '/td')
    ->getText()));
  $this
    ->assertText($newsletters['default']->name . ' (' . t('Unconfirmed') . ')');
  $this
    ->drupalGet('admin/people/simplenews', [
    'query' => [
      'subscriptions_status' => SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED,
    ],
  ]);
  $row = $this
    ->xpath('//tbody/tr');
  $this
    ->assertEqual(1, count($row));
  $this
    ->assertEqual($subscribers[2]
    ->getMail(), trim($row[0]
    ->find('xpath', '/td')
    ->getText()));
  $this
    ->assertText($newsletters['default']->name . ' (' . t('Unsubscribed') . ')');
}