Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9a1a1c4

Browse files
committedDec 5, 2011
Implement mc_tag_get_all, mc_tag_add, mc_tag_delete
Fixes #13446: Add tags support to soap api
1 parent 23ef296 commit 9a1a1c4

File tree

6 files changed

+368
-0
lines changed

6 files changed

+368
-0
lines changed
 

‎api/soap/mantisconnect.php

+93
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,52 @@
553553
'tns:CustomFieldValueForIssueData'
554554
);
555555

556+
### TagData
557+
$l_oServer->wsdl->addComplexType(
558+
'TagData',
559+
'complexType',
560+
'struct',
561+
'all',
562+
'',
563+
array(
564+
'id' => array( 'name' => 'id', 'type' => 'xsd:integer', 'minOccurs' => '0' ),
565+
'user_id' => array( 'name' => 'user_id', 'type' => 'tns:AccountData', 'minOccurs' => '0' ),
566+
'name' => array( 'name' => 'name', 'type' => 'xsd:string', 'minOccurs' => '0' ),
567+
'description' => array( 'name' => 'description', 'type' => 'xsd:string', 'minOccurs' => '0' ),
568+
'date_created' => array( 'name' => 'date_created', 'type' => 'xsd:dateTime', 'minOccurs' => '0' ),
569+
'date_updated' => array( 'name' => 'date_updated', 'type' => 'xsd:dateTime', 'minOccurs' => '0' )
570+
)
571+
);
572+
573+
### TagDataArray
574+
$l_oServer->wsdl->addComplexType(
575+
'TagDataArray',
576+
'complexType',
577+
'array',
578+
'',
579+
'SOAP-ENC:Array',
580+
array(),
581+
array(array(
582+
'ref' => 'SOAP-ENC:arrayType',
583+
'wsdl:arrayType' => 'tns:TagData[]'
584+
)
585+
),
586+
'tns:TagData'
587+
);
588+
589+
### TagDataSearchResult
590+
$l_oServer->wsdl->addComplexType(
591+
'TagDataSearchResult',
592+
'complexType',
593+
'struct',
594+
'all',
595+
'',
596+
array(
597+
'results' => array( 'name' => 'results', 'type' => 'tns:TagDataArray', 'minOccurs' => '0' ),
598+
'total_results' => array( 'name' => 'total_results', 'type' => 'xsd:integer', 'minOccurs' => '0' )
599+
)
600+
);
601+
556602
###
557603
### PUBLIC METHODS
558604
###
@@ -1479,6 +1525,53 @@
14791525
'Get the value for the specified user preference.'
14801526
);
14811527

1528+
###
1529+
### PUBLIC METHODS (defined in mc_tag_api.php)
1530+
###
1531+
1532+
$l_oServer->register( 'mc_tag_get_all',
1533+
array(
1534+
'username' => 'xsd:string',
1535+
'password' => 'xsd:string',
1536+
'page_number' => 'xsd:integer',
1537+
'per_page' => 'xsd:integer'
1538+
),
1539+
array(
1540+
'return' => 'tns:TagDataSearchResult'
1541+
),
1542+
$t_namespace,
1543+
false, false, false,
1544+
'Gets all the tags.'
1545+
);
1546+
1547+
$l_oServer->register( 'mc_tag_add',
1548+
array(
1549+
'username' => 'xsd:string',
1550+
'password' => 'xsd:string',
1551+
'tag' => 'tns:TagData'
1552+
),
1553+
array(
1554+
'return' => 'xsd:integer'
1555+
),
1556+
$t_namespace,
1557+
false, false, false,
1558+
'Creates a tag.'
1559+
);
1560+
1561+
$l_oServer->register( 'mc_tag_delete',
1562+
array(
1563+
'username' => 'xsd:string',
1564+
'password' => 'xsd:string',
1565+
'tag_id' => 'xsd:integer'
1566+
),
1567+
array(
1568+
'return' => 'xsd:boolean'
1569+
),
1570+
$t_namespace,
1571+
false, false, false,
1572+
'Deletes a tag.'
1573+
);
1574+
14821575
###
14831576
### IMPLEMENTATION
14841577
###

