Bug 9302: (QA follow-up) Consistency follow-up
[koha.git] / t / db_dependent / Koha / Ratings.t
blob00229e0d828fd6a4b917057f8011b11cc3168580
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Modern::Perl;
20 use Test::More tests => 6;
22 use Koha::Database;
23 use Koha::Rating;
24 use Koha::Ratings;
26 use t::lib::TestBuilder;
28 my $schema = Koha::Database->schema;
29 $schema->storage->txn_begin;
30 my $builder = t::lib::TestBuilder->new;
32 my $patron_1 = $builder->build( { source => 'Borrower', } );
33 my $patron_2 = $builder->build( { source => 'Borrower', } );
34 my $biblio_1 = $builder->build( { source => 'Biblio', } );
35 my $biblionumber = $biblio_1->{biblionumber};
37 my $rating_1 = Koha::Rating->new( { biblionumber => $biblionumber, borrowernumber => $patron_1->{borrowernumber}, rating_value => 3 } )->store;
38 my $rating_2 = Koha::Rating->new( { biblionumber => $biblionumber, borrowernumber => $patron_2->{borrowernumber}, rating_value => 4 } )->store;
40 is( Koha::Ratings->search( { biblionumber => $biblionumber } )->get_avg_rating, 3.5, 'get_avg_rating is 3.5' );
42 $rating_1->rating_value(5)->store;
44 is( Koha::Ratings->search( { biblionumber => $biblionumber } )->get_avg_rating, 4.5, 'get_avg_rating now up to 4.5' );
46 $rating_1->rating_value(42)->store;
47 is( Koha::Ratings->find( { biblionumber => $biblionumber, borrowernumber => $patron_1->{borrowernumber} } )->rating_value,
48 5, 'Koha::Ratings->store should mark out the boundaries of the rating values, 5 is max' );
50 $rating_1->rating_value(-42)->store;
51 is( Koha::Ratings->find( { biblionumber => $biblionumber, borrowernumber => $patron_1->{borrowernumber} } )->rating_value,
52 0, 'Koha::Ratings->store should mark out the boundaries of the rating values, 0 is min' );
54 Koha::Ratings->find( { biblionumber => $biblionumber, borrowernumber => $patron_1->{borrowernumber} } )->delete;
55 Koha::Ratings->find( { biblionumber => $biblionumber, borrowernumber => $patron_2->{borrowernumber} } )->delete;
56 is( Koha::Ratings->search( { biblionumber => $biblionumber } )->count, 0, 'Delete should have deleted the ratings' );
58 is( int(Koha::Ratings->search( { biblionumber => $biblionumber } )->get_avg_rating), 0, 'get_avg_rating should return 0 if no rating exist' );