mirror of
https://github.com/Mibew/mibew.git
synced 2025-04-11 18:30:10 +03:00
1242 lines
35 KiB
PHP
1242 lines
35 KiB
PHP
<?php
|
|
|
|
require_once dirname(__FILE__) . '/../../../../../webim/libs/classes/thread.php';
|
|
require_once dirname(__FILE__) . '/../../../../../webim/libs/classes/database.php';
|
|
require_once dirname(__FILE__) . '/thread_processor_mock.php';
|
|
require_once dirname(__FILE__) . '/../../../../../webim/libs/classes/settings.php';
|
|
require_once dirname(__FILE__) . '/../../../../../webim/libs/common/locale.php';
|
|
require_once dirname(__FILE__) . '/../config.php';
|
|
|
|
/**
|
|
* Test class for Thread.
|
|
* Generated by PHPUnit on 2012-09-07 at 19:04:21.
|
|
*/
|
|
class ThreadTest extends PHPUnit_Framework_TestCase {
|
|
|
|
/**
|
|
* Temporary variable to store thread lifetime
|
|
* @var int
|
|
*/
|
|
protected static $thread_lifetime;
|
|
|
|
/**
|
|
* Get Messages count from database
|
|
*
|
|
* @param int $kind Message kind
|
|
* @param string $message Message text
|
|
* @param int $last_msg_id Message id to start search from
|
|
* @return int Messages count
|
|
*/
|
|
protected function _helper_getMessagesCount($kind, $message, $last_msg_id) {
|
|
$db = Database::getInstance();
|
|
list($count) = $db->query(
|
|
"SELECT COUNT(*) FROM {chatmessage} " .
|
|
"WHERE ikind = :kind AND tmessage = :message AND messageid > :last_msg_id",
|
|
array(
|
|
':kind' => $kind,
|
|
':message' => $message,
|
|
':last_msg_id' => $last_msg_id
|
|
),
|
|
array('return_rows' => Database::RETURN_ONE_ROW, 'fetch_type' => Database::FETCH_NUM)
|
|
);
|
|
return $count;
|
|
}
|
|
|
|
/**
|
|
* Get total message count from database
|
|
*
|
|
* @param int $last_msg_id Message id to start search from
|
|
* @return int Messages count
|
|
*/
|
|
protected function _helper_getTotalMessagesCount($last_msg_id) {
|
|
$db = Database::getInstance();
|
|
list($count) = $db->query(
|
|
"SELECT COUNT(*) FROM {chatmessage} WHERE messageid > :last_msg_id",
|
|
array(':last_msg_id' => $last_msg_id),
|
|
array('return_rows' => Database::RETURN_ONE_ROW, 'fetch_type' => Database::FETCH_NUM)
|
|
);
|
|
return $count;
|
|
}
|
|
|
|
/**
|
|
* Returns last message id
|
|
*
|
|
* @return int Id of the last message
|
|
*/
|
|
protected function _helper_getLastMessageId() {
|
|
$db = Database::getInstance();
|
|
list($last_msg_id) = $db->query(
|
|
"SELECT MAX(messageid) FROM {chatmessage}",
|
|
NULL,
|
|
array('return_rows' => Database::RETURN_ONE_ROW, 'fetch_type' => Database::FETCH_NUM)
|
|
);
|
|
if (! $last_msg_id) {
|
|
$last_msg_id = 0;
|
|
}
|
|
return $last_msg_id;
|
|
}
|
|
|
|
/**
|
|
* Get threads count with specified thread id
|
|
*
|
|
* @param int $thread_id Id of the thread
|
|
* @return int Count of threads
|
|
*/
|
|
protected function _helper_getThreadCount($thread_id) {
|
|
$db = Database::getInstance();
|
|
list($count) = $db->query(
|
|
"SELECT COUNT(*) FROM {chatthread} WHERE threadid = :id",
|
|
array(':id' => $thread_id),
|
|
array('return_rows' => Database::RETURN_ONE_ROW, 'fetch_type' => Database::FETCH_NUM)
|
|
);
|
|
return $count;
|
|
}
|
|
|
|
/**
|
|
* Get thread info from database
|
|
*
|
|
* @param int $thread_id Id of the thread
|
|
* @return array Thread info
|
|
*/
|
|
protected function _helper_getThreadInfo($thread_id) {
|
|
$db = Database::getInstance();
|
|
return $db->query(
|
|
"SELECT * FROM {chatthread} WHERE threadid = :id",
|
|
array(':id' => $thread_id),
|
|
array('return_rows' => Database::RETURN_ONE_ROW)
|
|
);
|
|
}
|
|
|
|
public static function setUpBeforeClass() {
|
|
// Initialize database object
|
|
global $db_host, $db_name, $db_user, $db_pass, $tables_prefix,
|
|
$db_encoding, $force_charset_in_connection, $use_persistent_connection;
|
|
Database::initialize(
|
|
$db_host,
|
|
$db_user,
|
|
$db_pass,
|
|
$use_persistent_connection,
|
|
$db_name,
|
|
$tables_prefix,
|
|
$force_charset_in_connection,
|
|
$db_encoding
|
|
);
|
|
$db = Database::getInstance();
|
|
// Clear tables
|
|
$db->query("TRUNCATE {chatthread}");
|
|
$db->query("TRUNCATE {chatmessage}");
|
|
self::$thread_lifetime = Settings::get('thread_lifetime');
|
|
Settings::set('thread_lifetime', 600);
|
|
}
|
|
|
|
public static function tearDownAfterClass() {
|
|
$db = Database::getInstance();
|
|
// Clear tables
|
|
$db->query("TRUNCATE {chatthread}");
|
|
$db->query("TRUNCATE {chatmessage}");
|
|
Settings::set('thread_lifetime', self::$thread_lifetime);
|
|
// Destroy Database object
|
|
Database::destroy();
|
|
}
|
|
|
|
public function testCreate() {
|
|
// Create thread
|
|
$thread = Thread::create();
|
|
|
|
// Check thread
|
|
$this->assertNotEmpty($thread);
|
|
$this->assertNotEmpty($thread->id);
|
|
$this->assertNotEmpty($thread->lastToken);
|
|
|
|
// Check if thread in database
|
|
$this->assertEquals(1, $this->_helper_getThreadCount($thread->id));
|
|
|
|
$threadid = $thread->id;
|
|
unset($thread);
|
|
return $threadid;
|
|
}
|
|
|
|
/**
|
|
* @depends testCreate
|
|
*/
|
|
public function testLoad($threadid) {
|
|
// Load thread
|
|
$thread = Thread::load($threadid);
|
|
|
|
// Check thread
|
|
$this->assertNotEmpty($thread);
|
|
$this->assertEquals($threadid, $thread->id);
|
|
|
|
return $thread;
|
|
}
|
|
|
|
/**
|
|
* @depends testLoad
|
|
*/
|
|
public function testDelete(Thread $thread) {
|
|
$threadid = $thread->id;
|
|
|
|
// Check if thread in database
|
|
$this->assertEquals(1, $this->_helper_getThreadCount($threadid));
|
|
|
|
// Delete thread
|
|
$thread->delete();
|
|
unset($thread);
|
|
|
|
// Check thread in database
|
|
$this->assertEquals(0, $this->_helper_getThreadCount($threadid));
|
|
}
|
|
|
|
public function testCreateFromDbInfo() {
|
|
// Check incomplete fields list
|
|
$fields_list = array(
|
|
'threadid' => 1
|
|
);
|
|
$thread = Thread::createFromDbInfo($fields_list);
|
|
$this->assertFalse($thread);
|
|
|
|
// Check complete fields list
|
|
$fields_list = array(
|
|
'threadid' => 10,
|
|
|
|
'lrevision' => 189,
|
|
'istate' => Thread::STATE_QUEUE,
|
|
'ltoken' => 19908,
|
|
|
|
'nextagent' => 0,
|
|
'groupid' => 0,
|
|
|
|
'shownmessageid' => 0,
|
|
'messageCount' => 0,
|
|
|
|
'dtmcreated' => time() - 100,
|
|
'dtmmodified' => time() - 90,
|
|
'dtmchatstarted' => 0,
|
|
|
|
'agentId' => 0,
|
|
'agentName' => '',
|
|
'agentTyping' => 0,
|
|
'lastpingagent' => 0,
|
|
|
|
'locale' => 'en',
|
|
|
|
'userid' => 1112,
|
|
'userName' => 'Guest',
|
|
'userTyping' => 0,
|
|
'lastpinguser' => time() - 10,
|
|
|
|
'remote' => '127.0.0.1',
|
|
'referer' => 'http://google.com',
|
|
'userAgent' => 'Mozilla FireFox'
|
|
);
|
|
$thread = Thread::createFromDbInfo($fields_list);
|
|
$this->assertInstanceOf('Thread', $thread);
|
|
|
|
unset($thread);
|
|
}
|
|
|
|
public function test__isset() {
|
|
// Create new thread
|
|
$thread = Thread::create();
|
|
|
|
// Property exists and not empty
|
|
$this->assertNotEmpty($thread->id);
|
|
|
|
// assertTrue use instead of assertEmpty because of PHPUnit don't work correctly with __isset magic method
|
|
|
|
// Property exists in internal Thread::$propertyMap property but not set
|
|
$this->assertTrue(empty($thread->lastRevision));
|
|
$this->assertFalse(isset($thread->lastRevision));
|
|
|
|
// Property does not exists
|
|
$this->assertTrue(empty($thread->someFakeProp));
|
|
|
|
// Delete thread
|
|
$thread->delete();
|
|
unset($thread);
|
|
}
|
|
|
|
public function test__set() {
|
|
// Create thread
|
|
$thread = Thread::create();
|
|
|
|
// Try to set value for unexistent property
|
|
// Following code wait for trigger user notice, which converts by PHPUnit to an
|
|
// Exception
|
|
try {
|
|
$thread->someFakeField = 'some_test_value';
|
|
$this->fail("Exception must be thrown");
|
|
} catch(Exception $e) {}
|
|
|
|
// Try to set exist property
|
|
$thread->lastToken = 129;
|
|
|
|
return $thread;
|
|
}
|
|
|
|
/**
|
|
* @depends test__set
|
|
*/
|
|
public function test__get(Thread $thread) {
|
|
// Check property value from test_set() method
|
|
$this->assertEquals(129, $thread->lastToken);
|
|
|
|
// Try to get value of unexistent property
|
|
// Following code wait for trigger user notice, which converts by PHPUnit to an
|
|
// Exception
|
|
try {
|
|
$some_value = $thread->someFakeField;
|
|
$this->fail("Exception must be thrown");
|
|
} catch(Exception $e) {}
|
|
|
|
// Delete thread
|
|
$thread->delete();
|
|
unset($thread);
|
|
}
|
|
|
|
public function testSave() {
|
|
// Create new thread
|
|
$thread = Thread::create();
|
|
|
|
// Update thread values
|
|
$thread->state = $state = Thread::STATE_CHATTING;
|
|
$thread->lastToken = $last_token = 11;
|
|
|
|
$thread->nextAgent = $next_agent = 12;
|
|
$thread->groupId = $group_id = 13;
|
|
|
|
$thread->shownMessageId = $shown_message_id = 14;
|
|
$thread->messageCount = $message_count = 15;
|
|
|
|
$thread->created = $created = time() - 200;
|
|
$thread->chatStarted = $chat_started = time() - 180;
|
|
|
|
$thread->agentId = $agent_id = 16;
|
|
$thread->agentName = $agent_name = '17';
|
|
$thread->agentTyping = $agent_typing = 0;
|
|
$thread->lastPingAgent = $last_ping_agent = time() - 170;
|
|
|
|
$thread->locale = $locale = '18';
|
|
|
|
$thread->userId = $user_id = 19;
|
|
$thread->userName = $user_name = '20';
|
|
$thread->userTyping = $user_tiping = 1;
|
|
$thread->lastPingUser = $last_user_ping = time() - 160;
|
|
|
|
$thread->remote = $remote = '21';
|
|
$thread->referer = $referer = '22';
|
|
$thread->userAgent = $user_agent = '23';
|
|
|
|
|
|
// Save thread
|
|
$thread->save();
|
|
|
|
// Load thread info from database
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
|
|
// Check values
|
|
$this->assertEquals($thread_info['istate'], $state);
|
|
$this->assertEquals($thread_info['ltoken'], $last_token);
|
|
|
|
$this->assertEquals($thread_info['nextagent'], $next_agent);
|
|
$this->assertEquals($thread_info['groupid'], $group_id);
|
|
|
|
$this->assertEquals($thread_info['shownmessageid'], $shown_message_id);
|
|
$this->assertEquals($thread_info['messageCount'], $message_count);
|
|
|
|
$this->assertEquals($thread_info['dtmcreated'], $created);
|
|
$this->assertEquals($thread_info['dtmchatstarted'], $chat_started);
|
|
|
|
$this->assertEquals($thread_info['agentId'], $agent_id);
|
|
$this->assertEquals($thread_info['agentName'], $agent_name);
|
|
$this->assertEquals($thread_info['agentTyping'], $agent_typing);
|
|
$this->assertEquals($thread_info['lastpingagent'], $last_ping_agent);
|
|
|
|
$this->assertEquals($thread_info['locale'], $locale);
|
|
|
|
$this->assertEquals($thread_info['userid'], $user_id);
|
|
$this->assertEquals($thread_info['userName'], $user_name);
|
|
$this->assertEquals($thread_info['userTyping'], $user_tiping);
|
|
$this->assertEquals($thread_info['lastpinguser'], $last_user_ping);
|
|
|
|
$this->assertEquals($thread_info['remote'], $remote);
|
|
$this->assertEquals($thread_info['referer'], $referer);
|
|
$this->assertEquals($thread_info['userAgent'], $user_agent);
|
|
|
|
|
|
// Delete thread
|
|
$thread->delete();
|
|
unset($thread);
|
|
}
|
|
|
|
public function testReopen() {
|
|
global $home_locale;
|
|
|
|
$db = Database::getInstance();
|
|
|
|
// Create new thread
|
|
$thread = Thread::create();
|
|
$thread->state = Thread::STATE_CHATTING;
|
|
$thread->locale = $home_locale;
|
|
$thread->lastPingAgent = time() - Settings::get('thread_lifetime') * 2;
|
|
$thread->lastPingUser = time() - Settings::get('thread_lifetime') * 2;
|
|
$thread->save();
|
|
|
|
|
|
// Last ping was more than life_time ago
|
|
$this->assertFalse(Thread::reopen($thread->id));
|
|
|
|
|
|
// Try reopen closed thread
|
|
$thread->state = Thread::STATE_CLOSED;
|
|
$thread->lastPingAgent = time();
|
|
$thread->lastPingUser = time();
|
|
$thread->save();
|
|
$this->assertFalse(Thread::reopen($thread->id));
|
|
|
|
|
|
// Try to reopen left thread
|
|
$thread->state = Thread::STATE_LEFT;
|
|
$thread->save();
|
|
$this->assertFalse(Thread::reopen($thread->id));
|
|
|
|
|
|
// Get last message id
|
|
$last_msg_id = $this->_helper_getLastMessageId();
|
|
|
|
|
|
// Try to reopen thread with Thread::STATE_WAITING state
|
|
$thread->nextAgent = 2;
|
|
$thread->state = Thread::STATE_WAITING;
|
|
$thread->save();
|
|
|
|
$another_thread = Thread::reopen($thread->id);
|
|
$this->assertNotEmpty($another_thread);
|
|
|
|
// Load another thread info
|
|
$thread_info = $this->_helper_getThreadInfo($another_thread->id);
|
|
// Check next agent field
|
|
$this->assertEquals(0, $another_thread->nextAgent);
|
|
$this->assertEquals(0, $thread_info['nextagent']);
|
|
|
|
// Check sent messages
|
|
$message_count = $this->_helper_getTotalMessagesCount($last_msg_id);
|
|
$this->assertEquals(1, $message_count);
|
|
|
|
// Check message text
|
|
$message_count = $this->_helper_getMessagesCount(
|
|
Thread::KIND_EVENTS,
|
|
getstring_("chat.status.user.reopenedthread", $another_thread->locale),
|
|
$last_msg_id
|
|
);
|
|
$this->assertEquals(1, $message_count);
|
|
|
|
unset($another_thread);
|
|
|
|
|
|
// Try to reopen thread with Thread::STATE_QUEUE state
|
|
$thread->nextAgent = 2;
|
|
$thread->state = Thread::STATE_QUEUE;
|
|
$thread->save();
|
|
|
|
$another_thread = Thread::reopen($thread->id);
|
|
$this->assertNotEmpty($another_thread);
|
|
|
|
// Load another thread info
|
|
$thread_info = $this->_helper_getThreadInfo($another_thread->id);
|
|
// Check next agent field
|
|
$this->assertEquals(2, $another_thread->nextAgent);
|
|
$this->assertEquals(2, $thread_info['nextagent']);
|
|
|
|
unset($another_thread);
|
|
|
|
// Check equlity of treads returned by reopen method and returned by load method
|
|
$thread_id = $thread->id;
|
|
unset($thread);
|
|
$thread = Thread::load($thread_id);
|
|
$another_thread = Thread::reopen($thread_id);
|
|
$this->assertEquals($thread, $another_thread);
|
|
unset($another_thread);
|
|
|
|
// Delete thread
|
|
$thread->delete();
|
|
unset($thread);
|
|
}
|
|
|
|
public function testCloseOldThreads() {
|
|
$timeout = Settings::get('thread_lifetime');
|
|
|
|
// Create new thread
|
|
$thread = Thread::create();
|
|
|
|
|
|
// Try to close thread with state = Thread::STATE_CHATTING and time no timeout
|
|
// Update values
|
|
$thread->state = Thread::STATE_CHATTING;
|
|
$thread->lastPingAgent = time();
|
|
$thread->lastPingUser = time();
|
|
// Save thread
|
|
$thread->save();
|
|
// And close all threads
|
|
Thread::closeOldThreads();
|
|
|
|
// Load thread info from database
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check thread state
|
|
$this->assertNotEquals($thread_info['istate'], Thread::STATE_CLOSED);
|
|
|
|
|
|
// Try to close thread with timeout for user and no timeout for agent
|
|
// Update values
|
|
$thread->state = Thread::STATE_CHATTING;
|
|
$thread->lastPingAgent = time();
|
|
$thread->lastPingUser = time() - $timeout * 2;
|
|
// Save thread
|
|
$thread->save();
|
|
// And close all threads
|
|
Thread::closeOldThreads();
|
|
|
|
// Load thread info from database
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check thread state
|
|
$this->assertNotEquals($thread_info['istate'], Thread::STATE_CLOSED);
|
|
|
|
|
|
// Try to close thread with timeout for agent and no timeout for user
|
|
// Update values
|
|
$thread->state = Thread::STATE_CHATTING;
|
|
$thread->lastPingAgent = time() - $timeout * 2;
|
|
$thread->lastPingUser = time();
|
|
// Save thread
|
|
$thread->save();
|
|
// And close all threads
|
|
Thread::closeOldThreads();
|
|
|
|
// Load thread info from database
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check thread state
|
|
$this->assertNotEquals($thread_info['istate'], Thread::STATE_CLOSED);
|
|
|
|
|
|
// Try to close thread with timeout for agent and timeout for user
|
|
// Update values
|
|
$thread->state = Thread::STATE_CHATTING;
|
|
$thread->lastPingAgent = time() - $timeout * 2;
|
|
$thread->lastPingUser = time() - $timeout * 2;
|
|
// Save thread
|
|
$thread->save();
|
|
// And close all threads
|
|
Thread::closeOldThreads();
|
|
|
|
// Load thread info from database
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check thread state
|
|
$this->assertEquals($thread_info['istate'], Thread::STATE_CLOSED);
|
|
|
|
|
|
// Try to close thread with timeout for user and not started for agent(was no agent ping yet)
|
|
// Update values
|
|
$thread->state = Thread::STATE_CHATTING;
|
|
$thread->lastPingAgent = 0;
|
|
$thread->lastPingUser = time() - $timeout * 2;
|
|
// Save thread
|
|
$thread->save();
|
|
// And close all threads
|
|
Thread::closeOldThreads();
|
|
|
|
// Load thread info from database
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check thread state
|
|
$this->assertEquals($thread_info['istate'], Thread::STATE_CLOSED);
|
|
|
|
|
|
// Delete thread
|
|
$thread->delete();
|
|
unset($thread);
|
|
}
|
|
|
|
public function testPostMessage() {
|
|
$db = Database::getInstance();
|
|
|
|
// Create new thread
|
|
$thread = Thread::create();
|
|
|
|
// Set values
|
|
$message = array(
|
|
'threadid' => $thread->id,
|
|
'ikind' => Thread::KIND_INFO,
|
|
'agentId' => 12,
|
|
'tmessage' => 'New message text',
|
|
'dtmcreated' => time(),
|
|
'tname' => 'Sender name'
|
|
);
|
|
|
|
$message['messageid'] = $thread->postMessage(
|
|
$message['ikind'],
|
|
$message['tmessage'],
|
|
$message['tname'],
|
|
$message['agentId'],
|
|
$message['dtmcreated']
|
|
);
|
|
// Load message info from database
|
|
$msg_info = $db->query(
|
|
"SELECT * FROM {chatmessage} WHERE messageid = ?",
|
|
array($message['messageid']),
|
|
array('return_rows' => Database::RETURN_ONE_ROW)
|
|
);
|
|
// Check values
|
|
$this->assertEquals($message, $msg_info);
|
|
|
|
// Delete thread
|
|
$thread->delete();
|
|
unset($thread);
|
|
|
|
return $message['messageid'];
|
|
}
|
|
|
|
/**
|
|
* @depends testPostMessage
|
|
*/
|
|
public function testGetMessages($msg_id) {
|
|
// Create new thread
|
|
$thread = Thread::create();
|
|
|
|
// Create messages
|
|
// The first
|
|
$first_message = array(
|
|
'kind' => Thread::KIND_USER,
|
|
'message' => 'The first message',
|
|
'created' => time(),
|
|
'name' => 'System message only for agent'
|
|
);
|
|
// The second
|
|
$second_message = array(
|
|
'kind' => Thread::KIND_AGENT,
|
|
'message' => 'The second message',
|
|
'created' => time(),
|
|
'name' => 'User'
|
|
);
|
|
// The third
|
|
$third_message = array(
|
|
'kind' => Thread::KIND_FOR_AGENT,
|
|
'message' => 'The third message',
|
|
'created' => time(),
|
|
'name' => 'Agent'
|
|
);
|
|
|
|
// Send messages
|
|
// The first
|
|
$first_message['id'] = $thread->postMessage(
|
|
$first_message['kind'],
|
|
$first_message['message'],
|
|
$first_message['name'],
|
|
12,
|
|
$first_message['created']
|
|
);
|
|
// The second
|
|
$second_message['id'] = $thread->postMessage(
|
|
$second_message['kind'],
|
|
$second_message['message'],
|
|
$second_message['name'],
|
|
14,
|
|
$second_message['created']
|
|
);
|
|
// The third
|
|
$third_message['id'] = $thread->postMessage(
|
|
$third_message['kind'],
|
|
$third_message['message'],
|
|
$third_message['name'],
|
|
16,
|
|
$third_message['created']
|
|
);
|
|
|
|
// Check messages for agent with ids starts from $msg_id
|
|
$last_id = $msg_id;
|
|
$this->assertEquals(
|
|
array($first_message, $second_message, $third_message),
|
|
$thread->getMessages(false, $last_id)
|
|
);
|
|
// Check last message id
|
|
$this->assertEquals($third_message['id'], $last_id);
|
|
|
|
// Check messages for user with ids starts from $msg_id
|
|
$last_id = $msg_id;
|
|
$this->assertEquals(
|
|
array($first_message, $second_message),
|
|
$thread->getMessages(true, $last_id)
|
|
);
|
|
// Check last message id
|
|
$this->assertEquals($second_message['id'], $last_id);
|
|
|
|
// Check messages for agent with ids starts from first message's id
|
|
$last_id = $first_message['id'];
|
|
$this->assertEquals(
|
|
array($second_message, $third_message),
|
|
$thread->getMessages(false, $last_id)
|
|
);
|
|
// Check last message id
|
|
$this->assertEquals($third_message['id'], $last_id);
|
|
|
|
// Delete thread
|
|
$thread->delete();
|
|
unset($thread);
|
|
}
|
|
|
|
public function testRenameUser() {
|
|
// Create new thread
|
|
$thread = Thread::create();
|
|
// Set user initial name
|
|
$thread->userName = 'User name';
|
|
$thread->locale = 'en';
|
|
$thread->save();
|
|
|
|
// Rename user
|
|
$new_user_name = 'New user name';
|
|
$thread->renameUser($new_user_name);
|
|
|
|
// Load thread info from database
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check user name
|
|
$this->assertEquals($new_user_name, $thread_info['userName']);
|
|
|
|
// Delete thread
|
|
$thread->delete();
|
|
unset($thread);
|
|
}
|
|
|
|
public function testCheckForReassign() {
|
|
global $home_locale;
|
|
|
|
// Create new thread
|
|
$thread = Thread::create();
|
|
$thread->state = Thread::STATE_CHATTING;
|
|
$thread->locale = $home_locale;
|
|
$thread->agentName = "First name";
|
|
$thread->agentId = 1;
|
|
$thread->nextAgent = 0;
|
|
$thread->save();
|
|
|
|
// Create operator
|
|
$operator = array(
|
|
'operatorid' => 2,
|
|
'vclocalename' => 'Second local name',
|
|
'vccommonname' => 'Second common name',
|
|
'vcavatar' => 'avatar'
|
|
);
|
|
|
|
// Chat in progress. No reassign expected
|
|
$thread->checkForReassign($operator);
|
|
// Load thread info from database
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
$this->assertEquals(Thread::STATE_CHATTING, $thread_info['istate']);
|
|
$this->assertEquals(Thread::STATE_CHATTING, $thread->state);
|
|
$this->assertEquals(1, $thread_info['agentId']);
|
|
|
|
|
|
// User waiting for another agent
|
|
$thread->nextAgent = 5;
|
|
$thread->state = Thread::STATE_WAITING;
|
|
$thread->save();
|
|
$thread->checkForReassign($operator);
|
|
// Load thread info from database
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
$this->assertEquals(Thread::STATE_WAITING, $thread_info['istate']);
|
|
$this->assertEquals(Thread::STATE_WAITING, $thread->state);
|
|
|
|
|
|
// Get last message id
|
|
$last_msg_id = $this->_helper_getLastMessageId();
|
|
|
|
// User waiting for another agent, but last agent checks for reassign
|
|
$thread->nextAgent = 5;
|
|
$thread->agentId = 2;
|
|
$thread->state = Thread::STATE_WAITING;
|
|
$thread->save();
|
|
$thread->checkForReassign($operator);
|
|
|
|
// Load thread info from database
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check state
|
|
$this->assertEquals(Thread::STATE_CHATTING, $thread_info['istate']);
|
|
$this->assertEquals(Thread::STATE_CHATTING, $thread->state);
|
|
|
|
// Check sent message
|
|
$this->assertEquals(1, $this->_helper_getTotalMessagesCount($last_msg_id));
|
|
|
|
// Check messages text:
|
|
// Thread::KIND_EVENTS message
|
|
$message_count = $this->_helper_getMessagesCount(
|
|
Thread::KIND_EVENTS,
|
|
getstring2_("chat.status.operator.returned", array($operator['vclocalename']), $thread->locale),
|
|
$last_msg_id
|
|
);
|
|
$this->assertEquals(1, $message_count);
|
|
|
|
// Update last message id
|
|
$last_msg_id = $this->_helper_getLastMessageId();
|
|
|
|
|
|
// User waiting for this agent.
|
|
// It differs from previous test only in sent message text
|
|
$thread->nextAgent = 2;
|
|
$thread->agentId = 1;
|
|
$thread->agentName = 'First agent';
|
|
$thread->state = Thread::STATE_WAITING;
|
|
$thread->save();
|
|
$thread->checkForReassign($operator);
|
|
|
|
// Check message text
|
|
$message_count = $this->_helper_getMessagesCount(
|
|
Thread::KIND_EVENTS,
|
|
getstring2_(
|
|
"chat.status.operator.changed",
|
|
array($operator['vclocalename'], 'First agent'),
|
|
$thread->locale
|
|
),
|
|
$last_msg_id
|
|
);
|
|
$this->assertEquals(1, $message_count);
|
|
|
|
// Delete thread
|
|
$thread->delete();
|
|
unset($thread);
|
|
}
|
|
|
|
public function testClose() {
|
|
global $home_locale;
|
|
|
|
// Get last message id
|
|
$last_msg_id = $this->_helper_getLastMessageId();
|
|
|
|
// Create new thread
|
|
$thread = Thread::create();
|
|
$thread->state = Thread::STATE_CHATTING;
|
|
$thread->userName = 'User';
|
|
$thread->agentName = 'Agent';
|
|
$thread->locale = $home_locale;
|
|
$thread->save();
|
|
|
|
|
|
// Check close thread by user
|
|
$thread->close(true);
|
|
|
|
// Load thread info from database
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check state
|
|
$this->assertEquals(Thread::STATE_CLOSED, $thread_info['istate']);
|
|
$this->assertEquals(Thread::STATE_CLOSED, $thread->state);
|
|
|
|
// Check messages count
|
|
$this->assertEquals(0, $thread_info['messageCount']);
|
|
|
|
// Check sent messages
|
|
$this->assertEquals(1, $this->_helper_getTotalMessagesCount($last_msg_id));
|
|
|
|
// Check message text
|
|
$message_count = $this->_helper_getMessagesCount(
|
|
Thread::KIND_EVENTS,
|
|
getstring2_("chat.status.user.left", array($thread->userName), $thread->locale),
|
|
$last_msg_id
|
|
);
|
|
$this->assertEquals(1, $message_count);
|
|
|
|
|
|
// Get last message id
|
|
$last_msg_id = $this->_helper_getLastMessageId();
|
|
|
|
|
|
// Check close thread by agent.
|
|
// It differs from previous test only in message text
|
|
$thread->state = Thread::STATE_CHATTING;
|
|
$thread->save();
|
|
|
|
$thread->close(false);
|
|
|
|
// Check message text
|
|
$message_count = $this->_helper_getMessagesCount(
|
|
Thread::KIND_EVENTS,
|
|
getstring2_("chat.status.operator.left", array($thread->agentName), $thread->locale),
|
|
$last_msg_id
|
|
);
|
|
$this->assertEquals(1, $message_count);
|
|
|
|
// Check close thread with user's and agent's messages
|
|
$thread->state = Thread::STATE_CHATTING;
|
|
$thread->messageCount = 0;
|
|
$thread->save();
|
|
|
|
// Send messages
|
|
$thread->postMessage(Thread::KIND_USER, "Test message");
|
|
$thread->postMessage(Thread::KIND_USER, "Next test message");
|
|
$thread->postMessage(Thread::KIND_AGENT, "Test message");
|
|
$thread->postMessage(Thread::KIND_EVENTS, "Test message");
|
|
|
|
$thread->close(true);
|
|
|
|
// Load thread info from database
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
|
|
// Check messages count
|
|
$this->assertEquals(2, $thread_info['messageCount']);
|
|
|
|
// Delete thread
|
|
$thread->delete();
|
|
unset($thread);
|
|
}
|
|
|
|
public function testPing() {
|
|
global $home_locale;
|
|
|
|
// Get last message id
|
|
$last_msg_id = $this->_helper_getLastMessageId();
|
|
|
|
// Create new thread
|
|
$thread = Thread::create();
|
|
$thread->state = Thread::STATE_LOADING;
|
|
$thread->lastPingAgent = 0;
|
|
$thread->lastPingUser = 0;
|
|
$thread->locale = $home_locale;
|
|
$thread->save();
|
|
|
|
|
|
// Check first user ping
|
|
$thread->ping(true, false);
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check thread state
|
|
$this->assertEquals(Thread::STATE_QUEUE, $thread->state);
|
|
$this->assertEquals(Thread::STATE_QUEUE, $thread_info['istate']);
|
|
|
|
|
|
// Check not first ping user without any connection problems at the other side
|
|
// Update thread info
|
|
$thread->lastPingAgent = time();
|
|
$thread->lastPingUser = 0;
|
|
$thread->state = Thread::STATE_CHATTING;
|
|
$thread->save();
|
|
// Ping the thread
|
|
$thread->ping(true, false);
|
|
// Get thread info
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check no message sent
|
|
$this->assertEquals($last_msg_id, $this->_helper_getLastMessageId());
|
|
// Check last ping time updated for user
|
|
$this->assertLessThan($thread->lastPingUser, 0);
|
|
$this->assertLessThan($thread_info['lastpinguser'], 0);
|
|
// Check thread state not changed
|
|
$this->assertEquals(Thread::STATE_CHATTING, $thread->state);
|
|
$this->assertEquals(Thread::STATE_CHATTING, $thread_info['istate']);
|
|
|
|
|
|
// Check ping agent without any connection problems at the other side
|
|
// Update thread info
|
|
$thread->lastPingAgent = 0;
|
|
$thread->save();
|
|
// Ping the thread
|
|
$thread->ping(false, false);
|
|
// Get thread info
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check no message sent
|
|
$this->assertEquals($last_msg_id, $this->_helper_getLastMessageId());
|
|
// Check last ping time updated for user
|
|
$this->assertLessThan($thread->lastPingAgent, 0);
|
|
$this->assertLessThan($thread_info['lastpingagent'], 0);
|
|
// Check thread state not changed
|
|
$this->assertEquals(Thread::STATE_CHATTING, $thread->state);
|
|
$this->assertEquals(Thread::STATE_CHATTING, $thread_info['istate']);
|
|
|
|
|
|
// Check ping user with connection problem at the other side and thread state equals to
|
|
// Thread::STATE_WAITING
|
|
// Update thread info
|
|
$thread->lastPingAgent = time() - Thread::CONNECTION_TIMEOUT * 2;
|
|
$thread->state = Thread::STATE_WAITING;
|
|
$thread->save();
|
|
// Ping the thread
|
|
$thread->ping(true, false);
|
|
// Get thread info
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check no message sent
|
|
$this->assertEquals($last_msg_id, $this->_helper_getLastMessageId());
|
|
// Check last ping agent
|
|
$this->assertEquals(0, $thread->lastPingAgent);
|
|
$this->assertEquals(0, $thread_info['lastpingagent']);
|
|
|
|
|
|
// Check ping user with connection problem at the other side and thread state equals to Thread::STATE_CHATTING.
|
|
// In this case message must be sent and state must be changed
|
|
// Update thread info
|
|
$thread->lastPingAgent = time() - Thread::CONNECTION_TIMEOUT * 2;
|
|
$thread->state = Thread::STATE_CHATTING;
|
|
$thread->nextAgent = 1;
|
|
$thread->save();
|
|
// Ping the thread
|
|
$thread->ping(true, false);
|
|
// Get thread info
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check message sent
|
|
$this->assertEquals(1, $this->_helper_getTotalMessagesCount($last_msg_id));
|
|
// Check messsage text
|
|
$message_count = $this->_helper_getMessagesCount(
|
|
Thread::KIND_CONN,
|
|
getstring_("chat.status.operator.dead", $thread->locale),
|
|
$last_msg_id
|
|
);
|
|
$this->assertEquals(1, $message_count);
|
|
// Check last ping agent
|
|
$this->assertEquals(0, $thread->lastPingAgent);
|
|
$this->assertEquals(0, $thread_info['lastpingagent']);
|
|
// Check thread state
|
|
$this->assertEquals(Thread::STATE_WAITING, $thread->state);
|
|
$this->assertEquals(Thread::STATE_WAITING, $thread_info['istate']);
|
|
// Check next agent
|
|
$this->assertEquals(0, $thread->nextAgent);
|
|
$this->assertEquals(0, $thread_info['nextagent']);
|
|
|
|
|
|
// Get last message id
|
|
$last_msg_id = $this->_helper_getLastMessageId();
|
|
|
|
|
|
// Check ping agent with connection problem at the other side
|
|
// Update thread info
|
|
$thread->lastPingUser = time() - Thread::CONNECTION_TIMEOUT * 2;
|
|
$thread->save();
|
|
// Ping the thread
|
|
$thread->ping(false, false);
|
|
// Get thread info
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check message sent
|
|
$this->assertEquals(1, $this->_helper_getTotalMessagesCount($last_msg_id));
|
|
// Check messsage text
|
|
$message_count = $this->_helper_getMessagesCount(
|
|
Thread::KIND_FOR_AGENT,
|
|
getstring_("chat.status.user.dead", $thread->locale),
|
|
$last_msg_id
|
|
);
|
|
$this->assertEquals(1, $message_count);
|
|
// Check last ping user
|
|
$this->assertEquals(0, $thread->lastPingUser);
|
|
$this->assertEquals(0, $thread_info['lastpinguser']);
|
|
|
|
|
|
// Check user typing
|
|
// Update thread info
|
|
$thread->userTyping = '0';
|
|
$thread->save();
|
|
// Ping the thread
|
|
$thread->ping(true, true);
|
|
// Get thread info
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check
|
|
$this->assertEquals('1', $thread->userTyping);
|
|
$this->assertEquals('1', $thread_info['userTyping']);
|
|
|
|
// Check user not typing
|
|
// Update thread info
|
|
$thread->userTyping = '1';
|
|
$thread->save();
|
|
// Ping the thread
|
|
$thread->ping(true, false);
|
|
// Get thread info
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check
|
|
$this->assertEquals('0', $thread->userTyping);
|
|
$this->assertEquals('0', $thread_info['userTyping']);
|
|
|
|
|
|
// Check agent typing
|
|
// Update thread info
|
|
$thread->agentTyping = '0';
|
|
$thread->save();
|
|
// Ping the thread
|
|
$thread->ping(false, true);
|
|
// Get thread info
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check
|
|
$this->assertEquals('1', $thread->agentTyping);
|
|
$this->assertEquals('1', $thread_info['agentTyping']);
|
|
|
|
// Check agent not typing
|
|
// Update thread info
|
|
$thread->agentTyping = '1';
|
|
$thread->save();
|
|
// Ping the thread
|
|
$thread->ping(false, false);
|
|
// Get thread info
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check
|
|
$this->assertEquals('0', $thread->agentTyping);
|
|
$this->assertEquals('0', $thread_info['agentTyping']);
|
|
|
|
// Delete thread
|
|
$thread->delete();
|
|
unset($thread);
|
|
}
|
|
|
|
public function testTake() {
|
|
global $home_locale;
|
|
|
|
// Get last message id
|
|
$last_msg_id = $this->_helper_getLastMessageId();
|
|
|
|
// Create operator
|
|
$operator = array(
|
|
'operatorid' => 2,
|
|
'vclocalename' => 'Local name',
|
|
'vccommonname' => 'Common name',
|
|
'vcavatar' => 'avatar'
|
|
);
|
|
|
|
// Create new thread
|
|
$thread = Thread::create();
|
|
$thread->state = Thread::STATE_CLOSED;
|
|
$thread->locale = $home_locale;
|
|
$thread->agentName = 'Agent';
|
|
$thread->userName = 'User';
|
|
$thread->agentId = 2;
|
|
$thread->save();
|
|
|
|
|
|
// Try to take closed thread
|
|
$this->assertFalse($thread->take($operator));
|
|
|
|
|
|
// Try to take left thread
|
|
$thread->state = Thread::STATE_LEFT;
|
|
$thread->save();
|
|
$this->assertFalse($thread->take($operator));
|
|
|
|
|
|
// Try to take thread by current operator during chat
|
|
$thread->state = Thread::STATE_CHATTING;
|
|
$thread->save();
|
|
$this->assertTrue($thread->take($operator));
|
|
// No message must be sent
|
|
$this->assertEquals(0, $this->_helper_getTotalMessagesCount($last_msg_id));
|
|
|
|
|
|
// Try to take thread during chat
|
|
$thread->state = Thread::STATE_CHATTING;
|
|
$thread->agentId = 1;
|
|
$thread->nextAgent = 10;
|
|
$thread->save();
|
|
$this->assertTrue($thread->take($operator));
|
|
// Get thread info
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check thread state
|
|
$this->assertEquals(Thread::STATE_CHATTING, $thread->state);
|
|
$this->assertEquals(Thread::STATE_CHATTING, $thread_info['istate']);
|
|
// Check agent id
|
|
$this->assertEquals(2, $thread->agentId);
|
|
$this->assertEquals(2, $thread_info['agentId']);
|
|
// Check agent name
|
|
$this->assertEquals($operator['vclocalename'], $thread->agentName);
|
|
$this->assertEquals($operator['vclocalename'], $thread_info['agentName']);
|
|
// Check next agent id
|
|
$this->assertEquals(0, $thread->nextAgent);
|
|
$this->assertEquals(0, $thread_info['nextagent']);
|
|
// Check chat started time
|
|
$this->assertFalse(empty($thread->chatStarted));
|
|
$this->assertFalse(empty($thread_info['dtmchatstarted']));
|
|
// One messages must be sent
|
|
$this->assertEquals(1, $this->_helper_getTotalMessagesCount($last_msg_id));
|
|
// Check messages text
|
|
$message_count = $this->_helper_getMessagesCount(
|
|
Thread::KIND_EVENTS,
|
|
getstring2_(
|
|
"chat.status.operator.changed",
|
|
array($operator['vclocalename'], 'Agent'),
|
|
$thread->locale
|
|
),
|
|
$last_msg_id
|
|
);
|
|
$this->assertEquals(1, $message_count);
|
|
|
|
// Get last message id
|
|
$last_msg_id = $this->_helper_getLastMessageId();
|
|
|
|
|
|
// Try to take thread with state Thread::STATE_WAITING by current operator
|
|
$thread->state = Thread::STATE_WAITING;
|
|
$thread->save();
|
|
$this->assertTrue($thread->take($operator));
|
|
// Get thread info
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check thread state
|
|
$this->assertEquals(Thread::STATE_CHATTING, $thread->state);
|
|
$this->assertEquals(Thread::STATE_CHATTING, $thread_info['istate']);
|
|
// Check agent id
|
|
$this->assertEquals(2, $thread->agentId);
|
|
$this->assertEquals(2, $thread_info['agentId']);
|
|
// One message must be sent
|
|
$this->assertEquals(1, $this->_helper_getTotalMessagesCount($last_msg_id));
|
|
// Check messages text
|
|
$message_count = $this->_helper_getMessagesCount(
|
|
Thread::KIND_EVENTS,
|
|
getstring2_("chat.status.operator.returned", array($operator['vclocalename']), $thread->locale),
|
|
$last_msg_id
|
|
);
|
|
$this->assertEquals(1, $message_count);
|
|
|
|
// Get last message id
|
|
$last_msg_id = $this->_helper_getLastMessageId();
|
|
|
|
|
|
// Try to take thread with state Thread::STATE_WAITING
|
|
$thread->state = Thread::STATE_WAITING;
|
|
$thread->agentId = 1;
|
|
$thread->agentName = 'Agent';
|
|
$thread->save();
|
|
$this->assertTrue($thread->take($operator));
|
|
// Get thread info
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check thread state
|
|
$this->assertEquals(Thread::STATE_CHATTING, $thread->state);
|
|
$this->assertEquals(Thread::STATE_CHATTING, $thread_info['istate']);
|
|
// One message must be sent
|
|
$this->assertEquals(1, $this->_helper_getTotalMessagesCount($last_msg_id));
|
|
// Check messages text
|
|
$message_count = $this->_helper_getMessagesCount(
|
|
Thread::KIND_EVENTS,
|
|
getstring2_(
|
|
"chat.status.operator.changed",
|
|
array($operator['vclocalename'], 'Agent'),
|
|
$thread->locale
|
|
),
|
|
$last_msg_id
|
|
);
|
|
$this->assertEquals(1, $message_count);
|
|
|
|
|
|
// Get last message id
|
|
$last_msg_id = $this->_helper_getLastMessageId();
|
|
|
|
|
|
// Try to take thread with state Thread::STATE_QUEUE
|
|
$thread->state = Thread::STATE_QUEUE;
|
|
$thread->save();
|
|
$this->assertTrue($thread->take($operator));
|
|
// Get thread info
|
|
$thread_info = $this->_helper_getThreadInfo($thread->id);
|
|
// Check thread state
|
|
$this->assertEquals(Thread::STATE_CHATTING, $thread->state);
|
|
$this->assertEquals(Thread::STATE_CHATTING, $thread_info['istate']);
|
|
// One message must be sent
|
|
$this->assertEquals(1, $this->_helper_getTotalMessagesCount($last_msg_id));
|
|
// Check messages text
|
|
$message_count = $this->_helper_getMessagesCount(
|
|
Thread::KIND_EVENTS,
|
|
$message = getstring2_("chat.status.operator.joined", array($operator['vclocalename']), $thread->locale),
|
|
$last_msg_id
|
|
);
|
|
$this->assertEquals(1, $message_count);
|
|
|
|
// Delete thread
|
|
$thread->delete();
|
|
unset($thread);
|
|
}
|
|
|
|
}
|
|
|
|
?>
|