‎api/soap/mc_core.php

+1
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@
4141
require_once( $t_current_dir . 'mc_config_api.php' );
4242
require_once( $t_current_dir . 'mc_custom_field_api.php' );
4343
require_once( $t_current_dir . 'mc_user_pref_api.php' );
44+
require_once( $t_current_dir . 'mc_tag_api.php' );

‎api/soap/mc_tag_api.php

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
# MantisBT - A PHP based bugtracking system
3+
4+
# MantisBT is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 2 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# MantisBT is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Retrieves all tags, unless the users
19+
*
20+
* @param string $p_username The user's username
21+
* @param string $p_password The user's password
22+
* @param int $p_page_number The page number to return data for
23+
* @param string $p_per_page The number of issues to return per page
24+
* @return array The tag data
25+
*/
26+
function mc_tag_get_all( $p_username, $p_password, $p_page_number, $p_per_page) {
27+
$t_user_id = mci_check_login( $p_username, $p_password );
28+
if ( $t_user_id === false ) {
29+
return mci_soap_fault_login_failed();
30+
}
31+
32+
if ( !access_has_global_level( config_get( 'tag_view_threshold' ) ) )
33+
return mci_soap_fault_access_denied( $t_user_id , 'No rights to view tags');
34+
35+
if ( $p_per_page == 0 )
36+
$p_per_page = 1;
37+
38+
$t_results = array();
39+
$t_total_results = tag_count( '' );
40+
41+
foreach ( tag_get_all('', $p_per_page, $p_per_page * ( $p_page_number - 1 ) ) as $t_tag_row ) {
42+
$t_results[] = array (
43+
'id' => $t_tag_row['id'],
44+
'name' => $t_tag_row['name'],
45+
'description' => $t_tag_row['description'],
46+
'user_id' => mci_account_get_array_by_id ( $t_tag_row['user_id'] ),
47+
'date_created' => timestamp_to_iso8601($t_tag_row['date_created'], false),
48+
'date_updated' => timestamp_to_iso8601($t_tag_row['date_updated'], false)
49+
50+
);
51+
}
52+
53+
return array(
54+
'results' => $t_results,
55+
'total_results' => $t_total_results
56+
);
57+
}
58+
59+
/**
60+
* Creates a tag
61+
*
62+
* @param string $p_username The user's username
63+
* @param string $p_password The user's password
64+
* @param array $p_tag The tag to create
65+
* @return soap_fault|integer
66+
*/
67+
function mc_tag_add( $p_username, $p_password, $p_tag ) {
68+
69+
$t_user_id = mci_check_login( $p_username, $p_password );
70+
71+
if ( $t_user_id === false ) {
72+
return mci_soap_fault_login_failed();
73+
}
74+
75+
if ( !access_has_global_level( config_get( 'tag_create_threshold' ) ) )
76+
return mci_soap_fault_access_denied( $p_user_id );
Has conversations. Original line has conversations.
77+
78+
$t_valid_matches = array();
79+
80+
$t_tag_name = $p_tag['name'];
81+
$t_tag_description = array_key_exists('description', $p_tag) ? $p_tag['description'] : '';
82+
83+
if ( !tag_name_is_valid($t_tag_name, $t_valid_matches))
84+
return new soap_fault('client', '', 'Invalid tag name : "' . $t_tag_name .'"' );
85+
86+
$t_matching_by_name = tag_get_by_name( $t_tag_name);
87+
if ( $t_matching_by_name != false )
88+
return new soap_fault('client', '', 'A tag with the same name already exists , id: ' . $t_matching_by_name['id']);
89+
90+
return tag_create($t_tag_name, $t_user_id, $t_tag_description);
91+
}
92+
93+
/**
94+
*
95+
* Deletes a tag
96+
*
97+
* @param string $p_username The user's username
98+
* @param string $p_password The user's password * @param unknown_type $p_tag_id
99+
* @param int $p_tag_id The id of the tag
100+
* @return soap_fault|boolean
101+
*/
102+
function mc_tag_delete( $p_username, $p_password, $p_tag_id ) {
103+
104+
$t_user_id = mci_check_login( $p_username, $p_password );
105+
106+
if ( $t_user_id === false ) {
107+
return mci_soap_fault_login_failed();
108+
}
109+
110+
if ( !access_has_global_level( config_get( 'tag_edit_threshold' ) ) )
111+
return mci_soap_fault_access_denied( $p_user_id );
Has conversations. Original line has conversations.
112+
113+
if ( ! tag_exists( $p_tag_id ) )
114+
return new soap_fault('Client', '', 'No tag with id ' . $p_tag_id);
115+
116+
return tag_delete( $p_tag_id );
117+
}

