You are here

function SimplenewsSubscribeTest::testSubscribeAuthenticated in Simplenews 8

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

src/Tests/SimplenewsSubscribeTest.php, line 718
Simplenews subscribe test functions.

Class

SimplenewsSubscribeTest
(un)subscription of anonymous and authenticated users.

Namespace

Drupal\simplenews\Tests

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',
    'administer newsletters',
  ));
  $this
    ->drupalLogin($admin_user);

  // Create some newsletters for multi-sign up block.
  $this
    ->drupalGet('admin/config/services/simplenews');
  for ($i = 0; $i < 5; $i++) {
    $this
      ->clickLink(t('Add newsletter'));
    $name = $this
      ->randomMachineName();
    $edit = array(
      'name' => $name,
      'id' => strtolower($name),
      'description' => $this
        ->randomString(20),
      'opt_inout' => 'double',
    );
    $this
      ->drupalPostForm(NULL, $edit, t('Save'));
  }
  $newsletter_id = $this
    ->getRandomNewsletter();
  $this
    ->drupalLogout();

  // Setup subscription block with subscription form.
  $block_settings = array(
    'newsletters' => array(
      $newsletter_id,
    ),
    'message' => $this
      ->randomMachineName(4),
  );
  $single_block = $this
    ->setupSubscriptionBlock($block_settings);
  $subscriber_user = $this
    ->drupalCreateUser(array(
    'subscribe to newsletters',
  ));
  $this
    ->drupalLogin($subscriber_user);
  $this
    ->assertEqual($this
    ->countSubscribers(), 0);

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

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

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

  // 4. Unsubscribe authenticated via account page
  // Unsubscribe + submit
  // Assert confirmation message
  $edit = array(
    "subscriptions[{$newsletter_id}]" => FALSE,
  );
  $url = 'user/' . $subscriber_user
    ->id() . '/simplenews';
  $this
    ->drupalPostForm($url, $edit, t('Save'));
  $this
    ->assertRaw(t('Your newsletter subscriptions have been updated.', array(
    '%mail' => $subscriber_user
      ->getEmail(),
  )), t('Authenticated user unsubscribed on the account page.'));
  $subscriber = simplenews_subscriber_load_by_mail($subscriber_user
    ->getEmail());
  $subscription = $subscriber
    ->getSubscription($newsletter_id);
  $this
    ->assertEqual(SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED, $subscription->status, t('Subscription is unsubscribed'));

  // 5. Subscribe authenticated via account page
  // Subscribe + submit
  // Assert confirmation message
  $this
    ->resetSubscribers();
  $edit = array(
    "subscriptions[{$newsletter_id}]" => '1',
  );
  $url = 'user/' . $subscriber_user
    ->id() . '/simplenews';
  $this
    ->drupalPostForm($url, $edit, t('Save'));
  $this
    ->assertRaw(t('Your newsletter subscriptions have been updated.', array(
    '%mail' => $subscriber_user
      ->getEmail(),
  )), t('Authenticated user unsubscribed on the account page.'));
  $count = 1;
  $this
    ->assertEqual($this
    ->countSubscribers(), $count);

  // Disable the newsletter block.
  $single_block
    ->delete();

  // Setup subscription block with subscription form.
  $block_settings = array(
    'newsletters' => array_keys(simplenews_newsletter_get_all()),
    'message' => $this
      ->randomMachineName(4),
  );
  $multi_block = $this
    ->setupSubscriptionBlock($block_settings);

  // 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
    ->id() . '/simplenews');
  $this
    ->assertResponse(403);
  $this
    ->drupalGet('user/' . $subscriber_user2
    ->id() . '/simplenews');
  $this
    ->assertResponse(200);
  $this
    ->assertNoField('mail[0][value]');
  $this
    ->drupalPostForm(NULL, array(), t('Update'));
  $this
    ->assertText(t('The newsletter subscriptions for @mail have been updated.', array(
    '@mail' => $subscriber_user2
      ->getEmail(),
  )));

  // Nothing should have happened to subscriptions but this does create a subscriber.
  $this
    ->assertNoFieldChecked('edit-subscriptions-' . $newsletter_id);
  $count++;
  $this
    ->assertEqual($this
    ->countSubscribers(), $count);

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

  // Unsubscribe.
  $edit = array(
    'subscriptions[' . $newsletter_id . ']' => FALSE,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Update'));
  $this
    ->assertText(t('The newsletter subscriptions for @mail have been updated.', array(
    '@mail' => $subscriber_user2
      ->getEmail(),
  )));
  $this
    ->assertNoFieldChecked('edit-subscriptions-' . $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[0][value]');
  $this
    ->drupalPostForm('newsletter/subscriptions', array(), t('Update'));
  $this
    ->assertText(t('The newsletter subscriptions for @mail have been updated.', array(
    '@mail' => $subscriber_user3
      ->getEmail(),
  )));

  // Nothing should have happened to subscriptions but this does create a subscriber.
  $this
    ->assertNoFieldChecked('edit-subscriptions-' . $newsletter_id);
  $count++;
  $this
    ->assertEqual($this
    ->countSubscribers(), $count);

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

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