<?php
/**
 * =======================================
 * ###################################
 * SWIFT Framework
 *
 * @package	SWIFT
 * @author	Kayako Singapore Pte. Ltd.
 * @copyright	Copyright (c) 2001-Kayako Singapore Pte. Ltd.h Ltd.
 * @license	http://www.kayako.com/license
 * @link		http://www.kayako.com
 * @filesource
 * ###################################
 * =======================================
 */

/**
 * Auto-Completion Tests
 *
 * @author Andriy Lesyuk
 */
class SWIFT_KQLAutoCompleteTest extends SWIFT_TestCase
{

	/**
	 * The Test Set Up Function
	 *
	 * @author Andriy Lesyuk
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function setUp()
	{
		return true;
	}

	/**
	 * Test the Constructor
	 *
	 * @author Andriy Lesyuk
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function testConstruct()
	{
		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete('', "SELECT * FROM 'Tickets'", 'Tickets', array('Tickets'), 0, 0, '');

		$this->assertInstanceOf('SWIFT_KQLAutoComplete', $_SWIFT_KQLAutoCompletObject);

		return true;
	}

	/**
	 * Test the Destructor
	 *
	 * @author Andriy Lesyuk
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function testDestruct()
	{
		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete('', "SELECT * FROM 'Tickets'", 'Tickets', array('Tickets'), 0, 0, '');
		$_SWIFT_KQLAutoCompletObject->__destruct();

		$this->assertInstanceOf('SWIFT_KQLAutoComplete', $_SWIFT_KQLAutoCompletObject);

		return true;
	}

	/**
	 * Tests the RetrieveOptionsJSON()
	 *
	 * @author Andriy Lesyuk
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function testRetrieveOptionsJSON()
	{
		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete('', "SELECT * FROM 'Tickets'", 'Tickets', array('Tickets'), 0, 0, '');

		ob_start();
		$_jsonData = $_SWIFT_KQLAutoCompletObject->RetrieveOptionsJSON();
		ob_end_clean();

		$this->assertTrue(is_string($_jsonData));

		$_jsonObject = json_decode($_jsonData);

		$this->assertFalse(is_null($_jsonObject));
		$this->assertInstanceOf('stdClass', $_jsonObject);
		$this->assertObjectHasAttribute('result', $_jsonObject);
		$this->assertObjectHasAttribute('message', $_jsonObject);
		$this->assertObjectHasAttribute('start', $_jsonObject);
		$this->assertObjectHasAttribute('end', $_jsonObject);
		$this->assertObjectHasAttribute('data', $_jsonObject);

		$this->assertTrue(_is_array($_jsonObject->data));

		$_dataItem = reset($_jsonObject->data);

		$this->assertInstanceOf('stdClass', $_dataItem);
		$this->assertObjectHasAttribute('label', $_dataItem);
		$this->assertObjectHasAttribute('value', $_dataItem);

		$this->assertEquals(0, $_jsonObject->start);
		$this->assertEquals(0, $_jsonObject->end);

		return true;
	}

	/**
	 * Tests the testRetrieveOptions()
	 *
	 * @author Andriy Lesyuk
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function testRetrieveOptions()
	{
		/**
		 * Testing empty KQL
		 */

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete('SELECT ', '', 'Tickets', array('Tickets'), 7, 7, '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertTrue(is_array($_ajaxOptions));
		$this->assertCount(3, $_ajaxOptions);
		$this->assertEquals(7, $_ajaxOptions[0]);
		$this->assertEquals(7, $_ajaxOptions[1]);
		$this->assertTrue(_is_array($_ajaxOptions[2]));

		$_ajaxOption = reset($_ajaxOptions[2]);

		$this->assertCount(2, $_ajaxOption);

		// Testing suggestions

		$this->assertCount(10, $_ajaxOptions[2]); // No prefix so there should be 10 items

		$this->assertEquals('Tickets.', $_ajaxOption[0]); // The Tickets. table prefix should be suggested
		$this->assertEquals("'Tickets.", $_ajaxOption[1]);


		/**
		 * Testing field prefix
		 */

		$_kqlQuery = "SELECT Status";

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), strlen($_kqlQuery), strlen($_kqlQuery), '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(7, $_ajaxOptions[0]);
		$this->assertEquals(13, $_ajaxOptions[1]);

