Files
iTop/test/application/search/SearchFormTest.php
2018-03-23 16:05:37 +00:00

115 lines
3.8 KiB
PHP

<?php
/**
* Copyright (C) 2010-2018 Combodo SARL
*
* This file is part of iTop.
*
* iTop is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iTop is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with iTop. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace Combodo\iTop\Test\UnitTest\Application\Search;
use Combodo\iTop\Application\Search\SearchForm;
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
use Exception;
class SearchFormTest extends ItopDataTestCase
{
/**
* @throws Exception
*/
protected function setUp()
{
parent::setUp();
require_once(APPROOT."sources/application/search/searchform.class.inc.php");
}
/**
* @dataProvider GetFieldsProvider
* @throws \OQLException
*/
public function testGetFields($sOQL, $iNum, $sList)
{
$oSearchForm = new SearchForm();
$oSearch = \DBSearch::FromOQL($sOQL);
$aFields = $oSearchForm->GetFields(new \DBObjectSet($oSearch));
$this->debug(json_encode($aFields, JSON_PRETTY_PRINT));
$this->assertCount($iNum, $aFields[$sList]);
}
public function GetFieldsProvider()
{
return array(
array("SELECT Contact", 8, 'zlist'),
array("SELECT Contact AS C WHERE C.status = 'active'", 4, 'others'),
array("SELECT Person", 12, 'zlist'),
);
}
/**
* @dataProvider GetCriterionProvider
*
* @param $sOQL
* @param $iOrCount
*
*/
public function testGetCriterion($sOQL, $iOrCount)
{
$oSearchForm = new SearchForm();
try
{
$oSearch = \DBSearch::FromOQL($sOQL);
$aFields = $oSearchForm->GetFields(new \DBObjectSet($oSearch));
$aCriterion = $oSearchForm->GetCriterion($oSearch, $aFields);
} catch (\OQLException $e)
{
$this->assertTrue(false);
return;
}
$aRes = array('base_oql' => $sOQL, 'criterion' => $aCriterion);
$this->debug(json_encode($aRes));
$this->debug($sOQL);
$this->debug(json_encode($aCriterion, JSON_PRETTY_PRINT));
$this->assertCount($iOrCount, $aCriterion['or']);
}
public function GetCriterionProvider()
{
return array(
array('OQL' => "SELECT Contact", 1),
array('OQL' => "SELECT Contact WHERE status = 'active'", 1),
array('OQL' => "SELECT Contact AS C WHERE C.status = 'active'", 1),
array('OQL' => "SELECT Contact WHERE status = 'active' AND name LIKE 'toto%'", 1),
array('OQL' => "SELECT Contact WHERE status = 'active' AND org_id = 3", 1),
array('OQL' => "SELECT Contact WHERE status IN ('active', 'inactive')", 1),
array('OQL' => "SELECT Contact WHERE status NOT IN ('active')", 1),
array('OQL' => "SELECT Contact WHERE status NOT IN ('active', 'inactive')", 1),
array('OQL' => "SELECT Contact WHERE status = 'active' OR name LIKE 'toto%'", 2),
array('OQL' => "SELECT UserRequest WHERE DATE_SUB(NOW(), INTERVAL 14 DAY) < start_date", 1),
array('OQL' => "SELECT UserRequest WHERE start_date > '2017-01-01 00:00:00' AND '2018-01-01 00:00:00' >= start_date", 1),
array('OQL' => "SELECT UserRequest WHERE start_date > '2017-01-01 00:00:00' AND status = 'active' AND org_id = 3 AND '2018-01-01 00:00:00' >= start_date", 1),
array('OQL' => "SELECT UserRequest WHERE start_date >= '2017-01-01 00:00:00' AND '2017-01-01 00:00:00' >= start_date", 1),
array('OQL' => "SELECT UserRequest WHERE start_date >= '2017-01-01 00:00:00' AND '2017-01-01 01:00:00' > start_date", 1),
array('OQL' => "SELECT UserRequest WHERE start_date >= '2017-01-01 00:00:00' AND '2017-01-02 00:00:00' > start_date", 1),
);
}
}