2020-12-01 08:24:50 +08:00
< ? php
/*
* This file is part of Flarum .
*
* For detailed copyright and license information , please view the
* LICENSE file that was distributed with this source code .
*/
namespace Flarum\Tests\integration\extenders ;
use Carbon\Carbon ;
2021-03-24 05:33:51 +08:00
use Flarum\Api\Controller\ShowUserController ;
2020-12-01 08:24:50 +08:00
use Flarum\Api\Serializer\AbstractSerializer ;
use Flarum\Api\Serializer\BasicUserSerializer ;
use Flarum\Api\Serializer\DiscussionSerializer ;
use Flarum\Api\Serializer\ForumSerializer ;
use Flarum\Api\Serializer\PostSerializer ;
use Flarum\Api\Serializer\UserSerializer ;
use Flarum\Discussion\Discussion ;
use Flarum\Extend ;
use Flarum\Post\Post ;
2021-03-08 05:32:41 +08:00
use Flarum\Testing\integration\RetrievesAuthorizedUsers ;
use Flarum\Testing\integration\TestCase ;
2020-12-01 08:24:50 +08:00
use Flarum\User\User ;
class ApiSerializerTest extends TestCase
{
use RetrievesAuthorizedUsers ;
2021-01-07 11:34:32 +08:00
/**
* @ inheritDoc
*/
protected function setUp () : void
2020-12-01 08:24:50 +08:00
{
2021-01-07 11:34:32 +08:00
parent :: setUp ();
2020-12-01 08:24:50 +08:00
$this -> prepareDatabase ([
'users' => [
$this -> normalUser ()
],
'discussions' => [
[ 'id' => 1 , 'title' => 'Custom Discussion Title' , 'created_at' => Carbon :: now () -> toDateTimeString (), 'user_id' => 2 , 'first_post_id' => 0 , 'comment_count' => 1 , 'is_private' => 0 ],
[ 'id' => 2 , 'title' => 'Custom Discussion Title' , 'created_at' => Carbon :: now () -> toDateTimeString (), 'user_id' => 2 , 'first_post_id' => 0 , 'comment_count' => 1 , 'is_private' => 0 ],
[ 'id' => 3 , 'title' => 'Custom Discussion Title' , 'created_at' => Carbon :: now () -> toDateTimeString (), 'user_id' => 2 , 'first_post_id' => 0 , 'comment_count' => 1 , 'is_private' => 0 ],
],
'posts' => [
[ 'id' => 1 , 'discussion_id' => 3 , 'created_at' => Carbon :: now () -> toDateTimeString (), 'user_id' => 2 , 'type' => 'discussionRenamed' , 'content' => '<t><p>can i haz relationz?</p></t>' ],
],
]);
}
/**
* @ test
*/
public function custom_attributes_dont_exist_by_default ()
{
$this -> app ();
$response = $this -> send (
$this -> request ( 'GET' , '/api' , [
'authenticatedAs' => 1 ,
])
);
2021-03-05 22:43:35 +08:00
$payload = json_decode ( $response -> getBody () -> getContents (), true );
2020-12-01 08:24:50 +08:00
$this -> assertArrayNotHasKey ( 'customAttribute' , $payload [ 'data' ][ 'attributes' ]);
}
/**
* @ test
*/
public function custom_attributes_exist_if_added ()
{
$this -> extend (
( new Extend\ApiSerializer ( ForumSerializer :: class ))
2021-02-06 02:21:36 +08:00
-> attributes ( function () {
2020-12-01 08:24:50 +08:00
return [
'customAttribute' => true
];
})
);
$this -> app ();
$response = $this -> send (
$this -> request ( 'GET' , '/api' , [
'authenticatedAs' => 1 ,
])
);
2021-03-05 22:43:35 +08:00
$payload = json_decode ( $response -> getBody () -> getContents (), true );
2020-12-01 08:24:50 +08:00
$this -> assertArrayHasKey ( 'customAttribute' , $payload [ 'data' ][ 'attributes' ]);
}
/**
* @ test
*/
public function custom_attributes_with_invokable_exist_if_added ()
{
$this -> extend (
( new Extend\ApiSerializer ( ForumSerializer :: class ))
2021-02-06 02:21:36 +08:00
-> attributes ( CustomAttributesInvokableClass :: class )
2020-12-01 08:24:50 +08:00
);
$this -> app ();
$response = $this -> send (
$this -> request ( 'GET' , '/api' , [
'authenticatedAs' => 1 ,
])
);
2021-03-05 22:43:35 +08:00
$payload = json_decode ( $response -> getBody () -> getContents (), true );
2020-12-01 08:24:50 +08:00
$this -> assertArrayHasKey ( 'customAttributeFromInvokable' , $payload [ 'data' ][ 'attributes' ]);
}
/**
* @ test
*/
public function custom_attributes_exist_if_added_to_parent_class ()
{
$this -> extend (
( new Extend\ApiSerializer ( BasicUserSerializer :: class ))
2021-02-06 02:21:36 +08:00
-> attributes ( function () {
2020-12-01 08:24:50 +08:00
return [
'customAttribute' => true
];
})
);
$this -> app ();
$response = $this -> send (
$this -> request ( 'GET' , '/api/users/2' , [
'authenticatedAs' => 1 ,
])
);
2021-03-05 22:43:35 +08:00
$payload = json_decode ( $response -> getBody () -> getContents (), true );
2020-12-01 08:24:50 +08:00
$this -> assertArrayHasKey ( 'customAttribute' , $payload [ 'data' ][ 'attributes' ]);
}
/**
* @ test
*/
public function custom_attributes_prioritize_child_classes ()
{
$this -> extend (
( new Extend\ApiSerializer ( BasicUserSerializer :: class ))
2021-02-06 02:21:36 +08:00
-> attributes ( function () {
2020-12-01 08:24:50 +08:00
return [
'customAttribute' => 'initialValue'
];
}),
( new Extend\ApiSerializer ( UserSerializer :: class ))
2021-02-06 02:21:36 +08:00
-> attributes ( function () {
2020-12-01 08:24:50 +08:00
return [
'customAttribute' => 'newValue'
];
})
);
$this -> app ();
$response = $this -> send (
$this -> request ( 'GET' , '/api/users/2' , [
'authenticatedAs' => 1 ,
])
);
2021-03-05 22:43:35 +08:00
$payload = json_decode ( $response -> getBody () -> getContents (), true );
2020-12-01 08:24:50 +08:00
$this -> assertArrayHasKey ( 'customAttribute' , $payload [ 'data' ][ 'attributes' ]);
$this -> assertEquals ( 'newValue' , $payload [ 'data' ][ 'attributes' ][ 'customAttribute' ]);
}
/**
* @ test
*/
public function custom_single_attribute_exists_if_added ()
{
$this -> extend (
( new Extend\ApiSerializer ( ForumSerializer :: class ))
-> attribute ( 'customSingleAttribute' , function () {
return true ;
}) -> attribute ( 'customSingleAttribute_0' , function () {
return 0 ;
})
);
$this -> app ();
$response = $this -> send (
$this -> request ( 'GET' , '/api' , [
'authenticatedAs' => 1 ,
])
);
2021-03-05 22:43:35 +08:00
$payload = json_decode ( $response -> getBody () -> getContents (), true );
2020-12-01 08:24:50 +08:00
$this -> assertArrayHasKey ( 'customSingleAttribute' , $payload [ 'data' ][ 'attributes' ]);
$this -> assertArrayHasKey ( 'customSingleAttribute_0' , $payload [ 'data' ][ 'attributes' ]);
$this -> assertEquals ( 0 , $payload [ 'data' ][ 'attributes' ][ 'customSingleAttribute_0' ]);
}
/**
* @ test
*/
public function custom_single_attribute_with_invokable_exists_if_added ()
{
$this -> extend (
( new Extend\ApiSerializer ( ForumSerializer :: class ))
-> attribute ( 'customSingleAttribute_1' , CustomSingleAttributeInvokableClass :: class )
);
$this -> app ();
$response = $this -> send (
$this -> request ( 'GET' , '/api' , [
'authenticatedAs' => 1 ,
])
);
2021-03-05 22:43:35 +08:00
$payload = json_decode ( $response -> getBody () -> getContents (), true );
2020-12-01 08:24:50 +08:00
$this -> assertArrayHasKey ( 'customSingleAttribute_1' , $payload [ 'data' ][ 'attributes' ]);
}
/**
* @ test
*/
public function custom_single_attribute_exists_if_added_to_parent_class ()
{
$this -> extend (
( new Extend\ApiSerializer ( BasicUserSerializer :: class ))
-> attribute ( 'customSingleAttribute_2' , function () {
return true ;
})
);
$this -> app ();
$response = $this -> send (
$this -> request ( 'GET' , '/api/users/2' , [
'authenticatedAs' => 1 ,
])
);
2021-03-05 22:43:35 +08:00
$payload = json_decode ( $response -> getBody () -> getContents (), true );
2020-12-01 08:24:50 +08:00
$this -> assertArrayHasKey ( 'customSingleAttribute_2' , $payload [ 'data' ][ 'attributes' ]);
}
/**
* @ test
*/
public function custom_single_attribute_prioritizes_child_classes ()
{
$this -> extend (
( new Extend\ApiSerializer ( BasicUserSerializer :: class ))
-> attribute ( 'customSingleAttribute_3' , function () {
return 'initialValue' ;
}),
( new Extend\ApiSerializer ( UserSerializer :: class ))
-> attribute ( 'customSingleAttribute_3' , function () {
return 'newValue' ;
})
);
$this -> app ();
$response = $this -> send (
$this -> request ( 'GET' , '/api/users/2' , [
'authenticatedAs' => 1 ,
])
);
2021-03-05 22:43:35 +08:00
$payload = json_decode ( $response -> getBody () -> getContents (), true );
2020-12-01 08:24:50 +08:00
$this -> assertArrayHasKey ( 'customSingleAttribute_3' , $payload [ 'data' ][ 'attributes' ]);
$this -> assertEquals ( 'newValue' , $payload [ 'data' ][ 'attributes' ][ 'customSingleAttribute_3' ]);
}
/**
* @ test
*/
public function custom_attributes_can_be_overriden ()
{
$this -> extend (
( new Extend\ApiSerializer ( BasicUserSerializer :: class ))
-> attribute ( 'someCustomAttribute' , function () {
return 'newValue' ;
2021-02-06 02:21:36 +08:00
}) -> attributes ( function () {
2020-12-01 08:24:50 +08:00
return [
'someCustomAttribute' => 'initialValue' ,
'someOtherCustomAttribute' => 'initialValue' ,
];
}) -> attribute ( 'someOtherCustomAttribute' , function () {
return 'newValue' ;
})
);
$this -> app ();
$response = $this -> send (
$this -> request ( 'GET' , '/api/users/2' , [
'authenticatedAs' => 1 ,
])
);
2021-03-05 22:43:35 +08:00
$payload = json_decode ( $response -> getBody () -> getContents (), true );
2020-12-01 08:24:50 +08:00
$this -> assertArrayHasKey ( 'someCustomAttribute' , $payload [ 'data' ][ 'attributes' ]);
$this -> assertEquals ( 'newValue' , $payload [ 'data' ][ 'attributes' ][ 'someCustomAttribute' ]);
$this -> assertArrayHasKey ( 'someOtherCustomAttribute' , $payload [ 'data' ][ 'attributes' ]);
$this -> assertEquals ( 'newValue' , $payload [ 'data' ][ 'attributes' ][ 'someOtherCustomAttribute' ]);
}
2021-03-24 05:33:51 +08:00
/**
* @ test
*/
public function custom_relations_dont_exist_by_default ()
{
$this -> extend (
( new Extend\ApiController ( ShowUserController :: class ))
-> addInclude ([ 'customSerializerRelation' , 'postCustomRelation' , 'anotherCustomRelation' ])
);
$response = $this -> send (
$this -> request ( 'GET' , '/api/users/2' , [
'authenticatedAs' => 1 ,
])
);
$responseJson = json_decode ( $response -> getBody (), true );
$this -> assertArrayNotHasKey ( 'customSerializerRelation' , $responseJson [ 'data' ][ 'relationships' ]);
$this -> assertArrayNotHasKey ( 'postCustomRelation' , $responseJson [ 'data' ][ 'relationships' ]);
$this -> assertArrayNotHasKey ( 'anotherCustomRelation' , $responseJson [ 'data' ][ 'relationships' ]);
}
2020-12-01 08:24:50 +08:00
/**
* @ test
*/
public function custom_hasMany_relationship_exists_if_added ()
{
$this -> extend (
( new Extend\Model ( User :: class ))
-> hasMany ( 'customSerializerRelation' , Discussion :: class , 'user_id' ),
( new Extend\ApiSerializer ( UserSerializer :: class ))
2021-03-24 05:33:51 +08:00
-> hasMany ( 'customSerializerRelation' , DiscussionSerializer :: class ),
( new Extend\ApiController ( ShowUserController :: class ))
-> addInclude ( 'customSerializerRelation' )
2020-12-01 08:24:50 +08:00
);
2021-03-24 05:33:51 +08:00
$response = $this -> send (
$this -> request ( 'GET' , '/api/users/2' , [
'authenticatedAs' => 1 ,
])
);
2020-12-01 08:24:50 +08:00
2021-03-24 05:33:51 +08:00
$responseJson = json_decode ( $response -> getBody (), true );
2020-12-01 08:24:50 +08:00
2021-03-24 05:33:51 +08:00
$this -> assertArrayHasKey ( 'customSerializerRelation' , $responseJson [ 'data' ][ 'relationships' ]);
$this -> assertCount ( 3 , $responseJson [ 'data' ][ 'relationships' ][ 'customSerializerRelation' ][ 'data' ]);
2020-12-01 08:24:50 +08:00
}
/**
* @ test
*/
public function custom_hasOne_relationship_exists_if_added ()
{
$this -> extend (
( new Extend\Model ( User :: class ))
-> hasOne ( 'customSerializerRelation' , Discussion :: class , 'user_id' ),
( new Extend\ApiSerializer ( UserSerializer :: class ))
2021-03-24 05:33:51 +08:00
-> hasOne ( 'customSerializerRelation' , DiscussionSerializer :: class ),
( new Extend\ApiController ( ShowUserController :: class ))
-> addInclude ( 'customSerializerRelation' )
2020-12-01 08:24:50 +08:00
);
2021-03-24 05:33:51 +08:00
$response = $this -> send (
$this -> request ( 'GET' , '/api/users/2' , [
'authenticatedAs' => 1 ,
])
);
2020-12-01 08:24:50 +08:00
2021-03-24 05:33:51 +08:00
$responseJson = json_decode ( $response -> getBody (), true );
2020-12-01 08:24:50 +08:00
2021-03-24 05:33:51 +08:00
$this -> assertArrayHasKey ( 'customSerializerRelation' , $responseJson [ 'data' ][ 'relationships' ]);
$this -> assertEquals ( 'discussions' , $responseJson [ 'data' ][ 'relationships' ][ 'customSerializerRelation' ][ 'data' ][ 'type' ]);
2020-12-01 08:24:50 +08:00
}
/**
* @ test
*/
public function custom_relationship_exists_if_added ()
{
$this -> extend (
( new Extend\Model ( User :: class ))
-> hasOne ( 'customSerializerRelation' , Discussion :: class , 'user_id' ),
( new Extend\ApiSerializer ( UserSerializer :: class ))
-> relationship ( 'customSerializerRelation' , function ( AbstractSerializer $serializer , $model ) {
return $serializer -> hasOne ( $model , DiscussionSerializer :: class , 'customSerializerRelation' );
2021-03-24 05:33:51 +08:00
}),
( new Extend\ApiController ( ShowUserController :: class ))
-> addInclude ( 'customSerializerRelation' )
2020-12-01 08:24:50 +08:00
);
2021-03-24 05:33:51 +08:00
$response = $this -> send (
$this -> request ( 'GET' , '/api/users/2' , [
'authenticatedAs' => 1 ,
])
);
2020-12-01 08:24:50 +08:00
2021-03-24 05:33:51 +08:00
$responseJson = json_decode ( $response -> getBody (), true );
2020-12-01 08:24:50 +08:00
2021-03-24 05:33:51 +08:00
$this -> assertArrayHasKey ( 'customSerializerRelation' , $responseJson [ 'data' ][ 'relationships' ]);
$this -> assertEquals ( 'discussions' , $responseJson [ 'data' ][ 'relationships' ][ 'customSerializerRelation' ][ 'data' ][ 'type' ]);
2020-12-01 08:24:50 +08:00
}
/**
* @ test
*/
public function custom_relationship_with_invokable_exists_if_added ()
{
$this -> extend (
( new Extend\Model ( User :: class ))
-> hasOne ( 'customSerializerRelation' , Discussion :: class , 'user_id' ),
( new Extend\ApiSerializer ( UserSerializer :: class ))
2021-03-24 05:33:51 +08:00
-> relationship ( 'customSerializerRelation' , CustomRelationshipInvokableClass :: class ),
( new Extend\ApiController ( ShowUserController :: class ))
-> addInclude ( 'customSerializerRelation' )
2020-12-01 08:24:50 +08:00
);
2021-03-24 05:33:51 +08:00
$response = $this -> send (
$this -> request ( 'GET' , '/api/users/2' , [
'authenticatedAs' => 1 ,
])
);
2020-12-01 08:24:50 +08:00
2021-03-24 05:33:51 +08:00
$responseJson = json_decode ( $response -> getBody (), true );
2020-12-01 08:24:50 +08:00
2021-03-24 05:33:51 +08:00
$this -> assertArrayHasKey ( 'customSerializerRelation' , $responseJson [ 'data' ][ 'relationships' ]);
$this -> assertEquals ( 'discussions' , $responseJson [ 'data' ][ 'relationships' ][ 'customSerializerRelation' ][ 'data' ][ 'type' ]);
2020-12-01 08:24:50 +08:00
}
/**
* @ test
*/
public function custom_relationship_is_inherited_to_child_classes ()
{
$this -> extend (
( new Extend\Model ( User :: class ))
-> hasMany ( 'anotherCustomRelation' , Discussion :: class , 'user_id' ),
( new Extend\ApiSerializer ( BasicUserSerializer :: class ))
2021-03-24 05:33:51 +08:00
-> hasMany ( 'anotherCustomRelation' , DiscussionSerializer :: class ),
( new Extend\ApiController ( ShowUserController :: class ))
-> addInclude ( 'anotherCustomRelation' )
2020-12-01 08:24:50 +08:00
);
2021-03-24 05:33:51 +08:00
$response = $this -> send (
$this -> request ( 'GET' , '/api/users/2' , [
'authenticatedAs' => 1 ,
])
);
2020-12-01 08:24:50 +08:00
2021-03-24 05:33:51 +08:00
$responseJson = json_decode ( $response -> getBody (), true );
2020-12-01 08:24:50 +08:00
2021-03-24 05:33:51 +08:00
$this -> assertArrayHasKey ( 'anotherCustomRelation' , $responseJson [ 'data' ][ 'relationships' ]);
$this -> assertCount ( 3 , $responseJson [ 'data' ][ 'relationships' ][ 'anotherCustomRelation' ][ 'data' ]);
2020-12-01 08:24:50 +08:00
}
/**
* @ test
*/
public function custom_relationship_prioritizes_child_classes ()
{
$this -> extend (
( new Extend\Model ( User :: class ))
-> hasOne ( 'postCustomRelation' , Post :: class , 'user_id' ),
( new Extend\Model ( User :: class ))
-> hasOne ( 'discussionCustomRelation' , Discussion :: class , 'user_id' ),
( new Extend\ApiSerializer ( BasicUserSerializer :: class ))
-> hasOne ( 'postCustomRelation' , PostSerializer :: class ),
( new Extend\ApiSerializer ( UserSerializer :: class ))
-> relationship ( 'postCustomRelation' , function ( AbstractSerializer $serializer , $model ) {
return $serializer -> hasOne ( $model , DiscussionSerializer :: class , 'discussionCustomRelation' );
2021-03-24 05:33:51 +08:00
}),
( new Extend\ApiController ( ShowUserController :: class ))
-> addInclude ( 'postCustomRelation' )
2020-12-01 08:24:50 +08:00
);
2021-03-24 05:33:51 +08:00
$response = $this -> send (
$this -> request ( 'GET' , '/api/users/2' , [
'authenticatedAs' => 1 ,
])
);
2020-12-01 08:24:50 +08:00
2021-03-24 05:33:51 +08:00
$responseJson = json_decode ( $response -> getBody (), true );
2020-12-01 08:24:50 +08:00
2021-03-24 05:33:51 +08:00
$this -> assertArrayHasKey ( 'postCustomRelation' , $responseJson [ 'data' ][ 'relationships' ]);
$this -> assertEquals ( 'discussions' , $responseJson [ 'data' ][ 'relationships' ][ 'postCustomRelation' ][ 'data' ][ 'type' ]);
2020-12-01 08:24:50 +08:00
}
}
class CustomAttributesInvokableClass
{
public function __invoke ()
{
return [
'customAttributeFromInvokable' => true
];
}
}
class CustomSingleAttributeInvokableClass
{
public function __invoke ()
{
return true ;
}
}
class CustomRelationshipInvokableClass
{
public function __invoke ( AbstractSerializer $serializer , $model )
{
return $serializer -> hasOne ( $model , DiscussionSerializer :: class , 'customSerializerRelation' );
}
}