You are here

function SimplenewsSubscribeTestCase::testSubscribeAnonymousSingle in Simplenews 7.2

Same name and namespace in other branches
  1. 6.2 tests/simplenews.test \SimplenewsSubscribeTestCase::testSubscribeAnonymousSingle()
  2. 7 tests/simplenews.test \SimplenewsSubscribeTestCase::testSubscribeAnonymousSingle()

Test anonymous subscription with single opt in.

Steps performed: 0. Preparation 1. Subscribe anonymous via block

File

tests/simplenews.test, line 843
Simplenews test functions.

Class

SimplenewsSubscribeTestCase

Code

function testSubscribeAnonymousSingle() {

  // 0. Preparation
  // Login admin
  // Create single opt in newsletter.
  // Set permission for anonymous to subscribe
  // Enable newsletter block
  // Logout admin
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer blocks',
    'administer content types',
    'administer nodes',
    'access administration pages',
    'administer permissions',
    'administer newsletters',
  ));
  $this
    ->drupalLogin($admin_user);
  $this
    ->setAnonymousUserSubscription(TRUE);

  // Setup subscription block with subscription form.
  $block_settings = array(
    'message' => $this
      ->randomName(4),
    'form' => '1',
    'previous issues' => FALSE,
  );
  $this
    ->drupalGet('admin/config/services/simplenews');
  $this
    ->clickLink(t('Add newsletter'));
  $edit = array(
    'name' => $this
      ->randomName(),
    'description' => $this
      ->randomString(20),
    'opt_inout' => 'single',
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));

  // @todo: Don't hardcode this.
  $newsletter_id = 2;
  $this
    ->setupSubscriptionBlock($newsletter_id, $block_settings);
  $this
    ->drupalLogout();

  // 1. Subscribe anonymous via block
  // Subscribe + submit
  // Assert confirmation message
  // Verify subscription state.
  $mail = $this
    ->randomEmail(8, 'testmail');
  $edit = array(
    'mail' => $mail,
  );
  $this
    ->drupalPost(NULL, $edit, t('Subscribe'));
  $this
    ->assertText(t('You have been subscribed.'), t('Anonymous subscriber added to newsletter'));
  $subscriber = simplenews_subscriber_load_by_mail($mail);
  $this
    ->assertEqual(SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED, $subscriber->newsletter_subscription[$newsletter_id]->status);

  // Unsubscribe again.
  $edit = array(
    'mail' => $mail,
    'newsletters[' . $newsletter_id . ']' => TRUE,
  );
  $this
    ->drupalPost('newsletter/subscriptions', $edit, t('Unsubscribe'));
  entity_get_controller('simplenews_subscriber')
    ->resetCache();
  $subscriber = simplenews_subscriber_load_by_mail($mail);
  $this
    ->assertEqual(SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED, $subscriber->newsletter_subscription[$newsletter_id]->status);
}