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);
25 use_ok('C4::UsageStats');
26 use_ok('C4::Context');
28 use_ok( 'C4::AuthoritiesMarc', qw(AddAuthority) );
29 use_ok('C4::Reserves');
30 use_ok('MARC::Record');
31 use_ok('Koha::Acquisition::Order');
42 my $dbh = C4::Context->dbh;
43 $dbh->{AutoCommit} = 0;
44 $dbh->{RaiseError} = 1;
46 $dbh->do('DELETE FROM issues');
47 $dbh->do('DELETE FROM biblio');
48 $dbh->do('DELETE FROM items');
49 $dbh->do('DELETE FROM auth_header');
50 $dbh->do('DELETE FROM old_issues');
51 $dbh->do('DELETE FROM old_reserves');
52 $dbh->do('DELETE FROM borrowers');
53 $dbh->do('DELETE FROM aqorders');
54 $dbh->do('DELETE FROM subscription');
56 #################################################
58 #################################################
60 # ---------- Testing NeedUpdate -----------------
62 #Mocking C4::Context->preference("UsageStatsLastUpdateTime") to 0
63 my $now = strftime( "%s", localtime );
64 t::lib::Mocks::mock_preference( "UsageStatsLastUpdateTime", 0 );
66 my $update = C4::UsageStats->NeedUpdate;
67 is( $update, 1, "There is no last update, update needed" );
69 #Mocking C4::Context->preference("UsageStatsLastUpdateTime") to now
70 $now = strftime( "%s", localtime );
71 t::lib::Mocks::mock_preference( "UsageStatsLastUpdateTime", $now );
73 $update = C4::UsageStats->NeedUpdate;
74 is( $update, 0, "Last update just be done, no update needed " );
76 my $nb_of_libraries = Koha::Libraries->count;
78 # ---------- Testing BuildReport ----------------
80 #Test report->library -----------------
82 t::lib::Mocks::mock_preference( "UsageStatsID", 0 );
83 t::lib::Mocks::mock_preference( "UsageStatsLibraryName", 0 );
84 t::lib::Mocks::mock_preference( "UsageStatsLibrariesInfo", 0 );
85 t::lib::Mocks::mock_preference( "UsageStatsLibraryType", 0 );
86 t::lib::Mocks::mock_preference( "UsageStatsCountry", 0 );
87 t::lib::Mocks::mock_preference( "UsageStatsLibraryUrl", 0 );
89 my $report = C4::UsageStats->BuildReport();
91 isa_ok( $report, 'HASH', '$report is a HASH' );
92 isa_ok( $report->{libraries}, 'ARRAY', '$report->{libraries} is an ARRAY' );
93 is( scalar( @{ $report->{libraries} } ), 0, "There are 0 fields in libraries, libraries info are not shared" );
94 is( $report->{installation}->{koha_id}, 0, "UsageStatsID is good" );
95 is( $report->{installation}->{name}, '', "UsageStatsLibraryName is good" );
96 is( $report->{installation}->{url}, '', "UsageStatsLibraryUrl is good" );
97 is( $report->{installation}->{type}, '', "UsageStatsLibraryType is good" );
98 is( $report->{installation}->{country}, '', "UsageStatsCountry is good" );
102 t::lib::Mocks::mock_preference( "UsageStatsID", 1 );
103 t::lib::Mocks::mock_preference( "UsageStatsLibraryName", 'NAME' );
104 t::lib::Mocks::mock_preference( "UsageStatsLibraryUrl", 'URL' );
105 t::lib::Mocks::mock_preference( "UsageStatsLibraryType", 'TYPE' );
106 t::lib::Mocks::mock_preference( "UsageStatsCountry", 'COUNTRY' );
107 t::lib::Mocks::mock_preference( "UsageStatsLibrariesInfo", 1 );
108 t::lib::Mocks::mock_preference( "UsageStatsGeolocation", 1 );
111 $report = C4::UsageStats->BuildReport();
113 isa_ok( $report, 'HASH', '$report is a HASH' );
114 isa_ok( $report->{libraries}, 'ARRAY', '$report->{libraries} is an ARRAY' );
115 is( scalar( @{ $report->{libraries} } ), $nb_of_libraries, "There are 6 fields in $report->{libraries}" );
116 is( $report->{installation}->{koha_id}, 1, "UsageStatsID is good" );
117 is( $report->{installation}->{name}, 'NAME', "UsageStatsLibraryName is good" );
118 is( $report->{installation}->{url}, 'URL', "UsageStatsLibraryUrl is good" );
119 is( $report->{installation}->{type}, 'TYPE', "UsageStatsLibraryType is good" );
120 is( $report->{installation}->{country}, 'COUNTRY', "UsageStatsCountry is good" );
122 #Test report->volumetry ---------------
123 #with original values
124 $report = C4::UsageStats->BuildReport();
126 isa_ok( $report, 'HASH', '$report is a HASH' );
127 isa_ok( $report->{volumetry}, 'HASH', '$report->{volumetry} is a HASH' );
128 is( scalar( keys %{$report->{volumetry}} ), 8, "There are 8 fields in $report->{volumetry}" );
129 is( $report->{volumetry}->{biblio}, 0, "There is no biblio" );
130 is( $report->{volumetry}->{items}, 0, "There is no items" );
131 is( $report->{volumetry}->{auth_header}, 0, "There is no auth_header" );
132 is( $report->{volumetry}->{old_issues}, 0, "There is no old_issues" );
133 is( $report->{volumetry}->{old_reserves}, 0, "There is no old_reserves" );
134 is( $report->{volumetry}->{borrowers}, 0, "There is no borrowers" );
135 is( $report->{volumetry}->{aqorders}, 0, "There is no aqorders" );
136 is( $report->{volumetry}->{subscription}, 0, "There is no subscription" );
138 #after adding objects
139 construct_objects_needed();
141 $report = C4::UsageStats->BuildReport();
143 isa_ok( $report, 'HASH', '$report is a HASH' );
144 isa_ok( $report->{volumetry}, 'HASH', '$report->{volumetry} is a HASH' );
145 is( scalar( keys %{$report->{volumetry}} ), 8, "There are 8 fields in $report->{volumetry}" );
146 is( $report->{volumetry}->{biblio}, 3, "There are 3 biblio" );
147 is( $report->{volumetry}->{items}, 3, "There are 3 items" );
148 is( $report->{volumetry}->{auth_header}, 2, "There are 2 auth_header" );
149 is( $report->{volumetry}->{old_issues}, 1, "There is 1 old_issues" );
150 is( $report->{volumetry}->{old_reserves}, 1, "There is 1 old_reserves" );
151 is( $report->{volumetry}->{borrowers}, 3, "There are 3 borrowers" );
152 is( $report->{volumetry}->{aqorders}, 1, "There is 1 aqorders" );
153 is( $report->{volumetry}->{subscription}, 1, "There is 1 subscription" );
155 #Test report->systempreferences -------
157 mocking_systempreferences_to_a_set_value(0);
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, 0 );
165 mocking_systempreferences_to_a_set_value(1);
167 $report = C4::UsageStats->BuildReport();
168 isa_ok( $report, 'HASH', '$report is a HASH' );
169 isa_ok( $report->{systempreferences}, 'HASH', '$report->{systempreferences} is a HASH' );
170 verif_systempreferences_values( $report, 1 );
172 #Test if unwanted syspref are not sent
173 is( $report->{systempreferences}->{useDischarge}, undef, 'useDischarge should not be shared');
174 is( $report->{systempreferences}->{OpacUserJS}, undef, 'OpacUserJS should not be shared');
176 # ---------- Testing ReportToCommunity ----------
178 # ---------- Testing _count ---------------------
183 my $count = $dbh->selectrow_array($query);
185 my $nb_fields = C4::UsageStats::_count('borrowers');
186 is( $nb_fields, $count, "_count return the good number of fields" );
188 #################################################
190 #################################################
202 sub construct_objects_needed {
204 # ---------- 3 borrowers ---------------------
205 my $surname1 = 'Borrower 1';
206 my $surname2 = 'Borrower 2';
207 my $surname3 = 'Borrower 3';
208 my $firstname1 = 'firstname 1';
209 my $firstname2 = 'firstname 2';
210 my $firstname3 = 'firstname 3';
211 my $cardnumber1 = 'test_card1';
212 my $cardnumber2 = 'test_card2';
213 my $cardnumber3 = 'test_card3';
214 my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode();
215 my $branchcode = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode();
218 INSERT INTO borrowers
219 (surname, firstname, cardnumber, branchcode, categorycode)
221 my $insert_sth = $dbh->prepare($query);
222 $insert_sth->execute( $surname1, $firstname1, $cardnumber1, $branchcode, $categorycode );
223 my $borrowernumber1 = $dbh->last_insert_id( undef, undef, 'borrowers', undef );
224 $insert_sth->execute( $surname2, $firstname2, $cardnumber2, $branchcode, $categorycode );
225 my $borrowernumber2 = $dbh->last_insert_id( undef, undef, 'borrowers', undef );
226 $insert_sth->execute( $surname3, $firstname3, $cardnumber3, $branchcode, $categorycode );
227 my $borrowernumber3 = $dbh->last_insert_id( undef, undef, 'borrowers', undef );
229 # ---------- 3 biblios -----------------------
230 my $title1 = 'Title 1';
231 my $title2 = 'Title 2';
232 my $title3 = 'Title 3';
233 my $author1 = 'Author 1';
234 my $author2 = 'Author 2';
235 my $author3 = 'Author 3';
241 $insert_sth = $dbh->prepare($query);
242 $insert_sth->execute( $title1, $author1 );
243 my $biblionumber1 = $dbh->last_insert_id( undef, undef, 'biblio', undef );
244 $insert_sth->execute( $title2, undef );
245 my $biblionumber2 = $dbh->last_insert_id( undef, undef, 'biblio', undef );
246 $insert_sth->execute( $title3, $author3 );
247 my $biblionumber3 = $dbh->last_insert_id( undef, undef, 'biblio', undef );
249 # ---------- 3 biblio items -------------------------
251 INSERT INTO biblioitems
252 (biblionumber, itemtype, marcxml)
254 $insert_sth = $dbh->prepare($query);
255 $insert_sth->execute( $biblionumber1, 'Book', '' );
256 my $biblioitemnumber1 = $dbh->last_insert_id( undef, undef, 'biblioitems', undef );
257 $insert_sth->execute( $biblionumber2, 'Music', '' );
258 my $biblioitemnumber2 = $dbh->last_insert_id( undef, undef, 'biblioitems', undef );
259 $insert_sth->execute( $biblionumber3, 'Book', '' );
260 my $biblioitemnumber3 = $dbh->last_insert_id( undef, undef, 'biblioitems', undef );
262 # ---------- 3 items -------------------------
263 my $barcode1 = '111111';
264 my $barcode2 = '222222';
265 my $barcode3 = '333333';
269 (biblionumber, biblioitemnumber, barcode, itype)
271 $insert_sth = $dbh->prepare($query);
272 $insert_sth->execute( $biblionumber1, $biblioitemnumber1, $barcode1, 'Book' );
273 my $item_number1 = $dbh->last_insert_id( undef, undef, 'items', undef );
274 $insert_sth->execute( $biblionumber2, $biblioitemnumber2, $barcode2, 'Music' );
275 my $item_number2 = $dbh->last_insert_id( undef, undef, 'items', undef );
276 $insert_sth->execute( $biblionumber3, $biblioitemnumber3, $barcode3, 'Book' );
277 my $item_number3 = $dbh->last_insert_id( undef, undef, 'items', undef );
279 # ---------- Add 2 auth_header
281 INSERT INTO auth_header
284 $insert_sth = $dbh->prepare($query);
285 $insert_sth->execute('authtypecode1');
286 my $authid1 = $dbh->last_insert_id( undef, undef, 'auth_header', undef );
287 $insert_sth->execute('authtypecode2');
288 my $authid2 = $dbh->last_insert_id( undef, undef, 'auth_header', undef );
290 # ---------- Add 1 old_issues
292 INSERT INTO old_issues
293 (borrowernumber, branchcode, itemnumber)
295 $insert_sth = $dbh->prepare($query);
296 $insert_sth->execute( $borrowernumber1, $branchcode, $item_number1 );
297 my $issue_id1 = $dbh->last_insert_id( undef, undef, 'old_issues', undef );
299 # ---------- Add 1 old_reserves
300 AddReserve( $branchcode, $borrowernumber1, $biblionumber1, '', 1, undef, undef, '', 'Title', undef, undef );
301 my $reserves1 = GetReservesFromBiblionumber( { biblionumber => $biblionumber1 } );
302 my $reserve_id1 = $reserves1->[0]->{reserve_id};
303 my $reserve1 = CancelReserve( { reserve_id => $reserve_id1 } );
305 # ---------- Add 1 aqbudgets
307 INSERT INTO aqbudgets
310 $insert_sth = $dbh->prepare($query);
311 $insert_sth->execute("20.0");
312 my $aqbudgets1 = $dbh->last_insert_id( undef, undef, 'aqbudgets', undef );
314 # ---------- Add 1 aqorders
317 (budget_id, basketno, biblionumber, invoiceid, subscriptionid)
319 $insert_sth = $dbh->prepare($query);
320 $insert_sth->execute( $aqbudgets1, undef, undef, undef, undef );
321 my $aqorders1 = $dbh->last_insert_id( undef, undef, 'aqorders', undef );
323 # --------- Add 1 subscription
325 INSERT INTO subscription
328 $insert_sth = $dbh->prepare($query);
329 $insert_sth->execute($biblionumber1);
330 my $subscription1 = $dbh->last_insert_id( undef, undef, 'subscription', undef );
334 #Change systempreferences values to $set_value
335 sub mocking_systempreferences_to_a_set_value {
336 my $set_value = shift;
341 AcqWarnOnDuplicateInvoice
357 AutoCreateAuthorities
358 BiblioAddsAuthorities
360 UseAuthoritiesForTracings
363 IntranetBiblioDefaultView
369 DefaultClassificationSource
370 EasyAnalyticalRecords
377 SpineLabelShowPrintOnBibDetails
378 BlockReturnOfWithdrawnItems
379 CalculateFinesOnReturn
380 AgeRestrictionOverride
383 AllowItemsOnHoldCheckout
384 AllowNotForLoanOverride
385 AllowRenewalLimitOverride
389 AutoRemoveOverduesRestrictions
392 HomeOrHoldingBranchReturn
393 InProcessingToShelvingCart
396 ManInvInNoissuesCharge
400 RentalsInNoissuesCharge
403 TransfersMaxDaysWarning
404 UseBranchTransferLimits
406 UseTransportCostMatrix
409 FinesIncludeGracePeriod
411 RefundLostItemFeeOnReturn
412 WhenLostChargeReplacementFee
414 AllowHoldDateInFuture
415 AllowHoldPolicyOverride
416 AllowHoldsOnDamagedItems
417 AllowHoldsOnPatronsPossessions
418 AutoResumeSuspendedHolds
419 canreservefromotherbranches
420 decreaseLoanHighHolds
421 DisplayMultiPlaceHold
422 emailLibrarianWhenHoldIsPlaced
423 ExpireReservesMaxPickUpDelay
424 OPACAllowHoldDateInFuture
425 OPACAllowUserToChooseBranch
426 ReservesControlBranch
430 TransferWhenCancelAllWaitingHolds
431 AllowAllMessageDeletion
432 AllowOfflineCirculation
434 CircAutoPrintQuickSlip
435 DisplayClearScreenButton
436 FilterBeforeOverdueReport
438 itemBarcodeFallbackSearch
439 itemBarcodeInputFilter
440 previousIssuesDefaultSortOrder
441 RecordLocalUseOnReturn
444 todaysIssuesDefaultSortOrder
445 UpdateTotalIssuesOnCirc
447 WaitingNotifyAtCheckin
448 AllowSelfCheckReturns
453 OPACAmazonCoverImages
458 IDreamBooksReadometer
461 LibraryThingForLibrariesEnabled
464 NovelistSelectEnabled
471 CalendarFirstDayOfWeek
485 HighlightOwnItemsOnOPAC
486 OpacAddMastheadLibraryPulldown
487 OPACDisplay856uAsImage
495 OpacShowFiltersPulldownMobile
496 OPACShowHoldQueueDetails
497 OpacShowLibrariesPulldownMobile
498 OpacShowRecentComments
499 OPACShowUnusedAuthorities
502 OPACURLOpenInNewWindow
514 OPACPopupAuthorsSearch
524 AllowPurchaseSuggestionBranchChoice
525 OpacAllowPublicListCreation
526 OpacAllowSharingPrivateLists
529 OPACViewOthersSuggestions
533 EnableOpacSearchHistory
537 PatronSelfRegistration
540 AutoEmailPrimaryAddress
542 BorrowerRenewalPeriodBase
545 EnhancedMessagingPreferences
546 ExtendedPatronAttributes
547 intranetreadinghistory
550 TalkingTechItivaPhoneNotification
552 IncludeSeeFromInSearches
558 TraceCompleteSubfields
559 TraceSubjectSubdivisions
565 OPACItemsResultsDisplay
567 IntranetNumbersPreferPhrase
568 OPACNumbersPreferPhrase
570 RenewSerialAddsSuggestion
571 RoutingListAddReserves
580 StaffDetailItemSelection
590 t::lib::Mocks::mock_preference( $_, $set_value );
594 #Test if all systempreferences are at $value_to_test
595 sub verif_systempreferences_values {
596 my ( $report, $value_to_test ) = @_;
598 foreach my $key ( keys %{$report->{systempreferences}} ) {
599 is( $report->{systempreferences}->{$key}, $value_to_test, "\$report->{systempreferences}->{$key} = $value_to_test" );