You are here

function SimplenewsStatisticsTestCase::testCallOpenStatisticURLDirectlyAndCheckDatabaseOpenRateUpdate in Simplenews Statistics 7

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

Test Statistic Logic: Open Rate

test that calling the URL /simplenews/statistics/view for the node properly updates the statistics

File

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

Class

SimplenewsStatisticsTestCase
@file Simplenews Statistics test functions.

Code

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',
    )));
  }
}