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 => 551;
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 is( scalar( keys %{$report->{systempreferences}} ), 248, "There are 248 fields in $report->{systempreferences}" );
154 verif_systempreferences_values( $report, 0 );
157 mocking_systempreferences_to_a_set_value(1);
159 $report = C4::UsageStats->BuildReport();
160 isa_ok( $report, 'HASH', '$report is a HASH' );
161 isa_ok( $report->{systempreferences}, 'HASH', '$report->{systempreferences} is a HASH' );
162 is( scalar( keys %{$report->{systempreferences}} ), 248, "There are 248 fields in $report->{systempreferences}" );
163 verif_systempreferences_values( $report, 1 );
165 # ---------- Testing ReportToCommunity ----------
167 # ---------- Testing _count ---------------------
172 my $count = $dbh->selectrow_array($query);
174 my $nb_fields = C4::UsageStats::_count('borrowers');
175 is( $nb_fields, $count, "_count return the good number of fields" );
177 #################################################
179 #################################################
191 sub construct_objects_needed {
193 # ---------- 3 borrowers ---------------------
194 my $surname1 = 'Borrower 1';
195 my $surname2 = 'Borrower 2';
196 my $surname3 = 'Borrower 3';
197 my $firstname1 = 'firstname 1';
198 my $firstname2 = 'firstname 2';
199 my $firstname3 = 'firstname 3';
200 my $cardnumber1 = 'test_card1';
201 my $cardnumber2 = 'test_card2';
202 my $cardnumber3 = 'test_card3';
203 my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode();
204 my $branchcode = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode();
207 INSERT INTO borrowers
208 (surname, firstname, cardnumber, branchcode, categorycode)
210 my $insert_sth = $dbh->prepare($query);
211 $insert_sth->execute( $surname1, $firstname1, $cardnumber1, $branchcode, $categorycode );
212 my $borrowernumber1 = $dbh->last_insert_id( undef, undef, 'borrowers', undef );
213 $insert_sth->execute( $surname2, $firstname2, $cardnumber2, $branchcode, $categorycode );
214 my $borrowernumber2 = $dbh->last_insert_id( undef, undef, 'borrowers', undef );
215 $insert_sth->execute( $surname3, $firstname3, $cardnumber3, $branchcode, $categorycode );
216 my $borrowernumber3 = $dbh->last_insert_id( undef, undef, 'borrowers', undef );
218 # ---------- 3 biblios -----------------------
219 my $title1 = 'Title 1';
220 my $title2 = 'Title 2';
221 my $title3 = 'Title 3';
222 my $author1 = 'Author 1';
223 my $author2 = 'Author 2';
224 my $author3 = 'Author 3';
230 $insert_sth = $dbh->prepare($query);
231 $insert_sth->execute( $title1, $author1 );
232 my $biblionumber1 = $dbh->last_insert_id( undef, undef, 'biblio', undef );
233 $insert_sth->execute( $title2, undef );
234 my $biblionumber2 = $dbh->last_insert_id( undef, undef, 'biblio', undef );
235 $insert_sth->execute( $title3, $author3 );
236 my $biblionumber3 = $dbh->last_insert_id( undef, undef, 'biblio', undef );
238 # ---------- 3 biblio items -------------------------
240 INSERT INTO biblioitems
241 (biblionumber, itemtype, marcxml)
243 $insert_sth = $dbh->prepare($query);
244 $insert_sth->execute( $biblionumber1, 'Book', '' );
245 my $biblioitemnumber1 = $dbh->last_insert_id( undef, undef, 'biblioitems', undef );
246 $insert_sth->execute( $biblionumber2, 'Music', '' );
247 my $biblioitemnumber2 = $dbh->last_insert_id( undef, undef, 'biblioitems', undef );
248 $insert_sth->execute( $biblionumber3, 'Book', '' );
249 my $biblioitemnumber3 = $dbh->last_insert_id( undef, undef, 'biblioitems', undef );
251 # ---------- 3 items -------------------------
252 my $barcode1 = '111111';
253 my $barcode2 = '222222';
254 my $barcode3 = '333333';
258 (biblionumber, biblioitemnumber, barcode, itype)
260 $insert_sth = $dbh->prepare($query);
261 $insert_sth->execute( $biblionumber1, $biblioitemnumber1, $barcode1, 'Book' );
262 my $item_number1 = $dbh->last_insert_id( undef, undef, 'items', undef );
263 $insert_sth->execute( $biblionumber2, $biblioitemnumber2, $barcode2, 'Music' );
264 my $item_number2 = $dbh->last_insert_id( undef, undef, 'items', undef );
265 $insert_sth->execute( $biblionumber3, $biblioitemnumber3, $barcode3, 'Book' );
266 my $item_number3 = $dbh->last_insert_id( undef, undef, 'items', undef );
268 # ---------- Add 2 auth_header
270 INSERT INTO auth_header
273 $insert_sth = $dbh->prepare($query);
274 $insert_sth->execute('authtypecode1');
275 my $authid1 = $dbh->last_insert_id( undef, undef, 'auth_header', undef );
276 $insert_sth->execute('authtypecode2');
277 my $authid2 = $dbh->last_insert_id( undef, undef, 'auth_header', undef );
279 # ---------- Add 1 old_issues
281 INSERT INTO old_issues
282 (borrowernumber, branchcode, itemnumber)
284 $insert_sth = $dbh->prepare($query);
285 $insert_sth->execute( $borrowernumber1, $branchcode, $item_number1 );
286 my $issue_id1 = $dbh->last_insert_id( undef, undef, 'old_issues', undef );
288 # ---------- Add 1 old_reserves
289 AddReserve( $branchcode, $borrowernumber1, $biblionumber1, 'a', '', 1, undef, undef, '', 'Title', undef, undef );
290 my $reserves1 = GetReservesFromBiblionumber( { biblionumber => $biblionumber1 } );
291 my $reserve_id1 = $reserves1->[0]->{reserve_id};
292 my $reserve1 = CancelReserve( { reserve_id => $reserve_id1 } );
294 # ---------- Add 1 aqbudgets
296 INSERT INTO aqbudgets
299 $insert_sth = $dbh->prepare($query);
300 $insert_sth->execute("20.0");
301 my $aqbudgets1 = $dbh->last_insert_id( undef, undef, 'aqbudgets', undef );
303 # ---------- Add 1 aqorders
306 (budget_id, basketno, biblionumber, invoiceid, subscriptionid)
308 $insert_sth = $dbh->prepare($query);
309 $insert_sth->execute( $aqbudgets1, undef, undef, undef, undef );
310 my $aqorders1 = $dbh->last_insert_id( undef, undef, 'aqorders', undef );
312 # --------- Add 1 subscription
314 INSERT INTO subscription
317 $insert_sth = $dbh->prepare($query);
318 $insert_sth->execute($biblionumber1);
319 my $subscription1 = $dbh->last_insert_id( undef, undef, 'subscription', undef );
323 #Change systempreferences values to $set_value
324 sub mocking_systempreferences_to_a_set_value {
325 my $set_value = shift;
330 AcqWarnOnDuplicateInvoice
346 AutoCreateAuthorities
347 BiblioAddsAuthorities
349 UseAuthoritiesForTracings
352 IntranetBiblioDefaultView
358 DefaultClassificationSource
359 EasyAnalyticalRecords
366 SpineLabelShowPrintOnBibDetails
367 BlockReturnOfWithdrawnItems
368 CalculateFinesOnReturn
369 AgeRestrictionOverride
372 AllowItemsOnHoldCheckout
373 AllowNotForLoanOverride
374 AllowRenewalLimitOverride
378 AutoRemoveOverduesRestrictions
381 HomeOrHoldingBranchReturn
382 InProcessingToShelvingCart
385 ManInvInNoissuesCharge
389 RentalsInNoissuesCharge
392 TransfersMaxDaysWarning
393 UseBranchTransferLimits
395 UseTransportCostMatrix
398 FinesIncludeGracePeriod
400 RefundLostItemFeeOnReturn
401 WhenLostChargeReplacementFee
403 AllowHoldDateInFuture
404 AllowHoldPolicyOverride
405 AllowHoldsOnDamagedItems
406 AllowHoldsOnPatronsPossessions
407 AutoResumeSuspendedHolds
408 canreservefromotherbranches
409 decreaseLoanHighHolds
410 DisplayMultiPlaceHold
411 emailLibrarianWhenHoldIsPlaced
412 ExpireReservesMaxPickUpDelay
413 OPACAllowHoldDateInFuture
414 OPACAllowUserToChooseBranch
415 ReservesControlBranch
419 TransferWhenCancelAllWaitingHolds
420 AllowAllMessageDeletion
421 AllowOfflineCirculation
423 CircAutoPrintQuickSlip
424 DisplayClearScreenButton
425 FilterBeforeOverdueReport
427 itemBarcodeFallbackSearch
428 itemBarcodeInputFilter
429 previousIssuesDefaultSortOrder
430 RecordLocalUseOnReturn
433 todaysIssuesDefaultSortOrder
434 UpdateTotalIssuesOnCirc
436 WaitingNotifyAtCheckin
437 AllowSelfCheckReturns
442 OPACAmazonCoverImages
447 IDreamBooksReadometer
450 LibraryThingForLibrariesEnabled
453 NovelistSelectEnabled
459 CalendarFirstDayOfWeek
469 AuthorisedValueImages
474 HighlightOwnItemsOnOPAC
475 OpacAddMastheadLibraryPulldown
476 OPACDisplay856uAsImage
484 OpacShowFiltersPulldownMobile
485 OPACShowHoldQueueDetails
486 OpacShowLibrariesPulldownMobile
487 OpacShowRecentComments
488 OPACShowUnusedAuthorities
491 OPACURLOpenInNewWindow
503 OPACPopupAuthorsSearch
513 AllowPurchaseSuggestionBranchChoice
514 OpacAllowPublicListCreation
515 OpacAllowSharingPrivateLists
518 OPACViewOthersSuggestions
522 EnableOpacSearchHistory
526 PatronSelfRegistration
529 AutoEmailPrimaryAddress
531 BorrowerRenewalPeriodBase
534 EnhancedMessagingPreferences
535 ExtendedPatronAttributes
536 intranetreadinghistory
539 TalkingTechItivaPhoneNotification
541 IncludeSeeFromInSearches
547 TraceCompleteSubfields
548 TraceSubjectSubdivisions
554 OPACItemsResultsDisplay
556 IntranetNumbersPreferPhrase
557 OPACNumbersPreferPhrase
559 RenewSerialAddsSuggestion
560 RoutingListAddReserves
565 StaffAuthorisedValueImages
570 StaffDetailItemSelection
579 t::lib::Mocks::mock_preference( $_, $set_value );
583 #Test if all systempreferences are at $value_to_test
584 sub verif_systempreferences_values {
585 my ( $report, $value_to_test ) = @_;
587 foreach my $key ( keys %{$report->{systempreferences}} ) {
588 is( $report->{systempreferences}->{$key}, $value_to_test, "\$report->{systempreferences}->{$key} = $value_to_test" );