From 4d136966f2753b6b254a99786d8e14a50f1cfaca Mon Sep 17 00:00:00 2001
From: Franck Villaume <franck.villaume@capgemini.com>
Date: Thu, 16 Dec 2010 11:25:59 +0100
Subject: [PATCH] new feature: get all subprojects Id of a specific project using SOAP API

---
 api/soap/mantisconnect.php  |   19 +++++++++++++++-
 api/soap/mc_project_api.php |   20 ++++++++++++++++-
 tests/soap/ProjectTest.php  |   49 +++++++++++++++++++++++++++++++++---------
 3 files changed, 74 insertions(+), 14 deletions(-)

diff --git a/api/soap/mantisconnect.php b/api/soap/mantisconnect.php
index 540e888..cfb5ea7 100644
--- a/api/soap/mantisconnect.php
+++ b/api/soap/mantisconnect.php
@@ -1269,8 +1269,7 @@ $l_oServer->register( 'mc_project_get_attachments',
 	'Get the attachments that belong to the specified project.'
 );
 
-## mc_project_get_custom_fields
-### mc_project_get_unreleased_versions
+### mc_project_get_custom_fields
 $l_oServer->register( 'mc_project_get_custom_fields',
 	array(
 		'username'		=>	'xsd:string',
@@ -1335,6 +1334,22 @@ $l_oServer->register( 'mc_project_attachment_delete',
 	'Delete the project attachment with the specified id.'
 );
 
+### mc_project_get_subprojects
+$l_oServer->register( 'mc_project_get_all_subprojects',
+    array(
+        'username'    =>  'xsd:string',
+        'password'    =>  'xsd:string',
+        'project_id'  =>  'xsd:integer'
+    ),
+    array(
+        'return'      =>  'tns:StringArray'
+    ),
+    $t_namespace,
+    false, false, false,
+    'Get the subprojects ID of a specific project.'
+);
+
+
 ###
 ###  PUBLIC METHODS (defined in mc_filter_api.php)
 ###
diff --git a/api/soap/mc_project_api.php b/api/soap/mc_project_api.php
index ff1f6dd..7dfa8af 100644
--- a/api/soap/mc_project_api.php
+++ b/api/soap/mc_project_api.php
@@ -173,7 +173,7 @@ function mc_project_delete_category ($p_username, $p_password, $p_project_id, $p
  * @return bool returns true or false depending on the success of the update action
  */
 
-function mc_project_rename_category_by_name ($p_username, $p_password, $p_project_id, $p_category_name, $p_category_name_new, $p_assigned_to) {
+function mc_project_rename_category_by_name( $p_username, $p_password, $p_project_id, $p_category_name, $p_category_name_new, $p_assigned_to ) {
         $t_user_id = mci_check_login( $p_username, $p_password );
 
         if ( null === $p_assigned_to ) {
@@ -639,6 +639,24 @@ function mc_project_get_attachments( $p_username, $p_password, $p_project_id ) {
 	return $t_result;
 }
 
+function mc_project_get_all_subprojects( $p_username, $p_password, $p_project_id ) {
+	$t_user_id = mci_check_login( $p_username, $p_password );
+
+	if( $t_user_id === false ) {
+		return new soap_fault( 'Client', '', 'Access Denied' );
+	}
+
+	if( !project_exists( $p_project_id ) ) {
+		return new soap_fault( 'Client', '', "Project '$p_project_id' does not exist." );
+	}
+
+	if( !mci_has_readonly_access( $t_user_id, $p_project_id ) ) {
+		return mci_soap_fault_access_denied( $t_user_id );
+	}
+
+	return user_get_all_accessible_subprojects( $t_user_id, $p_project_id );
+}
+
 /**
  * Get a project definition.
  *
diff --git a/tests/soap/ProjectTest.php b/tests/soap/ProjectTest.php
index 600772a..e86b759 100644
--- a/tests/soap/ProjectTest.php
+++ b/tests/soap/ProjectTest.php
@@ -32,9 +32,10 @@ class ProjectTest extends SoapBase {
 
        /**
         * A test case that tests the following:
+        *
         * 1. Create a project.
         * 2. Rename the project.
-        */        
+        */
        public function testAddRenameDeleteProject() {
                $projectName = $this->getOriginalNameProject();
                $projectNewName = $this->getNewNameProject();
@@ -76,18 +77,18 @@ class ProjectTest extends SoapBase {
                        }
                }
        }
-       
+
        /**
         * A test case which does the following
-        * 
+        *
         * 1. Create a project
         * 2. Retrieve the project id by name
-        * 
+        *
         */
        public function testGetIdFromName() {
 
-       		   $projectName = 'TestProjectForIdFromName';
-       	
+               $projectName = 'TestProjectForIdFromName';
+
                $projectDataStructure = $this->newProjectAsArray($projectName);
 
                $projectId = $this->client->mc_project_add(
@@ -101,17 +102,43 @@ class ProjectTest extends SoapBase {
                        $this->userName,
                        $this->password,
                        $projectName);
-                       
+
                 $this->assertEquals($projectIdFromName, $projectId);
        }
-       
+
+       /**
+        * A test case which does the following
+        *
+        * 1. Create a project
+        * 2. Retrieve the subproject ids. Must returns empty array.
+        *
+        */
+       public function testGetSubprojects() {
+               $projectName = $this->getOriginalNameProject();
+               $projectDataStructure = $this->newProjectAsArray($projectName);
+
+               $projectId = $this->client->mc_project_add(
+                       $this->userName,
+                       $this->password,
+                       $projectDataStructure);
+
+               $this->projectIdToDelete[] = $projectId;
+
+               $projectsArray = $this->client->mc_project_get_all_subprojects(
+                       $this->userName,
+                       $this->password,
+                       $projectId);
+
+                $this->assertEquals(0, count($projectsArray));
+       }
+
        private function newProjectAsArray($projectName) {
-       	
-       	       $projectDataStructure = array();
+
+               $projectDataStructure = array();
                $projectDataStructure['name'] = $projectName;
                $projectDataStructure['status'] = "development";
                $projectDataStructure['view_state'] = 10;
-               
+
                return $projectDataStructure;
        }
 
-- 
1.7.3

