Bug 9302: (QA follow-up) Consistency follow-up
[koha.git] / t / db_dependent / Koha / Reviews.t
blob8688789f272c552ede4fdd27326424a1659a59bd
1 #!/usr/bin/perl
3 # Copyright 2015 Koha Development team
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use Test::More tests => 8;
24 use Koha::Patrons;
25 use Koha::Reviews;
26 use Koha::Database;
28 use t::lib::TestBuilder;
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
33 my $builder = t::lib::TestBuilder->new;
34 my $patron_1 = $builder->build({ source => 'Borrower', value => { flags => undef } });
35 my $patron_2 = $builder->build({ source => 'Borrower' });
36 $patron_1 = Koha::Patrons->find( $patron_1->{borrowernumber} );
37 $patron_2 = Koha::Patrons->find( $patron_2->{borrowernumber} );
38 my $biblio_1 = $builder->build({ source => 'Biblio' });
39 my $biblio_2 = $builder->build({ source => 'Biblio' });
40 my $nb_of_reviews = Koha::Reviews->search->count;
41 my $nb_of_approved_reviews = Koha::Reviews->search({ approved => 1 })->count;
42 my $new_review_1_1 = Koha::Review->new({
43 borrowernumber => $patron_1->borrowernumber,
44 biblionumber => $biblio_1->{biblionumber},
45 review => 'a kind review',
46 })->store;
47 my $new_review_1_2 = Koha::Review->new({
48 borrowernumber => $patron_1->borrowernumber,
49 biblionumber => $biblio_2->{biblionumber},
50 review => 'anoter kind review',
51 })->store;
52 my $new_review_2_1 = Koha::Review->new({
53 borrowernumber => $patron_2->borrowernumber,
54 biblionumber => $biblio_1->{biblionumber},
55 review => 'just anoter review',
56 })->store;
58 like( $new_review_1_1->reviewid, qr|^\d+$|, 'Adding a new review should have set the reviewid');
59 is( Koha::Reviews->search->count, $nb_of_reviews + 3, 'The 3 reviews should have been added' );
61 is( Koha::Reviews->search({approved => 1})->count, $nb_of_approved_reviews, 'There should not be new approved reviews' );
62 $new_review_1_1->approve;
63 is( Koha::Reviews->search({approved => 1})->count, $nb_of_approved_reviews + 1, 'There should be 1 new approved review' );
64 $new_review_1_1->unapprove;
65 is( Koha::Reviews->search({approved => 1})->count, $nb_of_approved_reviews, 'There should not be any new approved review anymore' );
67 my $retrieved_review_1_1 = Koha::Reviews->find( $new_review_1_1->reviewid );
68 is( $retrieved_review_1_1->review, $new_review_1_1->review, 'Find a review by id should return the correct review' );
70 subtest 'search_limited' => sub {
71 plan tests => 2;
72 C4::Context->_new_userenv('xxx');
73 my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
74 my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
75 Koha::Library::Group->new({ parent_id => $group_1->id, branchcode => $patron_1->branchcode })->store();
76 Koha::Library::Group->new({ parent_id => $group_2->id, branchcode => $patron_2->branchcode })->store();
77 set_logged_in_user( $patron_1 );
78 is( Koha::Reviews->search->count, $nb_of_approved_reviews + 3, 'Koha::Reviews->search should return all reviews' );
79 is( Koha::Reviews->search_limited->count, $nb_of_approved_reviews + 2, 'Koha::Reviews->search_limited should return reviews depending on patron permissions' );
82 $retrieved_review_1_1->delete;
83 is( Koha::Reviews->search->count, $nb_of_reviews + 2, 'Delete should have deleted the review' );
85 $schema->storage->txn_rollback;
87 sub set_logged_in_user {
88 my ($patron) = @_;
89 C4::Context->set_userenv(
90 $patron->borrowernumber, $patron->userid,
91 $patron->cardnumber, 'firstname',
92 'surname', $patron->library->branchcode,
93 'Midway Public Library', $patron->flags,
94 '', ''