You are here

function SimplenewsStatisticsTestCase::testCallClickStatisticURLDirectlyAndCheckDatabaseClickRateUpdate in Simplenews Statistics 7

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

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 also

testCallOpenStatisticURLDirectlyAndCheckDatabaseOpenRateUpdate

File

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

Class

SimplenewsStatisticsTestCase
@file Simplenews Statistics test functions.

Code

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