From 3f43fd16b7cc2ab773826dfb80bd772710d159b5 Mon Sep 17 00:00:00 2001
From: Franck Villaume <franck.villaume@capgemini.com>
Date: Mon, 16 May 2011 17:20:09 +0200
Subject: [PATCH] new feature soap api: update note for a specific issue

---
 api/soap/mantisconnect.php   |   23 +++++++++-
 api/soap/mc_issue_api.php    |   94 +++++++++++++++++++++++++++++++++--------
 tests/soap/IssueNoteTest.php |   41 ++++++++++++++----
 3 files changed, 127 insertions(+), 31 deletions(-)

diff --git a/api/soap/mantisconnect.php b/api/soap/mantisconnect.php
index 0c99df5..3c6588d 100644
--- a/api/soap/mantisconnect.php
+++ b/api/soap/mantisconnect.php
@@ -894,6 +894,24 @@ $l_oServer->register( 'mc_issue_note_delete',
 	'Delete the note with the specified id.'
 );
 
+### mc_issue_note_add
+$l_oServer->register( 'mc_issue_note_update',
+	array(
+		'username'	=>	'xsd:string',
+		'password'	=>	'xsd:string',
+		'issue_id'	=>	'xsd:integer',
+		'issue_note_id'	=>	'xsd:integer',
+		'note'		=>	'tns:IssueNoteData',
+		'type_update'	=>	'tns:string'
+	),
+	array(
+		'return'	=>	'xsd:integer'
+	),
+	$t_namespace,
+	false, false, false,
+	'Submit a new note.'
+);
+
 ### mc_issue_relationship_add
 $l_oServer->register( 'mc_issue_relationship_add',
 	array(
@@ -1028,8 +1046,8 @@ $l_oServer->register( 'mc_project_update',
 $l_oServer->register( 'mc_project_get_id_from_name',
 	array(
 		'username' => 'xsd:string',
-		'password' => 'xsd:string',	
-		'project_name' => 'xsd:string'		
+		'password' => 'xsd:string',
+		'project_name' => 'xsd:string'
 	),
 	array(
 		'return' => 'xsd:integer'
@@ -1356,7 +1374,6 @@ $l_oServer->register( 'mc_project_get_all_subprojects',
     'Get the subprojects ID of a specific project.'
 );
 
-
 ###
 ###  PUBLIC METHODS (defined in mc_filter_api.php)
 ###
diff --git a/api/soap/mc_issue_api.php b/api/soap/mc_issue_api.php
index aaaf941..5ebf46b 100644
--- a/api/soap/mc_issue_api.php
+++ b/api/soap/mc_issue_api.php
@@ -111,7 +111,7 @@ function mc_issue_get( $p_username, $p_password, $p_issue_id ) {
 	$t_issue_data['notes'] = mci_issue_get_notes( $p_issue_id );
 	$t_issue_data['custom_fields'] = mci_issue_get_custom_fields( $p_issue_id );
 	$t_issue_data['monitors'] = mci_account_get_array_by_ids( bug_get_monitors ( $p_issue_id ) );
-	
+
 	return $t_issue_data;
 }
 
@@ -320,7 +320,7 @@ function mci_issue_get_notes( $p_issue_id ) {
 		$t_bugnote['time_tracking'] = $t_has_time_tracking_access ? $t_value->time_tracking : 0;
 		$t_bugnote['note_type'] = $t_value->note_type;
 		$t_bugnote['note_attr'] = $t_value->note_attr;
-		
+
 		$t_result[] = $t_bugnote;
 	}
 
@@ -329,25 +329,25 @@ function mci_issue_get_notes( $p_issue_id ) {
 
 /**
  * Sets the monitors of the specified issue
- * 
+ *
  * <p>This functions performs access level checks and only performs operations which would
  * modify the existing monitors list.</p>
- * 
+ *
  * @param int $p_issue_id the issue id to set the monitors for
  * @param int $p_user_id the user which requests the monitor change
- * @param array $p_monitors An array of arrays with the <em>id</em> field set to the id 
+ * @param array $p_monitors An array of arrays with the <em>id</em> field set to the id
  *  of the users which should monitor this issue.
  */
 function mci_issue_set_monitors( $p_issue_id , $p_user_id, $p_monitors ) {
-    
+
     $t_existing_monitors = bug_get_monitors( $p_issue_id );
 
     $t_monitors = array();
-    foreach ( $p_monitors as $t_monitor ) 
+    foreach ( $p_monitors as $t_monitor )
         $t_monitors[] = $t_monitor['id'];
-    
+
     foreach ( $t_monitors as $t_user_id ) {
-        
+
     	if ( $p_user_id == $t_user_id ) {
     		if ( ! access_has_bug_level( config_get( 'monitor_bug_threshold' ), $p_issue_id ) )
     		    continue;
@@ -355,13 +355,13 @@ function mci_issue_set_monitors( $p_issue_id , $p_user_id, $p_monitors ) {
 	    	if ( !access_has_bug_level( config_get( 'monitor_add_others_bug_threshold' ), $p_issue_id ) )
 	    	    continue;
 	    }
-	        
+
        if ( in_array( $p_user_id, $t_existing_monitors) )
            continue;
-	        
+
         bug_monitor( $p_issue_id, $t_user_id);
     }
-    
+
     foreach ( $t_existing_monitors as $t_user_id ) {
 
     	if ( $p_user_id == $t_user_id ) {
@@ -371,10 +371,10 @@ function mci_issue_set_monitors( $p_issue_id , $p_user_id, $p_monitors ) {
 	    	if ( !access_has_bug_level( config_get( 'monitor_delete_others_bug_threshold' ), $p_issue_id ) )
 	    	    continue;
 	    }
-        
+
         if ( in_array( $p_user_id, $t_monitors) )
             continue;
-            
+
         bug_unmonitor( $p_issue_id, $t_user_id);
     }
 }
@@ -634,7 +634,7 @@ function mc_issue_add( $p_username, $p_password, $p_issue ) {
 	if( access_has_project_level( config_get( 'roadmap_update_threshold' ), $t_bug_data->project_id, $t_user_id ) ) {
 		$t_bug_data->target_version = isset( $p_issue['target_version'] ) ? $p_issue['target_version'] : '';
 	}
-	
+
 	# omitted:
 	# var $bug_text_id
 	# $t_bug_data->profile_id;
@@ -657,7 +657,7 @@ function mc_issue_add( $p_username, $p_password, $p_issue ) {
 			} else {
 				$t_view_state = config_get( 'default_bugnote_view_status' );
 			}
-			
+
 			$note_type = isset ( $t_note['note_type'] ) ? (int) $t_note['note_type'] : BUGNOTE;
 			$note_attr = isset ( $t_note['note_type'] ) ? $t_note['note_attr'] : '';
 
@@ -853,10 +853,10 @@ function mc_issue_update( $p_username, $p_password, $p_issue_id, $p_issue ) {
 				}
 			} else {
 				$t_view_state_id = mci_get_enum_id_from_objectref( 'view_state', $t_view_state );
-				
+
 				$note_type = isset ( $t_note['note_type'] ) ? (int) $t_note['note_type'] : BUGNOTE;
 			    $note_attr = isset ( $t_note['note_type'] ) ? $t_note['note_attr'] : '';
-				
+
 				bugnote_add( $p_issue_id, $t_note['text'], mci_get_time_tracking_from_note( $p_issue_id, $t_note ), $t_view_state_id == VS_PRIVATE, $note_type, $note_attr, $t_user_id, FALSE );
 			}
 		}
@@ -940,7 +940,7 @@ function mc_issue_note_add( $p_username, $p_password, $p_issue_id, $p_note ) {
 	}
 
 	$t_view_state_id = mci_get_enum_id_from_objectref( 'view_state', $t_view_state );
-	
+
 	$note_type = isset ( $p_note['note_type'] ) ? (int) $p_note['note_type'] : BUGNOTE;
 	$note_attr = isset ( $p_note['note_type'] ) ? $p_note['note_attr'] : '';
 
@@ -979,6 +979,62 @@ function mc_issue_note_delete( $p_username, $p_password, $p_issue_note_id ) {
 }
 
 /**
+ * Update a note
+ *
+ * @param string $p_username  The name of the user trying to add a note to an issue.
+ * @param string $p_password  The password of the user.
+ * @param integer $p_issue_id  The id of the issue to update the note to.
+ * @param integer $p_note_id  The id of the note to update.
+ * @param IssueNoteData $p_note  The note to update.
+ * @return integer The id of the added note.
+ */
+function mc_issue_note_update( $p_username, $p_password, $p_issue_id, $p_issue_note_id, $p_note, $p_type_update ) {
+	$t_user_id = mci_check_login( $p_username, $p_password );
+	if( $t_user_id === false ) {
+		return mci_soap_fault_login_failed();
+	}
+
+	if( (integer) $p_issue_id < 1 ) {
+		return new soap_fault( 'Client', '', "Invalid issue note id '$p_issue_note_id'.");
+	}
+
+	if( !bug_exists( $p_issue_id ) ) {
+		return new soap_fault( 'Client', '', "Issue '$p_issue_id' does not exist" );
+	}
+
+	if ( !isset( $p_note['text'] ) || is_blank( $p_note['text'] ) ) {
+		return new soap_fault( 'Client', '', "Issue note text must not be blank." );
+	}
+
+	if( !bugnote_exists( $p_issue_note_id ) ) {
+		return new soap_fault( 'Server', '', "Issue note '$p_issue_note_id' does not exist." );
+	}
+
+	$t_project_id = bug_get_field( $p_issue_id, 'project_id' );
+	if( !mci_has_readwrite_access( $t_user_id, $t_project_id ) ) {
+		return mci_soap_fault_access_denied( $t_user_id );
+	}
+
+	if( !access_has_bug_level( config_get( 'add_bugnote_threshold' ), $p_issue_id, $t_user_id ) ) {
+		return mci_soap_fault_access_denied( $t_user_id, "You do not have access rights to add notes to this issue" );
+	}
+
+	if( bug_is_readonly( $p_issue_id ) ) {
+		return mci_soap_fault_access_denied( $t_user_id, "Issue '$p_issue_id' is readonly" );
+	}
+
+	if( isset( $p_note ['view_state']) && $p_type_update == "state") {
+		$t_view_state = $p_note['view_state'];
+		$t_view_state_id = mci_get_enum_id_from_objectref( 'view_state', $t_view_state );
+		bugnote_date_update($p_note['id']);
+		return bugnote_set_view_state($p_note['id'],$t_view_state_id);
+	}else{
+		bugnote_date_update($p_note['id']);
+		return bugnote_set_text($p_note['id'], $p_note['text']);
+	}
+}
+
+/**
  * Submit a new relationship.
  *
  * @param string $p_username  The name of the user trying to add a note to an issue.
diff --git a/tests/soap/IssueNoteTest.php b/tests/soap/IssueNoteTest.php
index 1dda7e1..455c24a 100644
--- a/tests/soap/IssueNoteTest.php
+++ b/tests/soap/IssueNoteTest.php
@@ -144,16 +144,18 @@ class IssueNoteTest extends SoapBase {
 
 	/**
 	 * A test case that tests the following:
-	 * 1. Create an issue.
-	 * 2. Add a note to the issue.
-	 * 3. Get the issue.
-	 * 4. Verify that the issue has one note.
-	 * 5. Delete the note.
-	 * 6. Get the issue.
-	 * 7. Verify that the issue has no notes.
-	 * 8. Delete the issue.
+	 * 1.  Create an issue.
+	 * 2.  Add a note to the issue.
+	 * 3.  Get the issue.
+	 * 4.  Verify that the issue has one note.
+	 * 5.  Update this note
+	 * 6.  Verify that the note has been updated
+	 * 7.  Delete the note.
+	 * 8.  Get the issue.
+	 * 9.  Verify that the issue has no notes.
+	 * 10. Delete the issue.
 	 */
-	public function testAddThenDeleteNote() {
+	public function testAddThenUpdateThenDeleteNote() {
 		$issueToAdd = $this->getIssueToAdd( 'IssueNoteTest.testAddThenDeleteNote' );
 
 		$issueId = $this->client->mc_issue_add(
@@ -185,6 +187,27 @@ class IssueNoteTest extends SoapBase {
 
 		$this->assertEquals( 1, count( $issueWithNote->notes ) );
 
+		$noteDataNew = array(
+			'text' => "some new note",
+		);
+
+		$this->client->mc_issue_note_update(
+			$this->userName,
+			$this->password,
+			$issueId,
+			$issueNoteId,
+			$noteDataNew,
+			'text');
+
+		$issueWithNewNote = $this->client->mc_issue_get(
+			$this->userName,
+			$this->password,
+			$issueId);
+
+		$this->assertEquals( 1, count( $issueWithNote->notes ) );
+
+		$this->assertNotEquals( $issueWithNote->note[$issueWithNote]->text, $issueWithNewNote->note[$issueWithNewNote]->text );
+
 		$this->client->mc_issue_note_delete(
 			$this->userName,
 			$this->password,
-- 
1.7.3.4

