mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-23 02:28:44 +02:00
New methods for creating/import test data from XML files.
This commit is contained in:
@@ -28,6 +28,7 @@ namespace Combodo\iTop\Test\UnitTest;
|
||||
|
||||
use ArchivedObjectException;
|
||||
use CMDBSource;
|
||||
use CMDBObject;
|
||||
use Contact;
|
||||
use DBObject;
|
||||
use DBObjectSet;
|
||||
@@ -47,6 +48,7 @@ use Ticket;
|
||||
use URP_UserProfile;
|
||||
use VirtualHost;
|
||||
use VirtualMachine;
|
||||
use XMLDataLoader;
|
||||
|
||||
|
||||
/** @see \Combodo\iTop\Test\UnitTest\ItopDataTestCase::CreateObjectWithTagSet() */
|
||||
@@ -806,4 +808,43 @@ class ItopDataTestCase extends ItopTestCase
|
||||
static::assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Import a set of XML files describing a consistent set of iTop objects
|
||||
* @param string[] $aFiles
|
||||
* @param boolean $bSearch If true, a search will be performed on each object (based on its reconciliation keys)
|
||||
* before trying to import it (existing objects will be updated)
|
||||
* @return int Number of objects created
|
||||
*/
|
||||
protected function CreateFromXMLFiles($aFiles, $bSearch = false)
|
||||
{
|
||||
$oLoader = new XMLDataLoader();
|
||||
$oLoader->StartSession(CMDBObject::GetCurrentChange());
|
||||
foreach($aFiles as $sFilePath)
|
||||
{
|
||||
$oLoader->LoadFile($sFilePath, false, $bSearch);
|
||||
}
|
||||
$oLoader->EndSession();
|
||||
|
||||
return $oLoader->GetCountCreated();
|
||||
}
|
||||
|
||||
/**
|
||||
* Import a consistent set of iTop objects from the specified XML text string
|
||||
* @param string $sXmlDataset
|
||||
* @param boolean $bSearch If true, a search will be performed on each object (based on its reconciliation keys)
|
||||
* before trying to import it (existing objects will be updated)
|
||||
* @return int The number of objects created
|
||||
*/
|
||||
protected function CreateFromXMLString($sXmlDataset, $bSearch = false)
|
||||
{
|
||||
$sTmpFileName = tempnam(sys_get_temp_dir(), 'xml');
|
||||
file_put_contents($sTmpFileName, $sXmlDataset);
|
||||
|
||||
$ret = $this->CreateFromXMLFiles([$sTmpFileName], $bSearch);
|
||||
|
||||
unlink($sTmpFileName);
|
||||
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
|
||||
118
test/core/XMLDataLoaderTest.php
Normal file
118
test/core/XMLDataLoaderTest.php
Normal file
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
// Copyright (c) 2021 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/>
|
||||
//
|
||||
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: Eric
|
||||
* Date: 02/10/2017
|
||||
* Time: 13:58
|
||||
*/
|
||||
|
||||
namespace Combodo\iTop\Test\UnitTest\Core;
|
||||
|
||||
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
|
||||
use DBObject;
|
||||
use MetaModel;
|
||||
|
||||
|
||||
/**
|
||||
* @group specificOrgInSampleData
|
||||
* @runTestsInSeparateProcesses
|
||||
* @preserveGlobalState disabled
|
||||
* @backupGlobals disabled
|
||||
*/
|
||||
class XMLDataLoaderTest extends ItopDataTestCase
|
||||
{
|
||||
const CREATE_TEST_ORG = true;
|
||||
|
||||
public function testDataLoader()
|
||||
{
|
||||
$sXML =
|
||||
<<<XML
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Set>
|
||||
<Organization alias="Organization" id="71">
|
||||
<name>ZuperTest</name>
|
||||
<code></code>
|
||||
<status>active</status>
|
||||
<parent_id>0</parent_id>
|
||||
<parent_id_friendlyname></parent_id_friendlyname>
|
||||
<parent_name></parent_name>
|
||||
<parent_id_obsolescence_flag>no</parent_id_obsolescence_flag>
|
||||
<overview></overview>
|
||||
<deliverymodel_id>0</deliverymodel_id>
|
||||
<deliverymodel_id_friendlyname></deliverymodel_id_friendlyname>
|
||||
<deliverymodel_name></deliverymodel_name>
|
||||
<friendlyname>ZuperTest</friendlyname>
|
||||
<obsolescence_flag>no</obsolescence_flag>
|
||||
<obsolescence_date></obsolescence_date>
|
||||
</Organization>
|
||||
<Location alias="Location" id="4">
|
||||
<name>Zanzibar</name>
|
||||
<status>active</status>
|
||||
<org_id>71</org_id>
|
||||
<org_id_friendlyname>ZuperTest</org_id_friendlyname>
|
||||
<org_name>ZuperTest</org_name>
|
||||
<org_id_obsolescence_flag>no</org_id_obsolescence_flag>
|
||||
<address></address>
|
||||
<postal_code></postal_code>
|
||||
<city></city>
|
||||
<country></country>
|
||||
<friendlyname>Zanzibar</friendlyname>
|
||||
<obsolescence_flag>no</obsolescence_flag>
|
||||
<obsolescence_date></obsolescence_date>
|
||||
</Location>
|
||||
<Person alias="Person" id="60">
|
||||
<name>Zmillpatt</name>
|
||||
<status>active</status>
|
||||
<org_id>71</org_id>
|
||||
<org_id_friendlyname>ZuperTest</org_id_friendlyname>
|
||||
<org_name>ZuperTest</org_name>
|
||||
<org_id_obsolescence_flag>no</org_id_obsolescence_flag>
|
||||
<email></email>
|
||||
<phone></phone>
|
||||
<notify>yes</notify>
|
||||
<function></function>
|
||||
<picture></picture>
|
||||
<first_name>Zacharie</first_name>
|
||||
<employee_number></employee_number>
|
||||
<mobile_phone></mobile_phone>
|
||||
<location_id>4</location_id>
|
||||
<location_id_friendlyname>Zanzibar</location_id_friendlyname>
|
||||
<location_name>Zanzibar</location_name>
|
||||
<location_id_obsolescence_flag>no</location_id_obsolescence_flag>
|
||||
<manager_id>0</manager_id>
|
||||
<manager_id_friendlyname></manager_id_friendlyname>
|
||||
<manager_name></manager_name>
|
||||
<manager_id_obsolescence_flag>no</manager_id_obsolescence_flag>
|
||||
<finalclass>Person</finalclass>
|
||||
<friendlyname>Zacharie Zmillpatt</friendlyname>
|
||||
<obsolescence_flag>no</obsolescence_flag>
|
||||
<obsolescence_date></obsolescence_date>
|
||||
</Person>
|
||||
</Set>
|
||||
XML;
|
||||
$this->CreateFromXMLString($sXML);
|
||||
|
||||
$oPerson = MetaModel::GetObjectByName('Person', 'Zacharie Zmillpatt');
|
||||
|
||||
$this->assertEquals('Zanzibar', $oPerson->Get('location_id_friendlyname'));
|
||||
$this->assertEquals('ZuperTest', $oPerson->Get('org_id_friendlyname'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user