]> git.koha-community.org Git - koha.git/blob - t/db_dependent/Koha/Ratings.t
Bug 17089: Koha::Ratings - move tests
[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 => 4;
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( { source => '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, );
41
42 $rating_1->rating_value(5)->store;
43
44 is( Koha::Ratings->search( { biblionumber => $biblionumber } )->get_avg_rating, 4.5, );
45
46 Koha::Ratings->find( { biblionumber => $biblionumber, borrowernumber => $patron_1->{borrowernumber} } )->delete;
47 Koha::Ratings->find( { biblionumber => $biblionumber, borrowernumber => $patron_2->{borrowernumber} } )->delete;
48 is( Koha::Ratings->search( { biblionumber => $biblionumber } )->count, 0, 'Delete should have deleted the ratings' );
49
50 is( int(Koha::Ratings->search( { biblionumber => $biblionumber } )->get_avg_rating), 0, 'get_avg_rating should return 0 if no rating exist' );
51
52 1;