View source
<?php
require_once 'ServicesWebTestCase.php';
class ServicesEndpointTests extends ServicesWebTestCase {
public function setUp() {
parent::setUp('autoload', 'ctools', 'services', 'rest_server', 'inputstream');
}
public function testEndpointForm() {
$this->privilegedUser = $this
->drupalCreateUser(array(
'administer services',
'administer site configuration',
));
$this
->drupalLogin($this->privilegedUser);
$edit = $this
->populateEndpointFAPI();
$this
->drupalPost('admin/build/services/add', $edit, t('Save'));
$this
->drupalGet($edit['path']);
}
public function testSuccessfulAddEndpoint() {
$edit = $this
->populateEndpointFAPI();
$this->privilegedUser = $this
->drupalCreateUser(array(
'administer services',
'administer nodes',
'administer site configuration',
));
$this
->drupalLogin($this->privilegedUser);
$this
->drupalPost('admin/build/services/add', $edit, t('Save'));
$this
->assertResponse('200', t('expected 200'));
$this
->drupalGet('admin/build/services/list');
$this
->assertResponse('200', t('expected 200'));
$this
->assertText($edit['name'], t('Endpoint path appears'));
$this
->assertText('Normal', t('Storage is in database'));
}
public function testMissingPath() {
$edit = $this
->populateEndpointFAPI();
unset($edit['path']);
$this->privilegedUser = $this
->drupalCreateUser(array(
'administer services',
'administer nodes',
'administer site configuration',
));
$this
->drupalLogin($this->privilegedUser);
$this
->drupalPost('admin/build/services/add', $edit, t('Save'));
$this
->assertResponse('200', 'expected 200');
$this
->assertText('Path to endpoint field is required.', t('Endpoint path missing error message.'));
$this
->assertFieldByName('server', 'rest_server', t('Server is rest server'));
$this
->assertFieldChecked('edit-services-use-content-permissions', t('Storage use content permission is checked.'));
}
public function testMissingServer() {
$edit = $this
->populateEndpointFAPI();
unset($edit['server']);
$this->privilegedUser = $this
->drupalCreateUser(array(
'administer services',
'administer nodes',
'administer site configuration',
));
$this
->drupalLogin($this->privilegedUser);
$this
->drupalPost('admin/build/services/add', $edit, t('Save'));
$this
->assertResponse('200', 'expected 200');
$this
->assertText('Server field is required.', t('Server missing error message.'));
$this
->assertFieldByName('name', $edit['name'], t('Name field remains.'));
$this
->assertFieldChecked('edit-services-use-content-permissions', t('Storage use content permission is checked.'));
}
public function populateEndpointFAPI() {
return array(
'name' => 'machinename',
'path' => $this
->randomName(10),
'server' => 'rest_server',
'services_use_content_permissions' => TRUE,
);
}
public function saveNewEndpoint() {
$edit = $this
->populateEndpointFAPI();
$endpoint = new stdClass();
$endpoint->disabled = FALSE;
$endpoint->api_version = 3;
$endpoint->name = $edit['name'];
$endpoint->server = $edit['server'];
$endpoint->path = $edit['path'];
$endpoint->authentication = array();
$endpoint->resources = array(
'node' => array(
'alias' => '',
'operations' => array(
'create' => array(
'enabled' => 1,
),
'retrieve' => array(
'enabled' => 1,
),
'update' => array(
'enabled' => 1,
),
'delete' => array(
'enabled' => 1,
),
'index' => array(
'enabled' => 1,
),
),
),
'system' => array(
'alias' => '',
'actions' => array(
'connect' => array(
'enabled' => 1,
),
'get_variable' => array(
'enabled' => 1,
),
'set_variable' => array(
'enabled' => 1,
),
),
),
'taxonomy_term' => array(
'alias' => '',
'operations' => array(
'create' => array(
'enabled' => 1,
),
'retrieve' => array(
'enabled' => 1,
),
'update' => array(
'enabled' => 1,
),
'delete' => array(
'enabled' => 1,
),
),
'actions' => array(
'selectNodes' => array(
'enabled' => 1,
),
),
),
'taxonomy_vocabulary' => array(
'alias' => '',
'operations' => array(
'create' => array(
'enabled' => 1,
),
'retrieve' => array(
'enabled' => 1,
),
'update' => array(
'enabled' => 1,
),
'delete' => array(
'enabled' => 1,
),
),
'actions' => array(
'getTree' => 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,
),
),
),
'comment' => array(
'alias' => '',
'operations' => array(
'create' => array(
'enabled' => 1,
),
'retrieve' => array(
'enabled' => 1,
),
'update' => array(
'enabled' => 1,
),
'delete' => array(
'enabled' => 1,
),
),
'actions' => array(
'loadNodeComments' => array(
'enabled' => 1,
),
'countAll' => array(
'enabled' => 1,
),
'countNew' => array(
'enabled' => 1,
),
),
),
'file' => array(
'alias' => '',
'operations' => array(
'create' => array(
'enabled' => 1,
),
'retrieve' => array(
'enabled' => 1,
),
'delete' => array(
'enabled' => 1,
),
),
'relationships' => array(
'files' => array(
'enabled' => 1,
),
'comments' => array(
'enabled' => 1,
),
),
),
'echo' => array(
'alias' => '',
'operations' => array(
'index' => array(
'enabled' => 1,
),
),
),
);
$endpoint->debug = 1;
$endpoint->status = 1;
services_endpoint_save($endpoint);
$endpoint = services_endpoint_load($endpoint->name);
$this
->assertEqual($endpoint->name, $edit['name'], t('Endpoint successfully created'));
$this
->servicesGet($endpoint->path);
return $endpoint;
}
public function services_build_postfields($data = array()) {
$post_data = '';
if (is_array($data) && !empty($data)) {
array_walk($data, array(
$this,
'services_flatten_fields',
));
$post_data = implode('&', $data);
}
if (is_object($data) && !empty($data)) {
array_walk(get_object_vars($data), array(
$this,
'services_flatten_fields',
));
$post_data = implode('&', $data);
}
return $post_data;
}
public function services_flatten_fields(&$item, $key) {
$item = $key . '=' . $item;
}
}