You are here

function SimplenewsSubscribeTestCase::testSubscribeAuthenticated in Simplenews 7.2

Same name and namespace in other branches
  1. 6.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 915
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',
    'previous issues' => FALSE,
  );
  $newsletter_id = $this
    ->getRandomNewsletter();
  $this
    ->setupSubscriptionBlock($newsletter_id, $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[{$newsletter_id}]" => 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[{$newsletter_id}]" => '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[{$newsletter_id}]" => FALSE,
  );
  $url = 'user/' . $subscriber_user->uid . '/edit/simplenews';
  $this
    ->drupalPost($url, $edit, t('Save'));
  $this
    ->assertRaw(t('The changes have been saved.', array(
    '%mail' => $subscriber_user->mail,
  )), t('Authenticated user unsubscribed on the account page.'));
  $subscriber = simplenews_subscriber_load_by_mail($subscriber_user->mail);
  $subscription = db_query('SELECT * FROM {simplenews_subscription} WHERE snid = :snid AND newsletter_id = :newsletter_id', array(
    ':snid' => $subscriber->snid,
    ':newsletter_id' => $newsletter_id,
  ))
    ->fetchObject();
  $this
    ->assertEqual(SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED, $subscription->status, t('Subscription is unsubscribed'));

  // 5. Subscribe authenticated via account page
  // Subscribe + submit
  // Assert confirmation message
  $edit = array(
    "newsletters[{$newsletter_id}]" => '1',
  );
  $url = 'user/' . $subscriber_user->uid . '/edit/simplenews';
  $this
    ->drupalPost($url, $edit, t('Save'));
  $this
    ->assertRaw(t('The changes have been saved.', 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 newsletter block.
  $edit = array(
    'blocks[simplenews_' . $newsletter_id . '][region]' => -1,
  );
  $this
    ->drupalPost(NULL, $edit, t('Save blocks'));
  $this
    ->drupalLogout();

  // Try to submit multi-signup form without selecting a newsletter.
  $subscriber_user2 = $this
    ->drupalCreateUser(array(
    'subscribe to newsletters',
  ));
  $this
    ->drupalLogin($subscriber_user2);

  // Check that the user has only access to his own subscriptions page.
  $this
    ->drupalGet('user/' . $subscriber_user->uid . '/edit/simplenews');
  $this
    ->assertResponse(403);
  $this
    ->drupalGet('user/' . $subscriber_user2->uid . '/edit/simplenews');
  $this
    ->assertResponse(200);
  $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-' . $newsletter_id);

  // Now fill out the form and try again.
  $edit = array(
    'newsletters[' . $newsletter_id . ']' => 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-' . $newsletter_id);

  // Unsubscribe.
  $edit = array(
    'newsletters[' . $newsletter_id . ']' => 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-' . $newsletter_id);

  // 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-' . $newsletter_id);

  // Now fill out the form and try again.
  $edit = array(
    'newsletters[' . $newsletter_id . ']' => 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-' . $newsletter_id);

  // Unsubscribe.
  $edit = array(
    'newsletters[' . $newsletter_id . ']' => 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-' . $newsletter_id);
}