diff --git a/src/messenger/tests/server_side/webim/libs/classes/ThreadTest.php b/src/messenger/tests/server_side/webim/libs/classes/ThreadTest.php index 77087d50..be7bfe3c 100644 --- a/src/messenger/tests/server_side/webim/libs/classes/ThreadTest.php +++ b/src/messenger/tests/server_side/webim/libs/classes/ThreadTest.php @@ -186,6 +186,54 @@ class ThreadTest extends PHPUnit_Framework_TestCase { $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(); diff --git a/src/messenger/webim/libs/classes/thread.php b/src/messenger/webim/libs/classes/thread.php index a162d664..9ebdbbdc 100644 --- a/src/messenger/webim/libs/classes/thread.php +++ b/src/messenger/webim/libs/classes/thread.php @@ -217,6 +217,31 @@ Class Thread { return $thread; } + /** + * Create thread object from database info. + * + * @param array $thread_info Associative array of Thread info from database. It must contains ALL thread table's + * FIELDS from the database. + * @return boolean|Thread Returns an object of the Thread class or boolean false on failure + */ + public static function createFromDbInfo($thread_info) { + // Create new empty thread + $thread = new self(); + + // Check thread fields + $obligatory_fields = array_values($thread->propertyMap); + foreach($obligatory_fields as $field) { + if (!array_key_exists($field, $thread_info)) { + // Obligatory field is missing + unset($thread); + return false; + } + // Copy field to Thread object + $thread->threadInfo[$field] = $thread_info[$field]; + } + return $thread; + } + /** * Load thread from database *