From 0d70f8360e3a8c89dd4509f3c2932f1d4bec4102 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. --- 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 1bdeb69..94186d0 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 2d99e94..2f8ead9 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.8.1