1 # Copyright 2015 BibLibre
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Test::More tests => 545;
19 use t::lib::Mocks qw(mock_preference);
20 use POSIX qw(strftime);
23 use_ok('C4::UsageStats');
24 use_ok('C4::Context');
26 use_ok( 'C4::AuthoritiesMarc', qw(AddAuthority) );
27 use_ok('C4::Reserves');
28 use_ok('MARC::Record');
29 use_ok('Koha::Acquisition::Order');
40 my $dbh = C4::Context->dbh;
41 $dbh->{AutoCommit} = 0;
42 $dbh->{RaiseError} = 1;
44 $dbh->do('DELETE FROM issues');
45 $dbh->do('DELETE FROM biblio');
46 $dbh->do('DELETE FROM items');
47 $dbh->do('DELETE FROM auth_header');
48 $dbh->do('DELETE FROM old_issues');
49 $dbh->do('DELETE FROM old_reserves');
50 $dbh->do('DELETE FROM borrowers');
51 $dbh->do('DELETE FROM aqorders');
52 $dbh->do('DELETE FROM subscription');
54 #################################################
56 #################################################
58 # ---------- Testing NeedUpdate -----------------
60 #Mocking C4::Context->preference("UsageStatsLastUpdateTime") to 0
61 my $now = strftime( "%s", localtime );
62 t::lib::Mocks::mock_preference( "UsageStatsLastUpdateTime", 0 );
64 my $update = C4::UsageStats->NeedUpdate;
65 is( $update, 1, "There is no last update, update needed" );
67 #Mocking C4::Context->preference("UsageStatsLastUpdateTime") to now
68 $now = strftime( "%s", localtime );
69 t::lib::Mocks::mock_preference( "UsageStatsLastUpdateTime", $now );
71 $update = C4::UsageStats->NeedUpdate;
72 is( $update, 0, "Last update just be done, no update needed " );
74 # ---------- Testing BuildReport ----------------
76 #Test report->library -----------------
78 t::lib::Mocks::mock_preference( "UsageStatsID", 0 );
79 t::lib::Mocks::mock_preference( "UsageStatsLibraryName", 0 );
80 t::lib::Mocks::mock_preference( "UsageStatsLibraryUrl", 0 );
81 t::lib::Mocks::mock_preference( "UsageStatsLibraryType", 0 );
82 t::lib::Mocks::mock_preference( "UsageStatsCountry", 0 );
84 my $report = C4::UsageStats->BuildReport();
86 isa_ok( $report, 'HASH', '$report is a HASH' );
87 isa_ok( $report->{library}, 'HASH', '$report->{library} is a HASH' );
88 is( scalar( keys %{$report->{library}} ), 5, "There are 5 fields in $report->{library}" );
89 is( $report->{library}->{id}, 0, "UsageStatsID is good" );
90 is( $report->{library}->{name}, '', "UsageStatsLibraryName is good" );
91 is( $report->{library}->{url}, '', "UsageStatsLibraryUrl is good" );
92 is( $report->{library}->{type}, '', "UsageStatsLibraryType is good" );
93 is( $report->{library}->{country}, '', "UsageStatsCountry is good" );
96 t::lib::Mocks::mock_preference( "UsageStatsID", 1 );
97 t::lib::Mocks::mock_preference( "UsageStatsLibraryName", 'NAME' );
98 t::lib::Mocks::mock_preference( "UsageStatsLibraryUrl", 'URL' );
99 t::lib::Mocks::mock_preference( "UsageStatsLibraryType", 'TYPE' );
100 t::lib::Mocks::mock_preference( "UsageStatsCountry", 'COUNTRY' );
102 $report = C4::UsageStats->BuildReport();
104 isa_ok( $report, 'HASH', '$report is a HASH' );
105 isa_ok( $report->{library}, 'HASH', '$report->{library} is a HASH' );
106 is( scalar( keys %{$report->{library}} ), 5, "There are 5 fields in $report->{library}" );
107 is( $report->{library}->{id}, 1, "UsageStatsID is good" );
108 is( $report->{library}->{name}, 'NAME', "UsageStatsLibraryName is good" );
109 is( $report->{library}->{url}, 'URL', "UsageStatsLibraryUrl is good" );
110 is( $report->{library}->{type}, 'TYPE', "UsageStatsLibraryType is good" );
111 is( $report->{library}->{country}, 'COUNTRY', "UsageStatsCountry is good" );
113 #Test report->volumetry ---------------
114 #with original values
115 $report = C4::UsageStats->BuildReport();
117 isa_ok( $report, 'HASH', '$report is a HASH' );
118 isa_ok( $report->{volumetry}, 'HASH', '$report->{volumetry} is a HASH' );
119 is( scalar( keys %{$report->{volumetry}} ), 8, "There are 8 fields in $report->{volumetry}" );
120 is( $report->{volumetry}->{biblio}, 0, "There is no biblio" );
121 is( $report->{volumetry}->{items}, 0, "There is no items" );
122 is( $report->{volumetry}->{auth_header}, 0, "There is no auth_header" );
123 is( $report->{volumetry}->{old_issues}, 0, "There is no old_issues" );
124 is( $report->{volumetry}->{old_reserves}, 0, "There is no old_reserves" );
125 is( $report->{volumetry}->{borrowers}, 0, "There is no borrowers" );
126 is( $report->{volumetry}->{aqorders}, 0, "There is no aqorders" );
127 is( $report->{volumetry}->{subscription}, 0, "There is no subscription" );
129 #after adding objects
130 construct_objects_needed();
132 $report = C4::UsageStats->BuildReport();
134 isa_ok( $report, 'HASH', '$report is a HASH' );
135 isa_ok( $report->{volumetry}, 'HASH', '$report->{volumetry} is a HASH' );
136 is( scalar( keys %{$report->{volumetry}} ), 8, "There are 8 fields in $report->{volumetry}" );
137 is( $report->{volumetry}->{biblio}, 3, "There are 3 biblio" );
138 is( $report->{volumetry}->{items}, 3, "There are 3 items" );
139 is( $report->{volumetry}->{auth_header}, 2, "There are 2 auth_header" );
140 is( $report->{volumetry}->{old_issues}, 1, "There is 1 old_issues" );
141 is( $report->{volumetry}->{old_reserves}, 1, "There is 1 old_reserves" );
142 is( $report->{volumetry}->{borrowers}, 3, "There are 3 borrowers" );
143 is( $report->{volumetry}->{aqorders}, 1, "There is 1 aqorders" );
144 is( $report->{volumetry}->{subscription}, 1, "There is 1 subscription" );
146 #Test report->systempreferences -------
148 mocking_systempreferences_to_a_set_value(0);
150 $report = C4::UsageStats->BuildReport();
151 isa_ok( $report, 'HASH', '$report is a HASH' );
152 isa_ok( $report->{systempreferences}, 'HASH', '$report->{systempreferences} is a HASH' );
153 verif_systempreferences_values( $report, 0 );
156 mocking_systempreferences_to_a_set_value(1);
158 $report = C4::UsageStats->BuildReport();
159 isa_ok( $report, 'HASH', '$report is a HASH' );
160 isa_ok( $report->{systempreferences}, 'HASH', '$report->{systempreferences} is a HASH' );
161 verif_systempreferences_values( $report, 1 );
163 #Test if unwanted syspref are not sent
164 is( $report->{systempreferences}->{useDischarge}, undef, 'useDischarge should not be shared');
165 is( $report->{systempreferences}->{OpacUserJS}, undef, 'OpacUserJS should not be shared');
167 # ---------- Testing ReportToCommunity ----------
169 # ---------- Testing _count ---------------------
174 my $count = $dbh->selectrow_array($query);
176 my $nb_fields = C4::UsageStats::_count('borrowers');
177 is( $nb_fields, $count, "_count return the good number of fields" );
179 #################################################
181 #################################################
193 sub construct_objects_needed {
195 # ---------- 3 borrowers ---------------------
196 my $surname1 = 'Borrower 1';
197 my $surname2 = 'Borrower 2';
198 my $surname3 = 'Borrower 3';
199 my $firstname1 = 'firstname 1';
200 my $firstname2 = 'firstname 2';
201 my $firstname3 = 'firstname 3';
202 my $cardnumber1 = 'test_card1';
203 my $cardnumber2 = 'test_card2';
204 my $cardnumber3 = 'test_card3';
205 my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode();
206 my $branchcode = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode();
209 INSERT INTO borrowers
210 (surname, firstname, cardnumber, branchcode, categorycode)
212 my $insert_sth = $dbh->prepare($query);
213 $insert_sth->execute( $surname1, $firstname1, $cardnumber1, $branchcode, $categorycode );
214 my $borrowernumber1 = $dbh->last_insert_id( undef, undef, 'borrowers', undef );
215 $insert_sth->execute( $surname2, $firstname2, $cardnumber2, $branchcode, $categorycode );
216 my $borrowernumber2 = $dbh->last_insert_id( undef, undef, 'borrowers', undef );
217 $insert_sth->execute( $surname3, $firstname3, $cardnumber3, $branchcode, $categorycode );
218 my $borrowernumber3 = $dbh->last_insert_id( undef, undef, 'borrowers', undef );
220 # ---------- 3 biblios -----------------------
221 my $title1 = 'Title 1';
222 my $title2 = 'Title 2';
223 my $title3 = 'Title 3';
224 my $author1 = 'Author 1';
225 my $author2 = 'Author 2';
226 my $author3 = 'Author 3';
232 $insert_sth = $dbh->prepare($query);
233 $insert_sth->execute( $title1, $author1 );
234 my $biblionumber1 = $dbh->last_insert_id( undef, undef, 'biblio', undef );
235 $insert_sth->execute( $title2, undef );
236 my $biblionumber2 = $dbh->last_insert_id( undef, undef, 'biblio', undef );
237 $insert_sth->execute( $title3, $author3 );
238 my $biblionumber3 = $dbh->last_insert_id( undef, undef, 'biblio', undef );
240 # ---------- 3 biblio items -------------------------
242 INSERT INTO biblioitems
243 (biblionumber, itemtype, marcxml)
245 $insert_sth = $dbh->prepare($query);
246 $insert_sth->execute( $biblionumber1, 'Book', '' );
247 my $biblioitemnumber1 = $dbh->last_insert_id( undef, undef, 'biblioitems', undef );
248 $insert_sth->execute( $biblionumber2, 'Music', '' );
249 my $biblioitemnumber2 = $dbh->last_insert_id( undef, undef, 'biblioitems', undef );
250 $insert_sth->execute( $biblionumber3, 'Book', '' );
251 my $biblioitemnumber3 = $dbh->last_insert_id( undef, undef, 'biblioitems', undef );
253 # ---------- 3 items -------------------------
254 my $barcode1 = '111111';
255 my $barcode2 = '222222';
256 my $barcode3 = '333333';
260 (biblionumber, biblioitemnumber, barcode, itype)
262 $insert_sth = $dbh->prepare($query);
263 $insert_sth->execute( $biblionumber1, $biblioitemnumber1, $barcode1, 'Book' );
264 my $item_number1 = $dbh->last_insert_id( undef, undef, 'items', undef );
265 $insert_sth->execute( $biblionumber2, $biblioitemnumber2, $barcode2, 'Music' );
266 my $item_number2 = $dbh->last_insert_id( undef, undef, 'items', undef );
267 $insert_sth->execute( $biblionumber3, $biblioitemnumber3, $barcode3, 'Book' );
268 my $item_number3 = $dbh->last_insert_id( undef, undef, 'items', undef );
270 # ---------- Add 2 auth_header
272 INSERT INTO auth_header
275 $insert_sth = $dbh->prepare($query);
276 $insert_sth->execute('authtypecode1');
277 my $authid1 = $dbh->last_insert_id( undef, undef, 'auth_header', undef );
278 $insert_sth->execute('authtypecode2');
279 my $authid2 = $dbh->last_insert_id( undef, undef, 'auth_header', undef );
281 # ---------- Add 1 old_issues
283 INSERT INTO old_issues
284 (borrowernumber, branchcode, itemnumber)
286 $insert_sth = $dbh->prepare($query);
287 $insert_sth->execute( $borrowernumber1, $branchcode, $item_number1 );
288 my $issue_id1 = $dbh->last_insert_id( undef, undef, 'old_issues', undef );
290 # ---------- Add 1 old_reserves
291 AddReserve( $branchcode, $borrowernumber1, $biblionumber1, '', 1, undef, undef, '', 'Title', undef, undef );
292 my $reserves1 = GetReservesFromBiblionumber( { biblionumber => $biblionumber1 } );
293 my $reserve_id1 = $reserves1->[0]->{reserve_id};
294 my $reserve1 = CancelReserve( { reserve_id => $reserve_id1 } );
296 # ---------- Add 1 aqbudgets
298 INSERT INTO aqbudgets
301 $insert_sth = $dbh->prepare($query);
302 $insert_sth->execute("20.0");
303 my $aqbudgets1 = $dbh->last_insert_id( undef, undef, 'aqbudgets', undef );
305 # ---------- Add 1 aqorders
308 (budget_id, basketno, biblionumber, invoiceid, subscriptionid)
310 $insert_sth = $dbh->prepare($query);
311 $insert_sth->execute( $aqbudgets1, undef, undef, undef, undef );
312 my $aqorders1 = $dbh->last_insert_id( undef, undef, 'aqorders', undef );
314 # --------- Add 1 subscription
316 INSERT INTO subscription
319 $insert_sth = $dbh->prepare($query);
320 $insert_sth->execute($biblionumber1);
321 my $subscription1 = $dbh->last_insert_id( undef, undef, 'subscription', undef );
325 #Change systempreferences values to $set_value
326 sub mocking_systempreferences_to_a_set_value {
327 my $set_value = shift;
332 AcqWarnOnDuplicateInvoice
348 AutoCreateAuthorities
349 BiblioAddsAuthorities
351 UseAuthoritiesForTracings
354 IntranetBiblioDefaultView
360 DefaultClassificationSource
361 EasyAnalyticalRecords
368 SpineLabelShowPrintOnBibDetails
369 BlockReturnOfWithdrawnItems
370 CalculateFinesOnReturn
371 AgeRestrictionOverride
374 AllowItemsOnHoldCheckout
375 AllowNotForLoanOverride
376 AllowRenewalLimitOverride
380 AutoRemoveOverduesRestrictions
383 HomeOrHoldingBranchReturn
384 InProcessingToShelvingCart
387 ManInvInNoissuesCharge
391 RentalsInNoissuesCharge
394 TransfersMaxDaysWarning
395 UseBranchTransferLimits
397 UseTransportCostMatrix
400 FinesIncludeGracePeriod
402 RefundLostItemFeeOnReturn
403 WhenLostChargeReplacementFee
405 AllowHoldDateInFuture
406 AllowHoldPolicyOverride
407 AllowHoldsOnDamagedItems
408 AllowHoldsOnPatronsPossessions
409 AutoResumeSuspendedHolds
410 canreservefromotherbranches
411 decreaseLoanHighHolds
412 DisplayMultiPlaceHold
413 emailLibrarianWhenHoldIsPlaced
414 ExpireReservesMaxPickUpDelay
415 OPACAllowHoldDateInFuture
416 OPACAllowUserToChooseBranch
417 ReservesControlBranch
421 TransferWhenCancelAllWaitingHolds
422 AllowAllMessageDeletion
423 AllowOfflineCirculation
425 CircAutoPrintQuickSlip
426 DisplayClearScreenButton
427 FilterBeforeOverdueReport
429 itemBarcodeFallbackSearch
430 itemBarcodeInputFilter
431 previousIssuesDefaultSortOrder
432 RecordLocalUseOnReturn
435 todaysIssuesDefaultSortOrder
436 UpdateTotalIssuesOnCirc
438 WaitingNotifyAtCheckin
439 AllowSelfCheckReturns
444 OPACAmazonCoverImages
449 IDreamBooksReadometer
452 LibraryThingForLibrariesEnabled
455 NovelistSelectEnabled
462 CalendarFirstDayOfWeek
476 HighlightOwnItemsOnOPAC
477 OpacAddMastheadLibraryPulldown
478 OPACDisplay856uAsImage
486 OpacShowFiltersPulldownMobile
487 OPACShowHoldQueueDetails
488 OpacShowLibrariesPulldownMobile
489 OpacShowRecentComments
490 OPACShowUnusedAuthorities
493 OPACURLOpenInNewWindow
505 OPACPopupAuthorsSearch
515 AllowPurchaseSuggestionBranchChoice
516 OpacAllowPublicListCreation
517 OpacAllowSharingPrivateLists
520 OPACViewOthersSuggestions
524 EnableOpacSearchHistory
528 PatronSelfRegistration
531 AutoEmailPrimaryAddress
533 BorrowerRenewalPeriodBase
536 EnhancedMessagingPreferences
537 ExtendedPatronAttributes
538 intranetreadinghistory
541 TalkingTechItivaPhoneNotification
543 IncludeSeeFromInSearches
549 TraceCompleteSubfields
550 TraceSubjectSubdivisions
556 OPACItemsResultsDisplay
558 IntranetNumbersPreferPhrase
559 OPACNumbersPreferPhrase
561 RenewSerialAddsSuggestion
562 RoutingListAddReserves
571 StaffDetailItemSelection
581 t::lib::Mocks::mock_preference( $_, $set_value );
585 #Test if all systempreferences are at $value_to_test
586 sub verif_systempreferences_values {
587 my ( $report, $value_to_test ) = @_;
589 foreach my $key ( keys %{$report->{systempreferences}} ) {
590 is( $report->{systempreferences}->{$key}, $value_to_test, "\$report->{systempreferences}->{$key} = $value_to_test" );