|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2018 Cloud Creativity Limited |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +namespace CloudCreativity\LaravelJsonApi\Tests\Integration; |
| 20 | + |
| 21 | +use CloudCreativity\LaravelJsonApi\Routing\ApiGroup; |
| 22 | +use CloudCreativity\LaravelJsonApi\Tests\Models\Comment; |
| 23 | + |
| 24 | +class ValidationTest extends TestCase |
| 25 | +{ |
| 26 | + |
| 27 | + /** |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + protected $resourceType; |
| 31 | + |
| 32 | + /** |
| 33 | + * @return void |
| 34 | + */ |
| 35 | + protected function setUp() |
| 36 | + { |
| 37 | + parent::setUp(); |
| 38 | + $this->withDefaultApi([], function (ApiGroup $api) { |
| 39 | + $api->resource('posts'); |
| 40 | + $api->resource('comments'); |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * The client must receive a 400 error with a correct JSON API pointer if an invalid |
| 46 | + * resource type is sent for a resource relationship. |
| 47 | + * |
| 48 | + * @see https://github.com/cloudcreativity/laravel-json-api/issues/139 |
| 49 | + */ |
| 50 | + public function testRejectsUnrecognisedTypeInResourceRelationship() |
| 51 | + { |
| 52 | + $this->resourceType = 'comments'; |
| 53 | + $comment = factory(Comment::class)->make(); |
| 54 | + |
| 55 | + $data = [ |
| 56 | + 'type' => 'comments', |
| 57 | + 'attributes' => [ |
| 58 | + 'content' => $comment->content, |
| 59 | + ], |
| 60 | + 'relationships' => [ |
| 61 | + 'post' => [ |
| 62 | + 'data' => [ |
| 63 | + 'type' => 'post', // invalid type as expecting the plural, |
| 64 | + 'id' => (string) $comment->post_id, |
| 65 | + ], |
| 66 | + ], |
| 67 | + ], |
| 68 | + ]; |
| 69 | + |
| 70 | + $this->doCreate($data) |
| 71 | + ->assertStatus(400) |
| 72 | + ->assertErrors() |
| 73 | + ->assertPointers('/data/relationships/post/data/type'); |
| 74 | + } |
| 75 | +} |
0 commit comments