Code cleanup (MakeHyperlink and GetHyperlink) + Data load and web services unit tests

SVN:code[35]
This commit is contained in:
Romain Quetiez
2009-04-14 16:19:51 +00:00
parent 65ffc8c2f8
commit 74da660194
6 changed files with 188 additions and 29 deletions

View File

@@ -58,12 +58,6 @@ class cmdbObjectHomeMade extends cmdbObject
);
MetaModel::Init_Params($aParams);
}
protected static function MakeHyperLink($sObjClass, $sObjKey, $aAvailableFields)
{
$sLabel = htmlentities(implode(' / ', $aAvailableFields));
$sHint = htmlentities("$sObjClass::$sObjKey");
return "<a href=\".blahforINFRA/?objclass=$sObjClass&objkey=$sObjKey\" title=\"$sHint\">$sLabel</a>";
}
public static function GetRelationQueries($sRelCode)
{

View File

@@ -26,7 +26,7 @@
//todo MetaModel::RegisterZList("list3", array("description"=>"la variante qui tue", "type"=>"filters"));
class Animal extends dbObject
class Animal extends cmdbObject
{
public static function Init()
{
@@ -84,7 +84,7 @@ class Mammal extends Animal
MetaModel::Init_InheritAttributes();
MetaModel::Init_AddAttribute(new AttributeString("name", array("label"=>"name", "description"=>"name", "allowed_values"=>null, "sql"=>"name", "default_value"=>"xxx", "is_null_allowed"=>false, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeInteger("height", array("label"=>"height", "description"=>"size in centimeters", "allowed_values"=>null, "sql"=>"height", "default_value"=>1, "is_null_allowed"=>false, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeDate("birth", array("label"=>"birth date", "description"=>"birth date", "allowed_values"=>null, "sql"=>"birth", "default_value"=>1, "is_null_allowed"=>false, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeDate("birth", array("label"=>"birth date", "description"=>"birth date", "allowed_values"=>null, "sql"=>"birth", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
MetaModel::Init_AddAttribute(new AttributeExternalKey("member", array("label"=>"member", "description"=>"leader", "allowed_values"=>null, "sql"=>"member", "targetclass"=>"Group", "is_null_allowed"=>true, "depends_on"=>array())));
// ? MetaModel::Init_AddAttribute(new AttributeLinkedSet("a2a", array("label"=>"animal to animal", "description"=>"interanimal relations", "depends_on"=>array(), "linked_class"=>"Animal2animal", "ext_key_to_me"=>"animal1", "count_min"=>0, "count_max"=>10, "allowed_values"=>null)));
@@ -176,7 +176,7 @@ class FlyingBird extends Bird
}
}
class AnimalRelation extends dbObject
class AnimalRelation extends cmdbObject
{
public static function Init()
{
@@ -235,7 +235,7 @@ class EaterToEaten extends AnimalRelation
}
}
class Group extends dbObject
class Group extends cmdbObject
{
public static function Init()
{

View File

@@ -287,6 +287,15 @@ abstract class DBObject
return $this->m_aCurrValues[$sAttCode];
}
public function GetOriginal($sAttCode)
{
if (!array_key_exists($sAttCode, MetaModel::ListAttributeDefs(get_class($this))))
{
trigger_error("Unknown attribute code '$sAttCode' for the class ".get_class($this), E_USER_ERROR);
}
return $this->m_aOrigValues[$sAttCode];
}
public function ComputeFields()
{
if (is_callable(array($this, 'ComputeValues')))
@@ -305,16 +314,6 @@ abstract class DBObject
}
}
public function GetHyperLink($sLabel = "")
{
if (empty($sLabel))
{
$sLabel = $this->GetName();
}
$aAvailableFields = array($sLabel);
return call_user_func(array(get_class($this), 'MakeHyperLink'), get_class($this), $this->GetKey(), $aAvailableFields);
}
public function GetAsHTML($sAttCode)
{
$sClass = get_class($this);
@@ -334,7 +333,15 @@ abstract class DBObject
}
$sTargetClass = $oAtt->GetTargetClass(EXTKEY_ABSOLUTE);
return call_user_func(array(get_class($this), 'MakeHyperLink'), $sTargetClass, $this->Get($sAttCode), $aAvailableFields);
$aMakeHLink = array(get_class($this), 'MakeHyperLink');
if (is_callable($aMakeHLink))
{
return call_user_func($aMakeHLink, $sTargetClass, $this->Get($sAttCode), $aAvailableFields);
}
else
{
return $this->Get($sAttCode);
}
}
// That's a standard attribute (might be an ext field or a direct field, etc.)

View File

@@ -2600,14 +2600,6 @@ abstract class MetaModel
return self::GetLabel($sLinkClass, $sAttCode);
}
// To be overloaded
protected static function MakeHyperLink($sObjClass, $sObjKey, $aAvailableFields)
{
$sLabel = htmlentities(implode(' / ', $aAvailableFields));
$sHint = htmlentities("$sObjClass::$sObjKey");
return "<a href=\"./?objclass=$sObjClass&objkey=$sObjKey\" title=\"$sHint\">$sLabel</a>";
}
} // class MetaModel

View File

@@ -162,6 +162,9 @@ abstract class TestHandler
}
}
/**
* Test to execute a piece of code (checks if an error occurs)
*
@@ -177,6 +180,68 @@ abstract class TestFunction extends TestHandler
// simply overload DoExecute (temporary)
}
/**
* Test to execute a piece of code (checks if an error occurs)
*
* @package iTopORM
* @author Romain Quetiez <romainquetiez@yahoo.fr>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.itop.com
* @since 1.0
* @version $itopversion$
*/
abstract class TestWebServices extends TestHandler
{
// simply overload DoExecute (temporary)
static protected function DoPostRequestAuth($sRelativeUrl, $aData, $sLogin = 'root', $sPassword = '', $sOptionnalHeaders = null)
{
$aDataAndAuth = $aData;
$aDataAndAuth['operation'] = 'login';
$aDataAndAuth['auth_user'] = $sLogin;
$aDataAndAuth['auth_pwd'] = $sPassword;
$sHost = $GLOBALS['_SERVER']['HTTP_HOST'];
$sUrl = "http://$sHost/$sRelativeUrl";
return self::DoPostRequest($sUrl, $aDataAndAuth, $sOptionnalHeaders);
}
// Source: http://netevil.org/blog/2006/nov/http-post-from-php-without-curl
// originaly named after do_post_request
// Partially adapted to our coding conventions
static protected function DoPostRequest($sUrl, $aData, $sOptionnalHeaders = null)
{
// $sOptionnalHeaders is a string containing additional HTTP headers that you would like to send in your request.
$sData = http_build_query($aData);
$aParams = array('http' => array(
'method' => 'POST',
'content' => $sData,
'header'=> "Content-type: application/x-www-form-urlencoded\r\nContent-Length: ".strlen($sData)."\r\n",
));
if ($sOptionnalHeaders !== null)
{
$aParams['http']['header'] .= $sOptionnalHeaders;
}
$ctx = stream_context_create($aParams);
$fp = @fopen($sUrl, 'rb', false, $ctx);
if (!$fp)
{
throw new Exception("Problem with $sUrl, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false)
{
throw new Exception("Problem reading data from $sUrl, $php_errormsg");
}
return $response;
}
}
/**
* Test to check that a function outputs some values depending on its input
*

View File

@@ -852,4 +852,105 @@ class TestQueriesOnFarm extends TestBizModel
}
}
///////////////////////////////////////////////////////////////////////////
// Test data load
///////////////////////////////////////////////////////////////////////////
class TestBulkChangeOnFarm extends TestBizModel
{
static public function GetName()
{
return 'Farm test - data load';
}
static public function GetDescription()
{
return 'Bulk load';
}
static public function GetConfigFile() {return '../config-test-farm.php';}
protected function DoPrepare()
{
parent::DoPrepare();
$this->ResetDB();
MetaModel::DBCheckIntegrity();
}
protected function DoExecute()
{
// $this->ReportError("Found two different SibuSQL expression out of the (same?) filter: <em>$sExpr1</em> != <em>$sExpr2</em>");
// $this->ReportSuccess('Found '.$oSet->Count()." objects of class $sClassName");
$oParser = new CSVParser("#denomination,hauteur,age
suzy,123,2009-01-01
chita,456,
");
$oParser->SetSeparator(',');
$aData = $oParser->ToArray(array('_name', '_height', '_birth'));
MyHelpers::var_dump_html($aData);
$oBulk = new BulkChange(
'Mammal',
$aData,
array('name' => '_name', 'height' => '_height', 'birth' => '_birth'),
array('name'),
array()
);
$oMyChange = MetaModel::NewObject("CMDBChange");
$oMyChange->Set("date", time());
$oMyChange->Set("userinfo", "Testor");
$iChangeId = $oMyChange->DBInsert();
// echo "Created new change: $iChangeId</br>";
echo "<h3>Planned for loading...</h3>";
$aRes = $oBulk->Process();
print_r($aRes);
echo "<h3>Go for loading...</h3>";
$aRes = $oBulk->Process($oMyChange);
print_r($aRes);
return true;
$oRawData = array(
'Mammal',
array('species', 'sex', 'speed', 'mother', 'father', 'name', 'height', 'birth'),
"human,male,23,0,0,romulus,192,1971
human,male,23,0,0,remus,154,-50
human,male,23,0,0,julius,160,-49
human,female,23,0,0,cleopatra,142,-50
pig,female,23,0,0,confucius,50,2003"
);
}
}
///////////////////////////////////////////////////////////////////////////
// Test data load
///////////////////////////////////////////////////////////////////////////
class TestItopWebServices extends TestWebServices
{
static public function GetName()
{
return 'Itop - web services';
}
static public function GetDescription()
{
return 'Bulk load and ???';
}
protected function DoExecute()
{
$aPostData = array('csvdata' => "name;code
WorldCompany;WCY
hp;HP");
$sRes = self::DoPostRequestAuth('webservices/import.php?class=bizOrganization', $aPostData);
echo "<div>$sRes</div>";
return true;
}
}
?>