From 05a7ab158b51a05dbb38ac7b02abf0d2c1df9891 Mon Sep 17 00:00:00 2001 From: Arnaud Aujon Date: Thu, 28 May 2015 15:40:38 +0200 Subject: [PATCH] add deleteThirdParty api call + add tests --- htdocs/webservices/server_thirdparty.php | 106 +++++++++ test/phpunit/WebservicesThirdpartyTest.php | 258 +++++++++++++++++++-- 2 files changed, 343 insertions(+), 21 deletions(-) diff --git a/htdocs/webservices/server_thirdparty.php b/htdocs/webservices/server_thirdparty.php index c7ca5911d62..542e2980c45 100644 --- a/htdocs/webservices/server_thirdparty.php +++ b/htdocs/webservices/server_thirdparty.php @@ -258,8 +258,31 @@ $server->register( 'WS to get list of thirdparties id and ref' ); +// Register WSDL +$server->register( + 'deleteThirdParty', + // Entry values + array('authentication'=>'tns:authentication','id'=>'xsd:string','ref'=>'xsd:string','ref_ext'=>'xsd:string'), + // Exit values + array('result'=>'tns:result','id'=>'xsd:string'), + $ns, + $ns.'#deleteThirdParty', + $styledoc, + $styleuse, + 'WS to delete a thirdparty from its id, ref or ref_ext' +); + // Full methods code +/** + * Get a thirdparty + * + * @param array $authentication Array of authentication information + * @param string $id internal id + * @param string $ref internal reference + * @param string $ref_ext external reference + * @return array Array result + */ function getThirdParty($authentication,$id='',$ref='',$ref_ext='') { global $db,$conf,$langs; @@ -728,5 +751,88 @@ function getListOfThirdParties($authentication,$filterthirdparty) return $objectresp; } +/** + * Delete a thirdparty + * + * @param array $authentication Array of authentication information + * @param string $id internal id + * @param string $ref internal reference + * @param string $ref_ext external reference + * @return array Array result + */ +function deleteThirdParty($authentication,$id='',$ref='',$ref_ext='') +{ + global $db,$conf,$langs; + + dol_syslog("Function: deleteThirdParty login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext); + + if ($authentication['entity']) $conf->entity=$authentication['entity']; + + // Init and check authentication + $objectresp=array(); + $errorcode='';$errorlabel=''; + $error=0; + $fuser=check_authentication($authentication,$error,$errorcode,$errorlabel); + // Check parameters + if (! $error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) + { + dol_syslog("Function: deleteThirdParty checkparam"); + $error++; + $errorcode='BAD_PARAMETERS'; $errorlabel="Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both."; + } + dol_syslog("Function: deleteThirdParty 1"); + + if (! $error) + { + $fuser->getrights(); + + if ($fuser->rights->societe->lire && $fuser->rights->societe->supprimer) + { + $thirdparty=new Societe($db); + $result=$thirdparty->fetch($id,$ref,$ref_ext); + + if ($result > 0) + { + $db->begin(); + + $result=$thirdparty->delete($thirdparty->id, $fuser); + + if ($result > 0) + { + $db->commit(); + + $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>'')); + } + else + { + $db->rollback(); + $error++; + $errorcode='KO'; + $errorlabel=$thirdparty->error; + dol_syslog("Function: deleteThirdParty cant delete"); + } + } + else + { + $error++; + $errorcode='NOT_FOUND'; $errorlabel='Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext; + + } + } + else + { + $error++; + $errorcode='PERMISSION_DENIED'; $errorlabel='User does not have permission for this request'; + } + } + + if ($error) + { + $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel)); + } + + return $objectresp; +} + // Return the results. $server->service(file_get_contents("php://input")); diff --git a/test/phpunit/WebservicesThirdpartyTest.php b/test/phpunit/WebservicesThirdpartyTest.php index 0c99d7d362a..71c90b3f636 100755 --- a/test/phpunit/WebservicesThirdpartyTest.php +++ b/test/phpunit/WebservicesThirdpartyTest.php @@ -52,6 +52,13 @@ class WebservicesThirdpartyTest extends PHPUnit_Framework_TestCase protected $savuser; protected $savlangs; protected $savdb; + protected $soapclient; + + private $WS_DOL_URL; + private $ns='http://www.dolibarr.org/ns/'; + + + /** * Constructor @@ -67,6 +74,16 @@ class WebservicesThirdpartyTest extends PHPUnit_Framework_TestCase $this->savuser=$user; $this->savlangs=$langs; $this->savdb=$db; + + $this->WS_DOL_URL = DOL_MAIN_URL_ROOT.'/webservices/server_thirdparty.php'; + + // Set the WebService URL + print __METHOD__." create nusoap_client for URL=".$this->WS_DOL_URL."\n"; + $this->soapclient = new nusoap_client($this->WS_DOL_URL); + if ($this->soapclient) { + $this->soapclient->soap_defencoding='UTF-8'; + $this->soapclient->decodeUTF8(false); + } print __METHOD__." db->type=".$db->type." user->id=".$user->id; //print " - db ".$db->db; @@ -117,31 +134,113 @@ class WebservicesThirdpartyTest extends PHPUnit_Framework_TestCase print __METHOD__."\n"; } - /** - * testWSThirdpartygetThirdParty + * testWSThirdpartycreateThirdParty * * @return int */ - public function testWSThirdpartygetThirdParty() + public function testWSThirdpartycreateThirdParty() + { + global $conf,$user,$langs,$db; + $conf=$this->savconf; + $user=$this->savuser; + $langs=$this->savlangs; + $db=$this->savdb; + + $WS_METHOD = 'createThirdParty'; + + + // Call the WebService method and store its result in $result. + $authentication=array( + 'dolibarrkey'=>$conf->global->WEBSERVICES_KEY, + 'sourceapplication'=>'DEMO', + 'login'=>'admin', + 'password'=>'admin', + 'entity'=>''); + + $body = array ( + "id" => NULL, + "ref" => "name", + "ref_ext" => "12", + "fk_user_author" => NULL, + "status" => NULL, + "client" => 1, + "supplier" => 0, + "customer_code" => "", + "supplier_code" => "", + "customer_code_accountancy" => "", + "supplier_code_accountancy" => "", + "date_creation" => "", // dateTime + "date_modification" => "", // dateTime + "note_private" => "", + "note_public" => "", + "address" => "", + "zip" => "", + "town" => "", + "province_id" => "", + "country_id" => "", + "country_code" => "", + "country" => "", + "phone" => "", + "fax" => "", + "email" => "", + "url" => "", + "profid1" => "", + "profid2" => "", + "profid3" => "", + "profid4" => "", + "profid5" => "", + "profid6" => "", + "capital" => "", + "vat_used" => "", + "vat_number" => "" + ); + + // Test URL + $result=''; + $parameters = array('authentication'=>$authentication, 'thirdparty'=>$body); + print __METHOD__." call method ".$WS_METHOD."\n"; + try { + $result = $this->soapclient->call($WS_METHOD,$parameters,$thid->ns,''); + } catch(SoapFault $exception) { + echo $exception; + $result=0; + } + if (! $result || ! empty($result['faultstring'])) { + //var_dump($soapclient); + print $this->soapclient->error_str; + print "\n
\n"; + print $this->soapclient->request; + print "\n
\n"; + print $this->soapclient->response; + print "\n"; + } + + print __METHOD__." result=".$result['result']['result_code']."\n"; + $this->assertEquals('OK',$result['result']['result_code']); + $this->assertEquals('name',$result['ref']); + + return $result; + } + + /** + * testWSThirdpartygetThirdPartyById + * + * Use id to retrieve thirdparty + * @depends testWSThirdpartycreateThirdParty + * + * @return int + */ + public function testWSThirdpartygetThirdPartyById($result) { global $conf,$user,$langs,$db; $conf=$this->savconf; $user=$this->savuser; $langs=$this->savlangs; $db=$this->savdb; + $id = $result['id']; - $WS_DOL_URL = DOL_MAIN_URL_ROOT.'/webservices/server_thirdparty.php'; $WS_METHOD = 'getThirdParty'; - $ns='http://www.dolibarr.org/ns/'; - - // Set the WebService URL - print __METHOD__." create nusoap_client for URL=".$WS_DOL_URL."\n"; - $soapclient = new nusoap_client($WS_DOL_URL); - if ($soapclient) { - $soapclient->soap_defencoding='UTF-8'; - $soapclient->decodeUTF8(false); - } // Call the WebService method and store its result in $result. $authentication=array( @@ -151,30 +250,147 @@ class WebservicesThirdpartyTest extends PHPUnit_Framework_TestCase 'password'=>'admin', 'entity'=>''); - // Test URL $result=''; - $parameters = array('authentication'=>$authentication, 'id'=>1); + $parameters = array('authentication'=>$authentication, 'id'=>$id); print __METHOD__." call method ".$WS_METHOD."\n"; try { - $result = $soapclient->call($WS_METHOD,$parameters,$ns,''); + $result = $this->soapclient->call($WS_METHOD,$parameters,$this->ns,''); } catch(SoapFault $exception) { echo $exception; $result=0; } if (! $result || ! empty($result['faultstring'])) { //var_dump($soapclient); - print $soapclient->error_str; + print $this->soapclient->error_str; print "\n
\n"; - print $soapclient->request; + print $this->soapclient->request; print "\n
\n"; - print $soapclient->response; + print $this->soapclient->response; print "\n"; } - print __METHOD__." result=".$result."\n"; + print __METHOD__." result=".$result['result']['result_code']."\n"; $this->assertEquals('OK',$result['result']['result_code']); - + $this->assertEquals($id, $result['thirdparty']['id']); + $this->assertEquals('name', $result['thirdparty']['ref']); + $this->assertEquals('12', $result['thirdparty']['ref_ext']); + $this->assertEquals('0', $result['thirdparty']['status']); + $this->assertEquals('1', $result['thirdparty']['client']); + $this->assertEquals('0', $result['thirdparty']['supplier']); + + return $result; } + + /** + * testWSThirdpartygetThirdPartyByRefExt + * + * Use ref_ext to retrieve thirdparty + * + * @depends testWSThirdpartycreateThirdParty + * @return int + */ + public function testWSThirdpartygetThirdPartyByRefExt($result) + { + global $conf,$user,$langs,$db; + $conf=$this->savconf; + $user=$this->savuser; + $langs=$this->savlangs; + $db=$this->savdb; + $id = $result['id']; + + $WS_METHOD = 'getThirdParty'; + + // Call the WebService method and store its result in $result. + $authentication=array( + 'dolibarrkey'=>$conf->global->WEBSERVICES_KEY, + 'sourceapplication'=>'DEMO', + 'login'=>'admin', + 'password'=>'admin', + 'entity'=>''); + + // Test URL + $result=''; + $parameters = array('authentication'=>$authentication, 'id'=>'', 'ref'=>'', 'ref_ext'=>'12'); + print __METHOD__." call method ".$WS_METHOD."\n"; + try { + $result = $this->soapclient->call($WS_METHOD,$parameters,$this->ns,''); + } catch(SoapFault $exception) { + echo $exception; + $result=0; + } + print $this->soapclient->response; + if (! $result || ! empty($result['faultstring'])) { + //var_dump($soapclient); + print $this->soapclient->error_str; + print "\n
\n"; + print $this->soapclient->request; + print "\n
\n"; + print $this->soapclient->response; + print "\n"; + } + + print __METHOD__." result=".$result['result']['result_code']."\n"; + $this->assertEquals('OK',$result['result']['result_code']); + $this->assertEquals($id, $result['thirdparty']['id']); + $this->assertEquals('name', $result['thirdparty']['ref']); + $this->assertEquals('12', $result['thirdparty']['ref_ext']); + $this->assertEquals('0', $result['thirdparty']['status']); + $this->assertEquals('1', $result['thirdparty']['client']); + $this->assertEquals('0', $result['thirdparty']['supplier']); + + + return $result; + } + + /** + * testWSThirdpartydeleteThirdParty + * + * @depends testWSThirdpartycreateThirdParty + * + * @return int + */ + public function testWSThirdpartydeleteThirdPartyById($result) + { + global $conf,$user,$langs,$db; + $conf=$this->savconf; + $user=$this->savuser; + $langs=$this->savlangs; + $db=$this->savdb; + $id = $result['id']; + + $WS_METHOD = 'deleteThirdParty'; + + // Call the WebService method and store its result in $result. + $authentication=array( + 'dolibarrkey'=>$conf->global->WEBSERVICES_KEY, + 'sourceapplication'=>'DEMO', + 'login'=>'admin', + 'password'=>'admin', + 'entity'=>''); + + $result=''; + $parameters = array('authentication'=>$authentication, 'id'=>$id, 'ref'=>'', 'ref_ext'=>''); + print __METHOD__." call method ".$WS_METHOD."\n"; + try { + $result = $this->soapclient->call($WS_METHOD,$parameters,$this->ns,''); + } catch(SoapFault $exception) { + echo $exception; + $result=0; + } + //if (! $result || ! empty($result['faultstring'])) { + print $this->soapclient->error_str; + print "\n
\n"; + print $this->soapclient->request; + print "\n
\n"; + print $this->soapclient->response; + print "\n"; + //} + + print __METHOD__." result=".$result['result']['result_code']."\n"; + $this->assertEquals('OK',$result['result']['result_code']); + + return $result; + } }