View source
<?php
class ServicesXMLRPCTestCase extends DrupalWebTestCase {
protected $endpoint = NULL;
protected $sessid = NULL;
protected $session_name = NULL;
public static function getInfo() {
return array(
'name' => 'XMLRPC Server',
'description' => 'Test XMLRPC server.',
'group' => 'Services',
'dependencies' => array(
'ctools',
),
);
}
public function setUp(array $modules = array()) {
$modules[] = 'ctools';
$modules[] = 'services';
$modules[] = 'xmlrpc_server';
$modules[] = 'services_test_resource';
parent::setUp($modules);
$this->endpoint = $this
->saveNewEndpoint();
}
function testlistMethods() {
$result = $this
->servicesXMLRPC('system.listMethods', array());
$this
->assertFalse(in_array('node.index', $result['body']), 'Not able to find not enabled node.index method.', 'XMLRPC: listMethods');
}
function testUserLogin() {
$user = $this
->drupalCreateUser(array(
'access user profiles',
));
$args = array(
'username' => $user->name,
'password' => $user->pass_raw,
);
$result = $this
->servicesXMLRPC('user.login', $args);
$this
->assertEqual($result['body']['user']['uid'], $user->uid, format_string('User %user logged in successfully.', array(
'%user' => $user->name,
)), 'XMLRPC: UserLogin');
$this->sessid = $result['body']['sessid'];
$this->session_name = $result['body']['session_name'];
$args = array(
'page' => 0,
'fields' => '*',
'parameters' => array(),
);
$result = $this
->servicesXMLRPC('user.index', $args);
$this
->assertTrue(count($result['body']) == 3, 'Users listed properly.', 'XMLRPC: UserLogin');
}
function testPrecedence() {
$args = array(
'arg1' => $this
->randomName(),
);
$result = $this
->servicesXMLRPC('services_test.retrieve', $args);
$this
->assertEqual($result['body'], 'CRUD Retrieve ' . $args['arg1'], 'XMLRPC precedence works properly (CRUD higher priority than action).', 'XMLRPC: Precedence');
}
public function saveNewEndpoint() {
$edit = $this
->populateEndpointFAPI();
$endpoint = new stdClass();
$endpoint->disabled = FALSE;
$endpoint->api_version = 3;
$endpoint->name = $edit['name'];
$endpoint->title = $edit['title'];
$endpoint->server = $edit['server'];
$endpoint->path = $edit['path'];
$endpoint->authentication = array(
'services' => 'services',
);
$endpoint->server_settings = array(
'formatters' => array(
'json' => TRUE,
'bencode' => TRUE,
'rss' => TRUE,
'plist' => TRUE,
'xmlplist' => TRUE,
'php' => TRUE,
'yaml' => TRUE,
'jsonp' => FALSE,
'xml' => FALSE,
),
'parsers' => array(
'application/x-yaml' => TRUE,
'application/json' => TRUE,
'application/vnd.php.serialized' => TRUE,
'application/plist' => TRUE,
'application/plist+xml' => TRUE,
'application/x-www-form-urlencoded' => TRUE,
),
);
$endpoint->resources = array(
'system' => array(
'alias' => '',
'actions' => array(
'connect' => array(
'enabled' => 1,
),
'get_variable' => array(
'enabled' => 1,
),
'set_variable' => array(
'enabled' => 1,
),
),
),
'user' => array(
'alias' => '',
'operations' => array(
'create' => array(
'enabled' => 1,
),
'retrieve' => array(
'enabled' => 1,
),
'update' => array(
'enabled' => 1,
),
'delete' => array(
'enabled' => 1,
),
'index' => array(
'enabled' => 1,
),
),
'actions' => array(
'login' => array(
'enabled' => 1,
),
'logout' => array(
'enabled' => 1,
),
'token' => array(
'enabled' => 1,
),
),
),
'services_test' => array(
'alias' => '',
'operations' => array(
'retrieve' => array(
'enabled' => 1,
),
),
'actions' => array(
'retrieve' => array(
'enabled' => 1,
),
),
),
);
$endpoint->debug = 1;
$endpoint->export_type = FALSE;
services_endpoint_save($endpoint);
$endpoint = services_endpoint_load($endpoint->name);
$this
->assertTrue($endpoint->name == $edit['name'], 'Endpoint successfully created');
return $endpoint;
}
public function populateEndpointFAPI() {
return array(
'name' => 'machinename',
'title' => $this
->randomName(20),
'path' => $this
->randomName(10),
'server' => 'xmlrpc_server',
);
}
public function servicesXMLRPC($method, $args = array(), $sessid = TRUE, $assert_no_error = TRUE) {
if (!is_array($args)) {
$args = array(
$args,
);
}
$options = array(
'headers' => array(),
);
if ($sessid && !empty($this->sessid)) {
$options['headers']['Cookie'] = $this->session_name . '=' . $this->sessid;
}
$csrf_token_response = xmlrpc(url($this->endpoint->path, array(
'absolute' => TRUE,
)), array(
'user.token' => array(),
), $options);
$options['headers']['X-CSRF-Token'] = $csrf_token_response['token'];
$output = xmlrpc(url($this->endpoint->path, array(
'absolute' => TRUE,
)), array(
$method => $args,
), $options);
$error_message = xmlrpc_error_msg();
if ($assert_no_error) {
$this
->assertTrue(empty($error_message), format_string('XMLRPC call %method run without errors.', array(
'%method' => $method,
)), 'XMLRPC call');
}
$this
->verbose('XMLRPC request to: ' . $method . '<hr />Arguments: ' . highlight_string('<?php ' . var_export($args, TRUE), TRUE) . '<hr />Response: ' . highlight_string('<?php ' . var_export($output, TRUE), TRUE) . '<hr />Error: ' . $error_message);
if (!empty($error_message)) {
return array(
'error_message' => $error_message,
'body' => '',
);
}
return array(
'error_message' => '',
'body' => $output,
);
}
}