RestfulUpdateEntityCurlTestCase.test in RESTful 7
Same filename and directory in other branches
Contains RestfulUpdateEntityCurlTestCase
File
tests/RestfulUpdateEntityCurlTestCase.testView source
<?php
/**
* @file
* Contains RestfulUpdateEntityCurlTestCase
*/
class RestfulUpdateEntityCurlTestCase extends RestfulCurlBaseTestCase {
/**
* User account.
*
* @var stdClass
*/
protected $account;
public static function getInfo() {
return array(
'name' => 'Update entity with CURL',
'description' => 'Test the updating of an entity using PUT and PATCH methods using CURL.',
'group' => 'RESTful',
);
}
function setUp() {
parent::setUp('restful_test');
$this->account = $this
->drupalCreateUser(array(
'administer site configuration',
'administer nodes',
'edit own article content',
'edit any article content',
));
$this
->drupalLogin($this->account);
$this->httpauth_credentials = $this->account->name . ':' . $this->account->pass_raw;
}
/**
* Test update an entity (PUT & PATCH methods).
*/
function testUpdateEntityAsPutPatch() {
// Test update an entity (PUT method).
$label = $this
->randomName();
$new_label = $this
->randomName();
$text = $this
->randomName();
$settings = array(
'type' => 'article',
'title' => $label,
);
$settings['body'][LANGUAGE_NONE][0]['value'] = $text;
$node = $this
->drupalCreateNode($settings);
$id = $node->nid;
$request = array(
'label' => $new_label,
);
$result = $this
->httpRequest('api/v1.0/test_articles/' . $id, \RestfulInterface::PUT, $request);
$expected_result = array(
'data' => array(
array(
'id' => $id,
'label' => $new_label,
'self' => url('api/v1.0/test_articles/' . $id, array(
'absolute' => TRUE,
)),
'body' => NULL,
),
),
'self' => array(
'title' => 'Self',
'href' => url('api/v1.0/test_articles/' . $id, array(
'absolute' => TRUE,
)),
),
);
$this
->assertEqual($result['body'], drupal_json_encode($expected_result));
// Update an entity with invalid property name.
$request['invalid'] = 'wrong';
$result = $this
->httpRequest('api/v1.0/test_articles/' . $id, \RestfulInterface::PUT, $request);
$this
->assertEqual($result['code'], 400, 'User cannot update using PUT method an entity with invalid property name.');
// Test update an entity (PATCH method).
$label = $this
->randomName();
$new_label = $this
->randomName();
$text = $this
->randomName();
$settings = array(
'type' => 'article',
'title' => $label,
);
$settings['body'][LANGUAGE_NONE][0]['value'] = $text;
$node = $this
->drupalCreateNode($settings);
$id = $node->nid;
$request = array(
'label' => $new_label,
);
$result = $this
->httpRequest("api/v1.0/test_articles/{$id}", \RestfulInterface::PATCH, $request);
$expected_result = array(
'data' => array(
array(
'id' => $id,
'label' => $new_label,
'self' => url('api/v1.0/test_articles/' . $id, array(
'absolute' => TRUE,
)),
'body' => $text,
),
),
'self' => array(
'title' => 'Self',
'href' => url('api/v1.0/test_articles/' . $id, array(
'absolute' => TRUE,
)),
),
);
$result = drupal_json_decode($result['body']);
$result['data'][0]['body'] = trim(strip_tags($result['data'][0]['body']));
ksort($result);
ksort($expected_result);
$this
->assertEqual($result, $expected_result);
// Update an entity with invalid property name.
$request['invalid'] = 'wrong';
$result = $this
->httpRequest("api/v1.0/test_articles/{$id}", \RestfulInterface::PATCH, $request);
$this
->assertEqual($result['code'], 400, 'User cannot update using PATCH method an entity with invalid property name.');
}
}
Classes
Name | Description |
---|---|
RestfulUpdateEntityCurlTestCase | @file Contains RestfulUpdateEntityCurlTestCase |