View source
<?php
namespace Drupal\Tests\jsonapi\Functional;
use Drupal\Component\Utility\NestedArray;
use Drupal\contact\Entity\ContactForm;
use Drupal\contact\Entity\Message;
use Drupal\Core\Url;
use GuzzleHttp\RequestOptions;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
class MessageTest extends ResourceTestBase {
public static $modules = [
'contact',
];
protected static $entityTypeId = 'contact_message';
protected static $resourceTypeName = 'contact_message--camelids';
protected $entity;
protected static $labelFieldName = 'subject';
protected function setUpAuthorization($method) {
$this
->grantPermissionsToTestedRole([
'access site-wide contact form',
]);
}
protected function createEntity() {
if (!ContactForm::load('camelids')) {
ContactForm::create([
'id' => 'camelids',
'label' => 'Llama',
'message' => 'Let us know what you think about llamas',
'reply' => 'Llamas are indeed awesome!',
'recipients' => [
'llama@example.com',
'contact@example.com',
],
])
->save();
}
$message = Message::create([
'contact_form' => 'camelids',
'subject' => 'Llama Gabilondo',
'message' => 'Llamas are awesome!',
]);
$message
->save();
return $message;
}
protected function getExpectedDocument() {
throw new \Exception('Not yet supported.');
}
protected function getPostDocument() {
return [
'data' => [
'type' => 'contact_message--camelids',
'attributes' => [
'subject' => 'Dramallama',
'message' => 'http://www.urbandictionary.com/define.php?term=drama%20llama',
],
],
];
}
protected function getExpectedUnauthorizedAccessMessage($method) {
if ($method === 'POST') {
return "The 'access site-wide contact form' permission is required.";
}
return parent::getExpectedUnauthorizedAccessMessage($method);
}
public function testGetIndividual() {
$this
->setExpectedException(RouteNotFoundException::class, 'Route "jsonapi.contact_message--camelids.individual" does not exist.');
Url::fromRoute('jsonapi.contact_message--camelids.individual')
->toString(TRUE);
}
public function testPatchIndividual() {
$this
->setExpectedException(RouteNotFoundException::class, 'Route "jsonapi.contact_message--camelids.individual" does not exist.');
Url::fromRoute('jsonapi.contact_message--camelids.individual')
->toString(TRUE);
}
public function testDeleteIndividual() {
$this
->setExpectedException(RouteNotFoundException::class, 'Route "jsonapi.contact_message--camelids.individual" does not exist.');
Url::fromRoute('jsonapi.contact_message--camelids.individual')
->toString(TRUE);
}
public function testRelated() {
$this
->setExpectedException(RouteNotFoundException::class, 'Route "jsonapi.contact_message--camelids.related" does not exist.');
Url::fromRoute('jsonapi.contact_message--camelids.related')
->toString(TRUE);
}
public function testRelationships() {
$this
->setExpectedException(RouteNotFoundException::class, 'Route "jsonapi.contact_message--camelids.relationship.get" does not exist.');
Url::fromRoute('jsonapi.contact_message--camelids.relationship.get')
->toString(TRUE);
}
public function testCollection() {
$collection_url = Url::fromRoute('jsonapi.contact_message--camelids.collection.post')
->setAbsolute(TRUE);
$request_options = [];
$request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
$request_options = NestedArray::mergeDeep($request_options, $this
->getAuthenticationRequestOptions());
$response = $this
->request('GET', $collection_url, $request_options);
$this
->assertSame(405, $response
->getStatusCode());
$this
->assertSame([
'POST',
], $response
->getHeader('Allow'));
}
public function testRevisions() {
$this
->setExpectedException(RouteNotFoundException::class, 'Route "jsonapi.contact_message--camelids.individual" does not exist.');
Url::fromRoute('jsonapi.contact_message--camelids.individual')
->toString(TRUE);
}
}