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 => 552;
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');
30 use_ok('t::lib::TestBuilder');
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 ---------------
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 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}, 4, "There are 4 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 is( scalar( keys( $report->{systempreferences} ) ), 248, "There are 248 fields in $report->{systempreferences}" );
155 verif_systempreferences_values( $report, 0 );
158 mocking_systempreferences_to_a_set_value(1);
160 $report = C4::UsageStats->BuildReport();
161 isa_ok( $report, 'HASH', '$report is a HASH' );
162 isa_ok( $report->{systempreferences}, 'HASH', '$report->{systempreferences} is a HASH' );
163 is( scalar( keys( $report->{systempreferences} ) ), 248, "There are 248 fields in $report->{systempreferences}" );
164 verif_systempreferences_values( $report, 1 );
166 # ---------- Testing ReportToCommunity ----------
168 # ---------- Testing _count ---------------------
170 $dbh->do('DROP TABLE IF EXISTS _exmpl_tbl');
171 $dbh->do('CREATE TABLE _exmpl_tbl (id INT, val VARCHAR(10))');
172 $dbh->do( 'INSERT INTO _exmpl_tbl VALUES(1, ?)', undef, 'Hello' );
173 $dbh->do( 'INSERT INTO _exmpl_tbl VALUES(2, ?)', undef, 'World' );
179 my $count = $dbh->selectrow_array($query);
181 my $nb_fields = C4::UsageStats::_count('_exmpl_tbl');
182 is( $nb_fields, $count, "_exmpl_tbl has 2 fields" );
184 #################################################
186 #################################################
198 sub construct_objects_needed {
200 # ---------- 3 borrowers ---------------------
201 my $surname1 = 'Borrower 1';
202 my $surname2 = 'Borrower 2';
203 my $surname3 = 'Borrower 3';
204 my $firstname1 = 'firstname 1';
205 my $firstname2 = 'firstname 2';
206 my $firstname3 = 'firstname 3';
207 my $cardnumber1 = '00001';
208 my $cardnumber2 = '00002';
209 my $cardnumber3 = '00003';
210 my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode();
211 my $branchcode = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode();
214 INSERT INTO borrowers
215 (surname, firstname, cardnumber, branchcode, categorycode)
217 my $insert_sth = $dbh->prepare($query);
218 $insert_sth->execute( $surname1, $firstname1, $cardnumber1, $branchcode, $categorycode );
219 $insert_sth->execute( $surname2, $firstname2, $cardnumber2, $branchcode, $categorycode );
220 $insert_sth->execute( $surname3, $firstname3, $cardnumber3, $branchcode, $categorycode );
223 SELECT borrowernumber
226 my $borrowernumber1 = $dbh->selectrow_array( $query, {}, $surname1 );
227 my $borrowernumber2 = $dbh->selectrow_array( $query, {}, $surname2 );
228 my $borrowernumber3 = $dbh->selectrow_array( $query, {}, $surname3 );
230 # ---------- 3 biblios -----------------------
231 my $title1 = 'Title 1';
232 my $title2 = 'Title 2';
233 my $title3 = 'Title 3';
234 my $author1 = 'Author 1';
235 my $author2 = 'Author 2';
236 my $author3 = 'Author 3';
242 $insert_sth = $dbh->prepare($query);
243 $insert_sth->execute( $title1, $author1 );
244 my $biblionumber1 = $dbh->last_insert_id( undef, undef, 'biblio', undef );
245 $insert_sth->execute( $title2, undef );
246 my $biblionumber2 = $dbh->last_insert_id( undef, undef, 'biblio', undef );
247 $insert_sth->execute( $title3, $author3 );
248 my $biblionumber3 = $dbh->last_insert_id( undef, undef, 'biblio', undef );
250 # ---------- 3 biblio items -------------------------
252 INSERT INTO biblioitems
253 (biblionumber, itemtype, marcxml)
255 $insert_sth = $dbh->prepare($query);
256 $insert_sth->execute( $biblionumber1, 'Book', '' );
257 $insert_sth->execute( $biblionumber2, 'Music', '' );
258 $insert_sth->execute( $biblionumber3, 'Book', '' );
261 SELECT biblioitemnumber
263 WHERE biblionumber = ?';
264 my $biblioitemnumber1 = $dbh->selectrow_array( $query, {}, $biblionumber1 );
265 my $biblioitemnumber2 = $dbh->selectrow_array( $query, {}, $biblionumber2 );
266 my $biblioitemnumber3 = $dbh->selectrow_array( $query, {}, $biblionumber3 );
268 # ---------- 3 items -------------------------
269 my $barcode1 = '111111';
270 my $barcode2 = '222222';
271 my $barcode3 = '333333';
275 (biblionumber, biblioitemnumber, barcode, itype)
277 $insert_sth = $dbh->prepare($query);
278 $insert_sth->execute( $biblionumber1, $biblioitemnumber1, $barcode1, 'Book' );
279 $insert_sth->execute( $biblionumber2, $biblioitemnumber2, $barcode2, 'Music' );
280 $insert_sth->execute( $biblionumber3, $biblioitemnumber3, $barcode3, 'Book' );
286 my $item_number1 = $dbh->selectrow_array( $query, {}, $barcode1 );
287 my $item_number2 = $dbh->selectrow_array( $query, {}, $barcode2 );
288 my $item_number3 = $dbh->selectrow_array( $query, {}, $barcode3 );
290 # ---------- Add 2 auth_header
292 INSERT INTO auth_header
295 $insert_sth = $dbh->prepare($query);
296 $insert_sth->execute('authtypecode1');
297 $insert_sth->execute('authtypecode2');
302 WHERE authtypecode = ?';
303 my $authid1 = $dbh->selectrow_array( $query, {}, 'authtypecode1' );
304 my $authid2 = $dbh->selectrow_array( $query, {}, 'authtypecode2' );
306 # ---------- Add 1 old_issues
308 INSERT INTO old_issues
309 (borrowernumber, branchcode, itemnumber)
311 $insert_sth = $dbh->prepare($query);
312 $insert_sth->execute( $borrowernumber1, $branchcode, $item_number1 );
317 WHERE borrowernumber = ?';
318 my $issue_id1 = $dbh->selectrow_array( $query, {}, $borrowernumber1 );
320 # ---------- Add 1 old_reserves
321 AddReserve( $branchcode, $borrowernumber1, $biblionumber1, 'a', '', 1, undef, undef, '', 'Title', undef, undef );
322 my $reserves1 = GetReservesFromBiblionumber( { biblionumber => $biblionumber1 } );
323 my $reserve_id1 = $reserves1->[0]->{reserve_id};
324 my $reserve1 = CancelReserve( { reserve_id => $reserve_id1 } );
326 # ---------- Add 1 biblio, 1 subscription and 1 aqorder
327 my $builder = t::lib::TestBuilder->new();
328 $builder->clear( { source => 'Aqorder' } );
329 my $order1 = $builder->build(
330 { source => 'Aqorder',
331 value => { datecancellationprinted => undef, },
335 my $newordernumber = Koha::Acquisition::Order->new($order1)->insert->{ordernumber};
338 #Change systempreferences values to $set_value
339 sub mocking_systempreferences_to_a_set_value {
340 my $set_value = shift;
345 AcqWarnOnDuplicateInvoice
361 AutoCreateAuthorities
362 BiblioAddsAuthorities
364 UseAuthoritiesForTracings
367 IntranetBiblioDefaultView
373 DefaultClassificationSource
374 EasyAnalyticalRecords
381 SpineLabelShowPrintOnBibDetails
382 BlockReturnOfWithdrawnItems
383 CalculateFinesOnReturn
384 AgeRestrictionOverride
387 AllowItemsOnHoldCheckout
388 AllowNotForLoanOverride
389 AllowRenewalLimitOverride
393 AutoRemoveOverduesRestrictions
396 HomeOrHoldingBranchReturn
397 InProcessingToShelvingCart
400 ManInvInNoissuesCharge
404 RentalsInNoissuesCharge
407 TransfersMaxDaysWarning
408 UseBranchTransferLimits
410 UseTransportCostMatrix
413 FinesIncludeGracePeriod
415 RefundLostItemFeeOnReturn
416 WhenLostChargeReplacementFee
418 AllowHoldDateInFuture
419 AllowHoldPolicyOverride
420 AllowHoldsOnDamagedItems
421 AllowHoldsOnPatronsPossessions
422 AutoResumeSuspendedHolds
423 canreservefromotherbranches
424 decreaseLoanHighHolds
425 DisplayMultiPlaceHold
426 emailLibrarianWhenHoldIsPlaced
427 ExpireReservesMaxPickUpDelay
428 OPACAllowHoldDateInFuture
429 OPACAllowUserToChooseBranch
430 ReservesControlBranch
434 TransferWhenCancelAllWaitingHolds
435 AllowAllMessageDeletion
436 AllowOfflineCirculation
438 CircAutoPrintQuickSlip
439 DisplayClearScreenButton
440 FilterBeforeOverdueReport
442 itemBarcodeFallbackSearch
443 itemBarcodeInputFilter
444 previousIssuesDefaultSortOrder
445 RecordLocalUseOnReturn
448 todaysIssuesDefaultSortOrder
449 UpdateTotalIssuesOnCirc
451 WaitingNotifyAtCheckin
452 AllowSelfCheckReturns
457 OPACAmazonCoverImages
462 IDreamBooksReadometer
465 LibraryThingForLibrariesEnabled
468 NovelistSelectEnabled
474 CalendarFirstDayOfWeek
484 AuthorisedValueImages
489 HighlightOwnItemsOnOPAC
490 OpacAddMastheadLibraryPulldown
491 OPACDisplay856uAsImage
499 OpacShowFiltersPulldownMobile
500 OPACShowHoldQueueDetails
501 OpacShowLibrariesPulldownMobile
502 OpacShowRecentComments
503 OPACShowUnusedAuthorities
506 OPACURLOpenInNewWindow
518 OPACPopupAuthorsSearch
528 AllowPurchaseSuggestionBranchChoice
529 OpacAllowPublicListCreation
530 OpacAllowSharingPrivateLists
533 OPACViewOthersSuggestions
537 EnableOpacSearchHistory
541 PatronSelfRegistration
544 AutoEmailPrimaryAddress
546 BorrowerRenewalPeriodBase
549 EnhancedMessagingPreferences
550 ExtendedPatronAttributes
551 intranetreadinghistory
554 TalkingTechItivaPhoneNotification
556 IncludeSeeFromInSearches
562 TraceCompleteSubfields
563 TraceSubjectSubdivisions
569 OPACItemsResultsDisplay
571 IntranetNumbersPreferPhrase
572 OPACNumbersPreferPhrase
574 RenewSerialAddsSuggestion
575 RoutingListAddReserves
580 StaffAuthorisedValueImages
585 StaffDetailItemSelection
594 t::lib::Mocks::mock_preference( $_, $set_value );
598 #Test if all systempreferences are at $value_to_test
599 sub verif_systempreferences_values {
600 my ( $report, $value_to_test ) = @_;
602 foreach my $key ( keys $report->{systempreferences} ) {
603 is( $report->{systempreferences}->{$key}, $value_to_test, "\$report->{systempreferences}->{$key} = $value_to_test" );