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