‎tests/soap/AllTests.php

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
require_once 'VersionTest.php';
4141
require_once 'RelationshipTest.php';
4242
require_once 'UserTest.php';
43+
require_once 'TagTest.php';
4344

4445
/**
4546
* @package Tests
@@ -77,6 +78,7 @@ public static function suite()
7778
$suite->addTestSuite('VersionTest');
7879
$suite->addTestSuite('RelationshipTest');
7980
$suite->addTestSuite('UserTest');
81+
$suite->addTestSuite('TagTest');
8082

8183
return $suite;
8284
}

‎tests/soap/SoapBase.php

+16
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class SoapBase extends PHPUnit_Framework_TestCase {
3838
protected $mantisPath;
3939
private $issueIdsToDelete = array();
4040
private $versionIdsToDelete = array();
41+
private $tagIdsToDelete = array();
4142
private $defaultSoapClientOptions = array( 'trace' => true,
4243
'exceptions' => true,
4344
'cache_wsdl' => WSDL_CACHE_NONE,
@@ -81,6 +82,10 @@ protected function tearDown() {
8182
$this->password,
8283
$issueIdToDelete);
8384
}
85+
86+
foreach ( $this->tagIdsToDelete as $tagIdToDelete ) {
87+
$this->client->mc_tag_delete ( $this->userName, $this->password, $tagIdToDelete );
88+
}
8489
}
8590

8691
protected function getProjectId() {
@@ -128,6 +133,17 @@ protected function deleteVersionAfterRun( $versionId ) {
128133

129134
$this->versionIdsToDelete[] = $versionId;
130135
}
136+
137+
/**
138+
* Registers a tag for deletion after the test method has run
139+
*
140+
* @param int $tagId
141+
* @return void
142+
*/
143+
protected function deleteTagAfterRun ( $tagId ) {
144+
145+
$this->tagIdsToDelete[] = $tagId;
146+
}
131147

