You are here

class SimplenewsStatisticsTestCase in Simplenews Statistics 7

Same name and namespace in other branches
  1. 7.2 tests/simplenews_statistics.test \SimplenewsStatisticsTestCase

@file Simplenews Statistics test functions.

Hierarchy

Expanded class hierarchy of SimplenewsStatisticsTestCase

See also

SimplenewsTestCase

File

tests/simplenews_statistics.test, line 11
Simplenews Statistics test functions.

View source
class SimplenewsStatisticsTestCase extends SimplenewsTestCase {
  private $newsletter_nid = NULL;

  /**
   * Implementation of getInfo().
   */
  function getInfo() {
    return array(
      'name' => t('Simplenews Statistics'),
      'description' => t('Tests newsletter statistic procedures for the simplenews module.'),
      'group' => t('Simplenews'),
    );
  }
  function setUp($modules = array()) {
    $modules = array_merge(array(
      'simplenews_statistics',
    ), $modules);
    parent::setUp($modules);

    /**
     * @var $user
     *   Create a user with all simplenews, simplenews statistics and core permissions
     */
    $this->user = $this
      ->drupalCreateUser(array(
      'administer newsletter statistics',
      'view newsletters statistics',
      'administer newsletters',
      'send newsletter',
      'administer nodes',
      'administer simplenews subscriptions',
      'create simplenews content',
      'edit any simplenews content',
      'view own unpublished content',
      'delete any simplenews content',
    ));
    $this
      ->drupalLogin($this->user);

    // Subscribe a few users. Use a higher amount because we want to test statistics
    $this
      ->setUpSubscribers(37);
  }

  /**
   * Function that will create a newsletter in the default category
   *
   * @see SimplenewsSourceTestCase::testSendHTML
   */
  private function createNewsletter() {

    // Use custom testing mail system to support HTML mails.
    variable_set('mail_system', array(
      'default-system' => 'SimplenewsHTMLTestingMailSystem',
    ));

    // Set the format to HTML.
    $this
      ->drupalGet('admin/config/services/simplenews');
    $this
      ->clickLink(t('edit newsletter category'));
    $edit_category = array(
      'format' => 'html',
      // Use umlaut to provoke mime encoding.
      'from_name' => 'Drupäl Simplenews Statistic Testing',
      // @todo: load the website default email
      'from_address' => $this
        ->randomEmail(),
      // Request a confirmation receipt.
      'receipt' => TRUE,
    );
    $this
      ->drupalPost(NULL, $edit_category, t('Save'));
    $body_text = <<<TEXT
Mail token: <strong>[simplenews-subscriber:mail]</strong><br />
add a link in the mail to drupal.org so we can test later <br />
<a title="Simplenews Statistics Link" href="http://drupal.org/project/simplenews_statistics ">Simplenews Statistics Module Page</a>
TEXT;
    $edit = array(
      'title' => $this
        ->randomName(),
      'body[und][0][value]' => $body_text,
    );
    $this
      ->drupalPost('node/add/simplenews', $edit, 'Save');
    $this
      ->assertTrue(preg_match('|node/(\\d+)$|', $this
      ->getUrl(), $matches), 'Node created');
    $node = node_load($matches[1]);
    $this->newsletter_nid = $node->nid;
  }

  /**
   * Function that will create a newsletter in the default category and publish it
   */
  private function createAndPublishNewsletter() {
    $this
      ->createNewsletter();
    $node = node_load($this->newsletter_nid);
    $node->status = 1;
    node_save($node);
  }

  /**
   * Function that will create a newsletter in the default category and send it
   */
  private function createAndSendNewsletter() {
    $this
      ->createAndPublishNewsletter();
    $node = node_load($this->newsletter_nid);

    // Send the node.
    simplenews_add_node_to_spool($node);

    // Send mails.
    simplenews_mail_spool();
  }

