Skip to content

Commit

Permalink
Fix and test legacy _kksr_avg calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
kamalkhan committed Jul 18, 2019
1 parent 19938f3 commit 9731455
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ function vote($idOrPost, $rating)
update_post_meta($id, '_'.prefix('ratings'), $ratings);
update_post_meta($id, '_'.prefix('casts'), $count);
// For legacy reasons.
update_post_meta($id, '_'.prefix('avg'), calculateScore($ratings, $count));
update_post_meta($id, '_'.prefix('avg'), calculateScore($ratings, $count, $stars));

do_action(prefix('vote'), $id, $rating);

Expand Down
12 changes: 11 additions & 1 deletion tests/AjaxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ function it_allows_authenticated_users_to_vote()
/** @test*/
function it_allows_guests_to_vote_if_guest_voting_is_allowed()
{
update_option('kksr_stars', 10);

$post = static::factory()->post->create_and_get();

$_POST['id'] = $post->ID;
$_POST['rating'] = 4;
$_POST['rating'] = 8;

$this->assertFalse(is_user_logged_in());
$this->assertContains('guests', get_option('kksr_strategies', []));
Expand Down Expand Up @@ -104,10 +106,18 @@ function assertVote($ratings, $count, $post, array $extra = [])
$this->assertPayloadSubset($extra);
}

$this->assertAvg($score, $post);
$this->assertRatings($ratings, $post);
$this->assertVoteCount($count, $post);
}

function assertAvg($score, $post)
{
$id = is_object($post) ? $post->ID : $post;

$this->assertEquals($score, get_post_meta($id, '_kksr_avg', true));
}

function assertRatings($ratings, $post)
{
$id = is_object($post) ? $post->ID : $post;
Expand Down

0 comments on commit 9731455

Please sign in to comment.