View source
<?php
namespace Drupal\Tests\farm_location\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Url;
use Drupal\farm_geo\Traits\WktTrait;
use Drupal\Tests\farm_test\Functional\FarmBrowserTestBase;
use Drupal\Tests\jsonapi\Functional\JsonApiRequestTestTrait;
use GuzzleHttp\RequestOptions;
class LocationAPITest extends FarmBrowserTestBase {
use WktTrait;
use JsonApiRequestTestTrait;
use LocationFunctionalTestTrait;
protected static $modules = [
'basic_auth',
'farm_location',
'farm_location_test',
'farm_api',
];
public function testApiAssetPostResponse() {
$this
->drupalLogout();
$request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
$request_options[RequestOptions::HEADERS]['Content-Type'] = 'application/vnd.api+json';
$request_options[RequestOptions::HEADERS]['Authorization'] = 'Basic ' . base64_encode($this->user->name->value . ':' . $this->user->passRaw);
$asset_uri = "base://api/asset/object";
$post_data = [
'data' => [
'type' => 'asset--object',
'attributes' => [
'name' => 'Test object',
],
],
];
$request_options[RequestOptions::BODY] = Json::encode($post_data);
$response = $this
->request('POST', Url::fromUri($asset_uri), $request_options);
$this
->assertEquals(201, $response
->getStatusCode());
$response_data = Json::decode((string) $response
->getBody());
$this
->assertArrayhasKey('data', $response_data);
$this
->assertArrayHasKey('attributes', $response_data['data']);
$this
->assertArrayHasKey('geometry', $response_data['data']['attributes']);
$this
->assertEmpty($response_data['data']['attributes']['geometry']);
$this
->assertArrayHasKey('relationships', $response_data['data']);
$this
->assertArrayHasKey('location', $response_data['data']['relationships']);
$this
->assertEmpty($response_data['data']['relationships']['location']['data']);
$post_data['data']['attributes']['is_fixed'] = TRUE;
$post_data['data']['attributes']['intrinsic_geometry'] = $this
->reduceWkt(\Drupal::service('geofield.wkt_generator')
->wktGeneratePolygon(NULL, 5));
$request_options[RequestOptions::BODY] = Json::encode($post_data);
$response = $this
->request('POST', Url::fromUri($asset_uri), $request_options);
$this
->assertEquals(201, $response
->getStatusCode());
$response_data = Json::decode((string) $response
->getBody());
$this
->assertArrayhasKey('data', $response_data);
$this
->assertArrayHasKey('attributes', $response_data['data']);
$this
->assertArrayHasKey('geometry', $response_data['data']['attributes']);
$this
->assertArrayHasKey('value', $response_data['data']['attributes']['geometry']);
$this
->assertEquals($response_data['data']['attributes']['geometry']['value'], $post_data['data']['attributes']['intrinsic_geometry']);
$this
->assertArrayHasKey('relationships', $response_data['data']);
$this
->assertArrayHasKey('location', $response_data['data']['relationships']);
$this
->assertEmpty($response_data['data']['relationships']['location']['data']);
}
}