From 8a1477557e8832c68583b1fac8d662f6e056bc1e Mon Sep 17 00:00:00 2001 From: Matt Blenkinsop Date: Fri, 1 Mar 2024 10:29:40 +0000 Subject: [PATCH] Bug 36093: Add unit test and fix QA tests prove t/db_dependent/api/v1/erm_custom_reports.t Signed-off-by: Marcel de Rooy Signed-off-by: Katrin Fischer --- Koha/REST/V1/ERM/EUsage/CustomReports.pm | 52 ++++++++------- t/db_dependent/api/v1/erm_custom_reports.t | 78 +++++++++++++++++++++- 2 files changed, 105 insertions(+), 25 deletions(-) diff --git a/Koha/REST/V1/ERM/EUsage/CustomReports.pm b/Koha/REST/V1/ERM/EUsage/CustomReports.pm index 9835ac1f4e..73b780ff3b 100644 --- a/Koha/REST/V1/ERM/EUsage/CustomReports.pm +++ b/Koha/REST/V1/ERM/EUsage/CustomReports.pm @@ -50,7 +50,7 @@ sub monthly_report { return try { my $json = JSON->new; - my $args = $json->decode($c->param('q')); + my $args = $json->decode( $c->param('q') ); my @query_params_array; if ( ref($args) eq 'ARRAY' ) { @@ -66,8 +66,7 @@ sub monthly_report { my $usage_data_providers = Koha::ERM::EUsage::UsageDataProviders->search( {}, {} )->unblessed; - my $metric_types = - $query_params_array[0]->{'erm_usage_muses.metric_type'}; + my $metric_types = $query_params_array[0]->{'erm_usage_muses.metric_type'}; my $access_types = $query_params_array[0]->{'erm_usage_muses.access_type'} ? $query_params_array[0]->{'erm_usage_muses.access_type'} @@ -213,8 +212,9 @@ sub metric_types_report { push @{$data}, $missing_result if $missing_result; } - my @metric_types = ('metric_types_report'); # Dummy value to ensure the loop triggers at least once in _create_report_rows - my $report_data = _get_report_data( + my @metric_types = + ('metric_types_report'); # Dummy value to ensure the loop triggers at least once in _create_report_rows + my $report_data = _get_report_data( { data_type => $data_type, data => $data, @@ -257,11 +257,10 @@ sub provider_rollup_report { my $usage_data_providers_set = Koha::ERM::EUsage::UsageDataProviders->new; my $usage_data_providers = $c->objects->search($usage_data_providers_set); - my $data_type = $c->param('data_type'); - my $key = 'erm_usage_' . $data_type . 's'; - my $metric_types = - $query_params_array[0]->{ $key . '.erm_usage_muses.metric_type' }; - + my $data_type = $c->param('data_type'); + my $key = 'erm_usage_' . $data_type . 's'; + my $metric_types = $query_params_array[0]->{ $key . '.erm_usage_muses.metric_type' }; + my @usage_data_provider_report_data; for my $usage_data_provider ( @{$usage_data_providers} ) { @@ -276,7 +275,8 @@ sub provider_rollup_report { grep { $metric_type eq $_->{metric_type} } @$statistics; my @usage_counts = map { $_->{usage_count} } @filtered_statistics; - my $sum = scalar(@usage_counts) > 0 + my $sum = + scalar(@usage_counts) > 0 ? _get_usage_total( \@usage_counts ) : 0; @@ -299,7 +299,7 @@ sub provider_rollup_report { map { $_->{usage_total} } @filtered_object_data; my $provider_rollup_total = scalar(@data_object_usage_totals) > 0 - ? _get_usage_total(\@data_object_usage_totals) + ? _get_usage_total( \@data_object_usage_totals ) : 0; my %usage_data_provider_hash = ( @@ -405,13 +405,15 @@ sub _get_result_with_no_statistics { if ( !$check_result ) { my $missing_result = _get_missing_data( $data_type, $id ); my @blank_statistics = (); - return _get_object_hash({ - data_type => $data_type, - data_object => $missing_result, - statistics => \@blank_statistics, - provider => '', - metric_type => '', - }); + return _get_object_hash( + { + data_type => $data_type, + data_object => $missing_result, + statistics => \@blank_statistics, + provider => '', + metric_type => '', + } + ); } return 0; } @@ -623,7 +625,8 @@ sub _create_report_rows { grep { $metric_type eq $_->{metric_type} } @stats_by_access_type; my @usage_counts = map { $_->{usage_count} } @stats_by_metric_type; - my $sum = $period eq 'monthly' && scalar(@usage_counts) > 0 + my $sum = + $period eq 'monthly' && scalar(@usage_counts) > 0 ? _get_usage_total( \@usage_counts ) : 0; @@ -652,8 +655,9 @@ sub _create_report_rows { : @usage_stats; my @usage_counts = map { $_->{usage_count} } @stats_by_metric_type; - my $sum = $period eq 'monthly' && scalar(@usage_counts) > 0 - ? _get_usage_total(\@usage_counts) + my $sum = + $period eq 'monthly' && scalar(@usage_counts) > 0 + ? _get_usage_total( \@usage_counts ) : 0; my $data_object_hash = _get_object_hash( @@ -681,14 +685,14 @@ A method for summing the usage counts for a data object =cut sub _get_usage_total { - my ( $statistics ) = @_; + my ($statistics) = @_; my $sum = 0; foreach my $statistic (@$statistics) { $sum += $statistic; } - return $sum + return $sum; } 1; diff --git a/t/db_dependent/api/v1/erm_custom_reports.t b/t/db_dependent/api/v1/erm_custom_reports.t index d5658cb35a..5c64565063 100755 --- a/t/db_dependent/api/v1/erm_custom_reports.t +++ b/t/db_dependent/api/v1/erm_custom_reports.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 3; +use Test::More tests => 4; use Test::Mojo; use t::lib::TestBuilder; @@ -335,3 +335,79 @@ subtest "metric_types_report" => sub { $schema->storage->txn_rollback; }; + +subtest "provider_rollup_report" => sub { + plan tests => 9; + + $schema->storage->txn_begin; + + Koha::ERM::EUsage::MonthlyUsages->search->delete; + + # Run a harvest to populate the database with data + my $usage_data_provider = $builder->build_object( { class => 'Koha::ERM::EUsage::UsageDataProviders' } ); + my $counter_file = $sushi_counter_TR_J1->get_COUNTER_from_SUSHI; + + $usage_data_provider->counter_files( + [ + { + usage_data_provider_id => $usage_data_provider->erm_usage_data_provider_id, + file_content => $counter_file, + date_uploaded => POSIX::strftime( "%Y%m%d%H%M%S", localtime ), + filename => "Test_TR_J1", + } + ] + ); + + my $librarian = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 2**28 } + } + ); + my $password = 'thePassword123'; + $librarian->set_password( { password => $password, skip_validation => 1 } ); + my $userid = $librarian->userid; + + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 0 } + } + ); + + $patron->set_password( { password => $password, skip_validation => 1 } ); + my $unauth_userid = $patron->userid; + + # Unauthorized access + $t->get_ok("//$unauth_userid:$password@/api/v1/erm/eUsage/provider_rollup_report/title")->status_is(403); + + # Authorised access + my $query_string_with_no_results = 'q=[ + { + "erm_usage_titles.erm_usage_muses.year":2023, + "erm_usage_titles.erm_usage_muses.report_type":"TR_J1", + "erm_usage_titles.erm_usage_muses.month":[1,2,3,4,5,6,7,8,9,10,11,12], + "erm_usage_titles.erm_usage_muses.metric_type":["Total_Item_Requests","Unique_Item_Requests"] + } + ]'; + + $t->get_ok( "//$userid:$password@/api/v1/erm/eUsage/provider_rollup_report/title?$query_string_with_no_results" => + { 'x-koha-embed' => 'erm_usage_titles.erm_usage_muses' } )->status_is(200)->json_is( [] ); + + my $query_string = 'q=[ + { + "erm_usage_titles.erm_usage_muses.year":2022, + "erm_usage_titles.erm_usage_muses.report_type":"TR_J1", + "erm_usage_titles.erm_usage_muses.month":[8], + "erm_usage_titles.erm_usage_muses.metric_type":["Total_Item_Requests","Unique_Item_Requests"] + } + ]'; + my $expected_results1 = 2; # One provider repeated once for each metric type + + $t->get_ok( "//$userid:$password@/api/v1/erm/eUsage/provider_rollup_report/title?$query_string" => + { 'x-koha-embed' => 'erm_usage_titles.erm_usage_muses' } )->status_is(200) + ->json_is( '/' . ( $expected_results1 - 2 ) . '/provider_rollup_total' => 2, 'Total is correct' ) + ->json_is( '/' . ( $expected_results1 - 1 ) . '/provider_rollup_total' => 1, 'Total is correct' ); + + $schema->storage->txn_rollback; +}; -- 2.39.5