Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[koha.git] / t / db_dependent / Koha / Ratings.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 6;
21
22 use Koha::Database;
23 use Koha::Rating;
24 use Koha::Ratings;
25
26 use t::lib::TestBuilder;
27
28 my $schema = Koha::Database->schema;
29 $schema->storage->txn_begin;
30 my $builder = t::lib::TestBuilder->new;
31
32 my $patron_1 = $builder->build( { source => 'Borrower', } );
33 my $patron_2 = $builder->build( { source => 'Borrower', } );
34 my $biblio_1 = $builder->build_sample_biblio;
35 my $biblionumber = $biblio_1->biblionumber;
36
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;
39
40 is( Koha::Ratings->search( { biblionumber => $biblionumber } )->get_avg_rating, 3.5, 'get_avg_rating is 3.5' );
41
42 $rating_1->rating_value(5)->store;
43
44 is( Koha::Ratings->search( { biblionumber => $biblionumber } )->get_avg_rating, 4.5, 'get_avg_rating now up to 4.5' );
45
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' );
49
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' );
53
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' );
57
58 is( int(Koha::Ratings->search( { biblionumber => $biblionumber } )->get_avg_rating), 0, 'get_avg_rating should return 0 if no rating exist' );
59