function RestfulUpdateEntityTestCase::testUpdateEntityAsPatch in RESTful 7.2
Same name and namespace in other branches
- 7 tests/RestfulUpdateEntityTestCase.test \RestfulUpdateEntityTestCase::testUpdateEntityAsPatch()
Test update an entity (PATCH method).
File
- tests/
RestfulUpdateEntityTestCase.test, line 122 - Contains RestfulUpdateEntityTestCase
Class
Code
function testUpdateEntityAsPatch() {
$resource_manager = restful()
->getResourceManager();
$label = $this
->randomName();
$new_label = $this
->randomName();
$text = $this
->randomName();
$settings = array(
'type' => 'article',
'title' => $label,
'uid' => $this->account->uid,
);
$settings['body'][LANGUAGE_NONE][0]['value'] = $text;
$node = $this
->drupalCreateNode($settings);
$id = $node->nid;
$handler = $resource_manager
->getPlugin('test_articles:1.2');
$handler
->setAccount($this->account);
$request = array(
'label' => $new_label,
);
$result = drupal_json_decode(restful()
->getFormatterManager()
->format($handler
->doPatch($id, $request), 'json'));
$result = $result['data'];
$expected_result = array(
array(
'id' => $id,
'label' => $new_label,
'self' => $handler
->versionedUrl($id),
'body' => $text,
),
);
$result[0]['body'] = trim(strip_tags($result[0]['body']));
$this
->assertEqual($result, $expected_result);
// Update an entity with invalid property name.
$request['invalid'] = 'wrong';
try {
$handler
->setRequest(Request::create('api/test_articles/v1.2/' . $id, array(), RequestInterface::METHOD_PATCH, NULL, FALSE, NULL, array(), array(), array(), $request));
$handler
->setPath($id);
restful()
->getFormatterManager()
->format($handler
->process(), 'json');
$this
->fail('User can update using PATCH method an entity with invalid property name.');
} catch (Exception $e) {
$this
->pass('User cannot update using PATCH method an entity with invalid property name.');
}
$new_body = $this
->randomName();
$request = array(
'body' => $new_body,
);
// Setting new value for 'body'.
$result = drupal_json_decode(restful()
->getFormatterManager()
->format($handler
->doPatch($id, $request), 'json'));
// Making sure the new body has been set.
$this
->assertEqual(trim(strip_tags($result['data'][0]['body'])), $new_body);
// Removing the body value.
$request = array(
'body' => NULL,
);
$result = drupal_json_decode(restful()
->getFormatterManager()
->format($handler
->doPatch($id, $request), 'json'));
$this
->assertEqual($result['data'][0]['body'], NULL);
}