From e4136c2b3ad4730255f8c39409756c82948f0cfd Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 9 Aug 2016 15:08:55 +0100 Subject: [PATCH] Bug 17089: Mark out the boundaries of ratings In the doc of C4::Ratings, it was written that the rate values should be >=0 and <=5, but it was not taken into account. This bug should be fixed on stable releases as well. Signed-off-by: Owen Leonard Signed-off-by: Marcel de Rooy Signed-off-by: Kyle M Hall --- Koha/Rating.pm | 15 +++++++++++++++ t/db_dependent/Koha/Ratings.t | 10 +++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Koha/Rating.pm b/Koha/Rating.pm index 1bdeb69e99..94186d0abc 100644 --- a/Koha/Rating.pm +++ b/Koha/Rating.pm @@ -33,6 +33,21 @@ Koha::Rating - Koha Rating Object class =cut +=head3 store + +=cut + +sub store { + my ($self) = @_; + if ( $self->rating_value > 5 ) { + $self->rating_value(5); + } + elsif ( $self->rating_value < 0 ) { + $self->rating_value(0); + } + return $self->SUPER::store; +} + =head3 type =cut diff --git a/t/db_dependent/Koha/Ratings.t b/t/db_dependent/Koha/Ratings.t index 2d99e945d7..2f8ead9c56 100755 --- a/t/db_dependent/Koha/Ratings.t +++ b/t/db_dependent/Koha/Ratings.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 4; +use Test::More tests => 6; use Koha::Database; use Koha::Rating; @@ -43,6 +43,14 @@ $rating_1->rating_value(5)->store; is( Koha::Ratings->search( { biblionumber => $biblionumber } )->get_avg_rating, 4.5, ); +$rating_1->rating_value(42)->store; +is( Koha::Ratings->find( { biblionumber => $biblionumber, borrowernumber => $patron_1->{borrowernumber} } )->rating_value, + 5, 'Koha::Ratings->store should mark out the boundaries of the rating values, 5 is max' ); + +$rating_1->rating_value(-42)->store; +is( Koha::Ratings->find( { biblionumber => $biblionumber, borrowernumber => $patron_1->{borrowernumber} } )->rating_value, + 0, 'Koha::Ratings->store should mark out the boundaries of the rating values, 0 is min' ); + Koha::Ratings->find( { biblionumber => $biblionumber, borrowernumber => $patron_1->{borrowernumber} } )->delete; Koha::Ratings->find( { biblionumber => $biblionumber, borrowernumber => $patron_2->{borrowernumber} } )->delete; is( Koha::Ratings->search( { biblionumber => $biblionumber } )->count, 0, 'Delete should have deleted the ratings' ); -- 2.39.5