Bug 11247: Add a simple unit test for TransformHtmlToXml
[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
27 use_ok('C4::Ratings');
28
29 my $dbh = C4::Context->dbh;
30 $dbh->{RaiseError} = 1;
31 $dbh->{AutoCommit} = 0;
32
33 my ($biblionumber) = AddBiblio( MARC::Record->new, '' );
34
35 my @categories   = C4::Category->all;
36 my $categorycode = $categories[0]->categorycode;
37 my $branchcode   = 'CPL';
38
39 my %john_doe = (
40     cardnumber   => '123456',
41     firstname    => 'John',
42     surname      => 'Doe',
43     categorycode => $categorycode,
44     branchcode   => $branchcode,
45     dateofbirth  => '',
46     dateexpiry   => '9999-12-31',
47     userid       => 'john.doe'
48 );
49
50 my %jane_doe = (
51     cardnumber   => '345678',
52     firstname    => 'Jane',
53     surname      => 'Doe',
54     categorycode => $categorycode,
55     branchcode   => $branchcode,
56     dateofbirth  => '',
57     dateexpiry   => '9999-12-31',
58     userid       => 'jane.doe'
59 );
60
61 my $borrowernumber1 = AddMember(%john_doe);
62 my $borrowernumber2 = AddMember(%jane_doe);
63
64 my $rating1 = AddRating( $biblionumber, $borrowernumber1, 3 );
65 my $rating2 = AddRating( $biblionumber, $borrowernumber2, 4 );
66 my $rating3 = ModRating( $biblionumber, $borrowernumber1, 5 );
67 my $rating4 = GetRating( $biblionumber, $borrowernumber2 );
68 my $rating5 = GetRating( $biblionumber );
69
70 ok( defined $rating1, 'add a rating' );
71 ok( defined $rating2, 'add another rating' );
72 ok( defined $rating3, 'update a rating' );
73 ok( defined $rating4, 'get a rating, with borrowernumber' );
74
75 ok( $rating3->{'rating_avg'} == '4',     "get a bib's average(float) rating" );
76 ok( $rating3->{'rating_avg_int'} == 4.5, "get a bib's average(int) rating" );
77 ok( $rating3->{'rating_total'} == 2, "get a bib's total number of ratings" );
78 ok( $rating3->{'rating_value'} == 5, "verify user's bib rating" );
79
80 my $rating_1   = GetRating( $biblionumber );
81 my $rating_1_1 = GetRating( $biblionumber, $borrowernumber1 );
82 is_deeply(
83     $rating_1,
84     {
85         rating_avg_int => 4.5,
86         rating_total   => 2,
87         rating_avg     => 4,
88         rating_value   => undef,
89     },
90     'GetRating should return total, avg_int and avg if  biblionumber is given'
91 );
92 is_deeply(
93     $rating_1_1,
94     {
95         rating_avg_int => 4.5,
96         rating_total   => 2,
97         rating_avg     => 4,
98         rating_value   => 5,
99     },
100 'GetRating should return total, avg_int, avg and value if biblionumber is given'
101 );
102
103 my $rating6 = DelRating( $biblionumber, $borrowernumber1 );
104 my $rating7 = DelRating( $biblionumber, $borrowernumber2 );
105
106 ok( defined $rating6, 'delete a rating' );
107 ok( defined $rating7, 'delete another rating' );
108
109 is( GetRating( $biblionumber, $borrowernumber1 ),
110     undef, 'GetRating should return undef if no rating exist' );
111
112 1;