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();
$this->beginning_aid = db_result(db_query("SELECT MAX(aid) FROM {activity}"));
$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');
if ($this
->assertTrue(function_exists('activity_get_info'), '%s ' . t('Asserting that activity_get_info exists'))) {
$info = activity_get_info();
$this
->assertTrue(is_array($info), '%s ' . t('Assrting that return value is an array.'));
$this
->assertTrue(in_array('nodeactivity', array_keys($info)), '%s ' . t('Asserting that nodeactivity is one of the returned values.'));
}
}
function testInsertActivity() {
$tokens = array(
'dummydata' => 'foobar',
);
$module = 'simpletest';
$type = 'testing';
$action = 'insert';
$uid = 4711;
$aid = activity_insert($module, $type, $action, $tokens, $uid);
$this
->assertTrue(is_numeric($aid), '%s ' . t('activity_insert is supposed to return a numeric id'));
$this
->assertEqual($aid, $this->sequences_id + 1, '%s ' . t('the insert created an aid that is one greater than the previous.'));
$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'));
global $user;
$global_uid = $user->uid;
$aid2 = activity_insert($module, $type, $action, serialize($tokens));
$this
->assertTrue(is_numeric($aid2), '%s ' . t('activity_insert is supposed to return a numeric id'));
$this
->assertEqual($aid2, $this->sequences_id + 2, '%s ' . t('the insert created an aid that is two greater than the beginning.'));
$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,
)));
}
function testGetActivity() {
$this
->assertTrue(function_exists('activity_get_activity'));
$tester = $this
->drupalCreateUserRolePerm();
$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.'));
$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'],
)));
}
$modules = array(
'huba',
'foobar',
'barbaz',
);
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.'));
}
}