Skip to content

Commit 94355c6

Browse files
committed
XML-RPC: Update addTwoNumbers demo method to check args prior to adding.
Invalid args now cause an `IXR_Error` to be returned. Comprehensive unit tests are also added for the method. Developed in WordPress#10690 Follow-up to [1348]. Props josephscott, westonruter. Fixes #64479. git-svn-id: https://develop.svn.wordpress.org/trunk@61460 602fd350-edb4-49c9-b593-d223f7449a82
1 parent e9058ee commit 94355c6

File tree

2 files changed

+164
-2
lines changed

2 files changed

+164
-2
lines changed

src/wp-includes/class-wp-xmlrpc-server.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,15 +262,20 @@ public function sayHello() {
262262
*
263263
* @since 1.5.0
264264
*
265-
* @param array $args {
265+
* @param int[] $args {
266266
* Method arguments. Note: arguments must be ordered as documented.
267267
*
268268
* @type int $0 A number to add.
269269
* @type int $1 A second number to add.
270270
* }
271-
* @return int Sum of the two given numbers.
271+
* @return int|IXR_Error Sum of the two given numbers.
272272
*/
273273
public function addTwoNumbers( $args ) {
274+
if ( ! is_array( $args ) || count( $args ) !== 2 || ! is_int( $args[0] ) || ! is_int( $args[1] ) ) {
275+
$this->error = new IXR_Error( 400, __( 'Invalid arguments passed to this XML-RPC method. Requires two integers.' ) );
276+
return $this->error;
277+
}
278+
274279
$number1 = $args[0];
275280
$number2 = $args[1];
276281
return $number1 + $number2;
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
3+
/**
4+
* Tests for the XML-RPC demo.addTwoNumbers method.
5+
*
6+
* @group xmlrpc
7+
*
8+
* @covers wp_xmlrpc_server::addTwoNumbers
9+
*/
10+
class Tests_XMLRPC_demo_addTwoNumbers extends WP_XMLRPC_UnitTestCase {
11+
12+
/**
13+
* Tests that addTwoNumbers returns the correct sum for valid integer inputs.
14+
*
15+
* @dataProvider data_valid_integers
16+
*
17+
* @param int $a First number.
18+
* @param int $b Second number.
19+
* @param int $expected Expected sum.
20+
*/
21+
public function test_add_two_numbers_with_valid_integers( $a, $b, $expected ) {
22+
$result = $this->myxmlrpcserver->addTwoNumbers( array( $a, $b ) );
23+
24+
$this->assertNotIXRError( $result );
25+
$this->assertSame( $expected, $result );
26+
}
27+
28+
/**
29+
* Data provider for test_add_two_numbers_with_valid_integers.
30+
*
31+
* @return array<string, int[]>
32+
*/
33+
public function data_valid_integers(): array {
34+
return array(
35+
'two positive integers' => array( 3, 5, 8 ),
36+
'positive and negative integer' => array( 10, -3, 7 ),
37+
'two negative integers' => array( -5, -7, -12 ),
38+
'zero and positive integer' => array( 0, 42, 42 ),
39+
'zero and negative integer' => array( 0, -42, -42 ),
40+
'two zeros' => array( 0, 0, 0 ),
41+
'large integers' => array( 1000000, 2000000, 3000000 ),
42+
);
43+
}
44+
45+
/**
46+
* Tests that addTwoNumbers returns an error when invalid types are passed.
47+
*
48+
* @dataProvider data_invalid_arguments
49+
*
50+
* @param mixed $a First argument.
51+
* @param mixed $b Second argument.
52+
* @param string $message Description of the test case.
53+
*/
54+
public function test_add_two_numbers_with_invalid_arguments( $a, $b, $message ) {
55+
$result = $this->myxmlrpcserver->addTwoNumbers( array( $a, $b ) );
56+
57+
$this->assertIsNotInt( $result );
58+
$this->assertSame( 400, $result->code, $message );
59+
$this->assertSame(
60+
'Invalid arguments passed to this XML-RPC method. Requires two integers.',
61+
$result->message,
62+
$message
63+
);
64+
}
65+
66+
/**
67+
* Data provider for test_add_two_numbers_with_invalid_arguments.
68+
*
69+
* @return array<string, array<mixed>>
70+
*/
71+
public function data_invalid_arguments(): array {
72+
return array(
73+
'first argument is string' => array( 'abc', 5, 'Should fail when first argument is a string.' ),
74+
'second argument is string' => array( 3, 'abc', 'Should fail when second argument is a string.' ),
75+
'both arguments are strings' => array( 'foo', 'bar', 'Should fail when both arguments are strings.' ),
76+
'first argument is float' => array( 3.14, 5, 'Should fail when first argument is a float.' ),
77+
'second argument is float' => array( 3, 5.5, 'Should fail when second argument is a float.' ),
78+
'both arguments are floats' => array( 1.1, 2.2, 'Should fail when both arguments are floats.' ),
79+
'first argument is null' => array( null, 5, 'Should fail when first argument is null.' ),
80+
'second argument is null' => array( 3, null, 'Should fail when second argument is null.' ),
81+
'first argument is boolean' => array( true, 5, 'Should fail when first argument is boolean.' ),
82+
'second argument is boolean' => array( 3, false, 'Should fail when second argument is boolean.' ),
83+
'first argument is array' => array( array( 1 ), 5, 'Should fail when first argument is an array.' ),
84+
'second argument is array' => array( 3, array( 2 ), 'Should fail when second argument is an array.' ),
85+
'numeric string as first argument' => array( '3', 5, 'Should fail when first argument is a numeric string.' ),
86+
'numeric string as second argument' => array( 3, '5', 'Should fail when second argument is a numeric string.' ),
87+
);
88+
}
89+
90+
/**
91+
* Tests that addTwoNumbers returns an error when no arguments are passed.
92+
*
93+
* When zero XML-RPC params are provided, IXR_Server passes an empty array to the method.
94+
*/
95+
public function test_add_two_numbers_with_no_arguments() {
96+
$result = $this->myxmlrpcserver->addTwoNumbers( array() );
97+
98+
$this->assertIsNotInt( $result );
99+
$this->assertSame( 400, $result->code );
100+
$this->assertSame(
101+
'Invalid arguments passed to this XML-RPC method. Requires two integers.',
102+
$result->message
103+
);
104+
}
105+
106+
/**
107+
* Tests that addTwoNumbers returns an error when only one argument is passed.
108+
*
109+
* When exactly one XML-RPC param is provided, IXR_Server unwraps it from the array
110+
* and passes the single value directly to the method (not wrapped in an array).
111+
*
112+
* @see IXR_Server::call() lines 95-98
113+
*/
114+
public function test_add_two_numbers_with_one_argument() {
115+
// IXR_Server passes single param directly, not as array.
116+
$result = $this->myxmlrpcserver->addTwoNumbers( 5 );
117+
118+
$this->assertIsNotInt( $result );
119+
$this->assertSame( 400, $result->code );
120+
$this->assertSame(
121+
'Invalid arguments passed to this XML-RPC method. Requires two integers.',
122+
$result->message
123+
);
124+
}
125+
126+
/**
127+
* Tests that addTwoNumbers returns an error when too many arguments are passed.
128+
*
129+
* @dataProvider data_too_many_arguments
130+
*
131+
* @param array $args Arguments to pass to addTwoNumbers.
132+
* @param string $message Description of the test case.
133+
*/
134+
public function test_add_two_numbers_with_too_many_arguments( $args, $message ) {
135+
$result = $this->myxmlrpcserver->addTwoNumbers( $args );
136+
137+
$this->assertIsNotInt( $result );
138+
$this->assertSame( 400, $result->code, $message );
139+
$this->assertSame(
140+
'Invalid arguments passed to this XML-RPC method. Requires two integers.',
141+
$result->message,
142+
$message
143+
);
144+
}
145+
146+
/**
147+
* Data provider for test_add_two_numbers_with_too_many_arguments.
148+
*
149+
* @return array<string, array<mixed>>
150+
*/
151+
public function data_too_many_arguments(): array {
152+
return array(
153+
'three arguments' => array( array( 3, 5, 100 ), 'Should fail with three arguments.' ),
154+
'four arguments' => array( array( 10, 20, 30, 40 ), 'Should fail with four arguments.' ),
155+
);
156+
}
157+
}

0 commit comments

Comments
 (0)