Fixed the file chat for all.test2.php CT#191

skala
Arthur Portugal 15 years ago
parent 73ec88119f
commit 7d29d1f3ca
  1. 27
      main/chat/chat_functions.lib.php
  2. 103
      tests/main/chat/chat_functions.lib.test.php

@ -40,7 +40,7 @@ function exit_of_chat ($user_id) {
foreach($list_course as $courses) {
$response=user_connected_in_chat($user_id,$courses['db_name']);
if ($response===true) {
$tbl_chat_connected = Database::get_course_chat_connected_table($courses['db_name']);;
$tbl_chat_connected = Database::get_course_chat_connected_table($courses['db_name']);
$sql='DELETE FROM '.$tbl_chat_connected.' WHERE user_id='.$user_id.$extra_condition;
Database::query($sql,__FILE__,__LINE__);
}
@ -75,9 +75,15 @@ function user_connected_in_chat ($user_id,$database_name) {
* @param void
* @return void
*/
function disconnect_user_of_chat () {
function disconnect_user_of_chat($database_name='') {
$list_info_user_in_chat = array();
$list_info_user_in_chat = users_list_in_chat ();
if (!empty($database_name)) {
$list_info_user_in_chat = users_list_in_chat($database_name);
} else {
$list_info_user_in_chat = users_list_in_chat();
}
$cd_date = date('Y-m-d',time());
$cdate_h = date('H',time());
@ -85,6 +91,8 @@ function disconnect_user_of_chat () {
$cdate_s = date('s',time());
$cd_count_time_seconds=$cdate_h*3600 + $cdate_m*60 + $cdate_s;
if (is_array($list_info_user_in_chat) && count($list_info_user_in_chat) > 0 ) {
foreach ($list_info_user_in_chat as $list_info_user) {
$date_db_date = date('Y-m-d',strtotime($list_info_user['last_connection']));
$date_db_h = date('H',strtotime($list_info_user['last_connection']));
@ -93,28 +101,33 @@ function disconnect_user_of_chat () {
$date_count_time_seconds=$date_db_h*3600 + $date_db_m*60 + $date_db_s;
if ($cd_date==$date_db_date) {
if (($cd_count_time_seconds - $date_count_time_seconds)>5) {
$tbl_chat_connected = Database::get_course_chat_connected_table();
if (!empty($database_name)) {
$tbl_chat_connected = Database::get_course_chat_connected_table($database_name);
}
$sql='DELETE FROM '.$tbl_chat_connected.' WHERE user_id='.$list_info_user['user_id'];
//return $sql;
Database::query($sql,__FILE__,__LINE__);
}
}
}
}
}
/**
* @param void
* @return array user list in chat
*/
function users_list_in_chat () {
function users_list_in_chat ($database_name = '') {
$list_users_in_chat=array();
$tbl_chat_connected = Database::get_course_chat_connected_table();
$tbl_chat_connected = Database::get_course_chat_connected_table($database_name);
$group_id = intval($_SESSION['id_group']);
$session_id = intval($_SESSION['id_session']);
$extra_condition = '';
if (!empty($group_id)) $extra_condition = " WHERE to_group_id = '$group_id'";
else $extra_condition = api_get_session_condition($session_id,false);
$sql='SELECT user_id,last_connection FROM '.$tbl_chat_connected.$extra_condition;
$result=Database::query($sql,__FILE__,__LINE__);
while ($row = Database::fetch_array($result,'ASSOC')) {

@ -5,34 +5,105 @@ require_once(api_get_path(LIBRARY_PATH).'course.lib.php');
Mock::generate('CourseManager');
class TestChatFunctions extends UnitTestCase {
function testdisconnect_user_of_chat() {
$res = disconnect_user_of_chat();
$this->assertTrue(is_null($res));
//var_dump($res);
public function setUp() {
$this->tcourse = new CourseManager();
}
function testexit_of_chat () {
$docu = new MockCourseManager();
$user_id=1;
$res = exit_of_chat();
$docu->expectOnce(CourseManager::get_courses_list_by_user_id($user_id),'admin');
$this->assertTrue(is_object($docu));
$this->assertTrue(is_null($res));
//var_dump($res);
public function tearDown() {
$this->tcourse = null;
}
function testuser_connected_in_chat () {
global $_configuration;
require_once api_get_path(SYS_PATH).'tests/main/inc/lib/add_course.lib.inc.test.php';
// create a course
$course_datos = array(
'wanted_code'=> 'COD21',
'title'=>'metodologia de calculo diferencial',
'tutor_name'=>'R. J. Wolfagan',
'category_code'=>'2121',
'course_language'=>'english',
'course_admin_id'=>'1211',
'db_prefix'=> $_configuration['db_prefix'].'COD21',
'db_prefix'=> $_configuration['db_prefix'].'COD21',
'firstExpirationDelay'=>'112'
);
$res = create_course($course_datos['wanted_code'], $course_datos['title'],
$course_datos['tutor_name'], $course_datos['category_code'],
$course_datos['course_language'],$course_datos['course_admin_id'],
$course_datos['db_prefix'], $course_datos['firstExpirationDelay']);
if ($res) {
$course_code = 'COD21';
$course_info = api_get_course_info($course_code);
}
$user_id=1;
$database_name='';
$course_info = api_get_course_info($course_code);
$database_name = $course_info['dbName'];
$res = user_connected_in_chat($user_id,$database_name);
$this->assertTrue(is_bool($res));
//var_dump($res);
}
function testusers_list_in_chat () {
$res = users_list_in_chat();
function testUsersListInChat () {
$course_code = 'COD21';
$course_info = api_get_course_info($course_code);
$database_name = $course_info['dbName'];
$res = users_list_in_chat($database_name);
$this->assertTrue(is_array($res));
//var_dump($res);
}
function CreateChatConnection($database_name) {
$session_id = 1;
$tbl_chat_connected = Database::get_course_chat_connected_table($database_name);
$sql = "SELECT user_id FROM $tbl_chat_connected WHERE user_id = 1";
$result = Database::query($sql);
//The user_id exists so we must do an UPDATE and not a INSERT
$current_time = date('Y-m-d H:i:s');
if (Database::num_rows($result)==0) {
$query="INSERT INTO $tbl_chat_connected(user_id,last_connection,session_id) VALUES(1,'$current_time','$session_id')";
} else {
$query="UPDATE $tbl_chat_connected set last_connection='".$current_time."' WHERE user_id=1 AND session_id='$session_id'";
}
Database::query($query,__FILE__,__LINE__);
}
function testExitOfChat () {
$course_code = 'COD21';
$course_info = api_get_course_info($course_code);
$database_name = $course_info['dbName'];
$this->CreateChatConnection($database_name);
$user_id = 1;
$res = exit_of_chat($user_id);
//$resu = $this->tcourse->delete_course($course_code);
$this->assertTrue(is_null($res));
//var_dump($res);
}
function testDisconnectUserOfChat() {
$_SESSION['is_courseAdmin'] = 1;
$course_code = 'COD21';
$course_info = api_get_course_info($course_code);
$database_name = $course_info['dbName'];
$this->CreateChatConnection($database_name);
$res = disconnect_user_of_chat($database_name);
$this->assertTrue(is_null($res));
$resu = $this->tcourse->delete_course($course_code);
}
}
?>

Loading…
Cancel
Save