Bug 20287: Use DBIC transaction instead of AutoCommit=0
[koha.git] / t / db_dependent / Utils / Datatables_Virtualshelves.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 => 13;
21
22 use C4::Biblio;
23 use C4::Context;
24
25 use Koha::Library;
26 use Koha::Patrons;
27 use Koha::Patron::Categories;
28 use Koha::Virtualshelves;
29
30 use_ok( "C4::Utils::DataTables::VirtualShelves" );
31
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
34 my $dbh = C4::Context->dbh;
35
36 $dbh->do(q|DELETE FROM virtualshelves|);
37
38 # Pick a categorycode from the DB
39 my @categories   = Koha::Patron::Categories->search_limited;
40 my $categorycode = $categories[0]->categorycode;
41 my $branchcode   = "ABC";
42 my $branch_data = {
43     branchcode     => $branchcode,
44     branchname     => 'my branchname',
45 };
46 Koha::Library->new( $branch_data )->store;
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   => '234567',
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 my %john_smith = (
70     cardnumber   => '345678',
71     firstname    =>  'John',
72     surname      => 'Smith',
73     categorycode => $categorycode,
74     branchcode   => $branchcode,
75     dateofbirth  => '',
76     dateexpiry   => '9999-12-31',
77     userid       => 'john.smith',
78 );
79
80 $john_doe{borrowernumber} = Koha::Patron->new( \%john_doe )->store->borrowernumber;
81 $jane_doe{borrowernumber} = Koha::Patron->new( \%jane_doe )->store->borrowernumber;
82 $john_smith{borrowernumber} = Koha::Patron->new( \%john_smith )->store->borrowernumber;
83
84 my $shelf1 = Koha::Virtualshelf->new(
85     {
86         shelfname => 'my first private list (empty)',
87         category  => 1, # private
88         sortfield => 'author',
89         owner     => $john_doe{borrowernumber},
90     }
91 )->store;
92
93 my $shelf2 = Koha::Virtualshelf->new(
94     {
95         shelfname => 'my second private list',
96         category  => 1, # private
97         sortfield => 'title',
98         owner     => $john_doe{borrowernumber},
99     }
100 )->store;
101 my $biblionumber1 = _add_biblio('title 1');
102 my $biblionumber2 = _add_biblio('title 2');
103 my $biblionumber3 = _add_biblio('title 3');
104 my $biblionumber4 = _add_biblio('title 4');
105 my $biblionumber5 = _add_biblio('title 5');
106 $shelf2->add_biblio( $biblionumber1, $john_doe{borrowernumber} );
107 $shelf2->add_biblio( $biblionumber2, $john_doe{borrowernumber} );
108 $shelf2->add_biblio( $biblionumber3, $john_doe{borrowernumber} );
109 $shelf2->add_biblio( $biblionumber4, $john_doe{borrowernumber} );
110 $shelf2->add_biblio( $biblionumber5, $john_doe{borrowernumber} );
111
112 my $shelf3 = Koha::Virtualshelf->new(
113     {
114         shelfname => 'The first public list',
115         category  => 2, # public
116         sortfield => 'author',
117         owner     => $jane_doe{borrowernumber},
118     }
119 )->store;
120 my $biblionumber6 = _add_biblio('title 6');
121 my $biblionumber7 = _add_biblio('title 7');
122 my $biblionumber8 = _add_biblio('title 8');
123 $shelf3->add_biblio( $biblionumber6, $jane_doe{borrowernumber} );
124 $shelf3->add_biblio( $biblionumber7, $jane_doe{borrowernumber} );
125 $shelf3->add_biblio( $biblionumber8, $jane_doe{borrowernumber} );
126
127 my $shelf4 = Koha::Virtualshelf->new(
128     {
129         shelfname => 'my second public list',
130         category  => 2, # public
131         sortfield => 'title',
132         owner     => $jane_doe{borrowernumber},
133     }
134 )->store;
135 my $biblionumber9  = _add_biblio('title 9');
136 my $biblionumber10 = _add_biblio('title 10');
137 my $biblionumber11 = _add_biblio('title 11');
138 my $biblionumber12 = _add_biblio('title 12');
139 $shelf3->add_biblio( $biblionumber9, $jane_doe{borrowernumber} );
140 $shelf3->add_biblio( $biblionumber10, $jane_doe{borrowernumber} );
141 $shelf3->add_biblio( $biblionumber11, $jane_doe{borrowernumber} );
142 $shelf3->add_biblio( $biblionumber12, $jane_doe{borrowernumber} );
143
144 my $shelf5 = Koha::Virtualshelf->new(
145     {
146         shelfname => 'my third private list',
147         category  => 1, # private
148         sortfield => 'title',
149         owner     => $jane_doe{borrowernumber},
150     }
151 )->store;
152 my $biblionumber13 = _add_biblio('title 13');
153 my $biblionumber14 = _add_biblio('title 14');
154 my $biblionumber15 = _add_biblio('title 15');
155 my $biblionumber16 = _add_biblio('title 16');
156 my $biblionumber17 = _add_biblio('title 17');
157 my $biblionumber18 = _add_biblio('title 18');
158 $shelf5->add_biblio( $biblionumber13, $jane_doe{borrowernumber} );
159 $shelf5->add_biblio( $biblionumber14, $jane_doe{borrowernumber} );
160 $shelf5->add_biblio( $biblionumber15, $jane_doe{borrowernumber} );
161 $shelf5->add_biblio( $biblionumber16, $jane_doe{borrowernumber} );
162 $shelf5->add_biblio( $biblionumber17, $jane_doe{borrowernumber} );
163 $shelf5->add_biblio( $biblionumber18, $jane_doe{borrowernumber} );
164
165 for my $i ( 6 .. 15 ) {
166     Koha::Virtualshelf->new(
167         {
168             shelfname => "another public list $i",
169             category  => 2,
170             owner     => $john_smith{borrowernumber},
171         }
172     )->store;
173 }
174
175 # Set common datatables params
176 my %dt_params = (
177     iDisplayLength   => 10,
178     iDisplayStart    => 0
179 );
180 my $search_results;
181
182 C4::Context->_new_userenv ('DUMMY_SESSION_ID');
183 C4::Context->set_userenv($john_doe{borrowernumber}, $john_doe{userid}, 'usercnum', 'First name', 'Surname', 'MYLIBRARY', 'My Library', 0);
184
185 # Search private lists by title
186 $search_results = C4::Utils::DataTables::VirtualShelves::search({
187     shelfname => "ist",
188     dt_params => \%dt_params,
189     type => 1,
190 });
191
192 is( $search_results->{ iTotalRecords }, 2,
193     "There should be 2 private shelves in total" );
194
195 is( $search_results->{ iTotalDisplayRecords }, 2,
196     "There should be 2 private shelves with title like '%ist%" );
197
198 is( @{ $search_results->{ shelves } }, 2,
199     "There should be 2 private shelves returned" );
200
201 # Search by type only
202 $search_results = C4::Utils::DataTables::VirtualShelves::search({
203     dt_params => \%dt_params,
204     type => 2,
205 });
206 is( $search_results->{ iTotalRecords }, 12,
207     "There should be 12 public shelves in total" );
208
209 is( $search_results->{ iTotalDisplayRecords }, 12,
210     "There should be 12 private shelves" );
211
212 is( @{ $search_results->{ shelves } }, 10,
213     "There should be 10 public shelves returned" );
214
215 # Search by owner
216 $search_results = C4::Utils::DataTables::VirtualShelves::search({
217     owner => "jane",
218     dt_params => \%dt_params,
219     type => 2,
220 });
221 is( $search_results->{ iTotalRecords }, 12,
222     "There should be 12 public shelves in total" );
223
224 is( $search_results->{ iTotalDisplayRecords }, 2,
225     "There should be 1 public shelves for jane" );
226
227 is( @{ $search_results->{ shelves } }, 2,
228     "There should be 1 public shelf returned" );
229
230 # Search by owner and shelf name
231 $search_results = C4::Utils::DataTables::VirtualShelves::search({
232     owner => "smith",
233     shelfname => "public list 1",
234     dt_params => \%dt_params,
235     type => 2,
236 });
237 is( $search_results->{ iTotalRecords }, 12,
238     "There should be 12 public shelves in total" );
239
240 is( $search_results->{ iTotalDisplayRecords }, 6,
241     "There should be 6 public shelves for john with name like %public list 1%" );
242
243 is( @{ $search_results->{ shelves } }, 6,
244     "There should be 6 public chalves returned" );
245
246 sub _add_biblio {
247     my ( $title ) = @_;
248     my $biblio = MARC::Record->new();
249     $biblio->append_fields(
250         MARC::Field->new('245', ' ', ' ', a => $title),
251     );
252     my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
253     return $biblionumber;
254 }
255