<?php
/**
 * Created by PhpStorm.
 * User: Eric
 * Date: 17/09/2018
 * Time: 12:31
 */

namespace Combodo\iTop\Test\UnitTest\Core;

use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
use DBObjectSet;
use DBSearch;

/**
 * Tests of the DBSearch class.
 * <ul>
 * <li>MakeGroupByQuery</li>
 * </ul>
 */
class DBSearchCommitTest extends ItopDataTestCase
{
	// Need database COMMIT in order to create the FULLTEXT INDEX of MySQL
	const USE_TRANSACTION = false;

	/**
	 * @group itopFaqLight
	 * @throws \CoreException
	 * @throws \MissingQueryArgument
	 * @throws \MySQLException
	 * @throws \MySQLHasGoneAwayException
	 * @throws \OQLException
	 * @throws \Exception
	 */
	public function testAttributeTagSet()
	{
		// Create a tag
		$this->CreateTagData(TAG_CLASS, TAG_ATTCODE, 'tag1', 'UNIT First');
		$this->CreateTagData(TAG_CLASS, TAG_ATTCODE, 'tag2', 'UNIT Second');
		//Use it
		$oObjWithTagSet = $this->CreateObjectWithTagSet();
		$oObjWithTagSet->Set(TAG_ATTCODE, 'tag1');
		$oObjWithTagSet->DBWrite();

		$oSearch = DBSearch::FromOQL("SELECT ".TAG_CLASS);
		$oSearch->AddCondition(TAG_ATTCODE, 'tag1', 'MATCHES');
		$oSet = new DBObjectSet($oSearch);
		static::assertEquals(1, $oSet->Count());


		$oObjWithTagSet->Set(TAG_ATTCODE, 'tag1 tag2');
		$oObjWithTagSet->DBWrite();

		$oSet = new DBObjectSet($oSearch);
		static::assertEquals(1, $oSet->Count());
	}

	/**
	 * @group itopFaqLight
	 * @throws \CoreException
	 * @throws \CoreUnexpectedValue
	 * @throws \MissingQueryArgument
	 * @throws \MySQLException
	 * @throws \MySQLHasGoneAwayException
	 * @throws \OQLException
	 * @throws \Exception
	 */
	public function testAttributeTagSet2()
	{
		// Create a tag
		$this->CreateTagData(TAG_CLASS, TAG_ATTCODE, 'tag1', 'UNIT First');
		$this->CreateTagData(TAG_CLASS, TAG_ATTCODE, 'tag2', 'UNIT Second');
		//Use it
		$oObjWithTagSet = $this->CreateObjectWithTagSet();
		$oObjWithTagSet->Set(TAG_ATTCODE, 'tag1');
		$oObjWithTagSet->DBWrite();

		$oSearch = DBSearch::FromOQL("SELECT ".TAG_CLASS);
		$oSearch->AddCondition(TAG_ATTCODE, 'tag1');
		$oSet = new \DBObjectSet($oSearch);
		static::assertEquals(1, $oSet->Count());


		$oObjWithTagSet->Set(TAG_ATTCODE, 'tag1 tag2');
		$oObjWithTagSet->DBWrite();

		$oSet = new \DBObjectSet($oSearch);
		static::assertEquals(0, $oSet->Count());
	}

	/**
	 * @covers N°8511 - Enhance DBObjectSet to be able to order by id
 */
	public function testDbObjectSetFetchMethodWorksWithOptimizeColumnLoadOrderedById(){
		$sUID=uniqid();
		$oOrg1 = $this->CreateOrganization($sUID);
		$oOrg2 = $this->CreateOrganization($sUID);

		$oSearch = DBSearch::FromOQL("SELECT Organization WHERE name=\"$sUID\"", ['uuid' => $sUID]);
		$oSet = new \DBObjectSet($oSearch, ['name' => true, 'id' => false ]);
		$oSet->OptimizeColumnLoad(['Organization' => ['name']]);

		static::assertEquals(2, $oSet->Count());
		static::assertEquals($oOrg2->GetKey(), $oSet->Fetch()->GetKey());

		$oSet = new \DBObjectSet($oSearch, ['name' => true, 'id' => true ]);
		static::assertEquals(2, $oSet->Count());
		static::assertEquals($oOrg1->GetKey(), $oSet->Fetch()->GetKey());
	}

}
