Browse Source

Add more REST phpunit tests

Laurent Destailleur 1 year ago
parent
commit
256fe9ecf8

+ 0 - 69
dev/initdemo/mysqldump_dolibarr_3.5.0.sql

@@ -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`
 --

+ 2 - 2
htdocs/core/lib/files.lib.php

@@ -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
@@ -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(

+ 48 - 18
test/phpunit/RestAPIContactTest.php

@@ -123,9 +123,24 @@ class RestAPIContactTest extends PHPUnit\Framework\TestCase
 		$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";
 	}
@@ -201,10 +216,12 @@ class RestAPIContactTest extends PHPUnit\Framework\TestCase
 		//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
@@ -219,21 +236,40 @@ class RestAPIContactTest extends PHPUnit\Framework\TestCase
 
 		$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);
@@ -242,17 +278,11 @@ class RestAPIContactTest extends PHPUnit\Framework\TestCase
 		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);
 

+ 20 - 1
test/phpunit/RestAPIDocumentTest.php

@@ -106,6 +106,7 @@ class RestAPIDocumentTest extends PHPUnit\Framework\TestCase
 
 	/**
 	 * Init phpunit tests.
+	 *
 	 * @return void
 	 */
 	protected function setUp(): void
@@ -118,13 +119,29 @@ class RestAPIDocumentTest extends PHPUnit\Framework\TestCase
 
 		$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
@@ -235,5 +252,7 @@ class RestAPIDocumentTest extends PHPUnit\Framework\TestCase
 
 
 		dol_delete_dir_recursive(DOL_DATA_ROOT.'/medias/tmpphpunit');
+
+		return 0;
 	}
 }

+ 14 - 8
test/phpunit/RestAPIUserTest.php

@@ -123,21 +123,25 @@ class RestAPIUserTest extends PHPUnit\Framework\TestCase
 		$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";
 	}
 
@@ -223,17 +227,19 @@ class RestAPIUserTest extends PHPUnit\Framework\TestCase
 			"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";