Bug 26250: Fix tests when SearchEngine=Elastic
[koha.git] / t / db_dependent / Koha / Reviews.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 Koha Development team
4 #
5 # This file is part of Koha
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 8;
23
24 use Koha::Patrons;
25 use Koha::Reviews;
26 use Koha::Database;
27
28 use t::lib::TestBuilder;
29 use t::lib::Mocks;
30
31 my $schema = Koha::Database->new->schema;
32 $schema->storage->txn_begin;
33
34 my $builder = t::lib::TestBuilder->new;
35 my $patron_1 = $builder->build({ source => 'Borrower', value => { flags => undef } });
36 my $patron_2 = $builder->build({ source => 'Borrower' });
37 $patron_1 = Koha::Patrons->find( $patron_1->{borrowernumber} );
38 $patron_2 = Koha::Patrons->find( $patron_2->{borrowernumber} );
39 my $biblio_1 = $builder->build_sample_biblio;
40 my $biblio_2 = $builder->build_sample_biblio;
41 my $nb_of_reviews = Koha::Reviews->search->count;
42 my $nb_of_approved_reviews = Koha::Reviews->search({ approved => 1 })->count;
43 my $new_review_1_1 = Koha::Review->new({
44     borrowernumber => $patron_1->borrowernumber,
45     biblionumber => $biblio_1->biblionumber,
46     review => 'a kind review',
47 })->store;
48 my $new_review_1_2 = Koha::Review->new({
49     borrowernumber => $patron_1->borrowernumber,
50     biblionumber => $biblio_2->biblionumber,
51     review => 'anoter kind review',
52 })->store;
53 my $new_review_2_1 = Koha::Review->new({
54     borrowernumber => $patron_2->borrowernumber,
55     biblionumber => $biblio_1->biblionumber,
56     review => 'just anoter review',
57 })->store;
58
59 like( $new_review_1_1->reviewid, qr|^\d+$|, 'Adding a new review should have set the reviewid');
60 is( Koha::Reviews->search->count, $nb_of_reviews + 3, 'The 3 reviews should have been added' );
61
62 is( Koha::Reviews->search({approved => 1})->count, $nb_of_approved_reviews, 'There should not be new approved reviews' );
63 $new_review_1_1->approve;
64 is( Koha::Reviews->search({approved => 1})->count, $nb_of_approved_reviews + 1, 'There should be 1 new approved review' );
65 $new_review_1_1->unapprove;
66 is( Koha::Reviews->search({approved => 1})->count, $nb_of_approved_reviews, 'There should not be any new approved review anymore' );
67
68 my $retrieved_review_1_1 = Koha::Reviews->find( $new_review_1_1->reviewid );
69 is( $retrieved_review_1_1->review, $new_review_1_1->review, 'Find a review by id should return the correct review' );
70
71 subtest 'search_limited' => sub {
72     plan tests => 2;
73     C4::Context->_new_userenv('xxx');
74     my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
75     my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
76     Koha::Library::Group->new({ parent_id => $group_1->id,  branchcode => $patron_1->branchcode })->store();
77     Koha::Library::Group->new({ parent_id => $group_2->id,  branchcode => $patron_2->branchcode })->store();
78     t::lib::Mocks::mock_userenv( { patron => $patron_1 } );
79     is( Koha::Reviews->search->count, $nb_of_approved_reviews + 3, 'Koha::Reviews->search should return all reviews' );
80     is( Koha::Reviews->search_limited->count, $nb_of_approved_reviews + 2, 'Koha::Reviews->search_limited should return reviews depending on patron permissions' );
81 };
82
83 $retrieved_review_1_1->delete;
84 is( Koha::Reviews->search->count, $nb_of_reviews + 2, 'Delete should have deleted the review' );
85
86 $schema->storage->txn_rollback;