View source
<?php
namespace Drupal\xmlrpc_example\Tests;
use Drupal\Tests\BrowserTestBase;
use Drupal\xmlrpc\XmlRpcTrait;
class XmlRpcExampleTest extends BrowserTestBase {
use XmlRpcTrait;
public static $modules = [
'xmlrpc',
'xmlrpc_example',
];
protected $defaultTheme = 'stark';
protected $xmlRpcUrl;
public function testXmlrpcExampleBasic() {
$this->xmlRpcUrl = $this
->getEndpoint();
$result = xmlrpc($this->xmlRpcUrl, [
'xmlrpc_example.add' => [
3,
4,
],
]);
$this
->assertEqual($result, 7, 'Successfully added 3+4 = 7');
$result = xmlrpc($this->xmlRpcUrl, [
'xmlrpc_example.subtract' => [
4,
3,
],
]);
$this
->assertEqual($result, 1, 'Successfully subtracted 4-3 = 1');
$options = [
'xmlrpc_example.add' => [
5,
2,
],
'xmlrpc_example.subtract' => [
5,
2,
],
];
$expected = [
7,
3,
];
$result = xmlrpc($this->xmlRpcUrl, $options);
$this
->assertEqual($result, $expected, 'Successfully called multicall request');
$result = xmlrpc($this->xmlRpcUrl, [
'xmlrpc_example.subtract' => [
3,
4,
],
]);
$this
->assertEqual(xmlrpc_errno(), 10002, 'Results below minimum return custom error: 10002');
$result = xmlrpc($this->xmlRpcUrl, [
'xmlrpc_example.add' => [
7,
4,
],
]);
$this
->assertEqual(xmlrpc_errno(), 10001, 'Results beyond maximum return custom error: 10001');
}
public function testXmlrpcExampleClient() {
$edit = [
'num1' => 3,
'num2' => 5,
];
$this
->drupalPostForm('xmlrpc_example/client', $edit, t('Add the integers'));
$this
->assertText(t('The XML-RPC server returned this response: @num', [
'@num' => 8,
]));
$edit = [
'num1' => 8,
'num2' => 3,
];
$this
->drupalPostForm('xmlrpc_example/client', $edit, t('Subtract the integers'));
$this
->assertText(t('The XML-RPC server returned this response: @num', [
'@num' => 5,
]));
$this
->drupalPostForm('xmlrpc_example/client', $edit, t('Request methods'));
$this
->assertText('xmlrpc_example.add', 'The XML-RPC Add method was found.');
$this
->assertText('xmlrpc_example.subtract', 'The XML-RPC Subtract method was found.');
$this
->assertText('system.multicall', 'The XML-RPC Multicall method was found.');
$edit = [
'num1' => 5,
'num2' => 2,
];
$this
->drupalPostForm('xmlrpc_example/client', $edit, t('Add and Subtract'));
$this
->assertText('[0] => 7', 'The XML-RPC server returned the addition result.');
$this
->assertText('[1] => 3', 'The XML-RPC server returned the subtraction result.');
}
public function testXmlrpcExampleServer() {
$this->xmlRpcUrl = $this
->getEndpoint();
$options = [
'min' => 3,
'max' => 7,
];
$this
->drupalPostForm('xmlrpc_example/server', $options, t('Save configuration'));
$this
->assertText(t('The configuration options have been saved'), 'Results limited to >= 3 and <= 7');
$edit = [
'num1' => 8,
'num2' => 3,
];
$this
->drupalPostForm('xmlrpc_example/client', $edit, t('Subtract the integers'));
$this
->assertText(t('The XML-RPC server returned this response: @num', [
'@num' => 5,
]));
$result = xmlrpc($this->xmlRpcUrl, [
'xmlrpc_example.add' => [
3,
4,
],
]);
$this
->assertEqual($result, 7, 'Successfully added 3+4 = 7');
$result = xmlrpc($this->xmlRpcUrl, [
'xmlrpc_example.subtract' => [
4,
3,
],
]);
$this
->assertEqual(xmlrpc_errno(), 10002, 'subtracting 4-3 = 1 returns custom error: 10002');
$result = xmlrpc($this->xmlRpcUrl, [
'xmlrpc_example.add' => [
7,
4,
],
]);
$this
->assertEqual(xmlrpc_errno(), 10001, 'Adding 7 + 4 = 11 returns custom error: 10001');
}
public function testXmlrpcExampleAlter() {
$this->xmlRpcUrl = $this
->getEndpoint();
$options = [
'alter_enabled' => TRUE,
];
$this
->drupalPostForm('xmlrpc_example/alter', $options, t('Save configuration'));
$this
->assertText(t('The configuration options have been saved'), 'Results are not limited due to methods alteration');
$edit = [
'num1' => 80,
'num2' => 3,
];
$this
->drupalPostForm('xmlrpc_example/client', $edit, t('Subtract the integers'));
$this
->assertText(t('The XML-RPC server returned this response: @num', [
'@num' => 77,
]));
$result = xmlrpc($this->xmlRpcUrl, [
'xmlrpc_example.add' => [
30,
4,
],
]);
$this
->assertEqual($result, 34, 'Successfully added 30+4 = 34');
$result = xmlrpc($this->xmlRpcUrl, [
'xmlrpc_example.subtract' => [
4,
30,
],
]);
$this
->assertEqual($result, -26, 'Successfully subtracted 4-30 = -26');
}
}