Skip to content

Commit

Permalink
more test scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Sep 16, 2024
1 parent 89d72dd commit 2335b1a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Access/PollPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public function changeVote(User $actor, Poll $poll)
if (!$poll->allow_change_vote) {
return $this->deny();
}

if ($poll->allow_change_vote) {

Check failure on line 69 in src/Access/PollPolicy.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 7.3

If condition is always true.

Check failure on line 69 in src/Access/PollPolicy.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 7.4

If condition is always true.

Check failure on line 69 in src/Access/PollPolicy.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.0

If condition is always true.

Check failure on line 69 in src/Access/PollPolicy.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.1

If condition is always true.

Check failure on line 69 in src/Access/PollPolicy.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.2

If condition is always true.

Check failure on line 69 in src/Access/PollPolicy.php

View workflow job for this annotation

GitHub Actions / run / PHPStan PHP 8.3

If condition is always true.
return $this->allow();
}
}

public function edit(User $actor, Poll $poll)
Expand Down
62 changes: 60 additions & 2 deletions tests/integration/api/ChangeVoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ public function setUp(): void
],
'polls' => [
['id' => 1, 'question' => 'Testing Poll--Global', 'subtitle' => 'Testing subtitle', 'image' => 'pollimage-abcdef.png', 'image_alt' => 'test alt', 'post_id' => null, 'user_id' => 1, 'public_poll' => 0, 'end_date' => null, 'created_at' => '2021-01-01 00:00:00', 'updated_at' => '2021-01-01 00:00:00', 'vote_count' => 0, 'allow_multiple_votes' => 0, 'max_votes' => 0, 'settings' => '{"max_votes": 0,"hide_votes": false,"public_poll": false,"allow_change_vote": false,"allow_multiple_votes": false}'],
['id' => 2, 'question' => 'Testing Poll--Global 2', 'subtitle' => 'Testing subtitle', 'image' => 'pollimage-abcdef.png', 'image_alt' => 'test alt', 'post_id' => null, 'user_id' => 1, 'public_poll' => 0, 'end_date' => null, 'created_at' => '2021-01-01 00:00:00', 'updated_at' => '2021-01-01 00:00:00', 'vote_count' => 0, 'allow_multiple_votes' => 0, 'max_votes' => 0, 'settings' => '{"max_votes": 0,"hide_votes": false,"public_poll": false,"allow_change_vote": true,"allow_multiple_votes": false}'],
],
'poll_options' => [
['id' => 1, 'answer' => 'Option 1', 'poll_id' => 1, 'vote_count' => 0, 'image_url' => 'pollimage-hijklm.png', 'created_at' => '2021-01-01 00:00:00', 'updated_at' => '2021-01-01 00:00:00'],
['id' => 2, 'answer' => 'Option 2', 'poll_id' => 1, 'vote_count' => 0, 'image_url' => 'pollimage-nopqrs.png', 'created_at' => '2021-01-01 00:00:00', 'updated_at' => '2021-01-01 00:00:00'],
['id' => 3, 'answer' => 'Option 3', 'poll_id' => 2, 'vote_count' => 0, 'image_url' => 'pollimage-hijklm.png', 'created_at' => '2021-01-01 00:00:00', 'updated_at' => '2021-01-01 00:00:00'],
['id' => 4, 'answer' => 'Option 4', 'poll_id' => 2, 'vote_count' => 0, 'image_url' => 'pollimage-nopqrs.png', 'created_at' => '2021-01-01 00:00:00', 'updated_at' => '2021-01-01 00:00:00'],
],
'group_user' => [
['user_id' => 4, 'group_id' => 4],
Expand All @@ -58,6 +61,9 @@ public function setUp(): void
['id' => 1, 'poll_id' => 1, 'option_id' => 1, 'user_id' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()],
['id' => 2, 'poll_id' => 1, 'option_id' => 1, 'user_id' => 2, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()],
['id' => 3, 'poll_id' => 1, 'option_id' => 1, 'user_id' => 4, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()],
['id' => 4, 'poll_id' => 2, 'option_id' => 3, 'user_id' => 1, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()],
['id' => 5, 'poll_id' => 2, 'option_id' => 3, 'user_id' => 2, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()],
['id' => 6, 'poll_id' => 2, 'option_id' => 3, 'user_id' => 4, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()],
],
]);
}
Expand Down Expand Up @@ -95,7 +101,7 @@ public function validation_error_when_no_data_is_passed()
*
* @dataProvider usersWhoCanChangeVote
*/
public function user_with_permission_can_change_vote(int $userId)
public function user_with_permission_can_change_vote_on_no_change_poll(int $userId)
{
$response = $this->send(
$this->request('PATCH', '/api/fof/polls/1/votes', [
Expand All @@ -120,7 +126,7 @@ public function user_with_permission_can_change_vote(int $userId)
/**
* @test
*/
public function user_without_permission_cannot_change_vote()
public function user_without_permission_cannot_change_vote_on_no_change_poll()
{
$response = $this->send(
$this->request('PATCH', '/api/fof/polls/1/votes', [
Expand All @@ -141,4 +147,56 @@ public function user_without_permission_cannot_change_vote()

$this->assertEquals(1, $vote->option_id);
}

/**
* @test
*
* @dataProvider usersWhoCanChangeVote
*/
public function user_with_permission_can_change_vote_on_change_poll(int $userId)
{
$response = $this->send(
$this->request('PATCH', '/api/fof/polls/2/votes', [
'authenticatedAs' => $userId,
'json' => [
'data' => [
'optionIds' => [
4,
],
],
],
])
);

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

$vote = PollVote::where('user_id', $userId)->where('poll_id', 2)->first();

$this->assertEquals(4, $vote->option_id);
}

/**
* @test
*/
public function user_without_permission_can_change_vote_on_change_poll()
{
$response = $this->send(
$this->request('PATCH', '/api/fof/polls/2/votes', [
'authenticatedAs' => 2,
'json' => [
'data' => [
'optionIds' => [
4,
],
],
],
])
);

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

$vote = PollVote::where('user_id', 2)->where('poll_id', 2)->first();

$this->assertEquals(4, $vote->option_id);
}
}

0 comments on commit 2335b1a

Please sign in to comment.