View source
<?php
define("SERVICES_NODE_TYPE_INCLUDE", 1);
define("SERVICES_NODE_TYPE_EMPTY", 2);
define("SERVICES_NODE_TYPE_REMOVED", 3);
class ServicesResourceNodetests extends ServicesWebTestCase {
protected $privilegedUser = NULL;
protected $endpoint = NULL;
public function setUp(array $modules = array()) {
$modules[] = 'comment';
parent::setUp($modules);
$this->endpoint = $this
->saveNewEndpoint();
}
public static function getInfo() {
return array(
'name' => 'Resource Node',
'description' => 'Test the resource Node methods and actions.',
'group' => 'Services',
'dependencies' => array(
'ctools',
'libraries',
),
);
}
public function testNewEndpointResourceNodeIndex() {
$this->privilegedUser = $this
->drupalCreateUser(array(
'administer services',
'perform unlimited index queries',
));
$this
->drupalLogin($this->privilegedUser);
$nodes = array();
$count = 50;
for ($i = 0; $i < $count; $i++) {
$node = $this
->drupalCreateNode();
$nodes[$node->nid] = $node;
}
$page_count = ceil(count($nodes) / 20);
$retrieved_nodes = array();
for ($page = 0; $page < $page_count; $page++) {
$responseArray = $this
->servicesGet($this->endpoint->path . '/node', array(
'page' => $page,
'fields' => 'nid,title',
));
$this
->assertTrue(count($responseArray['body']) <= 20, 'Correct number of items returned');
foreach ($responseArray['body'] as $node) {
if (isset($retrieved_nodes[$node->nid])) {
$this
->fail(format_string('Duplicate node @nid returned.', array(
'@nid' => $node->nid,
)));
}
$retrieved_nodes[$node->nid] = TRUE;
$this
->assertTrue($nodes[$node->nid]->title == $node->title, 'Successfully received Node info', 'NodeResource: Index');
}
}
$expected_nids = array_keys($nodes);
sort($expected_nids);
$retrieved_nids = array_keys($retrieved_nodes);
sort($retrieved_nids);
$this
->assertEqual($expected_nids, $retrieved_nids, 'Retrieved all nodes');
$responseArray = $this
->servicesGet($this->endpoint->path . '/node', array(
'page' => $page_count + 1,
));
$this
->assertEqual(count($responseArray['body']), 0, 'The n+1 page is empty');
$responseArray = $this
->servicesGet($this->endpoint->path . '/node', array(
'fields' => 'nid,title',
'pagesize' => 40,
));
$this
->assertTrue(count($responseArray['body']) == 40, 'Correct number of items returned');
$this->lessPrivilegedUser = $this
->drupalCreateUser(array(
'administer services',
));
$this
->drupalLogin($this->lessPrivilegedUser);
$responseArray = $this
->servicesGet($this->endpoint->path . '/node', array(
'fields' => 'nid,title',
'pagesize' => 40,
));
$this
->assertTrue(count($responseArray['body']) == 20, 'Correct number of items returned');
}
public function testNewEndpointResourceNodeGet() {
$this->privilegedUser = $this
->drupalCreateUser(array(
'administer services',
));
$this
->drupalLogin($this->privilegedUser);
$node = $this
->drupalCreateNode();
$responseArray = $this
->servicesGet($this->endpoint->path . '/node/' . $node->nid);
$this
->assertTrue($node->title == $responseArray['body']->title, 'Successfully received Node info', 'NodeResource: Retrieve');
unset($node);
$responseArray = $this
->servicesGet($this->endpoint->path . '/node/99');
$this
->assertTrue($responseArray['code'] == '404', 'Successfully was rejected to non existent node', 'NodeResource: Retrieve');
}
function testCommentLoadNodeComments() {
$path = $this->endpoint->path;
$this->privileged_user = $this
->drupalCreateUser();
$this
->drupalLogin($this->privileged_user);
$node = $this
->drupalCreateNode();
$nid = $node->nid;
$comments = array();
for ($i = 1; $i <= 15; $i++) {
$comment = (object) $this
->getCommentValues($nid);
comment_save($comment);
$comments[$i] = comment_load($comment->cid);
}
$node2 = $this
->drupalCreateNode();
for ($i = 1; $i <= 5; $i++) {
$comment = (object) $this
->getCommentValues($node2->nid);
comment_save($comment);
}
$response = $this
->servicesGet($path . '/node/' . $nid . '/comments');
$this
->assertEqual($comments, $response['body'], 'Received all 15 comments.', 'NodeResource: comments');
$response = $this
->servicesGet($path . '/node/' . $nid . '/comments', array(
'count' => 5,
));
$this
->assertEqual(array_slice($comments, 0, 5), array_slice($response['body'], 0, 5), 'Received last 5 comments.', 'NodeResource: comments');
$response = $this
->servicesGet($path . '/node/' . $nid . '/comments', array(
'count' => 5,
'offset' => 5,
));
$this
->assertEqual(array_slice($comments, 5, 5), array_merge(array(), $response['body']), 'Received 5 comments starting from fifth comment.', 'NodeResource: comments');
}
public function testEndpointResourceNodeCreate() {
$this->privilegedUser = $this
->drupalCreateUser(array(
'administer services',
'bypass node access',
));
$this
->drupalLogin($this->privilegedUser);
$node = array(
'title' => 'testing',
'body' => array(
LANGUAGE_NONE => array(
array(
'value' => $this
->randomString(),
),
),
),
'type' => 'page',
'name' => $this->privilegedUser->name,
'language' => LANGUAGE_NONE,
);
$responseArray = $this
->servicesPost($this->endpoint->path . '/node', $node);
$nodeResourceCreateReturn = $responseArray['body'];
$this
->assertTrue(isset($nodeResourceCreateReturn['nid']), 'Node was successfully created', 'NodeResource: Create');
$newNode = node_load($nodeResourceCreateReturn['nid']);
$this
->assertTrue($newNode->title = $node['title'], 'Title was the same', 'NodeResource: Create');
$this
->assertTrue($newNode->body = $node['body'], 'Body was the same', 'NodeResource: Create');
}
public function testEndpointResourceNodeCreateLegacy() {
$this->privilegedUser = $this
->drupalCreateUser(array(
'administer services',
'bypass node access',
));
$this
->drupalLogin($this->privilegedUser);
$node = array(
'title' => 'testing',
'body' => array(
LANGUAGE_NONE => array(
array(
'value' => $this
->randomString(),
),
),
),
'type' => 'page',
'name' => $this->privilegedUser->name,
'language' => LANGUAGE_NONE,
);
$responseArray = $this
->servicesPost($this->endpoint->path . '/node', array(
'node' => $node,
));
$nodeResourceCreateReturn = $responseArray['body'];
$this
->assertTrue(isset($nodeResourceCreateReturn['nid']), 'Node was successfully created', 'NodeResource: Create (Legacy)');
$newNode = node_load($nodeResourceCreateReturn['nid']);
$this
->assertTrue($newNode->title = $node['title'], 'Title was the same', 'NodeResource: Create (Legacy)');
$this
->assertTrue($newNode->body = $node['body'], 'Body was the same', 'NodeResource: Create (Legacy)');
}
public function testEndpointResourceNodeCreateFail() {
$this->privilegedUser = $this
->drupalCreateUser(array(
'administer services',
));
$this
->drupalLogin($this->privilegedUser);
$node = array(
'title' => 'testing',
'body' => array(
LANGUAGE_NONE => array(
array(
'value' => $this
->randomString(),
),
),
),
'type' => 'page',
'name' => $this->privilegedUser->name,
'language' => LANGUAGE_NONE,
);
$responseArray = $this
->servicesPost($this->endpoint->path . '/node', array(
'node' => $node,
));
$this
->assertTrue($responseArray['code'] == 403, 'User with not sufficient permissions cannot create node', 'NodeResource: Create');
}
public function testEndpointResourceNodeCreateMissingTitle() {
$this->privilegedUser = $this
->drupalCreateUser(array(
'administer services',
'bypass node access',
));
$this
->drupalLogin($this->privilegedUser);
$node = array(
'title' => '',
'body' => array(
LANGUAGE_NONE => array(
array(
'value' => $this
->randomString(),
),
),
),
'type' => 'page',
'name' => $this->privilegedUser->name,
'language' => LANGUAGE_NONE,
);
$responseArray = $this
->servicesPost($this->endpoint->path . '/node', array(
'node' => $node,
));
$nodeResourceUpdateReturn = $responseArray['body'];
$this
->assertTrue(strpos($responseArray['status'], 'Title field is required.'), 'Node was not created without title.', 'NodeResource: Create');
}
public function testAttachFileTargetedAction() {
$account = $this
->drupalCreateUser(array(
'bypass node access',
));
$this
->drupalLogin($account);
$settings = array(
'type' => 'article',
);
$node = $this
->drupalCreateNode($settings);
$testfiles = $this
->drupalGetTestFiles('image');
$testfile1 = array_pop($testfiles);
$testfile2 = array_pop($testfiles);
$result = $this
->servicesPostFile($this->endpoint->path . '/node/' . $node->nid . '/attach_file', array(
$testfile1->uri,
), array(), array(
'field_name' => 'field_image',
));
$node = node_load($node->nid, TRUE);
$this
->assertEqual($testfile1->filename, $node->field_image[LANGUAGE_NONE][0]['filename'], 'One file has been attached.');
$result = $this
->servicesPostFile($this->endpoint->path . '/node/' . $node->nid . '/attach_file', array(
$testfile2->uri,
), array(), array(
'field_name' => 'field_image',
'attach' => FALSE,
));
$node = node_load($node->nid, TRUE);
$this
->assertEqual($testfile2->filename, $node->field_image[LANGUAGE_NONE][0]['filename'], 'File has been replaced.');
$result = $this
->servicesPostFile($this->endpoint->path . '/node/' . $node->nid . '/attach_file', array(
$testfile1->uri,
), array(), array(
'field_name' => 'field_image',
));
$this
->assertEqual($result['body'], 'You cannot upload so many files.', 'Validation on cardinality works.');
$field_info = field_read_field('field_image');
$field_info['cardinality'] = 2;
field_update_field($field_info);
$result = $this
->servicesPostFile($this->endpoint->path . '/node/' . $node->nid . '/attach_file', array(
$testfile1->uri,
$testfile2->uri,
), array(), array(
'field_name' => 'field_image',
'attach' => FALSE,
));
$node = node_load($node->nid, TRUE);
$this
->assertTrue($testfile1->filename == $node->field_image[LANGUAGE_NONE][0]['filename'] && $testfile2->filename == $node->field_image[LANGUAGE_NONE][1]['filename'], 'Multiple files uploaded.');
$query = db_select('field_data_field_image', 'fd');
$deltas = $query
->condition('entity_type', 'node')
->condition('bundle', $node->type)
->condition('entity_id', $node->nid)
->fields('fd', array(
'delta',
))
->execute()
->fetchCol(0);
$this
->assertTrue($deltas == array(
0,
1,
), 'Attached file deltas are sequential.');
}
function update_node($exclude_type) {
$node = $this
->drupalCreateNode();
$node_update = (array) $node;
$node_update['title'] = $this
->randomName();
$node_update['body'][LANGUAGE_NONE][0]['value'] = $this
->randomName();
if ($exclude_type == SERVICES_NODE_TYPE_EMPTY) {
$node_update['type'] = '';
}
elseif ($exclude_type == SERVICES_NODE_TYPE_REMOVED) {
unset($node_update['type']);
}
$responseArray = $this
->servicesPut($this->endpoint->path . '/node/' . $node->nid, $node_update);
$nodeAfterUpdate = node_load($responseArray['body']['nid'], NULL, TRUE);
$this
->assertTrue(isset($nodeAfterUpdate->nid), 'Node was successfully updated', 'NodeResource: Updated');
$this
->assertEqual($nodeAfterUpdate->title, $node_update['title'], 'Title is the same', 'NodeResource: Update');
$this
->assertEqual($nodeAfterUpdate->body[LANGUAGE_NONE][0]['value'], $node_update['body'][LANGUAGE_NONE][0]['value'], 'Body is the same', 'NodeResource: Update');
}
public function testEndpointResourceNodeUpdate() {
$this->privilegedUser = $this
->drupalCreateUser(array(
'administer services',
'bypass node access',
));
$this
->drupalLogin($this->privilegedUser);
$this
->update_node(SERVICES_NODE_TYPE_INCLUDE);
$this
->update_node(SERVICES_NODE_TYPE_EMPTY);
$this
->update_node(SERVICES_NODE_TYPE_REMOVED);
}
public function testEndpointResourceNodeUpdateLegacy() {
$this->privilegedUser = $this
->drupalCreateUser(array(
'administer services',
'bypass node access',
));
$this
->drupalLogin($this->privilegedUser);
$node = $this
->drupalCreateNode();
$node_update = (array) $node;
$node_update['title'] = $this
->randomName();
$node_update['body'][LANGUAGE_NONE][0]['value'] = $this
->randomName();
$responseArray = $this
->servicesPut($this->endpoint->path . '/node/' . $node->nid, array(
'node' => $node_update,
));
$nodeAfterUpdate = node_load($responseArray['body']['nid'], NULL, TRUE);
$this
->assertTrue(isset($nodeAfterUpdate->nid), 'Node was successfully updated', 'NodeResource: Updated (legacy)');
$this
->assertEqual($nodeAfterUpdate->title, $node_update['title'], 'Title is the same', 'NodeResource: Update (legacy)');
$this
->assertEqual($nodeAfterUpdate->body[LANGUAGE_NONE][0]['value'], $node_update['body'][LANGUAGE_NONE][0]['value'], 'Body is the same', 'NodeResource: Update (legacy)');
}
public function testEndpointResourceNodeUpdatePermFail() {
$this->privilegedUser = $this
->drupalCreateUser(array(
'administer services',
'create page content',
'edit own page content',
));
$this
->drupalLogin($this->privilegedUser);
$node = $this
->drupalCreateNode(array(
'uid' => 1,
));
$node_update = (array) $node;
$node_update['title'] = $this
->randomName();
$node_update['body'][LANGUAGE_NONE][0]['value'] = $this
->randomName();
$responseArray = $this
->servicesPut($this->endpoint->path . '/node/' . $node->nid, array(
'node' => $node_update,
));
$this
->assertTrue(strpos($responseArray['status'], 'Access denied for user'), 'Updating the node failed without needed permissions. This is good!', 'NodeResource: Update');
}
public function testEndpointResourceNodeUpdateMissingTitle() {
$this->privilegedUser = $this
->drupalCreateUser(array(
'administer services',
'bypass node access',
));
$this
->drupalLogin($this->privilegedUser);
$node = $this
->drupalCreateNode();
$node_update = array(
'title' => '',
'body' => array(
LANGUAGE_NONE => array(
array(
'value' => $this
->randomString(),
),
),
),
'name' => $this->privilegedUser->name,
'type' => 'page',
);
$responseArray = $this
->servicesPut($this->endpoint->path . '/node/' . $node->nid, array(
'node' => $node_update,
));
$this
->assertTrue(strpos($responseArray['status'], 'Title field is required.'), 'Node was not updated without title.', 'NodeResource: Update');
}
public function testEndpointResourceNodeDelete() {
$this->privilegedUser = $this
->drupalCreateUser(array(
'administer services',
'bypass node access',
));
$this
->drupalLogin($this->privilegedUser);
$node = $this
->drupalCreateNode();
$data = '';
$responseArray = $this
->servicesDelete($this->endpoint->path . '/node/' . $node->nid, $data);
$deleted_node = node_load($node->nid, NULL, TRUE);
$this
->assertTrue(empty($deleted_node), 'Node was deleted.', 'NodeResource: Deleted');
$responseArray = $this
->servicesDelete($this->endpoint->path . '/node/' . $node->nid, $data);
$this
->assertFalse($responseArray['code'] == 200, 'Node was deleted. It shoudlnt have been because it doesnt exist', 'NodeResource: Deleted');
}
}
class ServicesResourceNodeTaxonomytests extends ServicesWebTestCase {
protected $admin_user = NULL;
protected $endpoint = NULL;
protected $instance = NULL;
public static function getInfo() {
return array(
'name' => 'Resource Node - taxonomy',
'description' => 'Test the resource Node taxonomy methods and actions.',
'group' => 'Services',
'dependencies' => array(
'ctools',
'libraries',
),
);
}
public function setUp(array $modules = array()) {
$modules[] = 'taxonomy';
parent::setUp($modules);
$this->admin_user = $this
->drupalCreateUser(array(
'administer taxonomy',
'bypass node access',
'administer services',
));
$this
->drupalLogin($this->admin_user);
$this->vocabulary = $this
->createVocabulary();
$field = array(
'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
'type' => 'taxonomy_term_reference',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => $this->vocabulary->machine_name,
'parent' => 0,
),
),
),
);
field_create_field($field);
$this->instance = array(
'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
'bundle' => 'article',
'entity_type' => 'node',
'widget' => array(
'type' => 'options_select',
),
'display' => array(
'default' => array(
'type' => 'taxonomy_term_reference_link',
),
),
);
field_create_instance($this->instance);
$this->endpoint = $this
->saveNewEndpoint();
}
function testServicesTaxonomyNode() {
$term1 = $this
->createTerm($this->vocabulary);
$term2 = $this
->createTerm($this->vocabulary);
$field_name = $this->instance['field_name'];
$edit = array();
$langcode = LANGUAGE_NONE;
$edit['title'] = $this
->randomName();
$edit["body[{$langcode}][0][value]"] = $this
->randomName();
$edit[$field_name][$langcode][0] = $term1->tid;
$edit['type'] = 'page';
$edit['name'] = $this->admin_user->name;
$edit['language'] = LANGUAGE_NONE;
$responseArray = $this
->servicesPost($this->endpoint->path . '/node', array(
'node' => $edit,
));
$nodeResourceCreateReturn = $responseArray['body'];
$this
->assertTrue(isset($nodeResourceCreateReturn['nid']), 'Node was successfully created', 'NodeResource: Create');
$newNode = node_load($nodeResourceCreateReturn['nid']);
$this
->assertTrue($newNode->{$field_name}[$langcode][0]['tid'] = $term1->tid, 'Term was the same', 'Taxonomy: Create');
$edit[$field_name][$langcode][0] = $term2->tid;
$responseArray = $this
->servicesPost($this->endpoint->path . '/node', array(
'node' => $edit,
));
$nodeResourceCreateReturn = $responseArray['body'];
$this
->assertTrue(isset($nodeResourceCreateReturn['nid']), 'Node was successfully created', 'NodeResource: Create');
$newNode = node_load($nodeResourceCreateReturn['nid']);
$this
->assertTrue($newNode->{$field_name}[$langcode][0]['tid'] = $term2->tid, 'Term was the same', 'Taxonomy: updated');
}
function createVocabulary() {
$vocabulary = new stdClass();
$vocabulary->name = $this
->randomName();
$vocabulary->description = $this
->randomName();
$vocabulary->machine_name = drupal_strtolower($this
->randomName());
$vocabulary->help = '';
$vocabulary->nodes = array(
'article' => 'article',
);
$vocabulary->weight = mt_rand(0, 10);
taxonomy_vocabulary_save($vocabulary);
return $vocabulary;
}
function createTerm($vocabulary) {
$term = new stdClass();
$term->name = $this
->randomName();
$term->description = $this
->randomName();
$term->format = db_query_range('SELECT format FROM {filter_format}', 0, 1)
->fetchField();
$term->vid = $vocabulary->vid;
taxonomy_term_save($term);
return $term;
}
}