You are here

ActivityAPITests.test in Activity 5.2

File

tests/ActivityAPITests.test
View source
<?php

class ActivityAPITest extends DrupalTestCase {
  private $beginning_aid;
  private $sequences_id;
  function get_info() {
    return array(
      'name' => t('Activity API tests'),
      'desc' => t('Test activity_insert_activity and activity_get_activity'),
      'group' => 'Activity Tests',
    );
  }
  function setUp() {
    parent::setUp();

    //count current activities.
    $this->beginning_aid = db_result(db_query("SELECT MAX(aid) FROM {activity}"));

    //get the last id from sequences.
    $this->sequences_id = db_result(db_query("SELECT id FROM {sequences} WHERE name = 'activity'"));
  }
  function tearDown() {
    parent::tearDown();
    db_query('DELETE FROM {activity} WHERE aid > %d', $this->beginning_aid);
    db_query("UPDATE {sequences} SET id = %d WHERE name = 'activity'", $this->sequences_id);
  }
  function testActivityGetInfo() {
    $this
      ->drupalModuleEnable('nodeactivity');

    // Assert that the api function activity_get_info exists
    if ($this
      ->assertTrue(function_exists('activity_get_info'), '%s ' . t('Asserting that activity_get_info exists'))) {

      // It exists. Let's call it.
      $info = activity_get_info();

      // The API's contract is to return an array.
      $this
        ->assertTrue(is_array($info), '%s ' . t('Assrting that return value is an array.'));

      // Assert that all of the modules we're looking for are in the array.
      $this
        ->assertTrue(in_array('nodeactivity', array_keys($info)), '%s ' . t('Asserting that nodeactivity is one of the returned values.'));
    }
  }

  /**
   * - ability to pass in user or null
   * - when null global user should be used.
   * - the return value should be 1 greater than the previous greatest activity.
   */
  function testInsertActivity() {

    //insert some permutations of activites.
    $tokens = array(
      'dummydata' => 'foobar',
    );
    $module = 'simpletest';
    $type = 'testing';
    $action = 'insert';
    $uid = 4711;
    $aid = activity_insert($module, $type, $action, $tokens, $uid);

    // $aid is supposed to be the id of the resultant insert. Assert that it is.
    $this
      ->assertTrue(is_numeric($aid), '%s ' . t('activity_insert is supposed to return a numeric id'));

    // $aid is supposed to have incremented by 1 over the begninng.
    $this
      ->assertEqual($aid, $this->sequences_id + 1, '%s ' . t('the insert created an aid that is one greater than the previous.'));

    // Manually get the information from the activity table which we just put in.
    $activity = db_fetch_object(db_query("SELECT * FROM {activity} WHERE aid = %d", $aid));
    $this
      ->assertEqual($activity->uid, $uid, '%s ' . t('uid is supposed to be @uid', array(
      '@uid' => $uid,
    )));
    $this
      ->assertEqual($activity->module, $module, '%s ' . t('module is supposed to be @module', array(
      '@module' => $module,
    )));
    $this
      ->assertEqual($activity->type, $type, '%s ' . t('type is supposed to be @type', array(
      '@type' => $type,
    )));
    $this
      ->assertEqual($activity->action, $action, '%s ' . t('action is supposed to be @action', array(
      '@action' => $action,
    )));
    $new_tokens = unserialize($activity->tokens);
    $this
      ->assertTrue(is_array($new_tokens), '%s ' . t('Supposed to be an array'));
    $this
      ->assertTrue(count(array_diff($new_tokens, $tokens)) === 0, '%s ' . t('There are supposed to be no differences between the tokens arrays'));

    // Test that not passing a $user_id results in the global $user->uid being used.
    global $user;
    $global_uid = $user->uid;
    $aid2 = activity_insert($module, $type, $action, serialize($tokens));

    // $aid is supposed to be the id of the resultant insert. Assert that it is.
    $this
      ->assertTrue(is_numeric($aid2), '%s ' . t('activity_insert is supposed to return a numeric id'));

    // $aid is supposed to have incremented by 2 over the begninng.
    $this
      ->assertEqual($aid2, $this->sequences_id + 2, '%s ' . t('the insert created an aid that is two greater than the beginning.'));

    // Now assert that the $global_uid and the inserted uid are the same.
    $activity2 = db_fetch_object(db_query("SELECT * FROM {activity} WHERE aid = %d", $aid2));
    $this
      ->assertEqual($global_uid, $activity2->uid, '%s ' . t('The inserted uid (@uid1) is supposed to be equal to the global $user->uid (@uid2).', array(
      '@uid1' => $aid2,
      '@uid2' => $activity2->uid,
    )));
  }

