You are here

public function SimplenewsSubscribeTest::testSubscribeAuthenticated in Simplenews 8.2

Same name and namespace in other branches
  1. 3.x tests/src/Functional/SimplenewsSubscribeTest.php \Drupal\Tests\simplenews\Functional\SimplenewsSubscribeTest::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/src/Functional/SimplenewsSubscribeTest.php, line 711

Class

SimplenewsSubscribeTest
Un/subscription of anonymous and authenticated users.

Namespace

Drupal\Tests\simplenews\Functional

Code

public function testSubscribeAuthenticated() {

  // 0. Preparation
  // Login admin
  // Set permission for anonymous to subscribe
  // Enable newsletter block
  // Logout admin
  // Login Subscriber.
  $admin_user = $this
    ->drupalCreateUser([
    '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 = [
      '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 = [
    'newsletters' => [
      $newsletter_id,
    ],
    'message' => $this
      ->randomMachineName(4),
  ];
  $single_block = $this
    ->setupSubscriptionBlock($block_settings);
  $subscriber_user = $this
    ->drupalCreateUser([
    '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.'));
  $this
    ->assertEqual($this
    ->countSubscribers(), 1);

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

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

  // 4. Unsubscribe authenticated via account page
  // Unsubscribe + submit
  // Assert confirmation message.
  $edit = [
    "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.', [
    '%mail' => $subscriber_user
      ->getEmail(),
  ]));
  $subscriber = Subscriber::loadByMail($subscriber_user
    ->getEmail());
  $subscription = $subscriber
    ->getSubscription($newsletter_id);
  $this
    ->assertEquals(SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED, $subscription->status, t('Subscription is unsubscribed'));

  // 5. Subscribe authenticated via account page
  // Subscribe + submit
  // Assert confirmation message.
  $this
    ->resetSubscribers();
  $edit = [
    "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.', [
    '%mail' => $subscriber_user
      ->getEmail(),
  ]));
  $count = 1;
  $this
    ->assertEqual($this
    ->countSubscribers(), $count);

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

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

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

  // Check that the user has only access to their 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, [], t('Update'));
  $this
    ->assertText(t('The newsletter subscriptions for @mail have been updated.', [
    '@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 = [
    'subscriptions[' . $newsletter_id . ']' => TRUE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Update'));
  $this
    ->assertText(t('The newsletter subscriptions for @mail have been updated.', [
    '@mail' => $subscriber_user2
      ->getEmail(),
  ]));
  $this
    ->assertEqual($this
    ->countSubscribers(), $count);
  $this
    ->assertFieldChecked('edit-subscriptions-' . $newsletter_id);

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

  // And now the same for the newsletter/subscriptions page.
  $subscriber_user3 = $this
    ->drupalCreateUser([
    'subscribe to newsletters',
  ]);
  $this
    ->drupalLogin($subscriber_user3);
  $this
    ->assertNoField('mail[0][value]');
  $this
    ->drupalPostForm('newsletter/subscriptions', [], t('Update'));
  $this
    ->assertText(t('The newsletter subscriptions for @mail have been updated.', [
    '@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 = [
    'subscriptions[' . $newsletter_id . ']' => TRUE,
  ];
  $this
    ->drupalPostForm('newsletter/subscriptions', $edit, t('Update'));
  $this
    ->assertText(t('The newsletter subscriptions for @mail have been updated.', [
    '@mail' => $subscriber_user3
      ->getEmail(),
  ]));
  $this
    ->assertEqual($this
    ->countSubscribers(), $count);
  $this
    ->assertFieldChecked('edit-subscriptions-' . $newsletter_id);

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