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