You are here

function SimplenewsSubscribeTestCase::testSubscribeAuthenticated in Simplenews 6.2

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

testSubscribeAuthenticated

Steps performed: 0. Preparation 1. Subscribe authenticated via block 2. Unsubscribe authenticated via subscription page 3. Subscribe authenticated via subscription page 4. Unsubscribe authenticated via account page 5. Subscribe authenticated via account page 6. Subscribe authenticated via multi block

File

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

Class

SimplenewsSubscribeTestCase

Code

function testSubscribeAuthenticated() {

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

  // Setup subscription block with subscription form.
  $block_settings = array(
    'message' => $this
      ->randomName(4),
    'form' => '1',
    'link to previous' => FALSE,
    'previous issues' => FALSE,
    'rss feed' => TRUE,
  );
  $tid = $this
    ->getRandomNewsletter();
  $this
    ->setupSubscriptionBlock($tid, $block_settings);
  $this
    ->drupalLogout();
  $subscriber_user = $this
    ->drupalCreateUser(array(
    'subscribe to newsletters',
  ));
  $this
    ->drupalLogin($subscriber_user);

  // 1. Subscribe authenticated via block
  // Subscribe + submit
  // Assert confirmation message
  $this
    ->drupalPost('', NULL, t('Subscribe'));
  $this
    ->assertText(t('You have been subscribed.'), t('Authenticated user subscribed using the subscription block.'));

  // 2. Unsubscribe authenticated via subscription page
  // Unsubscribe + submit
  // Assert confirmation message
  $edit = array(
    "newsletters[{$tid}]" => 0,
  );
  $this
    ->drupalPost('newsletter/subscriptions', $edit, t('Update'));
  $this
    ->assertRaw(t('The newsletter subscriptions for %mail have been updated.', array(
    '%mail' => $subscriber_user->mail,
  )), t('Authenticated user unsubscribed on the subscriptions page.'));

  // 3. Subscribe authenticated via subscription page
  // Subscribe + submit
  // Assert confirmation message
  $edit = array(
    "newsletters[{$tid}]" => '1',
  );
  $this
    ->drupalPost('newsletter/subscriptions', $edit, t('Update'));
  $this
    ->assertRaw(t('The newsletter subscriptions for %mail have been updated.', array(
    '%mail' => $subscriber_user->mail,
  )), t('Authenticated user subscribed on the subscriptions page.'));

  // 4. Unsubscribe authenticated via account page
  // Unsubscribe + submit
  // Assert confirmation message
  $edit = array(
    "newsletters[{$tid}]" => 0,
  );
  $url = 'user/' . $subscriber_user->uid . '/edit/newsletter';
  $this
    ->drupalPost($url, $edit, t('Save'));
  $this
    ->assertRaw(t('Your newsletter subscriptions have been updated.', array(
    '%mail' => $subscriber_user->mail,
  )), t('Authenticated user unsubscribed on the account page.'));

  // 5. Subscribe authenticated via account page
  // Subscribe + submit
  // Assert confirmation message
  $edit = array(
    "newsletters[{$tid}]" => '1',
  );
  $url = 'user/' . $subscriber_user->uid . '/edit/newsletter';
  $this
    ->drupalPost($url, $edit, t('Save'));
  $this
    ->assertRaw(t('Your newsletter subscriptions have been updated.', array(
    '%mail' => $subscriber_user->mail,
  )), t('Authenticated user unsubscribed on the account page.'));

  // Subscribe authenticated via multi block
  $this
    ->drupalLogin($admin_user);

  // Enable the multi-sign up block.
  $this
    ->setupSubscriptionBlock(0);

  // Disable the category block.
  $edit = array(
    'simplenews_' . $tid . '[region]' => -1,
  );
  $this
    ->drupalPost(NULL, $edit, t('Save blocks'));
  $this
    ->drupalLogout();

  // Try to submit multi-signup form without selecting a category.
  $subscriber_user2 = $this
    ->drupalCreateUser(array(
    'subscribe to newsletters',
  ));
  $this
    ->drupalLogin($subscriber_user2);
  $this
    ->assertNoField('mail');
  $this
    ->drupalPost(NULL, array(), t('Update'));
  $this
    ->assertText(t('The newsletter subscriptions for @mail have been updated.', array(
    '@mail' => $subscriber_user2->mail,
  )));

  // Nothing should have happened.
  $this
    ->assertNoFieldChecked('edit-newsletters-' . $tid);

  // Now fill out the form and try again.
  $edit = array(
    'newsletters[' . $tid . ']' => TRUE,
  );
  $this
    ->drupalPost(NULL, $edit, t('Update'));
  $this
    ->assertText(t('The newsletter subscriptions for @mail have been updated.', array(
    '@mail' => $subscriber_user2->mail,
  )));
  $this
    ->assertFieldChecked('edit-newsletters-' . $tid);

  // Unsubscribe.
  $edit = array(
    'newsletters[' . $tid . ']' => FALSE,
  );
  $this
    ->drupalPost(NULL, $edit, t('Update'));
  $this
    ->assertText(t('The newsletter subscriptions for @mail have been updated.', array(
    '@mail' => $subscriber_user2->mail,
  )));
  $this
    ->assertNoFieldChecked('edit-newsletters-' . $tid);

  // And now the same for the newsletter/subscriptions page.
  $subscriber_user3 = $this
    ->drupalCreateUser(array(
    'subscribe to newsletters',
  ));
  $this
    ->drupalLogin($subscriber_user3);
  $this
    ->assertNoField('mail');
  $this
    ->drupalPost('newsletter/subscriptions', array(), t('Update'));
  $this
    ->assertText(t('The newsletter subscriptions for @mail have been updated.', array(
    '@mail' => $subscriber_user3->mail,
  )));

  // Nothing should have happened.
  $this
    ->assertNoFieldChecked('edit-newsletters-' . $tid);

  // Now fill out the form and try again.
  $edit = array(
    'newsletters[' . $tid . ']' => TRUE,
  );
  $this
    ->drupalPost('newsletter/subscriptions', $edit, t('Update'));
  $this
    ->assertText(t('The newsletter subscriptions for @mail have been updated.', array(
    '@mail' => $subscriber_user3->mail,
  )));
  $this
    ->assertFieldChecked('edit-newsletters-' . $tid);

  // Unsubscribe.
  $edit = array(
    'newsletters[' . $tid . ']' => FALSE,
  );
  $this
    ->drupalPost('newsletter/subscriptions', $edit, t('Update'));
  $this
    ->assertText(t('The newsletter subscriptions for @mail have been updated.', array(
    '@mail' => $subscriber_user3->mail,
  )));
  $this
    ->assertNoFieldChecked('edit-newsletters-' . $tid);
}