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 => 57;
19 use t::lib::Mocks qw(mock_preference);
20 use POSIX qw(strftime);
24 use_ok('C4::UsageStats');
25 use_ok('C4::Context');
27 use_ok( 'C4::AuthoritiesMarc', qw(AddAuthority) );
28 use_ok('C4::Reserves');
29 use_ok('MARC::Record');
30 use_ok('Koha::Acquisition::Order');
41 my $dbh = C4::Context->dbh;
42 $dbh->{AutoCommit} = 0;
43 $dbh->{RaiseError} = 1;
45 $dbh->do('DELETE FROM issues');
46 $dbh->do('DELETE FROM biblio');
47 $dbh->do('DELETE FROM items');
48 $dbh->do('DELETE FROM auth_header');
49 $dbh->do('DELETE FROM old_issues');
50 $dbh->do('DELETE FROM old_reserves');
51 $dbh->do('DELETE FROM borrowers');
52 $dbh->do('DELETE FROM aqorders');
53 $dbh->do('DELETE FROM subscription');
55 #################################################
57 #################################################
59 # ---------- Testing NeedUpdate -----------------
61 #Mocking C4::Context->preference("UsageStatsLastUpdateTime") to 0
62 my $now = strftime( "%s", localtime );
63 t::lib::Mocks::mock_preference( "UsageStatsLastUpdateTime", 0 );
65 my $update = C4::UsageStats->NeedUpdate;
66 is( $update, 1, "There is no last update, update needed" );
68 #Mocking C4::Context->preference("UsageStatsLastUpdateTime") to now
69 $now = strftime( "%s", localtime );
70 t::lib::Mocks::mock_preference( "UsageStatsLastUpdateTime", $now );
72 $update = C4::UsageStats->NeedUpdate;
73 is( $update, 0, "Last update just be done, no update needed " );
75 # ---------- Testing BuildReport ----------------
77 #Test report->library -----------------
79 t::lib::Mocks::mock_preference( "UsageStatsID", 0 );
80 t::lib::Mocks::mock_preference( "UsageStatsLibraryName", 0 );
81 t::lib::Mocks::mock_preference( "UsageStatsLibraryUrl", 0 );
82 t::lib::Mocks::mock_preference( "UsageStatsLibraryType", 0 );
83 t::lib::Mocks::mock_preference( "UsageStatsCountry", 0 );
85 my $report = C4::UsageStats->BuildReport();
87 isa_ok( $report, 'HASH', '$report is a HASH' );
88 isa_ok( $report->{library}, 'HASH', '$report->{library} is a HASH' );
89 is( scalar( keys %{$report->{library}} ), 5, "There are 5 fields in $report->{library}" );
90 is( $report->{library}->{id}, 0, "UsageStatsID is good" );
91 is( $report->{library}->{name}, '', "UsageStatsLibraryName is good" );
92 is( $report->{library}->{url}, '', "UsageStatsLibraryUrl is good" );
93 is( $report->{library}->{type}, '', "UsageStatsLibraryType is good" );
94 is( $report->{library}->{country}, '', "UsageStatsCountry is good" );
97 t::lib::Mocks::mock_preference( "UsageStatsID", 1 );
98 t::lib::Mocks::mock_preference( "UsageStatsLibraryName", 'NAME' );
99 t::lib::Mocks::mock_preference( "UsageStatsLibraryUrl", 'URL' );
100 t::lib::Mocks::mock_preference( "UsageStatsLibraryType", 'TYPE' );
101 t::lib::Mocks::mock_preference( "UsageStatsCountry", 'COUNTRY' );
103 $report = C4::UsageStats->BuildReport();
105 isa_ok( $report, 'HASH', '$report is a HASH' );
106 isa_ok( $report->{library}, 'HASH', '$report->{library} is a HASH' );
107 is( scalar( keys %{$report->{library}} ), 5, "There are 5 fields in $report->{library}" );
108 is( $report->{library}->{id}, 1, "UsageStatsID is good" );
109 is( $report->{library}->{name}, 'NAME', "UsageStatsLibraryName is good" );
110 is( $report->{library}->{url}, 'URL', "UsageStatsLibraryUrl is good" );
111 is( $report->{library}->{type}, 'TYPE', "UsageStatsLibraryType is good" );
112 is( $report->{library}->{country}, 'COUNTRY', "UsageStatsCountry is good" );
114 #Test report->volumetry ---------------
115 #with original values
116 $report = C4::UsageStats->BuildReport();
118 isa_ok( $report, 'HASH', '$report is a HASH' );
119 isa_ok( $report->{volumetry}, 'HASH', '$report->{volumetry} is a HASH' );
120 is( scalar( keys %{$report->{volumetry}} ), 8, "There are 8 fields in $report->{volumetry}" );
121 is( $report->{volumetry}->{biblio}, 0, "There is no biblio" );
122 is( $report->{volumetry}->{items}, 0, "There is no items" );
123 is( $report->{volumetry}->{auth_header}, 0, "There is no auth_header" );
124 is( $report->{volumetry}->{old_issues}, 0, "There is no old_issues" );
125 is( $report->{volumetry}->{old_reserves}, 0, "There is no old_reserves" );
126 is( $report->{volumetry}->{borrowers}, 0, "There is no borrowers" );
127 is( $report->{volumetry}->{aqorders}, 0, "There is no aqorders" );
128 is( $report->{volumetry}->{subscription}, 0, "There is no subscription" );
130 #after adding objects
131 construct_objects_needed();
133 $report = C4::UsageStats->BuildReport();
135 isa_ok( $report, 'HASH', '$report is a HASH' );
136 isa_ok( $report->{volumetry}, 'HASH', '$report->{volumetry} is a HASH' );
137 is( scalar( keys %{$report->{volumetry}} ), 8, "There are 8 fields in $report->{volumetry}" );
138 is( $report->{volumetry}->{biblio}, 3, "There are 3 biblio" );
139 is( $report->{volumetry}->{items}, 3, "There are 3 items" );
140 is( $report->{volumetry}->{auth_header}, 2, "There are 2 auth_header" );
141 is( $report->{volumetry}->{old_issues}, 1, "There is 1 old_issues" );
142 is( $report->{volumetry}->{old_reserves}, 1, "There is 1 old_reserves" );
143 is( $report->{volumetry}->{borrowers}, 3, "There are 3 borrowers" );
144 is( $report->{volumetry}->{aqorders}, 1, "There is 1 aqorders" );
145 is( $report->{volumetry}->{subscription}, 1, "There is 1 subscription" );
147 #Test report->systempreferences -------
149 mocking_systempreferences_to_a_set_value(0);
151 $report = C4::UsageStats->BuildReport();
152 isa_ok( $report, 'HASH', '$report is a HASH' );
153 isa_ok( $report->{systempreferences}, 'HASH', '$report->{systempreferences} is a HASH' );
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 verif_systempreferences_values( $report, 1 );
164 #Test if unwanted syspref are not sent
165 is( $report->{systempreferences}->{useDischarge}, undef, 'useDischarge should not be shared');
166 is( $report->{systempreferences}->{OpacUserJS}, undef, 'OpacUserJS should not be shared');
168 # ---------- Testing ReportToCommunity ----------
170 # ---------- Testing _count ---------------------
175 my $count = $dbh->selectrow_array($query);
177 my $nb_fields = C4::UsageStats::_count('borrowers');
178 is( $nb_fields, $count, "_count return the good number of fields" );
180 #################################################
182 #################################################
194 sub construct_objects_needed {
196 # ---------- 3 borrowers ---------------------
197 my $surname1 = 'Borrower 1';
198 my $surname2 = 'Borrower 2';
199 my $surname3 = 'Borrower 3';
200 my $firstname1 = 'firstname 1';
201 my $firstname2 = 'firstname 2';
202 my $firstname3 = 'firstname 3';
203 my $cardnumber1 = 'test_card1';
204 my $cardnumber2 = 'test_card2';
205 my $cardnumber3 = 'test_card3';
206 my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode();
207 my $branchcode = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode();
210 INSERT INTO borrowers
211 (surname, firstname, cardnumber, branchcode, categorycode)
213 my $insert_sth = $dbh->prepare($query);
214 $insert_sth->execute( $surname1, $firstname1, $cardnumber1, $branchcode, $categorycode );
215 my $borrowernumber1 = $dbh->last_insert_id( undef, undef, 'borrowers', undef );
216 $insert_sth->execute( $surname2, $firstname2, $cardnumber2, $branchcode, $categorycode );
217 my $borrowernumber2 = $dbh->last_insert_id( undef, undef, 'borrowers', undef );
218 $insert_sth->execute( $surname3, $firstname3, $cardnumber3, $branchcode, $categorycode );
219 my $borrowernumber3 = $dbh->last_insert_id( undef, undef, 'borrowers', undef );
221 # ---------- 3 biblios -----------------------
222 my $title1 = 'Title 1';
223 my $title2 = 'Title 2';
224 my $title3 = 'Title 3';
225 my $author1 = 'Author 1';
226 my $author2 = 'Author 2';
227 my $author3 = 'Author 3';
233 $insert_sth = $dbh->prepare($query);
234 $insert_sth->execute( $title1, $author1 );
235 my $biblionumber1 = $dbh->last_insert_id( undef, undef, 'biblio', undef );
236 $insert_sth->execute( $title2, undef );
237 my $biblionumber2 = $dbh->last_insert_id( undef, undef, 'biblio', undef );
238 $insert_sth->execute( $title3, $author3 );
239 my $biblionumber3 = $dbh->last_insert_id( undef, undef, 'biblio', undef );
241 # ---------- 3 biblio items -------------------------
243 INSERT INTO biblioitems
244 (biblionumber, itemtype, marcxml)
246 $insert_sth = $dbh->prepare($query);
247 $insert_sth->execute( $biblionumber1, 'Book', '' );
248 my $biblioitemnumber1 = $dbh->last_insert_id( undef, undef, 'biblioitems', undef );
249 $insert_sth->execute( $biblionumber2, 'Music', '' );
250 my $biblioitemnumber2 = $dbh->last_insert_id( undef, undef, 'biblioitems', undef );
251 $insert_sth->execute( $biblionumber3, 'Book', '' );
252 my $biblioitemnumber3 = $dbh->last_insert_id( undef, undef, 'biblioitems', undef );
254 # ---------- 3 items -------------------------
255 my $barcode1 = '111111';
256 my $barcode2 = '222222';
257 my $barcode3 = '333333';
261 (biblionumber, biblioitemnumber, barcode, itype)
263 $insert_sth = $dbh->prepare($query);
264 $insert_sth->execute( $biblionumber1, $biblioitemnumber1, $barcode1, 'Book' );
265 my $item_number1 = $dbh->last_insert_id( undef, undef, 'items', undef );
266 $insert_sth->execute( $biblionumber2, $biblioitemnumber2, $barcode2, 'Music' );
267 my $item_number2 = $dbh->last_insert_id( undef, undef, 'items', undef );
268 $insert_sth->execute( $biblionumber3, $biblioitemnumber3, $barcode3, 'Book' );
269 my $item_number3 = $dbh->last_insert_id( undef, undef, 'items', undef );
271 # ---------- Add 2 auth_header
273 INSERT INTO auth_header
276 $insert_sth = $dbh->prepare($query);
277 $insert_sth->execute('authtypecode1');
278 my $authid1 = $dbh->last_insert_id( undef, undef, 'auth_header', undef );
279 $insert_sth->execute('authtypecode2');
280 my $authid2 = $dbh->last_insert_id( undef, undef, 'auth_header', undef );
282 # ---------- Add 1 old_issues
284 INSERT INTO old_issues
285 (borrowernumber, branchcode, itemnumber)
287 $insert_sth = $dbh->prepare($query);
288 $insert_sth->execute( $borrowernumber1, $branchcode, $item_number1 );
289 my $issue_id1 = $dbh->last_insert_id( undef, undef, 'old_issues', undef );
291 # ---------- Add 1 old_reserves
292 AddReserve( $branchcode, $borrowernumber1, $biblionumber1, '', 1, undef, undef, '', 'Title', undef, undef );
293 my $reserves1 = GetReservesFromBiblionumber( { biblionumber => $biblionumber1 } );
294 my $reserve_id1 = $reserves1->[0]->{reserve_id};
295 my $reserve1 = CancelReserve( { reserve_id => $reserve_id1 } );
297 # ---------- Add 1 aqbudgets
299 INSERT INTO aqbudgets
302 $insert_sth = $dbh->prepare($query);
303 $insert_sth->execute("20.0");
304 my $aqbudgets1 = $dbh->last_insert_id( undef, undef, 'aqbudgets', undef );
306 # ---------- Add 1 aqorders
309 (budget_id, basketno, biblionumber, invoiceid, subscriptionid)
311 $insert_sth = $dbh->prepare($query);
312 $insert_sth->execute( $aqbudgets1, undef, undef, undef, undef );
313 my $aqorders1 = $dbh->last_insert_id( undef, undef, 'aqorders', undef );
315 # --------- Add 1 subscription
317 INSERT INTO subscription
320 $insert_sth = $dbh->prepare($query);
321 $insert_sth->execute($biblionumber1);
322 my $subscription1 = $dbh->last_insert_id( undef, undef, 'subscription', undef );
326 #Change systempreferences values to $set_value
327 sub mocking_systempreferences_to_a_set_value {
328 my $set_value = shift;
333 AcqWarnOnDuplicateInvoice
349 AutoCreateAuthorities
350 BiblioAddsAuthorities
352 UseAuthoritiesForTracings
355 IntranetBiblioDefaultView
361 DefaultClassificationSource
362 EasyAnalyticalRecords
369 SpineLabelShowPrintOnBibDetails
370 BlockReturnOfWithdrawnItems
371 CalculateFinesOnReturn
372 AgeRestrictionOverride
375 AllowItemsOnHoldCheckout
376 AllowItemsOnHoldCheckoutSCO
377 AllowNotForLoanOverride
378 AllowRenewalLimitOverride
382 AutoRemoveOverduesRestrictions
385 HomeOrHoldingBranchReturn
386 InProcessingToShelvingCart
389 ManInvInNoissuesCharge
393 RentalsInNoissuesCharge
396 TransfersMaxDaysWarning
397 UseBranchTransferLimits
399 UseTransportCostMatrix
402 FinesIncludeGracePeriod
404 RefundLostOnReturnControl
405 WhenLostChargeReplacementFee
407 AllowHoldDateInFuture
408 AllowHoldPolicyOverride
409 AllowHoldsOnDamagedItems
410 AllowHoldsOnPatronsPossessions
411 AutoResumeSuspendedHolds
412 canreservefromotherbranches
413 decreaseLoanHighHolds
414 DisplayMultiPlaceHold
415 emailLibrarianWhenHoldIsPlaced
416 ExpireReservesMaxPickUpDelay
417 OPACAllowHoldDateInFuture
418 OPACAllowUserToChooseBranch
419 ReservesControlBranch
423 TransferWhenCancelAllWaitingHolds
424 AllowAllMessageDeletion
425 AllowOfflineCirculation
427 CircAutoPrintQuickSlip
428 DisplayClearScreenButton
429 FilterBeforeOverdueReport
431 itemBarcodeFallbackSearch
432 itemBarcodeInputFilter
433 previousIssuesDefaultSortOrder
434 RecordLocalUseOnReturn
437 todaysIssuesDefaultSortOrder
438 UpdateTotalIssuesOnCirc
440 WaitingNotifyAtCheckin
441 AllowSelfCheckReturns
446 OPACAmazonCoverImages
451 IDreamBooksReadometer
454 LibraryThingForLibrariesEnabled
457 NovelistSelectEnabled
464 CalendarFirstDayOfWeek
478 HighlightOwnItemsOnOPAC
479 OpacAddMastheadLibraryPulldown
480 OPACDisplay856uAsImage
488 OpacShowFiltersPulldownMobile
489 OPACShowHoldQueueDetails
490 OpacShowLibrariesPulldownMobile
491 OpacShowRecentComments
492 OPACShowUnusedAuthorities
495 OPACURLOpenInNewWindow
507 OPACPopupAuthorsSearch
517 AllowPurchaseSuggestionBranchChoice
518 OpacAllowPublicListCreation
519 OpacAllowSharingPrivateLists
522 OPACViewOthersSuggestions
526 EnableOpacSearchHistory
530 PatronSelfRegistration
533 AutoEmailPrimaryAddress
535 BorrowerRenewalPeriodBase
538 EnhancedMessagingPreferences
539 ExtendedPatronAttributes
540 intranetreadinghistory
543 TalkingTechItivaPhoneNotification
545 IncludeSeeFromInSearches
551 TraceCompleteSubfields
552 TraceSubjectSubdivisions
558 OPACItemsResultsDisplay
560 IntranetNumbersPreferPhrase
561 OPACNumbersPreferPhrase
563 RenewSerialAddsSuggestion
564 RoutingListAddReserves
573 StaffDetailItemSelection
583 t::lib::Mocks::mock_preference( $_, $set_value );
587 #Test if all systempreferences are at $value_to_test
588 sub verif_systempreferences_values {
589 my ( $report, $value_to_test ) = @_;
592 foreach my $key ( keys %{$report->{systempreferences}} ) {
593 if ( $report->{systempreferences}->{$key} ne $value_to_test ) {
595 push @missings, $key;
598 unless ( @missings ) {
599 ok(1, 'All prefs are present');
601 ok(0, 'Some prefs are missing: ' . Dumper(\@missings));