Skip to content

Commit

Permalink
Add more REST phpunit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Aug 25, 2023
1 parent bcd3401 commit 256fe9e
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 98 deletions.
69 changes: 0 additions & 69 deletions dev/initdemo/mysqldump_dolibarr_3.5.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4655,75 +4655,6 @@ INSERT INTO `llx_menu` VALUES (19289,'all',1,'cashdesk','top','cashdesk',0,NULL,
/*!40000 ALTER TABLE `llx_menu` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `llx_milestone`
--

DROP TABLE IF EXISTS `llx_milestone`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `llx_milestone` (
`rowid` int(11) NOT NULL AUTO_INCREMENT,
`fk_element` int(11) NOT NULL,
`elementtype` varchar(16) NOT NULL,
`label` varchar(255) NOT NULL,
`options` varchar(255) DEFAULT NULL,
`priority` int(11) DEFAULT '0',
`tms` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`fk_user_modif` int(11) DEFAULT NULL,
PRIMARY KEY (`rowid`),
UNIQUE KEY `uk_milestone_fk_element` (`fk_element`,`elementtype`),
KEY `idx_milestone_fk_user_modif` (`fk_user_modif`),
CONSTRAINT `fk_milestone_fk_user_modif` FOREIGN KEY (`fk_user_modif`) REFERENCES `llx_user` (`rowid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `llx_milestone`
--

LOCK TABLES `llx_milestone` WRITE;
/*!40000 ALTER TABLE `llx_milestone` DISABLE KEYS */;
INSERT INTO `llx_milestone` VALUES (2,779,'facture','azerty',NULL,0,'2013-03-09 12:19:30',NULL),(3,780,'facture','fsdf',NULL,0,'2013-03-09 13:01:08',NULL),(4,781,'facture','hhh',NULL,0,'2013-03-09 14:06:37',NULL);
/*!40000 ALTER TABLE `llx_milestone` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `llx_monitoring_probes`
--

DROP TABLE IF EXISTS `llx_monitoring_probes`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `llx_monitoring_probes` (
`rowid` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(64) NOT NULL,
`groupname` varchar(64) DEFAULT NULL,
`url` varchar(250) NOT NULL,
`useproxy` int(11) DEFAULT '0',
`checkkey` varchar(250) DEFAULT NULL,
`maxval` int(11) DEFAULT NULL,
`frequency` int(11) DEFAULT '60',
`active` int(11) DEFAULT '1',
`status` int(11) DEFAULT '0',
`lastreset` datetime DEFAULT NULL,
`oldesterrortext` text,
`oldesterrordate` datetime DEFAULT NULL,
PRIMARY KEY (`rowid`),
UNIQUE KEY `uk_monitoring_probes` (`title`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `llx_monitoring_probes`
--

LOCK TABLES `llx_monitoring_probes` WRITE;
/*!40000 ALTER TABLE `llx_monitoring_probes` DISABLE KEYS */;
INSERT INTO `llx_monitoring_probes` VALUES (1,'aaa',NULL,'http://www.chiensderace.com',0,'chiens',1000,10,1,1,'2011-04-20 23:46:41',NULL,NULL),(2,'ChatsDeRace',NULL,'http://www.chatsderace.com',0,'chats',1000,5,1,1,'2011-04-20 23:46:41',NULL,NULL);
/*!40000 ALTER TABLE `llx_monitoring_probes` ENABLE KEYS */;
UNLOCK TABLES;

--
-- Table structure for table `llx_notify`
--
Expand Down
4 changes: 2 additions & 2 deletions htdocs/core/lib/files.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ function dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disable
*/
function dol_delete_file($file, $disableglob = 0, $nophperrors = 0, $nohook = 0, $object = null, $allowdotdot = false, $indexdatabase = 1, $nolog = 0)
{
global $db, $conf, $user, $langs;
global $db, $user, $langs;
global $hookmanager;

// Load translation files required by the page
Expand All @@ -1351,7 +1351,7 @@ function dol_delete_file($file, $disableglob = 0, $nophperrors = 0, $nohook = 0,
}

$reshook = 0;
if (empty($nohook)) {
if (empty($nohook) && !empty($hookmanager)) {
$hookmanager->initHooks(array('fileslib'));

$parameters = array(
Expand Down
66 changes: 48 additions & 18 deletions test/phpunit/RestAPIContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,24 @@ protected function setUp(): void
$langs=$this->savlangs;
$db=$this->savdb;

$this->api_url=DOL_MAIN_URL_ROOT.'/api/index.php';
$this->api_url = DOL_MAIN_URL_ROOT.'/api/index.php';

$this->api_key = 'admin'; // Test on API to get this token is inside RestAPIUserTest.php
$login='admin';
$password='admin';
$url=$this->api_url.'/login?login='.$login.'&password='.$password;
// Call the API login method to save api_key for this test class.
// At first call, if token is not defined a random value is generated and returned.
$result=getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 2);
print __METHOD__." result = ".var_export($result, true)."\n";
print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
$this->assertEquals($result['curl_error_no'], '');
$object = json_decode($result['content'], true); // If success content is just an id, if not an array

$this->assertNotNull($object, "Parsing of json result must not be null");
$this->assertNotEquals(500, $object['error']['code'], $object['error']['code'].' '.$object['error']['message']);
$this->assertEquals('200', $object['success']['code']);

$this->api_key = $object['success']['token'];

print __METHOD__." api_key: $this->api_key \n";
}
Expand Down Expand Up @@ -201,10 +216,12 @@ public function testRestCreateContact()
//print __METHOD__." Result for creating incomplete contact".var_export($result, true)."\n";
//print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
$this->assertEquals($result['curl_error_no'], '');
$object=json_decode($result['content'], true);
$object = json_decode($result['content'], true); // If success content is just an id, if not an array
$this->assertNotNull($object, "Parsing of json result must no be null");
$this->assertEquals(400, $object['error']['code'], $object['error']['code'].' '.$object['error']['message']);

$idofcontactcreated = (int) $object;

// create regular contact
unset($result);
// Creating a Contact
Expand All @@ -219,21 +236,40 @@ public function testRestCreateContact()

$this->assertEquals($result['curl_error_no'], '');

$resid = json_decode($result['content'], true);
$this->assertNotNull($resid, "Parsing of json result must not be null");
$this->assertGreaterThan(0, $resid, $object['error']['code'].' '.$object['error']['message']);
$object = json_decode($result['content'], true); // If success content is just an id, if not an array
$this->assertNotNull($object, "Parsing of json result must not be null");
$this->assertNotEquals(500, $object['error']['code'], $object['error']['code'].' '.$object['error']['message']);
$this->assertGreaterThan(0, $object, $object['error']['code'].' '.$object['error']['message']);

return $idofcontactcreated;
}

/**
* testRestUpdateContact
*
* @param int $objid Id of object created at previous test
* @return int
*
* @depends testRestCreateContact
* The depends says test is run only if previous is ok
*/
public function testRestUpdateContact($objid)
{
global $conf,$user,$langs,$db;
// attempt to create without mandatory fields
$url = $this->api_url.'/contacts?api_key='.$this->api_key;
$addheaders=array('Content-Type: application/json');

//update the contact

// Update the firstname of the contact
/*
$updateBody = array(
"firstname" => "UpdatedFirstName",
);

$updateRequestBody = json_encode($updateBody);
$updateUrl = $this->api_url . '/contacts/' . $resid. '?api_key=' . $this->api_key;
$updateResult = getURLContent($updateUrl, 'PUT', $updateRequestBody, 1, $addheaders, array('http', 'https'), 2);
$updateUrl = $this->api_url . '/contacts/' . $objid. '?api_key=' . $this->api_key;
$updateResult = getURLContent($updateUrl, 'PUTALREADYFORMATED', $updateRequestBody, 1, $addheaders, array('http', 'https'), 2);
$this->assertEquals($updateResult['curl_error_no'], '');

$updateResponse = json_decode($updateResult['content'], true);
Expand All @@ -242,17 +278,11 @@ public function testRestCreateContact()
print_r($updateResponse);

// Check if the updated fields match the changes you made
if ($updateResponse['firstname'] === $updateBody['firstname']) {
// Update was successful
$this->assertTrue(true);
} else {
// Update might have failed
$this->assertTrue(false, "Update might have failed");
}
$this->assertTrue($updateResponse['firstname'] === $updateBody['firstname'], 'Update might have failed');

// Deleting the Contact
$deleteUrl = $this->api_url . '/contacts/' . $resid . '?api_key=' . $this->api_key;
/*
$deleteUrl = $this->api_url . '/contacts/' . $objid . '?api_key=' . $this->api_key;
$deleteResult = getURLContent($deleteUrl, 'DELETE', '', 1, $addheaders, array('http', 'https'), 2);
Expand Down
21 changes: 20 additions & 1 deletion test/phpunit/RestAPIDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public static function tearDownAfterClass(): void

/**
* Init phpunit tests.
*
* @return void
*/
protected function setUp(): void
Expand All @@ -118,13 +119,29 @@ protected function setUp(): void

$this->api_url = DOL_MAIN_URL_ROOT.'/api/index.php';

$this->api_key = 'admin'; // Test on API to get this token is inside RestAPIUserTest.php
$login='admin';
$password='admin';
$url=$this->api_url.'/login?login='.$login.'&password='.$password;
// Call the API login method to save api_key for this test class.
// At first call, if token is not defined a random value is generated and returned.
$result=getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 2);
print __METHOD__." result = ".var_export($result, true)."\n";
print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
$this->assertEquals($result['curl_error_no'], '');
$object = json_decode($result['content'], true); // If success content is just an id, if not an array

$this->assertNotNull($object, "Parsing of json result must not be null");
$this->assertNotEquals(500, $object['error']['code'], $object['error']['code'].' '.$object['error']['message']);
$this->assertEquals('200', $object['success']['code']);

$this->api_key = $object['success']['token'];

echo __METHOD__." api_key: $this->api_key \n";
}

/**
* End phpunit tests.
*
* @return void
*/
protected function tearDown(): void
Expand Down Expand Up @@ -235,5 +252,7 @@ public function testPushDocument()


dol_delete_dir_recursive(DOL_DATA_ROOT.'/medias/tmpphpunit');

return 0;
}
}
22 changes: 14 additions & 8 deletions test/phpunit/RestAPIUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,25 @@ protected function setUp(): void
$langs=$this->savlangs;
$db=$this->savdb;