  /**
   * Test Statistic Logic: Open Rate
   *
   * test that calling the URL /simplenews/statistics/view for the node
   * properly updates the statistics
   */
  function testCallOpenStatisticURLDirectlyAndCheckDatabaseOpenRateUpdate() {
    $this
      ->createAndSendNewsletter();

    //get the last email sent
    $mails = $this
      ->drupalGetMails();
    $mail = end($mails);
    $source = $mail['params']['simplenews_source'];
    $source_node = $source
      ->getNode();

    //obtain the full URL link by stripping off the characters
    $link = $mail['body'];
    $link = substr($link, strpos($link, check_plain(url('simplenews/statistics/view', array(
      'absolute' => TRUE,
    )))));
    $link = substr($link, 0, strpos($link, '"'));
    $this
      ->verbose('Link thus far is: ' . $link);

    //Before "viewing", verify the tables are properly initialized

    //the simplenews_statistics_open table should have no entry.
    $query = db_select('simplenews_statistics_open', 'ssc');
    $query
      ->addExpression('COUNT(*)', 'ct');
    $query
      ->condition('nid', $source_node->nid);
    $query
      ->condition('email', $mail['to']);
    if ($resultset = $query
      ->execute()) {
      if ($result = $resultset
        ->fetchField()) {
        $this
          ->assertEqual(0, $result, t('Simplenews newsletter @statistic statistic was recorded properly in @table.', array(
          '@statistic' => 'open',
          '@table' => 'simplenews_statistics_open',
        )) . ' ' . t('Expected @expected, received @received', array(
          '@expected' => 0,
          '@received' => $result,
        )));
      }
    }

    //Query that 0 views is recorded in simplenews_statistics; expected total opens = 0 and unique opens = 0
    $query = db_select('simplenews_statistics', 'ssc');
    $query
      ->fields('ssc', array(
      'total_opens',
      'unique_opens',
    ));
    $query
      ->condition('nid', $source_node->nid);
    $found = FALSE;
    if ($resultset = $query
      ->execute()) {
      if ($result = $resultset
        ->fetchObject()) {
        $this
          ->assertEqual(0, $result->total_opens, t('Simplenews newsletter @statistic statistic was recorded properly in @table in field: @field.', array(
          '@statistic' => 'open',
          '@table' => 'simplenews_statistics_open',
          '@field' => 'total_opens',
        )) . ' ' . t('Expected @expected, received @received', array(
          '@expected' => 0,
          '@received' => $result->total_opens,
        )));
        $this
          ->assertEqual(0, $result->unique_opens, t('Simplenews newsletter @statistic statistic was recorded properly in @table in field: @field.', array(
          '@statistic' => 'open',
          '@table' => 'simplenews_statistics_open',
          '@field' => 'unique_opens',
        )) . ' ' . t('Expected @expected, received @received', array(
          '@expected' => 0,
          '@received' => $result->unique_opens,
        )));
        $found = TRUE;
      }
    }
    if (!$found) {
      $this
        ->fail(t('Simplenews newsletter @statistic statistic was recorded properly in @table.', array(
        '@statistic' => 'open',
        '@table' => 'simplenews_statistics',
      )));
    }

    //load the image
    $this
      ->drupalGet($link);

    //the simplenews_statistics_open table should have 1 entry.
    $query = db_select('simplenews_statistics_open', 'ssc');
    $query
      ->addExpression('COUNT(*)', 'ct');
    $query
      ->condition('nid', $source_node->nid);
    $query
      ->condition('email', $mail['to']);
    $found = FALSE;
    if ($resultset = $query
      ->execute()) {
      if ($result = $resultset
        ->fetchField()) {
        $this
          ->assertEqual(1, $result, t('Simplenews newsletter @statistic statistic was recorded properly in @table.', array(
          '@statistic' => 'open',
          '@table' => 'simplenews_statistics_open',
        )) . ' ' . t('Expected @expected, received @received', array(
          '@expected' => 1,
          '@received' => $result,
        )));
        $found = TRUE;
      }
    }
    if (!$found) {
      $this
        ->fail(t('Simplenews newsletter @statistic statistic was recorded properly in @table.', array(
        '@statistic' => 'open',
        '@table' => 'simplenews_statistics_open',
      )));
    }

    //Query that 1 views is recorded in simplenews_statistics; expected total opens = 1 and unique opens = 1
    $query = db_select('simplenews_statistics', 'ssc');
    $query
      ->fields('ssc', array(
      'total_opens',
      'unique_opens',
    ));
    $query
      ->condition('nid', $source_node->nid);
    $found = FALSE;
    if ($resultset = $query
      ->execute()) {
      if ($result = $resultset
        ->fetchObject()) {
        $this
          ->assertEqual(1, $result->total_opens, t('Simplenews newsletter @statistic statistic was recorded properly in @table in field: @field.', array(
          '@statistic' => 'open',
          '@table' => 'simplenews_statistics_open',
          '@field' => 'total_opens',
        )) . ' ' . t('Expected @expected, received @received', array(
          '@expected' => 1,
          '@received' => $result->total_opens,
        )));
        $this
          ->assertEqual(1, $result->unique_opens, t('Simplenews newsletter @statistic statistic was recorded properly in @table in field: @field.', array(
          '@statistic' => 'open',
          '@table' => 'simplenews_statistics_open',
          '@field' => 'unique_opens',
        )) . ' ' . t('Expected @expected, received @received', array(
          '@expected' => 1,
          '@received' => $result->unique_opens,
        )));
        $found = TRUE;
      }
    }
    if (!$found) {
      $this
        ->fail(t('Simplenews newsletter @statistic statistic was recorded properly in @table.', array(
        '@statistic' => 'open',
        '@table' => 'simplenews_statistics',
      )));
    }

    //load the image a second time
    $this
      ->drupalGet($link);

    //the simplenews_statistics_open table should have 2 entries.
    $query = db_select('simplenews_statistics_open', 'ssc');
    $query
      ->addExpression('COUNT(*)', 'ct');
    $query
      ->condition('nid', $source_node->nid);
    $query
      ->condition('email', $mail['to']);
    $found = FALSE;
    if ($resultset = $query
      ->execute()) {
      if ($result = $resultset
        ->fetchField()) {
        $this
          ->assertEqual(2, $result, t('Simplenews newsletter @statistic statistic was recorded properly in @table.', array(
          '@statistic' => 'open',
          '@table' => 'simplenews_statistics_open',
        )) . ' ' . t('Expected @expected, received @received', array(
          '@expected' => 2,
          '@received' => $result,
        )));
        $found = TRUE;
      }
    }
    if (!$found) {
      $this
        ->fail(t('Simplenews newsletter @statistic statistic was recorded properly in @table.', array(
        '@statistic' => 'open',
        '@table' => 'simplenews_statistics_open',
      )));
    }

    //Query that 1 views is recorded in simplenews_statistics; expected total opens = 2 and unique opens = 1
    $query = db_select('simplenews_statistics', 'ssc');
    $query
      ->fields('ssc', array(
      'total_opens',
      'unique_opens',
    ));
    $query
      ->condition('nid', $source_node->nid);
    $found = FALSE;
    if ($resultset = $query
      ->execute()) {
      if ($result = $resultset
        ->fetchObject()) {
        $this
          ->assertEqual(2, $result->total_opens, t('Simplenews newsletter @statistic statistic was recorded properly in @table in field: @field.', array(
          '@statistic' => 'open',
          '@table' => 'simplenews_statistics_open',
          '@field' => 'total_opens',
        )) . ' ' . t('Expected @expected, received @received', array(
          '@expected' => 2,
          '@received' => $result->total_opens,
        )));
        $this
          ->assertEqual(1, $result->unique_opens, t('Simplenews newsletter @statistic statistic was recorded properly in @table in field: @field.', array(
          '@statistic' => 'open',
          '@table' => 'simplenews_statistics_open',
          '@field' => 'unique_opens',
        )) . ' ' . t('Expected @expected, received @received', array(
          '@expected' => 1,
          '@received' => $result->unique_opens,
        )));
        $found = TRUE;
      }
    }
    if (!$found) {
      $this
        ->fail(t('Simplenews newsletter @statistic statistic was recorded properly in @table.', array(
        '@statistic' => 'open',
        '@table' => 'simplenews_statistics',
      )));
    }
  }

