RestfulUpdateEntityTestCase.test in RESTful 7.2
Same filename and directory in other branches
Contains RestfulUpdateEntityTestCase
File
tests/RestfulUpdateEntityTestCase.testView source
<?php
/**
* @file
* Contains RestfulUpdateEntityTestCase
*/
use Drupal\restful\Http\Request;
use Drupal\restful\Http\RequestInterface;
class RestfulUpdateEntityTestCase extends DrupalWebTestCase {
/**
* Holds the user account that owns the content.
*
* @var object
*/
protected $account;
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'Update entity',
'description' => 'Test the updating of an entity using PUT and PATCH methods.',
'group' => 'RESTful',
);
}
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp('restful_test');
$this->account = $this
->drupalCreateUser(array(
'create article content',
'edit own article content',
));
}
/**
* Test update an entity (PUT method).
*/
public function testUpdateEntityAsPut() {
$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
->doPut($id, $request), 'json'));
$result = $result['data'];
$expected_result = array(
array(
'id' => $id,
'label' => $new_label,
'self' => $handler
->versionedUrl($id),
'body' => NULL,
),
);
$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_PUT, NULL, FALSE, NULL, array(), array(), array(), $request));
$handler
->setPath($id);
restful()
->getFormatterManager()
->format($handler
->process(), 'json');
$this
->fail('User can update using PUT method an entity with invalid property name.');
} catch (Exception $e) {
$this
->pass('User cannot update using PUT method an entity with invalid property name.');
}
$new_label = $this
->randomName();
$new_body = $this
->randomName();
$request = array(
'label' => $new_label,
'body' => $new_body,
);
// Setting new value for 'body'.
$result = drupal_json_decode(restful()
->getFormatterManager()
->format($handler
->doPut($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(
'label' => $new_label,
'body' => NULL,
);
$result = drupal_json_decode(restful()
->getFormatterManager()
->format($handler
->doPut($id, $request), 'json'));
$this
->assertEqual($result['data'][0]['body'], NULL);
}
/**
* Test update an entity (PATCH method).
*/
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);
}
}
Classes
Name | Description |
---|---|
RestfulUpdateEntityTestCase |