5 use DateTime::Duration;
13 use Test::More tests => 25;
16 use_ok('C4::Circulation');
36 my $dbh = C4::Context->dbh;
37 $dbh->{RaiseError} = 1;
38 $dbh->{AutoCommit} = 0;
40 $dbh->do(q|DELETE FROM issues|);
41 $dbh->do(q|DELETE FROM items|);
42 $dbh->do(q|DELETE FROM borrowers|);
43 $dbh->do(q|DELETE FROM branches|);
44 $dbh->do(q|DELETE FROM categories|);
45 $dbh->do(q|DELETE FROM accountlines|);
46 $dbh->do(q|DELETE FROM issuingrules|);
52 my $dt_today = dt_from_string;
53 my $today = output_pref({ dt => $dt_today, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
55 my $dt_today2 = dt_from_string;
56 my $dur10 = DateTime::Duration->new( days => -10 );
57 $dt_today2->add_duration($dur10);
58 my $daysago10 = output_pref({ dt => $dt_today2, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
60 #Add branch and category
64 branchname => 'Sample Branch',
65 branchaddress1 => 'sample adr1',
66 branchaddress2 => 'sample adr2',
67 branchaddress3 => 'sample adr3',
68 branchzip => 'sample zip',
69 branchcity => 'sample city',
70 branchstate => 'sample state',
71 branchcountry => 'sample country',
72 branchphone => 'sample phone',
73 branchfax => 'sample fax',
74 branchemail => 'sample email',
75 branchurl => 'sample url',
76 branchip => 'sample ip',
77 branchprinter => undef,
78 opac_info => 'sample opac',
83 branchname => 'Sample Branch2',
84 branchaddress1 => 'sample adr1_2',
85 branchaddress2 => 'sample adr2_2',
86 branchaddress3 => 'sample adr3_2',
87 branchzip => 'sample zip2',
88 branchcity => 'sample city2',
89 branchstate => 'sample state2',
90 branchcountry => 'sample country2',
91 branchphone => 'sample phone2',
92 branchfax => 'sample fax2',
93 branchemail => 'sample email2',
94 branchurl => 'sample url2',
95 branchip => 'sample ip2',
96 branchprinter => undef,
97 opac_info => 'sample opac2',
99 ModBranch($samplebranch1);
100 ModBranch($samplebranch2);
103 categorycode => 'CAT1',
104 description => 'Description1',
105 enrolmentperiod => 'Null',
106 enrolmentperioddate => 'Null',
107 dateofbirthrequired => 'Null',
110 enrolmentfee => 'Null',
111 overduenoticerequired => 'Null',
112 issuelimit => 'Null',
113 reservefee => 'Null',
115 category_type => 'Null'
118 "INSERT INTO categories (categorycode,description,enrolmentperiod,enrolmentperioddate,dateofbirthrequired ,finetype,bulk,enrolmentfee,overduenoticerequired,issuelimit ,reservefee ,hidelostitems ,category_type) VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?)";
121 $samplecat->{categorycode}, $samplecat->{description},
122 $samplecat->{enrolmentperiod}, $samplecat->{enrolmentperioddate},
123 $samplecat->{dateofbirthrequired}, $samplecat->{finetype},
124 $samplecat->{bulk}, $samplecat->{enrolmentfee},
125 $samplecat->{overduenoticerequired}, $samplecat->{issuelimit},
126 $samplecat->{reservefee}, $samplecat->{hidelostitems},
127 $samplecat->{category_type}
131 my $record = MARC::Record->new();
132 $record->append_fields(
133 MARC::Field->new( '952', '0', '0', a => $samplebranch1->{branchcode} ) );
134 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
136 my @sampleitem1 = C4::Items::AddItem(
138 barcode => 'barcode_1',
139 itemcallnumber => 'callnumber1',
140 homebranch => $samplebranch1->{branchcode},
141 holdingbranch => $samplebranch1->{branchcode},
147 my $item_id1 = $sampleitem1[2];
148 my @sampleitem2 = C4::Items::AddItem(
150 barcode => 'barcode_2',
151 itemcallnumber => 'callnumber2',
152 homebranch => $samplebranch2->{branchcode},
153 holdingbranch => $samplebranch2->{branchcode},
159 my $item_id2 = $sampleitem2[2];
162 my $borrower_id1 = C4::Members::AddMember(
163 firstname => 'firstname1',
164 surname => 'surname1 ',
165 categorycode => $samplecat->{categorycode},
166 branchcode => $samplebranch1->{branchcode},
168 my $borrower_1 = C4::Members::GetMember(borrowernumber => $borrower_id1);
169 my $borrower_id2 = C4::Members::AddMember(
170 firstname => 'firstname2',
171 surname => 'surname2 ',
172 categorycode => $samplecat->{categorycode},
173 branchcode => $samplebranch2->{branchcode},
175 my $borrower_2 = C4::Members::GetMember(borrowernumber => $borrower_id2);
177 # NEED TO BE FIXED !!!
178 # The first parameter for set_userenv is the class ref
179 #my @USERENV = ( $borrower_id1, 'test', 'MASTERTEST', 'firstname', 'username', 'CPL', 'CPL', 'email@example.org' );
180 my @USERENV = ( $borrower_id1, 'test', 'MASTERTEST', 'firstname', 'CPL', 'CPL', 'email@example.org' );
182 C4::Context->_new_userenv('DUMMY_SESSION_ID');
183 C4::Context->set_userenv(@USERENV);
185 my $userenv = C4::Context->userenv
186 or BAIL_OUT("No userenv");
191 $query = " SELECT count(*) FROM issues";
192 my $sth = $dbh->prepare($query);
194 my $countissue = $sth -> fetchrow_array;
195 is ($countissue ,0, "there is no issue");
196 my $datedue1 = C4::Circulation::AddIssue( $borrower_1, 'barcode_1', $daysago10,0, $today, '' );
199 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
200 "AddRenewal returns a date"
202 my $issue_id1 = $dbh->last_insert_id( undef, undef, 'issues', undef );
204 my $datedue2 = C4::Circulation::AddIssue( $borrower_1, 'nonexistent_barcode' );
205 is( $datedue2, undef, "AddIssue returns undef if no datedue is specified" );
206 my $issue_id2 = $dbh->last_insert_id( undef, undef, 'issues', undef );
209 $countissue = $sth -> fetchrow_array;
210 is ($countissue,1,"1 issues have been added");
212 #Test AddIssuingCharge
213 $query = " SELECT count(*) FROM accountlines";
214 $sth = $dbh->prepare($query);
216 my $countaccount = $sth -> fetchrow_array;
217 is ($countaccount,0,"0 accountline exists");
218 is( C4::Circulation::AddIssuingCharge( $item_id1, $borrower_id1, 10 ),
219 1, "An issuing charge has been added" );
220 my $account_id = $dbh->last_insert_id( undef, undef, 'accountlines', undef );
222 $countaccount = $sth -> fetchrow_array;
223 is ($countaccount,1,"1 accountline has been added");
227 AddRenewal( $borrower_id1, $item_id1, $samplebranch1->{branchcode},
228 $datedue1, $daysago10 );
231 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
232 "AddRenewal returns a date"
235 #Test GetBiblioIssues
236 is( GetBiblioIssues(), undef, "GetBiblio Issues without parameters" );
239 #FIXME : As the issues are not correctly added in the database, these tests don't work correctly
240 is(GetItemIssue,undef,"Without parameter GetItemIssue returns undef");
241 #is(GetItemIssue($item_id1),{},"Item1's issues");
244 #FIXME: this routine currently doesn't work be
245 #is_deeply (GetItemIssues,{},"Without parameter, GetItemIssue returns all the issues");
248 is( GetOpenIssue(), undef, "Without parameter GetOpenIssue returns undef" );
249 is( GetOpenIssue(-1), undef,
250 "With wrong parameter GetOpenIssue returns undef" );
251 my $openissue = GetOpenIssue($borrower_id1, $item_id1);
255 $datedue2 = C4::Circulation::AddIssue( $borrower_1, 'barcode_1' );
256 isnt( $datedue2, undef, "AddIssue does not return undef if datedue is specified" );
257 #Without anything in DB
258 @renewcount = C4::Circulation::GetRenewCount();
261 [ 0, undef, 0 ], # FIXME Need to be fixed
262 "Without issuing rules and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
264 @renewcount = C4::Circulation::GetRenewCount(-1);
267 [ 0, undef, 0 ], # FIXME Need to be fixed
268 "Without issuing rules and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
270 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
274 "Without issuing rules and with a valid parameter, renewcount = 2, renewsallowed = undef, renewsleft = 0"
277 #With something in DB
278 # Add a default rule: No renewal allowed
280 INSERT INTO issuingrules( categorycode, itemtype, branchcode, issuelength, renewalsallowed )
281 VALUES ( '*', '*', '*', 10, 0 )
283 @renewcount = C4::Circulation::GetRenewCount();
287 "With issuing rules (renewal disallowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
289 @renewcount = C4::Circulation::GetRenewCount(-1);
293 "With issuing rules (renewal disallowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
295 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
299 "With issuing rules (renewal disallowed) and with a valid parameter, Getrenewcount returns renewcount = 2, renewsallowed = 0, renewsleft = 0"
302 # Add a default rule: renewal is allowed
304 UPDATE issuingrules SET renewalsallowed = 3
306 @renewcount = C4::Circulation::GetRenewCount();
310 "With issuing rules (renewal allowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
312 @renewcount = C4::Circulation::GetRenewCount(-1);
316 "With issuing rules (renewal allowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
318 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
322 "With issuing rules (renewal allowed) and with a valid parameter, Getrenewcount of item1 returns 3 renews left"
325 AddRenewal( $borrower_id1, $item_id1, $samplebranch1->{branchcode},
326 $datedue3, $daysago10 );
327 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
331 "With issuing rules (renewal allowed, 1 remaining) and with a valid parameter, Getrenewcount of item1 returns 0 renews left"