132148
protected function skipIfDueDateIsNotEnabled() {
133149

‎tests/soap/TagTest.php

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
# MantisBT - A PHP based bugtracking system
3+
4+
# MantisBT is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 2 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# MantisBT is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU General Public License
15+
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* @package Tests
19+
* @subpackage UnitTests
20+
* @copyright Copyright (C) 2002 - 2011 MantisBT Team - mantisbt-dev@lists.sourceforge.net
21+
* @link http://www.mantisbt.org
22+
*/
23+
24+
require_once 'SoapBase.php';
25+
26+
/**
27+
* Test fixture for non-login user methods
28+
*/
29+
class TagTest extends SoapBase {
30+
31+
/**
32+
* Tests retrieving, creating and deleting tags
33+
*/
34+
public function testTagOperations() {
35+
36+
$currentTagCount = sizeof ( $this->client->mc_tag_get_all ( $this->userName, $this->password, 1, 500)->results );
37+
38+
$tagToCreate = array (
39+
'name' => 'TagTest.testTagOperations',
40+
'description' => 'Tag created by unit test'
41+
);
42+
43+
$newTagId = $this->client->mc_tag_add ( $this->userName, $this->password, $tagToCreate );
44+
45+
$resultsWithCreatedTag = $this->client->mc_tag_get_all ( $this->userName, $this->password, 1, 500)->results;
46+
47+
$newTagCount = sizeof ( $resultsWithCreatedTag );
48+
49+
$this->client->mc_tag_delete( $this->userName, $this->password, $newTagId );
50+
51+
$finalTagCount = sizeof ( $this->client->mc_tag_get_all ( $this->userName, $this->password, 1, 500)->results );
52+
53+
self::assertEquals ( $currentTagCount +1 , $newTagCount );
54+
self::assertEquals ( $currentTagCount , $finalTagCount );
55+
56+
$createdTag = null;
57+
58+
foreach ( $resultsWithCreatedTag as $tag ) {
59+
60+
if ( $tag->id == $newTagId ) {
61+
$createdTag = $tag;
62+
break;
63+
}
64+
}
65+
66+
self::assertNotNull( $createdTag );
67+
self::assertEquals ( $createdTag->name, $tagToCreate['name']);
68+
self::assertEquals ( $createdTag->description, $tagToCreate['description'] );
69+
self::assertNotNull ( $createdTag->date_created );
70+
self::assertNotNull ( $createdTag->date_updated );
71+
self::assertEquals ( $createdTag->user_id->name, $this->userName );
72+
73+
}
74+
75+
/**
76+
* Tests that creating tags with invalid names is not allowed
77+
*/
78+
public function testCreateTagWithInvalidName() {
79+
80+
$tagToCreate = array (
81+
'name' => '',
82+
'description' => ''
83+
);
84+
85+
try {
86+
$this->client->mc_tag_add ( $this->userName, $this->password, $tagToCreate );
87+
self::fail("Expected an error");
88+
} catch ( SoapFault $e ) {
89+
$this->assertContains( "Invalid tag name", $e->getMessage() );
90+
}
91+
}
92+
93+
/**
94+
* Tests that creating tags with invalid names is not allowed
95+
*/
96+
public function testDeleteNonExistantTag() {
97+
98+
try {
99+
$this->client->mc_tag_delete ( $this->userName, $this->password, -1 );
100+
self::fail("Expected an error");
101+
} catch ( SoapFault $e ) {
102+
$this->assertContains( "No tag with id", $e->getMessage() );
103+
}
104+
}
105+
106+
/**
107+
* Tests that creating a tag with no description works
108+
*/
109+
public function testCreateTagWithNoDescription() {
110+
111+
$tagToCreate = array (
112+
'name' => 'TagTest.testCreateTagWithNoDescription'
113+
);
114+
115+
$tagId = $this->client->mc_tag_add ( $this->userName, $this->password, $tagToCreate );
116+
117+
$this->deleteTagAfterRun( $tagId );
118+
}
119+
120+
/**
121+
* Tests that creating a tag with no description works
122+
*/
123+
public function testCreateTagWithExistingName() {
124+
125+
$tagToCreate = array (
126+
'name' => 'TagTest.testCreateTagWithExistingName'
127+
);
128+
$tagId = $this->client->mc_tag_add ( $this->userName, $this->password, $tagToCreate );
129+
$this->deleteTagAfterRun( $tagId );
130+
131+
try {
132+
$this->client->mc_tag_add ( $this->userName, $this->password, $tagToCreate );
133+
self::fail("Expected an error");
134+
} catch ( SoapFault $e ) {
135+
echo 'Testing';
136+
$this->assertContains( "A tag with the same name already exists", $e->getMessage() );
137+
}
138+
}
139+
}

0 commit comments

Comments
 (0)
Please sign in to comment.