  /**
   * Test Statistic Logic: Click Rate
   *
   * test that calling the URL /simplenews/statistics/click for the node
   * properly updates the statistics. Modelled after the Opens test (previous test).
   *
   * @see testCallOpenStatisticURLDirectlyAndCheckDatabaseOpenRateUpdate
   */
  function testCallClickStatisticURLDirectlyAndCheckDatabaseClickRateUpdate() {
    $this
      ->createAndSendNewsletter();

    //get the last email sent
    $mails = $this
      ->drupalGetMails();
    $mail = end($mails);
    $source = $mail['params']['simplenews_source'];
    $source_node = $source
      ->getNode();

    //obtain the full URL link by stripping off the characters
    $link = $mail['body'];
    $link = substr($link, strpos($link, check_plain(url('simplenews/statistics/click', array(
      'absolute' => TRUE,
    )))));

    //we only obtain the first of probobly several urls
    $link = substr($link, 0, strpos($link, '"'));
    $this
      ->verbose('Link thus far is: ' . $link);

    //Before "clicking", verify the tables are properly initialized

    //the simplenews_statistics_open table should have no entry.
    $query = db_select('simplenews_statistics_click', 'ssc');
    $query
      ->addExpression('COUNT(*)', 'ct');
    $query
      ->condition('nid', $source_node->nid);
    $query
      ->condition('email', $mail['to']);
    if ($resultset = $query
      ->execute()) {
      if ($result = $resultset
        ->fetchField()) {
        $this
          ->assertEqual(0, $result, t('Simplenews newsletter @statistic statistic was recorded properly in @table.', array(
          '@statistic' => 'open',
          '@table' => 'simplenews_statistics_click',
        )) . ' ' . t('Expected @expected, received @received', array(
          '@expected' => 0,
          '@received' => $result,
        )));
      }
    }

    //Query that 0 views is recorded in simplenews_statistics; expected total opens = 0 and unique opens = 0
    $query = db_select('simplenews_statistics', 'ssc');
    $query
      ->fields('ssc', array(
      'total_clicks',
      'user_unique_click_through',
    ));
    $query
      ->condition('nid', $source_node->nid);
    $found = FALSE;
    if ($resultset = $query
      ->execute()) {
      if ($result = $resultset
        ->fetchObject()) {
        $this
          ->assertEqual(0, $result->total_clicks, t('Simplenews newsletter @statistic statistic was recorded properly in @table in field: @field.', array(
          '@statistic' => 'open',
          '@table' => 'simplenews_statistics_click',
          '@field' => 'total_clicks',
        )) . ' ' . t('Expected @expected, received @received', array(
          '@expected' => 0,
          '@received' => $result->total_clicks,
        )));
        $this
          ->assertEqual(0, $result->user_unique_click_through, t('Simplenews newsletter @statistic statistic was recorded properly in @table in field: @field.', array(
          '@statistic' => 'open',
          '@table' => 'simplenews_statistics_click',
          '@field' => 'user_unique_click_through',
        )) . ' ' . t('Expected @expected, received @received', array(
          '@expected' => 0,
          '@received' => $result->user_unique_click_through,
        )));
        $found = TRUE;
      }
    }
    if (!$found) {
      $this
        ->fail(t('Simplenews newsletter @statistic statistic was recorded properly in @table.', array(
        '@statistic' => 'open',
        '@table' => 'simplenews_statistics',
      )));
    }

    //load the image
    $this
      ->drupalGet($link);

    //the simplenews_statistics_click table should have 1 entry.
    $query = db_select('simplenews_statistics_click', 'ssc');
    $query
      ->addExpression('COUNT(*)', 'ct');
    $query
      ->condition('nid', $source_node->nid);
    $query
      ->condition('email', $mail['to']);
    $found = FALSE;
    if ($resultset = $query
      ->execute()) {
      if ($result = $resultset
        ->fetchField()) {
        $this
          ->assertEqual(1, $result, t('Simplenews newsletter @statistic statistic was recorded properly in @table.', array(
          '@statistic' => 'open',
          '@table' => 'simplenews_statistics_click',
        )) . ' ' . t('Expected @expected, received @received', array(
          '@expected' => 1,
          '@received' => $result,
        )));
        $found = TRUE;
      }
    }
    if (!$found) {
      $this
        ->fail(t('Simplenews newsletter @statistic statistic was recorded properly in @table.', array(
        '@statistic' => 'open',
        '@table' => 'simplenews_statistics_click',
      )));
    }

    //Query that 1 views is recorded in simplenews_statistics; expected total opens = 1 and unique opens = 1
    $query = db_select('simplenews_statistics', 'ssc');
    $query
      ->fields('ssc', array(
      'total_clicks',
      'user_unique_click_through',
    ));
    $query
      ->condition('nid', $source_node->nid);
    $found = FALSE;
    if ($resultset = $query
      ->execute()) {
      if ($result = $resultset
        ->fetchObject()) {
        $this
          ->assertEqual(1, $result->total_clicks, t('Simplenews newsletter @statistic statistic was recorded properly in @table in field: @field.', array(
          '@statistic' => 'open',
          '@table' => 'simplenews_statistics_click',
          '@field' => 'total_clicks',
        )) . ' ' . t('Expected @expected, received @received', array(
          '@expected' => 1,
          '@received' => $result->total_clicks,
        )));
        $this
          ->assertEqual(1, $result->user_unique_click_through, t('Simplenews newsletter @statistic statistic was recorded properly in @table in field: @field.', array(
          '@statistic' => 'open',
          '@table' => 'simplenews_statistics_click',
          '@field' => 'user_unique_click_through',
        )) . ' ' . t('Expected @expected, received @received', array(
          '@expected' => 1,
          '@received' => $result->user_unique_click_through,
        )));
        $found = TRUE;
      }
    }
    if (!$found) {
      $this
        ->fail(t('Simplenews newsletter @statistic statistic was recorded properly in @table.', array(
        '@statistic' => 'open',
        '@table' => 'simplenews_statistics',
      )));
    }

    //load the image a second time
    $this
      ->drupalGet($link);

    //the simplenews_statistics_click table should have 2 entries.
    $query = db_select('simplenews_statistics_click', 'ssc');
    $query
      ->addExpression('COUNT(*)', 'ct');
    $query
      ->condition('nid', $source_node->nid);
    $query
      ->condition('email', $mail['to']);
    $found = FALSE;
    if ($resultset = $query
      ->execute()) {
      if ($result = $resultset
        ->fetchField()) {
        $this
          ->assertEqual(2, $result, t('Simplenews newsletter @statistic statistic was recorded properly in @table.', array(
          '@statistic' => 'open',
          '@table' => 'simplenews_statistics_click',
        )) . ' ' . t('Expected @expected, received @received', array(
          '@expected' => 2,
          '@received' => $result,
        )));
        $found = TRUE;
      }
    }
    if (!$found) {
      $this
        ->fail(t('Simplenews newsletter @statistic statistic was recorded properly in @table.', array(
        '@statistic' => 'open',
        '@table' => 'simplenews_statistics_click',
      )));
    }

    //Query that 1 views is recorded in simplenews_statistics; expected total opens = 2 and unique opens = 1
    $query = db_select('simplenews_statistics', 'ssc');
    $query
      ->fields('ssc', array(
      'total_clicks',
      'user_unique_click_through',
    ));
    $query
      ->condition('nid', $source_node->nid);
    $found = FALSE;
    if ($resultset = $query
      ->execute()) {
      if ($result = $resultset
        ->fetchObject()) {
        $this
          ->assertEqual(2, $result->total_clicks, t('Simplenews newsletter @statistic statistic was recorded properly in @table in field: @field.', array(
          '@statistic' => 'open',
          '@table' => 'simplenews_statistics_click',
          '@field' => 'total_clicks',
        )) . ' ' . t('Expected @expected, received @received', array(
          '@expected' => 2,
          '@received' => $result->total_clicks,
        )));
        $this
          ->assertEqual(1, $result->user_unique_click_through, t('Simplenews newsletter @statistic statistic was recorded properly in @table in field: @field.', array(
          '@statistic' => 'open',
          '@table' => 'simplenews_statistics_click',
          '@field' => 'user_unique_click_through',
        )) . ' ' . t('Expected @expected, received @received', array(
          '@expected' => 1,
          '@received' => $result->user_unique_click_through,
        )));
        $found = TRUE;
      }
    }
    if (!$found) {
      $this
        ->fail(t('Simplenews newsletter @statistic statistic was recorded properly in @table.', array(
        '@statistic' => 'open',
        '@table' => 'simplenews_statistics',
      )));
    }
  }