$this->api_url=DOL_MAIN_URL_ROOT.'/api/index.php';
$this->api_url = DOL_MAIN_URL_ROOT.'/api/index.php';

$login='admin';
$password='admin';
$url=$this->api_url.'/login?login='.$login.'&password='.$password;
// Call the API login method to save api_key for this test class
// Call the API login method to save api_key for this test class.
// At first call, if token is not defined a random value is generated and returned.
$result=getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 2);
print __METHOD__." result = ".var_export($result, true)."\n";
print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
$this->assertEquals($result['curl_error_no'], '');
$object=json_decode($result['content'], true);
$object = json_decode($result['content'], true); // If success content is just an id, if not an array

$this->assertNotNull($object, "Parsing of json result must not be null");
$this->assertNotEquals(500, $object['error']['code'], $object['error']['code'].' '.$object['error']['message']);
$this->assertEquals('200', $object['success']['code']);

$this->api_key = $object['success']['token'];

print __METHOD__." api_key: $this->api_key \n";
}

Expand Down Expand Up @@ -223,17 +227,19 @@ public function testRestCreateUser()
"login"=>"testRestLogin".mt_rand(),
"lastname"=>"testRestUser",
"password"=>"testRestPassword",
"email"=>"test@restuser.com"
"email"=>"test".mt_rand()."@restuser.com"
);
$body = json_encode($bodyobj);
print __METHOD__." Request POST url=".$url."\n";
$result=getURLContent($url, 'POST', $body, 1, $addheaders, array('http', 'https'), 2);
print __METHOD__." rclsesult code for creating user ".var_export($result, true)."\n";
print __METHOD__." result code for creating non existing user = ".var_export($result, true)."\n";
print __METHOD__." curl_error_no: ".$result['curl_error_no']."\n";
$this->assertEquals($result['curl_error_no'], '');
$resid=json_decode($result['content'], true);
$this->assertNotNull($resid, "Parsing of json result must no be null");
$this->assertGreaterThan(0, $resid, $object['error']['code'].' '.$object['error']['message']);
$object = json_decode($result['content'], true); // If success content is just an id, if not an array

$this->assertNotNull($object, "Parsing of json result must no be null");
$this->assertNotEquals(500, $object['error']['code'], $object['error']['code'].' '.$object['error']['message']);
$this->assertGreaterThan(0, $object, $object['error']['code'].' '.$object['error']['message']);

// attempt to create duplicated user
print __METHOD__." Request POST url=".$url."\n";
Expand Down

0 comments on commit 256fe9e

Please sign in to comment.