  /**
   * Test activity_get_activity
   * Assert that function can take an array of arbitrary uids or NULL
   * Can take an optional array of module names to filter the results
   * Takes a pager parameter
   * Support HTML tables with tablesort query additions
   * Should always return an array, even if empty.
   * array items shall be ordered by database so that tablesort queries are possible.
   */
  function testGetActivity() {

    // Does this function exist?
    $this
      ->assertTrue(function_exists('activity_get_activity'));

    // Make a new user to test with
    $tester = $this
      ->drupalCreateUserRolePerm();

    // Create some activity
    $module = 'simpletest';
    $type = 'test';
    $action = 'dosomething';
    $tokens = array(
      '[user_name]' => $tester->name,
    );
    $aid = activity_insert($module, $type, $action, $tokens, $tester->uid);
    $activity = activity_get_activity();
    $this
      ->assertTrue(is_array($activity), '%s ' . t('Return value must be array.'));

    // Assert that each of the following only returns the one activity we inserted.
    $activities[1] = activity_get_activity($tester->uid);
    $activities[2] = activity_get_activity(array(
      $tester->uid,
    ));
    $activities[3] = activity_get_activity(array(
      $tester->uid,
      999999,
    ));
    $activities[4] = activity_get_activity(array(
      $tester->uid,
    ), array(
      $module,
    ));
    $activities[5] = activity_get_activity($tester->uid, NULL, 40);
    foreach ($activities as $key => $activity) {
      $this
        ->assertEqual(1, count($activity), $key . ': %s ' . t('Expecting 1 activty.'));
      $this
        ->assertEqual($aid, $activity[0]['aid'], $key . ': %s ' . t('Expecting aid to be @aid. Found @activity_id.', array(
        '@aid' => $aid,
        '@activity_id' => $activity[0]['aid'],
      )));
    }

    // Test the filter process
    // Invent some more modules
    $modules = array(
      'huba',
      'foobar',
      'barbaz',
    );

    // Create a lot of new activities.
    for ($i = 0; $i < 3; $i++) {
      $module = $modules[$i];
      for ($j = 0; $j < 50; $j++) {
        activity_insert($module, $type, $action, $tokens, $tester->uid);
      }
    }
    $activity = activity_get_activity(array(
      $tester->uid,
    ));
    $this
      ->assertEqual(151, count($activity), '%s ' . t('Expecting 151 activities.'));
    $activity = activity_get_activity('*', array(
      'huba',
    ));
    $this
      ->assertEqual(50, count($activity), '%s ' . t('Expecting 50 huba activities.'));
    $activity = activity_get_activity('*', array(
      'huba',
      'foobar',
    ));
    $this
      ->assertEqual(100, count($activity), '%s ' . t('Expecting 100 huba and foobar activities.'));
    $activity = activity_get_activity('*', array(
      'huba',
      'foobar',
    ), 17);
    $this
      ->assertEqual(17, count($activity), '%s ' . t('Expecting 17 huba and foobar activities on one page.'));
    $activity = activity_get_activity(1, array(
      'huba',
      'foobar',
    ));
    $this
      ->assertEqual(0, count($activity), '%s ' . t('Expecting 0 huba and foobar activities created by user #1.'));
  }

}

Classes

Namesort descending Description
ActivityAPITest