		foreach ($_ajaxOptions[2] as $_ajaxOption) {
			$this->assertRegExp('/(, |\.)status/i', $_ajaxOption[1]);
		}

		$_ajaxOption = reset($_ajaxOptions[2]);

		$this->assertEquals('Tickets.Status', $_ajaxOption[0]);
		$this->assertEquals("'Tickets.Status'", $_ajaxOption[1]);


		/**
		 * Testing table prefix
		 */

		$_kqlQuery = "SELECT Tickets.";

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), strlen($_kqlQuery), strlen($_kqlQuery), '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(15, $_ajaxOptions[1]);

		foreach ($_ajaxOptions[2] as $_ajaxOption) {
			$this->assertRegExp('/^Tickets\./', $_ajaxOption[0]);
		}


		/**
		 * Testing inner field with table prefix
		 */

		$_kqlQuery = "SELECT 'Tickets.Ticket Mask ID', 'Tickets.', 'Tickets.Subject' FROM 'Tickets'";
		$_caretPosition = strpos($_kqlQuery, ".'") + 1;

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), $_caretPosition, $_caretPosition, '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(7, $_ajaxOptions[0]);
		$this->assertEquals(strpos($_kqlQuery, 'FROM'), $_ajaxOptions[1]);

		$_ajaxOption = reset($_ajaxOptions[2]);

		$this->assertStringEndsWith(' ', $_ajaxOption[1]);

		$_suggestedFields = explode(',', $_ajaxOption[1]);

		$this->assertCount(3, $_suggestedFields);
		$this->assertEquals("'Tickets.Ticket Mask ID'", trim($_suggestedFields[0]));
		$this->assertEquals("'Tickets.Subject'", trim($_suggestedFields[2]));


		/**
		 * Testing inner field without any prefix
		 */

		$_kqlQuery = "SELECT 'Tickets.Ticket Mask ID', , 'Tickets.Subject' FROM 'Tickets'";
		$_caretPosition = strpos($_kqlQuery, " ,") + 1;

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), $_caretPosition, $_caretPosition, '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(strpos($_kqlQuery, 'FROM'), $_ajaxOptions[1]);
		$this->assertCount(10, $_ajaxOptions[2]);

		$_ajaxOption = reset($_ajaxOptions[2]);

		$_suggestedFields = explode(',', $_ajaxOption[1]);
		$this->assertEquals("'Tickets.", trim($_suggestedFields[1]));


		/**
		 * Testing CUSTOMFIELD()
		 */

		$_kqlQuery = "SELECT CUSTOMFIELD() FROM 'Tickets'";
		$_caretPosition = strpos($_kqlQuery, ")");

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), $_caretPosition, $_caretPosition, '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals($_caretPosition, $_ajaxOptions[0]);
		$this->assertEquals($_caretPosition, $_ajaxOptions[1]);

		$this->assertTrue(_is_array($_ajaxOptions[2]));

		$_ajaxOption = reset($_ajaxOptions[2]);

		$this->assertRegExp('/^Tickets, /', $_ajaxOption[0]); // Should be all custom fields for Tickets
		$this->assertRegExp("/^'Tickets', /", $_ajaxOption[1]);


		/**
		 * Testing table suggestions
		 */

		$_kqlQuery = "SELECT 'Tickets.Ticket Mask ID', 'Tickets.Subject' FROM ";

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), strlen($_kqlQuery), strlen($_kqlQuery), '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(strlen($_kqlQuery), $_ajaxOptions[0]);
		$this->assertTrue(_is_array($_ajaxOptions[2]));

		$_ajaxOption = reset($_ajaxOptions[2]);

		$this->assertEquals("Tickets", trim($_ajaxOption[0]));
		$this->assertEquals("'Tickets'", trim($_ajaxOption[1]));


		/**
		 * Testing other tables suggestions
		 */

		$_kqlQuery = "SELECT 'Tickets.Ticket Mask ID', 'Tickets.Subject' FROM 'Tickets', ";

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), strlen($_kqlQuery), strlen($_kqlQuery), '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(strpos($_kqlQuery, 'FROM') + 5, $_ajaxOptions[0]);
		$this->assertTrue($_ajaxOptions[1] >= strlen($_kqlQuery)); // For some reason it is strlen + 1
		$this->assertTrue(_is_array($_ajaxOptions[2]));


		/**
		 * Testing fields suggestions after WHERE
		 */

		$_kqlQuery = "SELECT 'Tickets.Ticket Mask ID', 'Tickets.Subject' FROM 'Tickets' WHERE ";

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), strlen($_kqlQuery), strlen($_kqlQuery), '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(strlen($_kqlQuery), $_ajaxOptions[0]);
		$this->assertTrue(_is_array($_ajaxOptions[2]));
		$this->assertCount(10, $_ajaxOptions[2]);


		/**
		 * Testing operators suggestions
		 */

		$_kqlQuery = "SELECT 'Tickets.Ticket Mask ID', 'Tickets.Subject' FROM 'Tickets' WHERE 'Tickets.Type' ";

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), strlen($_kqlQuery), strlen($_kqlQuery), '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(strlen($_kqlQuery) - 1, $_ajaxOptions[0]);
		$this->assertEquals(strlen($_kqlQuery), $_ajaxOptions[1]);
		$this->assertTrue(_is_array($_ajaxOptions[2]));

		$_ajaxOption = reset($_ajaxOptions[2]);

		$this->assertEquals('=', $_ajaxOption[0]);
		$this->assertEquals('=', trim($_ajaxOption[1]));


		/**
		 * Testing functions suggestions for date type
		 */

		$_kqlQuery = "SELECT 'Tickets.Ticket Mask ID', 'Tickets.Subject' FROM 'Tickets' WHERE 'Tickets.Creation Date' = ";

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), strlen($_kqlQuery), strlen($_kqlQuery), '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(strlen($_kqlQuery), $_ajaxOptions[0]);
		$this->assertTrue(_is_array($_ajaxOptions[2]));

		foreach ($_ajaxOptions[2] as $_ajaxOption) {
			$this->assertRegExp('/^.+\(\)$/', $_ajaxOption[0]);
			$this->assertRegExp('/^.+\(\)$/', trim($_ajaxOption[1]));
		}


		/**
		 * Testing glue suggestions for WHERE
		 */

		$_kqlQuery = "SELECT 'Tickets.Ticket Mask ID', 'Tickets.Subject' FROM 'Tickets' WHERE 'Tickets.Creation Date' = ThisMonth() ";

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), strlen($_kqlQuery), strlen($_kqlQuery), '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(strlen($_kqlQuery), $_ajaxOptions[0]);
		$this->assertTrue(_is_array($_ajaxOptions[2]));

		$_suggestedGlues = array();
		foreach ($_ajaxOptions[2] as $_ajaxOption) {
			$_suggestedGlues[] = $_ajaxOption[0];
		}

		$this->assertContains('AND', $_suggestedGlues);
		$this->assertContains('OR', $_suggestedGlues);
		$this->assertContains('GROUP BY', $_suggestedGlues);
		$this->assertContains('ORDER BY', $_suggestedGlues);


		/**
		 * Testing CUSTOMFIELD() in WHERE
		 */

		$_kqlQuery = "SELECT 'Tickets.Ticket Mask ID', 'Tickets.Subject' FROM 'Tickets' WHERE CUSTOMFIELD(";

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), strlen($_kqlQuery), strlen($_kqlQuery), '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(strlen($_kqlQuery), $_ajaxOptions[0]);
		$this->assertEquals(strlen($_kqlQuery), $_ajaxOptions[1]);
		$this->assertTrue(_is_array($_ajaxOptions[2]));

		$_ajaxOption = reset($_ajaxOptions[2]);

		$this->assertRegExp('/^Tickets, /', $_ajaxOption[0]); // Should be all custom fields for Tickets
		$this->assertRegExp("/^'Tickets', /", $_ajaxOption[1]);


		/**
		 * Testing suggestions for GROUP BY
		 */

		$_kqlQuery = "SELECT 'Tickets.Ticket Mask ID', 'Tickets.Subject' FROM 'Tickets' GROUP BY ";

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), strlen($_kqlQuery), strlen($_kqlQuery), '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(strlen($_kqlQuery), $_ajaxOptions[0]);
		$this->assertEquals(strlen($_kqlQuery), $_ajaxOptions[1]);
		$this->assertTrue(_is_array($_ajaxOptions[2]));


		/**
		 * Testing suggestions for MULTIGROUP BY
		 */

		$_kqlQuery = "SELECT 'Tickets.Ticket Mask ID', 'Tickets.Subject' FROM 'Tickets' MULTIGROUP BY ";

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), strlen($_kqlQuery), strlen($_kqlQuery), '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(strlen($_kqlQuery), $_ajaxOptions[0]);
		$this->assertEquals(strlen($_kqlQuery), $_ajaxOptions[1]);
		$this->assertTrue(_is_array($_ajaxOptions[2]));


		/**
		 * Testing CUSTOMFIELD() in GROUP BY
		 */

		$_kqlQuery = "SELECT 'Tickets.Ticket Mask ID', 'Tickets.Subject' FROM 'Tickets' MULTIGROUP BY CUSTOMFIELD()";

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), strlen($_kqlQuery) - 1, strlen($_kqlQuery) - 1, '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(strlen($_kqlQuery) - 1, $_ajaxOptions[0]);
		$this->assertEquals(strlen($_kqlQuery) - 1, $_ajaxOptions[1]);
		$this->assertTrue(_is_array($_ajaxOptions[2]));

		$_ajaxOption = reset($_ajaxOptions[2]);

		$this->assertRegExp('/^Tickets, /', $_ajaxOption[0]); // Should be all custom fields for Tickets
		$this->assertRegExp("/^'Tickets', /", $_ajaxOption[1]);


		/**
		 * Testing suggestions for ORDER BY
		 */

		$_kqlQuery = "SELECT 'Tickets.Ticket Mask ID', 'Tickets.Subject' FROM 'Tickets' ORDER BY ";

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), strlen($_kqlQuery), strlen($_kqlQuery), '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(strlen($_kqlQuery), $_ajaxOptions[0]);
		$this->assertEquals(strlen($_kqlQuery), $_ajaxOptions[1]);
		$this->assertTrue(is_array($_ajaxOptions[2]));
		$this->assertCount(10, $_ajaxOptions[2]);

		$_ajaxOption = reset($_ajaxOptions[2]);

		$this->assertEquals('Tickets.', $_ajaxOption[0]);
		$this->assertEquals("'Tickets.", $_ajaxOption[1]);


		/**
		 * Testing suggestions for ORDER BY ...
		 */

		$_kqlQuery = "SELECT 'Tickets.Ticket Mask ID', 'Tickets.Subject' FROM 'Tickets' ORDER BY 'Tickets.Type' ";

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), strlen($_kqlQuery), strlen($_kqlQuery), '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(strpos($_kqlQuery, 'ORDER BY') + 9, $_ajaxOptions[0]);
		$this->assertEquals(strlen($_kqlQuery), $_ajaxOptions[1]);
		$this->assertTrue(is_array($_ajaxOptions[2]));
		$this->assertCount(2, $_ajaxOptions[2]);

		$_ajaxOption = reset($_ajaxOptions[2]);

		$this->assertEquals('ASC', $_ajaxOption[0]);
		$this->assertEquals("'Tickets.Type' ASC", trim($_ajaxOption[1]));

		$_ajaxOption = end($_ajaxOptions[2]);

		$this->assertEquals('DESC', $_ajaxOption[0]);
		$this->assertEquals("'Tickets.Type' DESC", trim($_ajaxOption[1]));


		/**
		 * Testing replacement of complex fields
		 */

		$_kqlQuery = "SELECT 'Tickets.Ticket Mask ID', (SUBSTRING_INDEX('Ticket Posts.Contents', '\\n', 1)) AS Post, 'Tickets.Subject' FROM 'Ticket Posts', 'Tickets'";
		$_caretPosition = strpos($_kqlQuery, "(SUBSTRING_INDEX(");

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), $_caretPosition, $_caretPosition, '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(7, $_ajaxOptions[0]);
		$this->assertEquals(strpos($_kqlQuery, 'FROM'), $_ajaxOptions[1]);
		$this->assertTrue(is_array($_ajaxOptions[2]));
		$this->assertCount(10, $_ajaxOptions[2]);

		$_ajaxOption = reset($_ajaxOptions[2]);

		$this->assertEquals('Tickets.', trim($_ajaxOption[0]));
		$this->assertEquals("'Tickets.Ticket Mask ID', 'Tickets., 'Tickets.Subject'", trim($_ajaxOption[1]));

		return true;
	}

	/**
	 * Testing suggestions for SELECT
	 *
	 * @author Andriy Lesyuk
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function testGetActiveCaretTokenForSelect()
	{
		$_fieldsPrefix = "'Tickets.Ticket Mask ID', 'Tickets.Subject', CONCAT('Subject: ', 'Tickets.Subject') AS Subject";

		ob_start();
		$_activeCaretToken = SWIFT_KQLAutoComplete::GetActiveCaretTokenForSelect($_fieldsPrefix, 0);
		ob_end_clean();

		$this->assertEquals('tickets.ticket mask id', $_activeCaretToken[0]);
		$this->assertEquals('', $_activeCaretToken[1]);

		ob_start();
		$_activeCaretToken = SWIFT_KQLAutoComplete::GetActiveCaretTokenForSelect($_fieldsPrefix, strpos($_fieldsPrefix, ','));
		ob_end_clean();

		$this->assertEquals('tickets.ticket mask id', $_activeCaretToken[0]);
		$this->assertEquals('tickets.ticket mask id', $_activeCaretToken[1]);

		ob_start();
		$_activeCaretToken = SWIFT_KQLAutoComplete::GetActiveCaretTokenForSelect($_fieldsPrefix, strpos($_fieldsPrefix, '.') + 1);
		ob_end_clean();

		$this->assertEquals('tickets.ticket mask id', $_activeCaretToken[0]);
		$this->assertEquals('tickets.', $_activeCaretToken[1]);

		ob_start();
		$_activeCaretToken = SWIFT_KQLAutoComplete::GetActiveCaretTokenForSelect($_fieldsPrefix, strpos($_fieldsPrefix, ',') + 1);
		ob_end_clean();

		$this->assertEquals('tickets.subject', $_activeCaretToken[0]);
		$this->assertEquals('', $_activeCaretToken[1]);

		ob_start();
		$_activeCaretToken = SWIFT_KQLAutoComplete::GetActiveCaretTokenForSelect($_fieldsPrefix, strpos($_fieldsPrefix, ',') + 15);
		ob_end_clean();

		$this->assertEquals('tickets.subject', $_activeCaretToken[0]);
		$this->assertEquals('tickets.subj', $_activeCaretToken[1]);

		ob_start();
		$_activeCaretToken = SWIFT_KQLAutoComplete::GetActiveCaretTokenForSelect($_fieldsPrefix, strrpos($_fieldsPrefix, 'AS') + 2);
		ob_end_clean();

		$this->assertEquals("concat('subject: ', 'tickets.subject') as subject", $_activeCaretToken[0]);
		$this->assertEquals("concat('subject: ', 'tickets.subject') as", $_activeCaretToken[1]);

		return true;
	}

	/**
	 * Testing operators suggestions
	 *
	 * @author Andriy Lesyuk
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function testGetOperatorListOnFieldType()
	{
		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete("SELECT * FROM 'Tickets'", '', 'Tickets', array('Tickets'), 0, 0, '');

		$_operators = $_SWIFT_KQLAutoCompletObject->GetOperatorListOnFieldType(SWIFT_KQLSchema::FIELDTYPE_BOOL, array());

		foreach (array('=', '!=') as $_operator) {
			$this->assertContains($_operator, $_operators);
		}

		$_operators = $_SWIFT_KQLAutoCompletObject->GetOperatorListOnFieldType(SWIFT_KQLSchema::FIELDTYPE_CUSTOM, array());

		foreach (array('=', '!=', 'in', 'not in') as $_operator) {
			$this->assertContains($_operator, $_operators);
		}

		$_operators = $_SWIFT_KQLAutoCompletObject->GetOperatorListOnFieldType(SWIFT_KQLSchema::FIELDTYPE_FLOAT, array());

		foreach (array('=', '!=', '>', '<', '>=', '<=') as $_operator) {
			$this->assertContains($_operator, $_operators);
		}

		$_operators = $_SWIFT_KQLAutoCompletObject->GetOperatorListOnFieldType(SWIFT_KQLSchema::FIELDTYPE_SECONDS, array());

		foreach (array('=', '!=', '>', '<', '>=', '<=') as $_operator) {
			$this->assertContains($_operator, $_operators);
		}

		$_operators = $_SWIFT_KQLAutoCompletObject->GetOperatorListOnFieldType(SWIFT_KQLSchema::FIELDTYPE_INT, array());

		foreach (array('=', '!=', '>', '<', '>=', '<=') as $_operator) {
			$this->assertContains($_operator, $_operators);
		}

		$_operators = $_SWIFT_KQLAutoCompletObject->GetOperatorListOnFieldType(SWIFT_KQLSchema::FIELDTYPE_UNIXTIME, array());

		foreach (array('=', '!=', '>', '<', '>=', '<=') as $_operator) {
			$this->assertContains($_operator, $_operators);
		}

		return true;
	}

	/**
	 * Testing operators suggestions for custom fields
	 *
	 * @author Andriy Lesyuk
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function testGetOperatorListForCustomField()
	{
		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete("SELECT * FROM 'Tickets'", '', 'Tickets', array('Tickets'), 0, 0, '');

		$_customFieldContainer = array(
			'encryptindb' => '1'
		);

		$_operators = $_SWIFT_KQLAutoCompletObject->GetOperatorListForCustomField($_customFieldContainer);

		$this->assertTrue(is_array($_operators));
		$this->assertTrue(empty($_operators));

		$_customFieldContainer['encryptindb'] = '0';
		$_customFieldContainer['type'] = SWIFT_CustomField::TYPE_TEXT;

		$_operators = $_SWIFT_KQLAutoCompletObject->GetOperatorListForCustomField($_customFieldContainer);

		foreach (array('=', '!=', 'like', 'not like', 'in', 'not in') as $_operator) {
			$this->assertContains($_operator, $_operators);
		}

		$_customFieldContainer['type'] = SWIFT_CustomField::TYPE_CHECKBOX;

		$_operators = $_SWIFT_KQLAutoCompletObject->GetOperatorListForCustomField($_customFieldContainer);

		foreach (array('=', '!=', 'in', 'not in') as $_operator) {
			$this->assertContains($_operator, $_operators);
		}

		$_customFieldContainer['type'] = SWIFT_CustomField::TYPE_RADIO;

		$_operators = $_SWIFT_KQLAutoCompletObject->GetOperatorListForCustomField($_customFieldContainer);

		foreach (array('=', '!=', 'in', 'not in') as $_operator) {
			$this->assertContains($_operator, $_operators);
		}

		$_customFieldContainer['type'] = SWIFT_CustomField::TYPE_SELECTLINKED;

		$_operators = $_SWIFT_KQLAutoCompletObject->GetOperatorListForCustomField($_customFieldContainer);

		foreach (array('=', '!=', 'in', 'not in') as $_operator) {
			$this->assertContains($_operator, $_operators);
		}

		$_customFieldContainer['type'] = SWIFT_CustomField::TYPE_DATE;

		$_operators = $_SWIFT_KQLAutoCompletObject->GetOperatorListForCustomField($_customFieldContainer);

		foreach (array('=', '!=', '>', '<', '>=', '<=') as $_operator) {
			$this->assertContains($_operator, $_operators);
		}

		return true;
	}

	/**
	 * Testing modes
	 *
	 * @author Andriy Lesyuk
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function testIsValidMode()
	{
		foreach (array(SWIFT_KQLAutoComplete::MODE_FIELDGLUE,
					   SWIFT_KQLAutoComplete::MODE_FIELDNAME,
					   SWIFT_KQLAutoComplete::MODE_FIELDOP,
					   SWIFT_KQLAutoComplete::MODE_FIELDVALUE,
					   SWIFT_KQLAutoComplete::MODE_NONE,
					   SWIFT_KQLAutoComplete::MODE_ORDERBY,
					   SWIFT_KQLAutoComplete::MODE_GROUPBY,
					   SWIFT_KQLAutoComplete::MODE_SELECT,
					   SWIFT_KQLAutoComplete::MODE_FROM,
					   SWIFT_KQLAutoComplete::MODE_CUSTOMFIELD) as $_mode) {
			$this->assertTrue(SWIFT_KQLAutoComplete::IsValidMode($_mode));
		}

		return true;
	}

	/**
	 * Testing active mode
	 *
	 * @author Andriy Lesyuk
	 * @return bool "true" on Success, "false" otherwise
	 */
	public function testGetActiveMode()
	{
		$_kqlQuery = "SELECT 'Tickets.Ticket Mask ID' FROM 'Tickets' WHERE 'Tickets.Subject' =";
		$_caretPosition = strpos($_kqlQuery, '=');

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), $_caretPosition, $_caretPosition, '');

		$this->assertEquals(SWIFT_KQLAutoComplete::MODE_NONE, $_SWIFT_KQLAutoCompletObject->GetActiveMode());

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(SWIFT_KQLAutoComplete::MODE_FIELDOP, $_SWIFT_KQLAutoCompletObject->GetActiveMode());


		$_kqlQuery = "SELECT 'Tickets.Ticket Mask ID' FROM 'Tickets' WHERE 'Tickets.Subject' = 'Test' ";
		$_caretPosition = strlen($_kqlQuery);

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), $_caretPosition, $_caretPosition, '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(SWIFT_KQLAutoComplete::MODE_FIELDGLUE, $_SWIFT_KQLAutoCompletObject->GetActiveMode());


		$_kqlQuery = "SELECT ";
		$_caretPosition = strlen($_kqlQuery);

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), $_caretPosition, $_caretPosition, '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(SWIFT_KQLAutoComplete::MODE_SELECT, $_SWIFT_KQLAutoCompletObject->GetActiveMode());


		$_kqlQuery = "SELECT 'Tickets.Ticket Mask ID' FROM 'Tickets' WHERE ";
		$_caretPosition = strlen($_kqlQuery);

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), $_caretPosition, $_caretPosition, '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(SWIFT_KQLAutoComplete::MODE_FIELDNAME, $_SWIFT_KQLAutoCompletObject->GetActiveMode());


		$_kqlQuery = "SELECT 'Tickets.Ticket Mask ID' FROM ";
		$_caretPosition = strlen($_kqlQuery);

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), $_caretPosition, $_caretPosition, '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(SWIFT_KQLAutoComplete::MODE_FROM, $_SWIFT_KQLAutoCompletObject->GetActiveMode());


		$_kqlQuery = "SELECT CUSTOMFIELD(";
		$_caretPosition = strlen($_kqlQuery);

		$_SWIFT_KQLAutoCompletObject = new SWIFT_KQLAutoComplete($_kqlQuery, '', 'Tickets', array('Tickets'), $_caretPosition, $_caretPosition, '');

		ob_start();
		$_ajaxOptions = $_SWIFT_KQLAutoCompletObject->RetrieveOptions();
		ob_end_clean();

		$this->assertEquals(SWIFT_KQLAutoComplete::MODE_CUSTOMFIELD, $_SWIFT_KQLAutoCompletObject->GetActiveMode());

		return true;
	}

}
?>