Bug 15578: Insert authority records before processing tests
[koha.git] / t / db_dependent / 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 => 14;
21
22 use C4::Biblio qw/AddBiblio/;
23 use C4::Members;
24 use C4::Context;
25 use C4::Category;
26 use Koha::Database;
27
28 use t::lib::TestBuilder;
29
30 use_ok('C4::Ratings');
31
32 my $schema = Koha::Database->schema;
33 $schema->storage->txn_begin;
34 my $builder = t::lib::TestBuilder->new;
35 my $dbh = C4::Context->dbh;
36 $dbh->{RaiseError} = 1;
37
38 my $library = $builder->build({
39     source => 'Branch',
40 });
41
42 my ($biblionumber) = AddBiblio( MARC::Record->new, '' );
43
44 my @categories   = C4::Category->all;
45 my $categorycode = $categories[0]->categorycode;
46 my $branchcode   = $library->{branchcode};
47
48 my %john_doe = (
49     cardnumber   => '123456',
50     firstname    => 'John',
51     surname      => 'Doe',
52     categorycode => $categorycode,
53     branchcode   => $branchcode,
54     dateofbirth  => '',
55     dateexpiry   => '9999-12-31',
56     userid       => 'john.doe'
57 );
58
59 my %jane_doe = (
60     cardnumber   => '345678',
61     firstname    => 'Jane',
62     surname      => 'Doe',
63     categorycode => $categorycode,
64     branchcode   => $branchcode,
65     dateofbirth  => '',
66     dateexpiry   => '9999-12-31',
67     userid       => 'jane.doe'
68 );
69
70 my $borrowernumber1 = AddMember(%john_doe);
71 my $borrowernumber2 = AddMember(%jane_doe);
72
73 my $rating1 = AddRating( $biblionumber, $borrowernumber1, 3 );
74 my $rating2 = AddRating( $biblionumber, $borrowernumber2, 4 );
75 my $rating3 = ModRating( $biblionumber, $borrowernumber1, 5 );
76 my $rating4 = GetRating( $biblionumber, $borrowernumber2 );
77 my $rating5 = GetRating( $biblionumber );
78
79 ok( defined $rating1, 'add a rating' );
80 ok( defined $rating2, 'add another rating' );
81 ok( defined $rating3, 'update a rating' );
82 ok( defined $rating4, 'get a rating, with borrowernumber' );
83
84 ok( $rating3->{'rating_avg'} == '4',     "get a bib's average(float) rating" );
85 ok( $rating3->{'rating_avg_int'} == 4.5, "get a bib's average(int) rating" );
86 ok( $rating3->{'rating_total'} == 2, "get a bib's total number of ratings" );
87 ok( $rating3->{'rating_value'} == 5, "verify user's bib rating" );
88
89 my $rating_1   = GetRating( $biblionumber );
90 my $rating_1_1 = GetRating( $biblionumber, $borrowernumber1 );
91 is_deeply(
92     $rating_1,
93     {
94         rating_avg_int => 4.5,
95         rating_total   => 2,
96         rating_avg     => 4,
97         rating_value   => undef,
98     },
99     'GetRating should return total, avg_int and avg if  biblionumber is given'
100 );
101 is_deeply(
102     $rating_1_1,
103     {
104         rating_avg_int => 4.5,
105         rating_total   => 2,
106         rating_avg     => 4,
107         rating_value   => 5,
108     },
109 'GetRating should return total, avg_int, avg and value if biblionumber is given'
110 );
111
112 my $rating6 = DelRating( $biblionumber, $borrowernumber1 );
113 my $rating7 = DelRating( $biblionumber, $borrowernumber2 );
114
115 ok( defined $rating6, 'delete a rating' );
116 ok( defined $rating7, 'delete another rating' );
117
118 is( GetRating( $biblionumber, $borrowernumber1 ),
119     undef, 'GetRating should return undef if no rating exist' );
120
121 1;