  /**
   * Test Workflow: Redirected to correct page
   *
   * send a newsletter, click a link, test that the user is forwarded to the correct page
   */
  function testClickStatisticLinkRedirect() {
    $this
      ->createAndSendNewsletter();

    //get the last email sent
    $mails = $this
      ->drupalGetMails();
    $mail = end($mails);
    $source = $mail['params']['simplenews_source'];
    $source_node = $source
      ->getNode();

    //obtain the full URL link by stripping off the characters
    $link = $mail['body'];
    $link = substr($link, strpos($link, 'title="Simplenews Statistics Link" href="'));

    //make certain we get the link that we need to test
    $link = substr($link, strpos($link, check_plain(url('simplenews/statistics/click', array(
      'absolute' => TRUE,
    )))));

    //we only obtain the first of probobly several urls
    $link = substr($link, 0, strpos($link, '"'));
    $this
      ->verbose('Link thus far is: ' . $link);
    $intended_url = "http://drupal.org/project/simplenews_statistics";

    //defined in the test mail we send above

    //click the link - see if we are re-directed to the target page
    $this
      ->drupalGet($link);

    //test
    $this
      ->assertEqual($intended_url, $this
      ->getUrl());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SimplenewsStatisticsTestCase::$newsletter_nid private property
SimplenewsStatisticsTestCase::createAndPublishNewsletter private function Function that will create a newsletter in the default category and publish it
SimplenewsStatisticsTestCase::createAndSendNewsletter private function Function that will create a newsletter in the default category and send it
SimplenewsStatisticsTestCase::createNewsletter private function Function that will create a newsletter in the default category
SimplenewsStatisticsTestCase::getInfo function Implementation of getInfo().
SimplenewsStatisticsTestCase::setUp function
SimplenewsStatisticsTestCase::testCallClickStatisticURLDirectlyAndCheckDatabaseClickRateUpdate function Test Statistic Logic: Click Rate
SimplenewsStatisticsTestCase::testCallOpenStatisticURLDirectlyAndCheckDatabaseOpenRateUpdate function Test Statistic Logic: Open Rate
SimplenewsStatisticsTestCase::testClickStatisticLinkRedirect function Test Workflow: Redirected to correct page