Index: includes/tests/session.test =================================================================== RCS file: includes/tests/session.test diff -N includes/tests/session.test --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ includes/tests/session.test 7 Aug 2008 20:43:15 -0000 @@ -0,0 +1,98 @@ + t('Session Tests'), + 'description' => t('Drupal session handling tests.'), + 'group' => t('Session') + ); + } + + /** + * Implementation of setUp(). + */ + function setUp() { + parent::setUp(); + } + + /** + * Implementation of tearDown(). + */ + function tearDown() { + parent::tearDown(); + } + + /** + * Test for sess_count(). + */ + function testSessCount() { + $this->session_count = db_result(db_query("SELECT COUNT(sid) AS count FROM {sessions}")); + // create a bunch of sessions for testing + $this->generateSessionData(); + + // test absolute count + $anonymous = sess_count(0, TRUE); + $authenticated = sess_count(0, FALSE); + $this->assertEqual($anonymous + $authenticated, $this->session_count, t('Correctly counted @count total sessions.', array('@count' => $anonymous + $authenticated))); + + // test anonymous count + $this->assertEqual($anonymous, $this->session_count_anonymous, t('Correctly counted @count anonymous sessions.', array('@count' => $anonymous))); + + // test authenticated count + $this->assertEqual($authenticated, $this->session_count_authenticated, t('Correctly counted @count authenticated sessions.', array('@count' => $authenticated))); + + // should return 0 sessions from 1 second from now + $this->assertEqual(sess_count(time() + 1), 0, t('Correctly returned 0 sessions newer than the current time.')); + } + + + /** + * Tests for session_save_session(). + */ + function testSessionSaveSession() { + $this->assertTrue(session_save_session(), t('session_save_session() correctly returns TRUE when initially primed with a NULL argument.')); + $this->assertFalse(session_save_session(FALSE), t('session_save_session() correctly returns FALSE when disabling.')); + $this->assertFalse(session_save_session(), t('session_save_session() correctly returns FALSE when saving has been disabled.')); + $this->assertTrue(session_save_session(TRUE), t('session_save_session() correctly returns TRUE when enabling.')); + $this->assertTrue(session_save_session(), t('session_save_session() correctly returns TRUE when saving has been enabled.')); + } + + /** + * Create session data for testing. + */ + function generateSessionData() { + // how many sessions to generate + $count = rand(20, 50); + $this->session_count += $count; + + // an array of created users + $this->generated_sessions = array(); + // starting uid + $current_uid = 15; + for ($i = 0; $i < $count; $i++) { + // random time between now and the last 10 hours + $time = time() - rand(0, 3600*10); + $sid = md5($this->randomName()); + $uid = rand(0, 1) ? 0 : $current_uid++; + + if ($uid == 0) { + $this->session_count_anonymous ++; + } + else { + $this->session_count_authenticated ++; + } + + db_query("INSERT INTO {sessions} (uid, sid, hostname, timestamp, cache, session) VALUES (%d, '%s', '%s', %d, %d, '%s')", $uid, $sid, ip_address(), $time, 0, $this->randomName()); + } + } +}