Skip to content

Commit

Permalink
list post reactions test
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Oct 22, 2023
1 parent b0ab72a commit cb3cf3a
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Api/Controller/ListPostReactionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class ListPostReactionsController extends AbstractListController
{
public $serializer = PostReactionSerializer::class;

public $include = ['reaction'];
public $include = ['reaction', 'user'];

public $optionalInclude = ['user', 'post'];
public $optionalInclude = ['post'];

/**
* @var PostRepository
Expand Down
114 changes: 114 additions & 0 deletions tests/integration/api/ListPostReactionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace FoF\Reactions\Tests\Integration\Api;

use Carbon\Carbon;
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
use Flarum\Testing\integration\TestCase;

class ListPostReactionsTest extends TestCase
{
use RetrievesAuthorizedUsers;

public function setUp(): void
{
parent::setUp();

$this->extension('fof-reactions');

$this->prepareDatabase([
'users' => [
$this->normalUser(),
['id' => 3, 'username' => 'Acme', 'email' => 'acme@machine.local', 'is_email_confirmed' => 1],
['id' => 4, 'username' => 'Acme2', 'email' => 'acme2@machine.local', 'is_email_confirmed' => 1],
['id' => 5, 'username' => 'Acme3', 'email' => 'acme3@machine.local', 'is_email_confirmed' => 1],
['id' => 6, 'username' => 'Acme4', 'email' => 'acme4@machine.local', 'is_email_confirmed' => 1],
],
'discussions' => [
['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now(), 'last_posted_at' => Carbon::now(), 'user_id' => 1, 'first_post_id' => 1, 'comment_count' => 2],
],
'posts' => [
['id' => 1, 'number' => 1, 'discussion_id' => 1, 'created_at' => Carbon::now(), 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p>something</p></t>'],
['id' => 3, 'number' => 2, 'discussion_id' => 1, 'created_at' => Carbon::now(), 'user_id' => 2, 'type' => 'comment', 'content' => '<t><p>something</p></t>'],
['id' => 5, 'number' => 3, 'discussion_id' => 1, 'created_at' => Carbon::now(), 'user_id' => 3, 'type' => 'discussionRenamed', 'content' => '<t><p>something</p></t>'],
['id' => 6, 'number' => 4, 'discussion_id' => 1, 'created_at' => Carbon::now(), 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p>something</p></t>'],
['id' => 6, 'number' => 4, 'discussion_id' => 1, 'created_at' => Carbon::now(), 'user_id' => 5, 'type' => 'comment', 'content' => '<t><p>something</p></t>'],
],
'post_reactions' => [
['id' => 1, 'post_id' => 1, 'reaction_id' => 1, 'user_id' => 2],
['id' => 2, 'post_id' => 1, 'reaction_id' => 1, 'user_id' => 3],
['id' => 3, 'post_id' => 1, 'reaction_id' => 2, 'user_id' => 4],
['id' => 4, 'post_id' => 1, 'reaction_id' => 4, 'user_id' => 5],
['id' => 5, 'post_id' => 1, 'reaction_id' => null, 'user_id' => 6],
],
'post_anonymous_reactions' => [
['id' => 1, 'post_id' => 1, 'reaction_id' => 1, 'guest_id' => '1234567'],
['id' => 2, 'post_id' => 1, 'reaction_id' => 1, 'guest_id' => 'abfdb'],
['id' => 3, 'post_id' => 1, 'reaction_id' => 1, 'guest_id' => '123098'],
['id' => 4, 'post_id' => 1, 'reaction_id' => 1, 'guest_id' => 'gd'],
['id' => 5, 'post_id' => 1, 'reaction_id' => null, 'guest_id' => 'gsadasdd'],
],
]);
}

/**
* @test
*/
public function guest_can_see_reactions_on_a_post_when_guest_reacting_is_off()
{
$response = $this->send(
$this->request('GET', '/api/posts/1/reactions', [
])
);

$this->assertEquals(200, $response->getStatusCode());

$response = json_decode($response->getBody()->getContents(), true);

$this->assertEquals(4, count($response['data']));

$this->assertEquals(1, $response['data'][0]['attributes']['reactionId']);
$this->assertEquals(1, $response['data'][1]['attributes']['reactionId']);
$this->assertEquals(2, $response['data'][2]['attributes']['reactionId']);
$this->assertEquals(4, $response['data'][3]['attributes']['reactionId']);

$this->assertEquals(2, $response['data'][0]['attributes']['userId']);
$this->assertEquals(3, $response['data'][1]['attributes']['userId']);
$this->assertEquals(4, $response['data'][2]['attributes']['userId']);
$this->assertEquals(5, $response['data'][3]['attributes']['userId']);

$this->assertEquals(1, $response['data'][0]['attributes']['postId']);
$this->assertEquals(1, $response['data'][1]['attributes']['postId']);
$this->assertEquals(1, $response['data'][2]['attributes']['postId']);
$this->assertEquals(1, $response['data'][3]['attributes']['postId']);

$this->assertEquals('reactions', $response['data'][0]['relationships']['reaction']['data']['type']);
$this->assertEquals('reactions', $response['data'][1]['relationships']['reaction']['data']['type']);
$this->assertEquals('reactions', $response['data'][2]['relationships']['reaction']['data']['type']);
$this->assertEquals('reactions', $response['data'][3]['relationships']['reaction']['data']['type']);

$this->assertEquals('users', $response['data'][0]['relationships']['user']['data']['type']);
$this->assertEquals('users', $response['data'][1]['relationships']['user']['data']['type']);
$this->assertEquals('users', $response['data'][2]['relationships']['user']['data']['type']);
$this->assertEquals('users', $response['data'][3]['relationships']['user']['data']['type']);
}

/**
* @test
*/
public function guest_can_see_reactions_on_a_post_when_guest_reacting_is_on()
{
$this->setting('fof-reactions.anonymousReactions', true);

$response = $this->send(
$this->request('GET', '/api/posts/1/reactions', [
])
);

$this->assertEquals(200, $response->getStatusCode());

$response = json_decode($response->getBody()->getContents(), true);

$this->assertEquals(8, count($response['data']));
}
}

0 comments on commit cb3cf3a

Please sign in to comment.