You are here

function ActivityAPITest::testGetActivity in Activity 5.2

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.

File

tests/ActivityAPITests.test, line 97

Class

ActivityAPITest

Code

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