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>.
19 use Test::More tests => 4;
21 use t::lib::Mocks qw(mock_preference);
22 use t::lib::TestBuilder;
24 use POSIX qw(strftime);
26 use C4::Reserves qw(AddReserve);
27 use Koha::Authorities;
31 use Koha::Old::Checkouts;
36 use_ok('C4::UsageStats', qw( NeedUpdate BuildReport ReportToCommunity _count ));
47 my $builder = t::lib::TestBuilder->new;
48 my $schema = Koha::Database->new->schema;
50 subtest 'NeedUpdate() tests' => sub {
54 #Mocking C4::Context->preference("UsageStatsLastUpdateTime") to 0
55 my $now = strftime( "%s", localtime );
56 t::lib::Mocks::mock_preference( "UsageStatsLastUpdateTime", 0 );
58 my $update = C4::UsageStats->NeedUpdate;
59 is( $update, 1, "There is no last update, update needed" );
61 #Mocking C4::Context->preference("UsageStatsLastUpdateTime") to now
62 $now = strftime( "%s", localtime );
63 t::lib::Mocks::mock_preference( "UsageStatsLastUpdateTime", $now );
65 $update = C4::UsageStats->NeedUpdate;
66 is( $update, 0, "Last update just be done, no update needed " );
69 subtest 'BuildReport() tests' => sub {
73 $schema->storage->txn_begin;
75 # make sure we have some data for each 'volumetry' key
76 my $category = $builder->build_object( { class => 'Koha::Patron::Categories' } );
77 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
78 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
79 my $biblio = $builder->build_sample_biblio();
80 my $item = $builder->build_sample_item();
81 $builder->build_object( { class => 'Koha::Old::Holds' } );
82 $builder->build_object( { class => 'Koha::Old::Checkouts' } );
84 t::lib::Mocks::mock_preference( "UsageStatsID", 0 );
85 t::lib::Mocks::mock_preference( "UsageStatsLibraryName", 0 );
86 t::lib::Mocks::mock_preference( "UsageStatsLibrariesInfo", 0 );
87 t::lib::Mocks::mock_preference( "UsageStatsLibraryType", 0 );
88 t::lib::Mocks::mock_preference( "UsageStatsCountry", 0 );
89 t::lib::Mocks::mock_preference( "UsageStatsLibraryUrl", 0 );
91 my $report = C4::UsageStats->BuildReport();
93 isa_ok( $report, 'HASH', '$report is a HASH' );
94 isa_ok( $report->{libraries}, 'ARRAY', '$report->{libraries} is an ARRAY' );
95 is( scalar( @{ $report->{libraries} } ), 0, "There are 0 fields in libraries, libraries info are not shared" );
96 is( $report->{installation}->{koha_id}, 0, "UsageStatsID is good" );
97 is( $report->{installation}->{name}, '', "UsageStatsLibraryName is good" );
98 is( $report->{installation}->{url}, '', "UsageStatsLibraryUrl is good" );
99 is( $report->{installation}->{type}, '', "UsageStatsLibraryType is good" );
100 is( $report->{installation}->{country}, '', "UsageStatsCountry is good" );
101 is( $report->{installation}->{geolocation}, '', "UsageStatsGeolocation is good" );
104 t::lib::Mocks::mock_preference( "UsageStatsID", 1 );
105 t::lib::Mocks::mock_preference( "UsageStatsLibraryName", 'NAME' );
106 t::lib::Mocks::mock_preference( "UsageStatsLibraryUrl", 'URL' );
107 t::lib::Mocks::mock_preference( "UsageStatsLibraryType", 'TYPE' );
108 t::lib::Mocks::mock_preference( "UsageStatsCountry", 'COUNTRY' );
109 t::lib::Mocks::mock_preference( "UsageStatsLibrariesInfo", 1 );
110 t::lib::Mocks::mock_preference( "UsageStatsGeolocation", 1 );
112 $report = C4::UsageStats->BuildReport();
114 isa_ok( $report, 'HASH', '$report is a HASH' );
115 isa_ok( $report->{libraries}, 'ARRAY', '$report->{libraries} is an ARRAY' );
116 is( scalar( @{ $report->{libraries} } ), Koha::Libraries->count, "There are 6 fields in $report->{libraries}" );
117 is( $report->{installation}->{koha_id}, 1, "UsageStatsID is good" );
118 is( $report->{installation}->{name}, 'NAME', "UsageStatsLibraryName is good" );
119 is( $report->{installation}->{url}, 'URL', "UsageStatsLibraryUrl is good" );
120 is( $report->{installation}->{type}, 'TYPE', "UsageStatsLibraryType is good" );
121 is( $report->{installation}->{country}, 'COUNTRY', "UsageStatsCountry is good" );
122 is( $report->{installation}->{geolocation}, '1', "UsageStatsGeolocation is good" );
123 ok( exists $report->{systempreferences}, 'systempreferences is present' );
125 isa_ok( $report, 'HASH', '$report is a HASH' );
126 isa_ok( $report->{volumetry}, 'HASH', '$report->{volumetry} is a HASH' );
127 is( scalar( keys %{ $report->{volumetry} } ), 8, "There are 8 fields in 'volumetry'" );
128 is( $report->{volumetry}->{biblio}, Koha::Biblios->count, "Biblios count correct" );
129 is( $report->{volumetry}->{items}, Koha::Items->count, "Items count correct" );
130 is( $report->{volumetry}->{auth_header}, Koha::Authorities->count, "Authorities count correct" );
131 is( $report->{volumetry}->{old_issues}, Koha::Old::Checkouts->count, "Old checkouts count correct" );
132 is( $report->{volumetry}->{old_reserves}, Koha::Old::Holds->count, "Old holds count correct" );
133 is( $report->{volumetry}->{borrowers}, Koha::Patrons->count, "Patrons count correct" );
134 is( $report->{volumetry}->{aqorders}, Koha::Acquisition::Orders->count, "Orders count correct" );
135 is( $report->{volumetry}->{subscription}, Koha::Subscriptions->count, "Suscriptions count correct" );
137 $schema->storage->txn_rollback;