RestfulHookMenuTestCase.test in RESTful 7.2
Same filename and directory in other branches
Contains \RestfulHookMenuTestCase.
File
tests/RestfulHookMenuTestCase.testView source
<?php
/**
* @file
* Contains \RestfulHookMenuTestCase.
*/
use Drupal\restful\Http\Request;
use Drupal\restful\Http\RequestInterface;
class RestfulHookMenuTestCase extends RestfulCurlBaseTestCase {
/**
* Overrides DrupalWebTestCase::getInfo().
*/
public static function getInfo() {
return array(
'name' => 'Menu API',
'description' => 'Test the hook_menu() and delivery callback implementations.',
'group' => 'RESTful',
);
}
/**
* Overrides DrupalWebTestCase::setUp().
*/
public function setUp() {
parent::setUp('restful_example');
// Allow anonymous users to edit articles.
user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
'edit any article content' => TRUE,
));
}
/**
* Test viewing an entity (GET method).
*/
public function testViewEntity() {
$user1 = $this
->drupalCreateUser(array(
'edit own article content',
));
$title = $this
->randomName();
$settings = array(
'type' => 'article',
'title' => $title,
'uid' => $user1->uid,
);
$node1 = $this
->drupalCreateNode($settings);
// Test version 1.0
$result = $this
->httpRequest('api/v1.0/articles/' . $node1->nid);
$expected_result = array(
'data' => array(
array(
'id' => $node1->nid,
'label' => $node1->title,
'self' => url('api/v1.0/articles/' . $node1->nid, array(
'absolute' => TRUE,
)),
),
),
'self' => array(
'title' => 'Self',
'href' => url('api/v1.0/articles/' . $node1->nid, array(
'absolute' => TRUE,
)),
),
);
$this
->assertEqual($result['body'], json_encode($expected_result));
// Test version 1.1
$result = $this
->httpRequest('api/v1.1/articles/' . $node1->nid, RequestInterface::METHOD_GET);
$expected_result['self']['href'] = url('api/v1.1/articles/' . $node1->nid, array(
'absolute' => TRUE,
));
unset($expected_result['data'][0]['self']);
$this
->assertEqual($result['body'], json_encode($expected_result));
// Test method override.
$headers = array(
'X-HTTP-Method-Override' => RequestInterface::METHOD_PATCH,
'X-CSRF-Token' => drupal_get_token(\Drupal\restful\Plugin\authentication\Authentication::TOKEN_VALUE),
'Content-Type' => 'application/json',
'Authorization' => 'Basic ' . base64_encode($user1->name . ':' . $user1->pass_raw),
);
$body = array(
'label' => 'new title',
);
$handler = restful()
->getResourceManager()
->getPlugin('articles:1.1');
$header_bag = new \Drupal\restful\Http\HttpHeaderBag($headers);
$handler
->setRequest(Request::create('api/v1.0/articles/' . $node1->nid, array(), RequestInterface::METHOD_POST, $header_bag, FALSE, NULL, array(), array(), array(), $body));
$handler
->setPath($node1->nid);
restful()
->getFormatterManager()
->format($handler
->process(), 'json');
$node1 = node_load($node1->nid);
$this
->assertEqual($node1->title, 'new title', 'HTTP method was overridden.');
}
/**
* Test HTTP headers change according to the response.
*/
public function testHttpHeadersAndStatus() {
// Valid request (even though it's empty).
$result = $this
->httpRequest('api/v1.0/articles/', RequestInterface::METHOD_GET);
$this
->assertTrue(strpos($result['headers'], 'application/json;'), '"application/json" found in valid request.');
// Invalid request.
$result = $this
->httpRequest('api/v1.0/articles/invalid_id', RequestInterface::METHOD_GET);
$this
->assertTrue(strpos($result['headers'], 'application/problem+json;') !== FALSE, '"application/problem+json" found in invalid request.');
// Switch site to offline mode.
variable_set('maintenance_mode', TRUE);
$this->httpauth_credentials = NULL;
$result = $this
->httpRequest('api/login');
$this
->assertEqual($result['code'], '503', '503 status code sent for site in offline mode.');
}
/**
* Test hijacking of api/* pages and showing proper error messages.
*/
public function testNotFoundDelivery() {
// Invalid URLs.
$urls = array(
'api/invalid',
);
foreach ($urls as $url) {
$result = $this
->httpRequest($url);
$body = drupal_json_decode($result['body']);
$this
->assertEqual($result['code'], '404', format_string('404 status code sent for @url url.', array(
'@url' => $url,
)));
$this
->assertTrue(strpos($result['headers'], 'application/problem+json;') !== FALSE, '"application/problem+json" found in invalid request.');
$this
->assertEqual($body['title'], 'Invalid URL path.', 'Correct error message.');
}
// Non-related url.
$result = $this
->httpRequest('api-api');
$this
->assertEqual($result['code'], '404', format_string('404 status code sent for @url url.', array(
'@url' => $url,
)));
$this
->assertFalse(strpos($result['headers'], 'application/problem+json;'), 'Only correct URL is hijacked.');
}
/**
* Test the version negotiation.
*/
public function testVersionNegotiation() {
// Fake the HTTP header.
$test_harness = array(
array(
'path' => 'api/v1.1/articles',
'version_header' => NULL,
'expected_version' => array(
1,
1,
),
'expected_resource' => 'articles',
),
array(
'path' => 'api/v1/articles',
'version_header' => NULL,
'expected_version' => array(
1,
7,
),
'expected_resource' => 'articles',
),
array(
'path' => 'api/articles',
'version_header' => 'v1',
'expected_version' => array(
1,
7,
),
'expected_resource' => 'articles',
),
array(
'path' => 'api/articles',
'version_header' => 'v1.0',
'expected_version' => array(
1,
0,
),
'expected_resource' => 'articles',
),
array(
'path' => 'api/articles',
'version_header' => NULL,
'expected_version' => array(
2,
1,
),
'expected_resource' => 'articles',
),
);
foreach ($test_harness as $test_item) {
$headers = NULL;
if (!empty($test_item['version_header'])) {
$headers = new \Drupal\restful\Http\HttpHeaderBag(array(
'X-API-Version' => $test_item['version_header'],
));
}
$request = \Drupal\restful\Http\Request::create($test_item['path'], array(), RequestInterface::METHOD_GET, $headers);
$resource_manager = new \Drupal\restful\Resource\ResourceManager($request);
drupal_static_reset('Drupal\\restful\\Resource\\ResourceManager::getVersionFromRequest');
$this
->assertEqual($resource_manager
->getVersionFromRequest(), $test_item['expected_version'], sprintf('%s resolves correctly.', $test_item['path']));
$this
->assertEqual($resource_manager
->getResourceIdFromRequest(), $test_item['expected_resource'], sprintf('Resource name obtained correctly from %s.', $test_item['path']));
}
}
}
Classes
Name | Description |
---|---|
RestfulHookMenuTestCase |