You are here

class ActivityAPITest in Activity 5.2

Same name and namespace in other branches
  1. 5.4 tests/ActivityAPITests.test \ActivityAPITest
  2. 5.3 tests/ActivityAPITests.test \ActivityAPITest
  3. 6 tests/ActivityAPITests.test \ActivityAPITest

Hierarchy

Expanded class hierarchy of ActivityAPITest

File

tests/ActivityAPITests.test, line 2

View source
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.'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ActivityAPITest::$beginning_aid private property
ActivityAPITest::$sequences_id private property
ActivityAPITest::get_info function
ActivityAPITest::setUp function
ActivityAPITest::tearDown function tearDown implementation, setting back switched modules etc Overrides DrupalTestCase::tearDown
ActivityAPITest::testActivityGetInfo function
ActivityAPITest::testGetActivity function 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…
ActivityAPITest::testInsertActivity function 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.
DrupalTestCase::$_cleanupModules property
DrupalTestCase::$_cleanupRoles property
DrupalTestCase::$_cleanupUsers property
DrupalTestCase::$_cleanupVariables property
DrupalTestCase::$_content property
DrupalTestCase::assertCopy function Will trigger a pass if both parameters refer to different objects. Fail otherwise.
DrupalTestCase::assertEqual function Will trigger a pass if the two parameters have the same value only. Otherwise a fail.
DrupalTestCase::assertError function Confirms that an error has occurred and optionally that the error text matches exactly.
DrupalTestCase::assertErrorPattern function Confirms that an error has occurred and that the error text matches a Perl regular expression.
DrupalTestCase::assertIdentical function Will trigger a pass if the two parameters have the same value and same type. Otherwise a fail.
DrupalTestCase::assertIsA function Type and class test. Will pass if class matches the type name or is a subclass or if not an object, but the type is correct.
DrupalTestCase::assertNoErrors function Confirms that no errors have occurred so far in the test method.
DrupalTestCase::assertNotA function Type and class mismatch test. Will pass if class name or underling type does not match the one specified.
DrupalTestCase::assertNotEqual function Will trigger a pass if the two parameters have a different value. Otherwise a fail.
DrupalTestCase::assertNotIdentical function Will trigger a pass if the two parameters have the different value or different type.
DrupalTestCase::assertNotNull function Will be true if the value is set.
DrupalTestCase::assertNoUnwantedPattern function Will trigger a pass if the Perl regex pattern is not present in subject. Fail if found.
DrupalTestCase::assertNoUnwantedRaw function Will trigger a pass if the raw text is NOT found on the loaded page Fail otherwise.
DrupalTestCase::assertNull function Will be true if the value is null.
DrupalTestCase::assertReference function Will trigger a pass if both parameters refer to the same object. Fail otherwise.
DrupalTestCase::assertWantedPattern function Will trigger a pass if the Perl regex pattern is found in the subject. Fail otherwise.
DrupalTestCase::assertWantedRaw function Will trigger a pass if the raw text is found on the loaded page Fail otherwise.
DrupalTestCase::clickLink function Follows a link by name. Will click the first link found with this link text by default, or a later one if an index is given. Match is case insensitive with normalised space. Does make assertations if the click was sucessful or not and it does…
DrupalTestCase::drupalCheckAuth function @abstract Checks to see if we need to send a http-auth header to authenticate when browsing a site.
DrupalTestCase::drupalCreateRolePerm function Create a role / perm combination specified by permissions
DrupalTestCase::drupalCreateUserRolePerm function Creates a user / role / permissions combination specified by permissions
DrupalTestCase::drupalGet function @abstract Brokder for the get function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::drupalGetContent function @TODO: needs documentation
DrupalTestCase::drupalLoginUser function Logs in a user with the internal browser
DrupalTestCase::drupalModuleDisable function Disables a drupal module
DrupalTestCase::drupalModuleEnable function Enables a drupal module
DrupalTestCase::drupalPostRequest function Do a post request on a drupal page. It will be done as usual post request with SimpleBrowser
DrupalTestCase::drupalRawPost function @abstract Broker for the post function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::DrupalTestCase function
DrupalTestCase::drupalVariableSet function Set a druapl variable and keep track of the changes for tearDown()
DrupalTestCase::randomName function Generates a random string, to be used as name or whatever
DrupalTestCase::run function Just some info for the reporter