diff --git a/resources/node_resource.inc b/resources/node_resource.inc index 934b272..ba0cbfa 100644 --- a/resources/node_resource.inc +++ b/resources/node_resource.inc @@ -272,6 +272,16 @@ function _node_resource_update($nid, $node) { // Load the required includes for drupal_execute module_load_include('inc', 'node', 'node.pages'); + // Make submission of node type optional. + if (empty($node['type'])) { + $node['type'] = $old_node->type; + } + // Check for type validity. + $node_types = node_type_get_types(); + if (!array_key_exists($node['type'], $node_types)) { + return services_error(t('Node type @type does not exist.', array('@type' => $node_type)), 406); + } + // Setup form_state. $form_state = array(); $form_state['values'] = $node; diff --git a/tests/functional/ServicesResourceNodeTests.test b/tests/functional/ServicesResourceNodeTests.test index 344d345..4f5f36c 100644 --- a/tests/functional/ServicesResourceNodeTests.test +++ b/tests/functional/ServicesResourceNodeTests.test @@ -244,6 +244,78 @@ class ServicesResourceNodetests extends ServicesWebTestCase { } /** + * testing node_resource Update with missing node type + */ + public function testEndpointResourceNodeUpdateMissingType() { + // Create and log in our privileged user. + $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(); + unset($node_update['type']); + + $responseArray = $this->servicesPut($this->endpoint->path . '/node/' . $node->nid, array('node' => $node_update)); + // Load node not from cache. + $nodeAfterUpdate = node_load($responseArray['body']['nid'], NULL, TRUE); + $this->assertTrue(isset($nodeAfterUpdate->nid), t('Node was successfully updated'), 'NodeResource: Updated'); + $this->assertEqual($nodeAfterUpdate->title, $node_update['title'], t('Title is the same'), 'NodeResource: Update'); + $this->assertEqual($nodeAfterUpdate->body[LANGUAGE_NONE][0]['value'], $node_update['body'][LANGUAGE_NONE][0]['value'], t('Body is the same'), 'NodeResource: Update'); + } + + /** + * testing node_resource Update with empty node type + */ + public function testEndpointResourceNodeUpdateEmptyType() { + // Create and log in our privileged user. + $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(); + $node_update['type'] = ''; + + $responseArray = $this->servicesPut($this->endpoint->path . '/node/' . $node->nid, array('node' => $node_update)); + // Load node not from cache. + $nodeAfterUpdate = node_load($responseArray['body']['nid'], NULL, TRUE); + $this->assertTrue(isset($nodeAfterUpdate->nid), t('Node was successfully updated'), 'NodeResource: Updated'); + $this->assertEqual($nodeAfterUpdate->title, $node_update['title'], t('Title is the same'), 'NodeResource: Update'); + $this->assertEqual($nodeAfterUpdate->body[LANGUAGE_NONE][0]['value'], $node_update['body'][LANGUAGE_NONE][0]['value'], t('Body is the same'), 'NodeResource: Update'); + } + + /** + * testing node_resource Update with invalid node type + */ + public function testEndpointResourceNodeUpdateInvalidType() { + // Create and log in our privileged user. + $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(); + $node_update['type'] = uniqid('', TRUE); + + $responseArray = $this->servicesPut($this->endpoint->path . '/node/' . $node->nid, array('node' => $node_update)); + + $this->assertTrue($responseArray['code'] == 406, t('Node can not be updated if type is invalid'), 'NodeResource: UPDATE'); + } + + /** * testing node_resource Update fail with no permissions */ public function testEndpointResourceNodeUpdatePermFail() { @@ -447,4 +519,4 @@ class ServicesResourceNodeTaxonomytests extends ServicesResourceNodetests { taxonomy_term_save($term); return $term; } -} \ No newline at end of file +}