From 82a878e789ae421c130690ef03dbe2fb6123592c Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Wed, 20 Sep 2017 14:55:53 +0000 Subject: [PATCH] Bug 17047 - [QA Followup] - Fix untranslatable strings - Remove onclick html attribute - Remove useless passing of mana_id variable in serials-toolbar.inc - Remove using CGI::param in list context - remove new subs from serials/subscription-add.pl - remove useless else - Explicitly list possible template in svc/mana/search - Remove useless library imports - Add unit tests for Koha/SharedContent.pm + code refactoring - Add mana config in debian/templates/koha-conf-site.xml.in - Rename getToken and addvaluetofield svc/mana scripts - Restore unit tests in t/db_dependent/Serials/GetFictiveIssueNumber.t - Fix indentation - QA stuff --- Koha/SharedContent.pm | 150 +++++----- Koha/Subscription/Numberpatterns.pm | 32 ++ debian/templates/koha-conf-site.xml.in | 3 + koha-tmpl/intranet-tmpl/lib/jquery/activatemana.js | 2 +- .../en/includes/mana/mana-report-search-result.inc | 31 +- .../mana/mana-subscription-search-result.inc | 30 +- .../prog/en/includes/reports-toolbar.inc | 4 +- .../prog/en/includes/serials-toolbar.inc | 10 +- .../en/modules/reports/guided_reports_start.tt | 9 +- .../prog/en/modules/serials/subscription-add.tt | 29 +- .../prog/en/modules/serials/subscription-detail.tt | 2 +- reports/guided_reports.pl | 4 +- serials/subscription-add.pl | 62 ++-- serials/subscription-detail.pl | 6 +- svc/mana/{addvaluetofield => increment} | 8 +- svc/mana/search | 6 +- svc/mana/share | 11 +- svc/mana/{getToken => token} | 0 svc/mana/use | 5 +- t/db_dependent/Koha/SharedContent.t | 224 +++++++++++++- t/db_dependent/Koha/Subscription/Numberpatterns.t | 97 ++++++ t/db_dependent/Serials/GetFictiveIssueNumber.t | 332 ++++++++++++++------- 22 files changed, 771 insertions(+), 286 deletions(-) rename svc/mana/{addvaluetofield => increment} (85%) rename svc/mana/{getToken => token} (100%) create mode 100644 t/db_dependent/Koha/Subscription/Numberpatterns.t diff --git a/Koha/SharedContent.pm b/Koha/SharedContent.pm index 30619d4..c54a8e1 100644 --- a/Koha/SharedContent.pm +++ b/Koha/SharedContent.pm @@ -52,15 +52,15 @@ sub manaRequest { $mana_request->content( to_json($content) ); } - my $response = $userAgent->request($mana_request); + my $response = $userAgent->request($mana_request); eval { $result = from_json( $response->decoded_content, { utf8 => 1} ); }; $result->{code} = $response->code; if ( $@ ){ - $result->{msg} = $response->{_msg}; + $result->{msg} = $@; } if ($response->is_error){ - $result->{msg} = "An error occurred, mana server returned: ".$result->{msg}; + $result->{msg} = "An error occurred, mana server returned: " . $response->message; } return $result ; } @@ -70,21 +70,7 @@ sub manaRequest { =cut sub manaIncrementRequest { - my $resource = shift; - my $id = shift; - my $field = shift; - my $step = shift; - my $param; - $param->{step} = $step || 1; - $param->{id} = $id; - $param->{resource} = $resource; - $param = join '&', - map { defined $param->{$_} ? $_ . "=" . $param->{$_} : () } - keys %$param; - my $url = "$MANA_IP/$resource/$id.json/increment/$field?$param"; - my $request = HTTP::Request->new( POST => $url ); - - return manaRequest($request); + return manaRequest(buildRequest('increment', @_)); } =head3 manaPostRequest @@ -92,68 +78,52 @@ sub manaIncrementRequest { =cut sub manaPostRequest { - my $resource = shift; - my $content = shift; + my ($lang, $loggedinuser, $resourceid, $resourcetype) = @_; + + my $content = prepareSharedData($lang, $loggedinuser, $resourceid, $resourcetype); - my $url = "$MANA_IP/$resource.json"; - my $request = HTTP::Request->new( POST => $url ); + my $result = manaRequest(buildRequest('post', $resourcetype, $content)); - $content->{bulk_import} = 0; - my $json = to_json( $content, { utf8 => 1 } ); - $request->content($json); - return manaRequest($request); + if ( $result and ($result->{code} eq "200" or $result->{code} eq "201") ) { + my $packages = "Koha::".ucfirst($resourcetype)."s"; + my $resource = $packages->find($resourceid); + eval { $resource->set( { mana_id => $result->{id} } )->store }; + } + return $result; } -=head3 manaShareInfos +=head3 prepareSharedData =cut -sub manaShareInfos{ - my ($query, $loggedinuser, $ressourceid, $ressourcetype) = @_; - my $mana_language; - if ( $query->param('mana_language') ) { - $mana_language = $query->param('mana_language'); - } - else { - my $result = $mana_language = C4::Context->preference('language'); - } +sub prepareSharedData { + my ($lang, $loggedinuser, $ressourceid, $ressourcetype) = @_; + $lang ||= C4::Context->preference('language'); my $mana_email; if ( $loggedinuser ne 0 ) { my $borrower = Koha::Patrons->find($loggedinuser); - if ($borrower){ - $mana_email = $borrower->email - if ( ( not defined($mana_email) ) or ( $mana_email eq '' ) ); - $mana_email = $borrower->emailpro - if ( ( not defined($mana_email) ) or ( $mana_email eq '' ) ); - $mana_email = - Koha::Libraries->find( C4::Context->userenv->{'branch'} )->branchemail - if ( ( not defined($mana_email) ) or ( $mana_email eq '' ) ); - } + $mana_email = $borrower->first_valid_email_address + || Koha::Libraries->find( C4::Context->userenv->{'branch'} )->branchemail } $mana_email = C4::Context->preference('KohaAdminEmailAddress') if ( ( not defined($mana_email) ) or ( $mana_email eq '' ) ); + my %versions = C4::Context::get_versions(); my $mana_info = { - language => $mana_language, + language => $lang, kohaversion => $versions{'kohaVersion'}, exportemail => $mana_email }; - my ($ressource, $ressource_mana_info); + + my $ressource_mana_info; my $packages = "Koha::".ucfirst($ressourcetype)."s"; my $package = "Koha::".ucfirst($ressourcetype); $ressource_mana_info = $package->get_sharable_info($ressourceid); $ressource_mana_info = { %$ressource_mana_info, %$mana_info }; - $ressource = $packages->find($ressourceid); - - my $result = Koha::SharedContent::manaPostRequest( $ressourcetype, - $ressource_mana_info ); - if ( $result and ($result->{code} eq "200" or $result->{code} eq "201") ) { - eval { $ressource->set( { mana_id => $result->{id} } )->store }; - } - return $result; + return $ressource_mana_info; } =head3 manaGetRequestWithId @@ -161,13 +131,7 @@ sub manaShareInfos{ =cut sub manaGetRequestWithId { - my $resource = shift; - my $id = shift; - - my $url = "$MANA_IP/$resource/$id.json"; - my $request = HTTP::Request->new( GET => $url ); - - return manaRequest($request); + return manaRequest(buildRequest('getwithid', @_)); } =head3 manaGetRequest @@ -175,16 +139,62 @@ sub manaGetRequestWithId { =cut sub manaGetRequest { - my $resource = shift; - my $parameters = shift; + return manaRequest(buildRequest('get', @_)); +} + +=head3 buildRequest - $parameters = join '&', - map { defined $parameters->{$_} ? $_ . "=" . $parameters->{$_} : () } - keys %$parameters; - my $url = "$MANA_IP/$resource.json?$parameters"; - my $request = HTTP::Request->new( GET => $url ); +=cut + +sub buildRequest { + my $type = shift; + my $resource = shift; - return manaRequest($request); + if ( $type eq 'get' ) { + my $params = shift; + $params = join '&', + map { defined $params->{$_} ? $_ . "=" . $params->{$_} : () } + keys %$params; + my $url = "$MANA_IP/$resource.json?$params"; + return HTTP::Request->new( GET => $url ); + } + + if ( $type eq 'getwithid' ) { + my $id = shift; + + my $url = "$MANA_IP/$resource/$id.json"; + return HTTP::Request->new( GET => $url ); + } + + if ( $type eq 'post' ) { + my $content = shift; + + my $url = "$MANA_IP/$resource.json"; + my $request = HTTP::Request->new( POST => $url ); + + $content->{bulk_import} = 0; + my $json = to_json( $content, { utf8 => 1 } ); + $request->content($json); + + return $request; + } + + if ( $type eq 'increment' ) { + my $id = shift; + my $field = shift; + my $step = shift; + my $param; + + $param->{step} = $step || 1; + $param->{id} = $id; + $param->{resource} = $resource; + $param = join '&', + map { defined $param->{$_} ? $_ . "=" . $param->{$_} : () } + keys %$param; + my $url = "$MANA_IP/$resource/$id.json/increment/$field?$param"; + my $request = HTTP::Request->new( POST => $url ); + + } } 1; diff --git a/Koha/Subscription/Numberpatterns.pm b/Koha/Subscription/Numberpatterns.pm index 271deb0..440489e 100644 --- a/Koha/Subscription/Numberpatterns.pm +++ b/Koha/Subscription/Numberpatterns.pm @@ -52,6 +52,38 @@ sub uniqueLabel { return $label; } +=head3 new_or_existing + +=cut + +sub new_or_existing { + my ($self, $params) = @_; + + my $params_np; + if ( $params->{'numbering_pattern'} eq 'mana' ) { + foreach (qw/numberingmethod label1 add1 every1 whenmorethan1 setto1 + numbering1 label2 add2 every2 whenmorethan2 setto2 numbering2 + label3 add3 every3 whenmorethan3 setto3 numbering3/) { + $params_np->{$_} = $params->{$_} if $params->{$_}; + } + + my $existing = Koha::Subscription::Numberpatterns->search($params_np)->next(); + + if ($existing) { + return $existing->id; + } + + $params_np->{label} = Koha::Subscription::Numberpatterns->uniqueLabel($params->{'patternname'}); + $params_np->{description} = $params->{'sndescription'}; + + + my $subscription_np = Koha::Subscription::Numberpattern->new()->set($params_np)->store(); + return $subscription_np->id; + } + + return $params->{'numbering_pattern'}; +} + =head3 type =cut diff --git a/debian/templates/koha-conf-site.xml.in b/debian/templates/koha-conf-site.xml.in index 8e8204f..4c60ab8 100644 --- a/debian/templates/koha-conf-site.xml.in +++ b/debian/templates/koha-conf-site.xml.in @@ -269,6 +269,9 @@ __END_SRU_PUBLICSERVER__ __LOG_DIR__ /usr/share/doc/koha-common /var/spool/koha/__KOHASITE__ + + + https://mana-kb.koha-community.org diff --git a/koha-tmpl/intranet-tmpl/lib/jquery/activatemana.js b/koha-tmpl/intranet-tmpl/lib/jquery/activatemana.js index b1cb9c9..49adfdd 100644 --- a/koha-tmpl/intranet-tmpl/lib/jquery/activatemana.js +++ b/koha-tmpl/intranet-tmpl/lib/jquery/activatemana.js @@ -5,7 +5,7 @@ $(document).ready(function(){ var myemail = $("#email").val() $.ajax( { type: "POST", - url: "/cgi-bin/koha/svc/mana/getToken", + url: "/cgi-bin/koha/svc/mana/token", data: { lastname: mylastname, firstname: myfirstname, email: myemail}, dataType: "json", }) diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/mana/mana-report-search-result.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/mana/mana-report-search-result.inc index 2de9fea..312af44 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/mana/mana-report-search-result.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/mana/mana-report-search-result.inc @@ -3,6 +3,28 @@ [% USE AuthorisedValues %] [% USE Branches %] + + [% IF statuscode == "200" AND reports %] @@ -45,14 +67,13 @@ [% UNLESS search_only %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/mana/mana-subscription-search-result.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/mana/mana-subscription-search-result.inc index e269237..30e10ca 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/mana/mana-subscription-search-result.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/mana/mana-subscription-search-result.inc @@ -3,6 +3,27 @@ [% USE AuthorisedValues %] [% USE Branches %] + [% IF statuscode == "200" %]
- - - [% FOREACH comment IN report.comments %] - [% END %] -
@@ -43,14 +64,13 @@ [% UNLESS search_only %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/reports-toolbar.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/reports-toolbar.inc index fd45b11..9e5e6ba 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/reports-toolbar.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/reports-toolbar.inc @@ -116,7 +116,7 @@ $(document).ready(function(){ function mana_increment(mana_id, resourcename, fieldvalue, stepvalue = 1){ $.ajax( { type: "POST", - url: "/cgi-bin/koha/svc/mana/addvaluetofield", + url: "/cgi-bin/koha/svc/mana/increment", data: {id: mana_id, field: fieldvalue, resource: resourcename, step: stepvalue}, datatype: "json", }).done( function() { @@ -151,7 +151,7 @@ $(document).ready(function(){ }) .done( function( result ) { $("#mana_search_result .modal-body").html(result); - $("#mana_search_result_label").text("Results from Mana Knowledge Base"); + $("#mana_search_result_label").text(_("Results from Mana Knowledge Base")); $("#mana_results_datatable").dataTable($.extend(true, {}, dataTablesDefaults,{ "sPaginationType":"four_button", "autoWidth": false, diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/serials-toolbar.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/serials-toolbar.inc index aa19207..dbddfbe 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/serials-toolbar.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/serials-toolbar.inc @@ -13,9 +13,6 @@ window.location="subscription-detail.pl?subscriptionid=[% subscriptionid %]&op=reopen"; } } - function share() { - window.location="subscription-detail.pl?subscriptionid=[% subscriptionid %]&op=share"; - } $(document).ready(function() { $("#deletesub").click(function(){ @@ -34,6 +31,9 @@ popup([% subscriptionid %]); return false; }) + $("#mana-subscription-share").click(function() { + window.location="subscription-detail.pl?subscriptionid=[% subscriptionid %]&op=share"; + }); }); //]]> @@ -50,7 +50,7 @@ [% IF one_language_enabled==0 or mana_id %]
Share
[% ELSE %] -
Share
+
Share
[% END %] [% END %] [% END %] @@ -200,7 +200,7 @@ [% IF one_language_enabled==0 %] [% ELSE %] -
Share
+
Share
[% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/reports/guided_reports_start.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/reports/guided_reports_start.tt index 9a6920c..569c88f 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/reports/guided_reports_start.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/reports/guided_reports_start.tt @@ -74,10 +74,6 @@ function load_group_subgroups () { } } -function share() { - window.location="/cgi-bin/koha/reports/guided_reports.pl?phase=Save"; -} - $(document).ready(function(){ $('#limit').change(function() { $('#limitselect').submit(); @@ -285,6 +281,9 @@ $("#delColumn").on("click",function(){ load_group_subgroups(); }); [% END %] + $('div#ManaShareButton').click(function() { + window.location="/cgi-bin/koha/reports/guided_reports.pl?phase=Share"; + }); }); @@ -668,7 +667,7 @@ canned reports and writing custom SQL reports.

[% IF one_language_enabled==0 %] [% ELSE %] -
Share
+
Share
[% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt index 87ee2b3..061b115 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt @@ -406,7 +406,7 @@ function show_page_2() { } function mana_search() { - $("#mana_search").html("

Mana kb is being asked for your subscription..

"); + $("#mana_search").html("

" + _("Mana kb is being asked for your subscription..") + "

"); $("#mana_search").show(); $.ajax({ @@ -417,7 +417,7 @@ function mana_search() { }) .done( function( result ) { $("#mana_search_result .modal-body").html(result); - $("#mana_search_result_label").text("Results from Mana Knowledge Base"); + $("#mana_search_result_label").text(_("Results from Mana Knowledge Base")); $("#mana_results_datatable").dataTable($.extend(true, {}, dataTablesDefaults, { "sPaginationType": "four_button", "order":[[4, "desc"], [5, "desc"]], @@ -432,10 +432,10 @@ function mana_search() { ] })); if( $("#mana_results_datatable").length && $("td.dataTables_empty").length == 0){ - $("#mana_search").html("

Subscription found on Mana Knowledge Base:

Quick fill

"); + $("#mana_search").html("

" + _("Subscription found on Mana Knowledge Base:") + "

" + _("Quick fill") + "

"); } else if ( $("#mana_results_datatable").length ){ - $("#mana_search").html("

No subscription found on Mana Knowledge Base :(

Please feel free to share you pattern with all others librarians once you are done

"); + $("#mana_search").html("

" + _("No subscription found on Mana Knowledge Base :(") + "

" + _(" Please feel free to share you pattern with all others librarians once you are done") + "

"); } else{ $("#mana_search").html( result ); @@ -468,10 +468,7 @@ function mana_search() { mana_increment($("#selectedcomment").val(), 'resource_comment', 'nb', -1); }); - }).fail(function(result, msg, longmsg){ - console.log( msg ); - console.log( longmsg ); - }); + }) } function mana_comment( target_id, manamsg, resource_type ){ @@ -486,7 +483,7 @@ function mana_comment( target_id, manamsg, resource_type ){ function mana_increment(mana_id, resource, fieldvalue, stepvalue = 1){ $.ajax( { type: "POST", - url: "/cgi-bin/koha/svc/mana/addvaluetofield", + url: "/cgi-bin/koha/svc/mana/increment", data: {id: mana_id, resource: resource, field: fieldvalue, step: stepvalue}, datatype: "json", }) @@ -567,16 +564,8 @@ function mana_use(mana_id){ }); } -function removeDisabledAttr() { - $('select:disabled').removeAttr('disabled'); -} - - $(document).ready(function() { - - - mana_search(); $("#displayexample").hide(); $("#mana_search_result").modal("hide"); @@ -716,7 +705,9 @@ $(document).ready(function() { e.preventDefault(); testPredictionPattern(); }); - + $('#save-subscription').on("click", function(e){ + $('select:disabled').removeAttr('disabled'); + }); }); //]]> @@ -1197,7 +1188,7 @@ $(document).ready(function() {
- +
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-detail.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-detail.tt index 185f892..8107ca3 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-detail.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-detail.tt @@ -42,7 +42,7 @@ $(document).ready(function() {
- [% INCLUDE 'serials-toolbar.inc' mana_id = mana_id %] + [% INCLUDE 'serials-toolbar.inc' %]

Subscription for [% bibliotitle%] [% IF closed %](closed)[% END %]

[% IF ( abouttoexpire ) %] diff --git a/reports/guided_reports.pl b/reports/guided_reports.pl index 331d160..fba35b8 100755 --- a/reports/guided_reports.pl +++ b/reports/guided_reports.pl @@ -181,7 +181,7 @@ elsif ( $phase eq 'Show SQL'){ 'notes' => $report->{notes}, 'sql' => $report->{savedsql}, 'showsql' => 1, - 'mana_success' => $input->param('mana_success'), + 'mana_success' => scalar $input->param('mana_success'), ); } @@ -658,7 +658,7 @@ elsif ( $phase eq 'Save Report' ) { } elsif ($phase eq 'Share'){ - my $result = Koha::SharedContent::manaShareInfos($input, $borrowernumber, $input->param('reportid'), 'report'); + my $result = Koha::SharedContent::manaPostRequest($input->param('mana_language'), $borrowernumber, scalar $input->param('reportid'), 'report'); if ( $result ) { print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved&manamsg=".$result->{msg}); }else{ diff --git a/serials/subscription-add.pl b/serials/subscription-add.pl index c7b8f94..1972016 100755 --- a/serials/subscription-add.pl +++ b/serials/subscription-add.pl @@ -290,35 +290,9 @@ sub _guess_enddate { return $enddate; } -sub manage_subscription_numbering_pattern_id { - my $params; - if ( $query->param('numbering_pattern') eq 'mana' ) { - foreach (qw/numberingmethod label1 add1 every1 whenmorethan1 setto1 - numbering1 label2 add2 every2 whenmorethan2 setto2 numbering2 - label3 add3 every3 whenmorethan3 setto3 numbering3/) { - $params->{$_} = $query->param($_) if $query->param($_); - } - - my $existing = Koha::Subscription::Numberpatterns->search($params)->next(); - - if ($existing) { - return $existing->id; - } - - $params->{label} = Koha::Subscription::Numberpatterns->uniqueLabel($query->param('patternname')); - $params->{description} = $query->param('sndescription'); - - - my $subscription_np = Koha::Subscription::Numberpattern->new()->set($params)->store(); - return $subscription_np->id; - } - - return $query->param('numbering_pattern'); -} - -sub manage_subscription_frequencies_id { - my $periodicity; - if ( $query->param('frequency') eq 'mana' ) { +sub redirect_add_subscription { + my $periodicity = $query->param('frequency'); + if ($periodicity eq 'mana') { my $subscription_freq = Koha::Subscription::Frequency->new()->set( { description => $query->param('sfdescription'), @@ -329,15 +303,7 @@ sub manage_subscription_frequencies_id { )->store(); $periodicity = $subscription_freq->id; } - else { - $periodicity = $query->param('frequency'); - } - return $periodicity; -} - -sub redirect_add_subscription { - my $periodicity = manage_subscription_frequencies_id(); - my $numberpattern = manage_subscription_numbering_pattern_id(); + my $numberpattern = Koha::Subscription::Numberpatterns->new_or_existing($query->Vars); my $auser = $query->param('user'); my $branchcode = $query->param('branchcode'); @@ -379,9 +345,6 @@ sub redirect_add_subscription { $mana_id = $query->param('mana_id'); Koha::SharedContent::manaIncrementRequest("subscription",$mana_id, "nbofusers"); } - else { - $mana_id = undef; - } my $startdate = output_pref( { str => scalar $query->param('startdate'), dateonly => 1, dateformat => 'iso' } ); my $enddate = output_pref( { str => scalar $query->param('enddate'), dateonly => 1, dateformat => 'iso' } ); @@ -405,7 +368,7 @@ sub redirect_add_subscription { $skip_serialseq, $mana_id ); if ( (C4::Context->preference('Mana')) and ( grep { $_ eq "subscription" } split(/,/, C4::Context->preference('AutoShareWithMana'))) ){ - my $result = Koha::SharedContent::manaShareInfos( $query, $loggedinuser, $subscriptionid, 'subscription'); + my $result = Koha::SharedContent::manaPostRequest( $query->param('mana_language'), $loggedinuser, $subscriptionid, 'subscription'); $template->param( mana_msg => $result->{msg} ); } my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } ); @@ -435,8 +398,19 @@ sub redirect_mod_subscription { ? output_pref( { str => $nextacquidate, dateonly => 1, dateformat => 'iso' } ) : $firstacquidate; - my $periodicity = manage_subscription_frequencies_id(); - my $numberpattern = manage_subscription_numbering_pattern_id(); + my $periodicity = $query->param('frequency'); + if ($periodicity eq 'mana') { + my $subscription_freq = Koha::Subscription::Frequency->new()->set( + { + description => $query->param('sfdescription'), + unit => $query->param('unit'), + unitsperissue => $query->param('unitsperissue'), + issuesperunit => $query->param('issuesperunit'), + } + )->store(); + $periodicity = $subscription_freq->id; + } + my $numberpattern = Koha::Subscription::Numberpatterns->new_or_existing($query->Vars); my $subtype = $query->param('subtype'); my $sublength = $query->param('sublength'); diff --git a/serials/subscription-detail.pl b/serials/subscription-detail.pl index 6c25f7e..ffb458e 100755 --- a/serials/subscription-detail.pl +++ b/serials/subscription-detail.pl @@ -33,11 +33,7 @@ use Koha::Acquisition::Bookseller; use Date::Calc qw/Today Day_of_Year Week_of_Year Add_Delta_Days/; use Carp; -use LWP::UserAgent; use Koha::SharedContent; -use Koha::Patrons; -use Koha::Subscriptions; -use Koha::Libraries; my $query = new CGI; my $op = $query->param('op') || q{}; @@ -100,7 +96,7 @@ if ($op eq 'del') { } } elsif ( $op and $op eq "share" ) { - my $result = Koha::SharedContent::manaShareInfos($query, $loggedinuser, $subscriptionid, 'subscription'); + my $result = Koha::SharedContent::manaPostRequest($query->param('mana_language'), $loggedinuser, $subscriptionid, 'subscription'); $template->param( mana_code => $result->{msg} ); $subs->{mana_id} = $result->{id}; } diff --git a/svc/mana/addvaluetofield b/svc/mana/increment similarity index 85% rename from svc/mana/addvaluetofield rename to svc/mana/increment index 18ae23a..b8e5f8e 100755 --- a/svc/mana/addvaluetofield +++ b/svc/mana/increment @@ -38,5 +38,11 @@ my ( $auth_status, $sessionID ) = if ( $auth_status ne "ok" ) { exit 0; } -my $result = Koha::SharedContent::manaIncrementRequest($input->param('resource'), $input->param('id'), $input->param('field'), $input->param('step') ); +my $result = Koha::SharedContent::manaIncrementRequest( + scalar $input->param('resource'), + scalar $input->param('id'), + scalar $input->param('field'), + scalar $input->param('step') +); + return $result; diff --git a/svc/mana/search b/svc/mana/search index 5ae14b3..96cb56f 100755 --- a/svc/mana/search +++ b/svc/mana/search @@ -39,7 +39,11 @@ if ( $auth_status ne "ok" ) { } my $templatename; -$templatename = "mana/mana-".$input->param( "resource" )."-search-result.tt"; +if ($input->param( "resource" ) eq 'report') { + $templatename = "mana/mana-report-search-result.tt"; +} else { + $templatename = "mana/mana-subscription-search-result.tt"; +} my ( $template, $loggedinuser, $cookie ) = get_template_and_user( { diff --git a/svc/mana/share b/svc/mana/share index 47db3e1..87de9cf 100755 --- a/svc/mana/share +++ b/svc/mana/share @@ -45,11 +45,14 @@ $content->{resource_id} = $input->param("resource_id"); $content->{resource_type} = $input->param("resource"); $content->{message} = $input->param("message"); Koha::SharedContent::manaPostRequest('resource_comment', $content); - my $package = "Koha::".ucfirst($input->param('resource')); - my $resource; - my $result; +my $package = "Koha::".ucfirst($input->param('resource')); +my $resource; +my $result; eval{ - $result = Koha::SharedContent::manaGetRequestWithId($input->param('resource'), $input->param('id') ); + $result = Koha::SharedContent::manaGetRequestWithId( + scalar $input->param('resource'), + scalar $input->param('id') + ); }; if ( $@ or $result->{code} == 500 ){ $resource->{errmsg} = "Error: mana access got broken, please try again later\n\n ( error: $@ )"; diff --git a/svc/mana/getToken b/svc/mana/token similarity index 100% rename from svc/mana/getToken rename to svc/mana/token diff --git a/svc/mana/use b/svc/mana/use index efb5bf3..42966ec 100755 --- a/svc/mana/use +++ b/svc/mana/use @@ -40,7 +40,10 @@ if ( $auth_status ne "ok" ) { exit 0; } -my $result = Koha::SharedContent::manaGetRequestWithId($input->param('resource'), $input->param('id') ); +my $result = Koha::SharedContent::manaGetRequestWithId( + scalar $input->param('resource'), + scalar $input->param('id') +); my $package = "Koha::".ucfirst($input->param('resource')); my $resource; diff --git a/t/db_dependent/Koha/SharedContent.t b/t/db_dependent/Koha/SharedContent.t index 3784028..3925b30 100644 --- a/t/db_dependent/Koha/SharedContent.t +++ b/t/db_dependent/Koha/SharedContent.t @@ -20,10 +20,230 @@ use Modern::Perl; use t::lib::TestBuilder; -use Test::More tests => 1; +use t::lib::Mocks; +use Test::MockModule; +use Test::MockObject; +use Test::More tests => 43; use Koha::Database; +use Koha::Patrons; +use Koha::Subscriptions; + +use HTTP::Status qw(:constants :is status_message); use_ok('Koha::SharedContent'); my $schema = Koha::Database->new->schema; -$schema->storage->txn_begin; # mode insertion +$schema->storage->txn_begin; + +my $builder = t::lib::TestBuilder->new(); + +my $want_error = 0; +my $post_request = 0; +my $query = {}; + +$Koha::SharedContent::MANA_IP = 'https://foo.bar'; +my $result = Koha::SharedContent::manaGetRequest('report', $query); +ok($result->{msg} =~ /Can\'t connect to foo.bar:443$/, 'Unable to connect'); +is($result->{code}, 500, 'Code is 500'); + +my $ua = Test::MockModule->new('LWP::UserAgent'); +$ua->mock('request', sub { + return mock_response(); +}); + +$want_error = 1; +$query = {query => 'foo', usecomments => 1}; +$result = Koha::SharedContent::manaGetRequest('report', $query); +ok($result->{msg} =~ /^Error thrown by decoded_content/, 'Error in decoded_content'); +is($result->{code}, 500, 'Code is 500'); + +$want_error = 0; +$query = {title => 'foo', usecomments => 1}; +$result = Koha::SharedContent::manaGetRequest('subscription', $query); +is($result->{code}, 200, 'manaGetRequest success'); + +$result = Koha::SharedContent::manaGetRequestWithId('subscription', 23); +is($result->{code}, 200, 'manaGetRequestWithId success'); + +my $params = { + title => 'The English historical review', + issn => '0013-8266', + ean => '', + publishercode => 'Longman' +}; + +# Search a subscription. +my $request = Koha::SharedContent::buildRequest('get', 'subscription', $params); +is($request->method, 'GET', 'Get subscription - Method is get'); + +my %query = $request->uri->query_form; +is($query{title}, 'The English historical review', 'Check title'); +is($query{issn}, '0013-8266', 'Check issn'); +is($query{ean}, '', 'Check ean'); +is($query{publishercode}, 'Longman', 'Check publisher'); + +is($request->uri->path, '/subscription.json', 'Path is subscription'); + +# Get a report by id. +$request = Koha::SharedContent::buildRequest('getwithid', 'report', 26); +is($request->method, 'GET', 'Get with id - Method is get'); + +is($request->uri->path, '/report/26.json', 'Path is report/26.json'); + +# Share a report. +my $content = { + 'kohaversion' => '17.06.00.008', + 'language' => 'fr-FR', + 'notes' => 'some notes', + 'report_group' => '', + 'exportemail' => 'xx@xx.com', + 'report_name' => 'A useless report', + 'savedsql' => 'SELECT * FROM ITEMS', + 'type' => undef +}; + +$request = Koha::SharedContent::buildRequest('post', 'report', $content); +is($request->method, 'POST', 'Share report - Method is post'); + +is($request->uri->path, '/report.json', 'Path is report.json'); + +# prepare shared data +my $loggedinuser = $builder->build({ + source => 'Borrower', + value => { + email => '', + emailpro => '', + B_email => '' + } +}); + +my $library = $builder->build({ + source => 'Branch', +}); + +my $biblio = $builder->build({ + source => 'Biblio', +}); + +my $biblioitem = $builder->build({ + source => 'Biblioitem', + value => { + biblionumber => $biblio->{biblionumber} + } +}); + +my $subscriptionFrequency = $builder->build({ + source => 'SubscriptionFrequency' +}); + +my $subscriptionNumberpattern = $builder->build({ + source => 'SubscriptionNumberpattern' +}); + +my $subscription = $builder->build({ + source => 'Subscription', + value => { + biblionumber => $biblio->{biblionumber}, + periodicity => $subscriptionFrequency->{id}, + numberpattern => $subscriptionNumberpattern->{id}, + mana_id => undef + } +}); + +C4::Context->_new_userenv('xxx'); +C4::Context->set_userenv(0,0,0, + $loggedinuser->{firstname}, + $loggedinuser->{surname}, + $library->{branchcode}, + 'Midway Public Library', '', '', ''); + +t::lib::Mocks::mock_preference('language', 'en'); + +$post_request = 1; +$result = Koha::SharedContent::manaPostRequest('en', $loggedinuser->{borrowernumber}, $subscription->{subscriptionid}, 'subscription'); +is($result->{code}, 200, 'manaPostRequest success'); + +my $s = Koha::Subscriptions->find($subscription->{subscriptionid}); +is($s->mana_id, 5, 'Mana id is set'); + +my $data = Koha::SharedContent::prepareSharedData( + '', + $loggedinuser->{borrowernumber}, + $subscription->{subscriptionid}, + 'subscription' +); + +is($data->{language}, 'en', 'Language is set to default'); +my $branch = Koha::Libraries->find($library->{branchcode}); +is($data->{exportemail}, $branch->branchemail, 'Email is set with the userenv branch one'); +is($data->{title}, $biblio->{title}, 'Shared title'); +is($data->{sfdescription}, $subscriptionFrequency->{description}, 'Shared sfdescription'); +is($data->{unit}, $subscriptionFrequency->{unit}, 'Shared unit'); +is($data->{unitsperissue}, $subscriptionFrequency->{unitsperissue}, 'Shared unitsperissue'); +is($data->{issuesperunit}, $subscriptionFrequency->{issuesperunit}, 'Shared issuesperunit'); + +is($data->{label}, $subscriptionNumberpattern->{label}, 'Shared np label'); +is($data->{sndescription}, $subscriptionNumberpattern->{description}, 'Shared np description'); +is($data->{numberingmethod}, $subscriptionNumberpattern->{numberingmethod}, 'Shared numberingmethod'); +is($data->{label1}, $subscriptionNumberpattern->{label1}, 'Shared label1'); +is($data->{add1}, $subscriptionNumberpattern->{add1}, 'Shared add1'); +is($data->{every1}, $subscriptionNumberpattern->{every1}, 'Shared every1'); +is($data->{whenmorethan1}, $subscriptionNumberpattern->{whenmorethan1}, 'Shared whenmorethan1'); +is($data->{setto1}, $subscriptionNumberpattern->{setto1}, 'Shared setto1'); +is($data->{numbering1}, $subscriptionNumberpattern->{numbering1}, 'Shared numbering1'); +is($data->{issn}, $biblioitem->{issn}, 'Shared ISSN'); +is($data->{ean}, $biblioitem->{ean}, 'Shared EAN'); +is($data->{publishercode}, $biblioitem->{publishercode}, 'Shared publishercode'); + +sub mock_response { + my $response = Test::MockObject->new(); + + if ($want_error) { + $response->mock('code', sub { + return 500; + }); + $response->mock('is_error', sub { + return 0; + }); + $response->mock('decoded_content', sub { + die 'Error thrown by decoded_content'; + }); + } elsif ( $post_request ) { + $response->mock('code', sub { + return 200; + }); + $response->mock('is_error', sub { + return 0; + }); + $response->mock('decoded_content', sub { + return '{"code": "200", "msg": "foo", "id": "5"}'; + }); + } else { + $response->mock('code', sub { + return 200; + }); + $response->mock('is_error', sub { + return 0; + }); + $response->mock('decoded_content', sub { + return ''; + }); + } +} + +# Increment request. +$request = Koha::SharedContent::buildRequest('increment', + 'subscription', + 12, + 'foo'); + +is($request->method, 'POST', 'Increment subscription - Method is post'); + +%query = $request->uri->query_form; +is($query{id}, 12, 'Check id'); +is($query{step}, 1, 'Step is default'); +is($query{resource}, 'subscription', 'Check ressource'); + +is($request->uri->path, '/subscription/12.json/increment/foo', 'Path is subscription'); + +$schema->storage->txn_rollback; diff --git a/t/db_dependent/Koha/Subscription/Numberpatterns.t b/t/db_dependent/Koha/Subscription/Numberpatterns.t new file mode 100644 index 0000000..6aa26c4 --- /dev/null +++ b/t/db_dependent/Koha/Subscription/Numberpatterns.t @@ -0,0 +1,97 @@ +#!/usr/bin/perl + +# Copyright 2017 BibLibre +# +# This file is part of Koha +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use t::lib::TestBuilder; +use Test::More tests => 6; +use Koha::Database; + +use_ok('Koha::Subscription::Numberpatterns'); + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; + +my $dbh = C4::Context->dbh; +$dbh->do('DELETE FROM subscription_numberpatterns'); + +my $numberpattern = $builder->build({ + source => 'SubscriptionNumberpattern', + value => { + label => 'Volume, Number, Issue', + description => 'Volume Number Issue 1', + numberingmethod => 'Vol.{X}, Number {Y}, Issue {Z}', + label1 => 'Volume', + add1 => '1', + every1 => '48', + whenmorethan1 => '99999', + setto1 => '1', + numbering1 => undef, + label2 => 'Number', + add2 => '1', + every2 => '4', + whenmorethan2 => '12', + setto2 => '1', + numbering2 => undef, + label3 => 'Issue', + add3 => '1', + every3 => '1', + whenmorethan3 => '4', + setto3 => '1', + numbering3 => undef + } +}); + +my $search_ok = { + umberingmethod => 'Vol.{X}, Number {Y}, Issue {Z}', + label1 => 'Volume', add1 => '1', every1 => '48', + whenmorethan1 => '99999', setto1 => '1', + label2 => 'Number', add2 => '1', every2 => '4', + whenmorethan2 => '12', setto2 => '1', + label3 => 'Issue', add3 => '1', every3 => '1', + whenmorethan3 => '4', setto3 => '1', + numbering_pattern => 'mana' +}; + +my $number_pattern_id = Koha::Subscription::Numberpatterns->new_or_existing($search_ok); +is($number_pattern_id, $numberpattern->{id}, 'new_or_existing method should find the existing number pattern'); + +$number_pattern_id = Koha::Subscription::Numberpatterns->new_or_existing({numbering_pattern => 1}); +is($number_pattern_id, 1, 'new_or_existing method should return passed numbering_pattern'); + +my $search_not_ok = { + patternname => 'Number', + sndescription => 'Simple Numbering method', + numberingmethod => 'No.{X}', + label1 => 'Number', + add1 => 1, + every1 => 1, + whenmorethan1 => 99999, + setto1 => 1, + numbering_pattern => 'mana' +}; + +$number_pattern_id = Koha::Subscription::Numberpatterns->new_or_existing($search_not_ok); +my $new_number_pattern = Koha::Subscription::Numberpatterns->find($number_pattern_id); +is($new_number_pattern->label, 'Number'); +is($new_number_pattern->description, 'Simple Numbering method'); +is($new_number_pattern->numberingmethod, 'No.{X}'); + +$schema->storage->txn_rollback; diff --git a/t/db_dependent/Serials/GetFictiveIssueNumber.t b/t/db_dependent/Serials/GetFictiveIssueNumber.t index e944de9..1ad66b2 100644 --- a/t/db_dependent/Serials/GetFictiveIssueNumber.t +++ b/t/db_dependent/Serials/GetFictiveIssueNumber.t @@ -1,126 +1,232 @@ #!/usr/bin/perl -use C4::Context; -use Test::More tests => 18; +# This test deals with GetFictiveIssueNumber (from C4::Serials) + use Modern::Perl; +use Test::More tests => 5; + +use Koha::Database; +use C4::Serials; +use C4::Serials::Frequency; +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; my $dbh = C4::Context->dbh; -$dbh->{RaiseError} = 1; -$dbh->{AutoCommit} = 0; -use C4::Serials::Frequency; -use C4::Serials; +subtest 'Tests for irregular frequency' => sub { + plan tests => 2; -# TEST CASE - 1 issue per day, no irregularities -my $frequency = { - description => "One issue per day", - unit => 'day', - issuesperunit => 1, - unitsperissue => 1, -}; + # Add a frequency + my $freq_irr = AddSubscriptionFrequency({ + description => "Irregular", + unit => undef, + }); -my $subscription = { - firstacquidate => '1970-01-01', - irregularity => '', - countissuesperunit => 1, -}; -my $issueNumber; - -$issueNumber = - C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-01', $frequency ); -is( $issueNumber, '1' ); - -$issueNumber = - C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-02', $frequency ); -is( $issueNumber, '2' ); -$issueNumber = - C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-03', $frequency ); -is( $issueNumber, '3' ); - -# TEST CASE - 2 issues per day, no irregularity -$frequency = { - description => "Two issues per day", - unit => 'day', - issuesperunit => 2, - unitsperissue => 1, + # Test it + my $subscription = { + periodicity => $freq_irr, + firstacquidate => '1972-02-07', + }; + is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-12-31'), undef, 'Irregular: should be undef' ); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-12-31'), undef, 'Irregular: still undef' ); }; -$subscription = { - firstacquidate => '1970-01-01', - irregularity => '', - countissuesperunit => 1, -}; -$issueNumber = - C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-01', $frequency ); -is( $issueNumber, '1' ); -$issueNumber = - C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-02', $frequency ); -is( $issueNumber, '3' ); -$issueNumber = - C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-03', $frequency ); -is( $issueNumber, '5' ); - -$subscription->{countissuesperunit} = 2; -$issueNumber = - C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-01', $frequency ); -is( $issueNumber, '2' ); -$issueNumber = - C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-02', $frequency ); -is( $issueNumber, '4' ); -$issueNumber = - C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-03', $frequency ); -is( $issueNumber, '6' ); - -# TEST CASE - 1 issue every 2 days, no irregularity -$frequency = { - description => "one issue every two days", - unit => 'day', - issuesperunit => 1, - unitsperissue => 2, + +subtest 'Tests for yearly frequencies' => sub { + plan tests => 10; + + # First add a few frequencies + my $freq_1i_1y = AddSubscriptionFrequency({ + description => "1 issue per year", + unit => 'year', + issuesperunit => 1, + unitsperissue => 1, + }); + my $freq_1i_3y = AddSubscriptionFrequency({ + description => "1 issue per 3 years", + unit => 'year', + issuesperunit => 1, + unitsperissue => 3, + }); + my $freq_5i_1y = AddSubscriptionFrequency({ + description => "5 issues per year", + unit => 'year', + issuesperunit => 5, + unitsperissue => 1, + }); + my $freq_366i_1y = AddSubscriptionFrequency({ + description => "366 issue per year", + unit => 'year', + issuesperunit => 366, + unitsperissue => 1, + }); + + # TEST CASE - 1 issue per year + my $subscription = { + periodicity => $freq_1i_1y, + firstacquidate => '1972-02-10', + countissuesperunit => 1, + }; + + my $frequency = GetSubscriptionFrequency($freq_1i_1y); + + is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-09', $frequency), 1, 'Feb 9 still 1' ); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-10', $frequency), 2, 'Feb 10 goes to 2' ); + + # TEST CASE - 1 issue per 3 years + $subscription->{periodicity} = $freq_1i_3y; + $subscription->{firstacquidate} = '1972-02-20'; + $frequency = GetSubscriptionFrequency($freq_1i_3y); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1975-02-19', $frequency), 1, 'Feb 19, 1975 still 1' ); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1975-02-20', $frequency), 2, 'Feb 20, 1975 goes to 2' ); + + # TEST CASE - 5 issues per year + $subscription->{periodicity} = $freq_5i_1y; + $subscription->{firstacquidate} = '1972-02-29'; #leap year + $subscription->{countissuesperunit} = 1; + $frequency = GetSubscriptionFrequency($freq_5i_1y); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-05-11', $frequency), 1, 'May 11 still 1' ); + $subscription->{countissuesperunit} = 2; + is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-05-12', $frequency), 2, 'May 12 goes to 2' ); + $subscription->{countissuesperunit} = 5; + is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-27', $frequency), 5, 'Feb 27 should still be 5' ); + $subscription->{countissuesperunit} = 1; + is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-28', $frequency), 6, 'Feb 28 goes to 6' ); + + # TEST CASE - 366 issues per year (hypothetical example) + # Testing prevention of divide by zero + $subscription->{periodicity} = $freq_366i_1y; + $subscription->{firstacquidate} = '1972-02-29'; #leap year + $subscription->{countissuesperunit} = 366; + $frequency = GetSubscriptionFrequency($freq_366i_1y); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-27', $frequency), 366, 'Feb 27 still at 366' ); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-02-28', $frequency), 732, 'Feb 28 goes to 732' ); + }; -$subscription = { - firstacquidate => '1970-01-01', - irregularity => '', - countissuesperunit => 1, + +subtest 'Tests for monthly frequencies' => sub { + plan tests => 8; + + # First add a few frequencies + my $freq_1i_5m = AddSubscriptionFrequency({ + description => "1 issue per 5 months", + unit => 'month', + issuesperunit => 1, + unitsperissue => 5, + }); + my $freq_4i_1m = AddSubscriptionFrequency({ + description => "4 issue per month", + unit => 'month', + issuesperunit => 4, + unitsperissue => 1, + }); + + # TEST CASE - 1 issue per 5 months + my $subscription = { + periodicity => $freq_1i_5m, + firstacquidate => '1972-02-10', + countissuesperunit => 1, + }; + my $frequency = GetSubscriptionFrequency($freq_1i_5m); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-07-09', $frequency), 1, 'Jul 9 still 1' ); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-07-10', $frequency), 2, 'Jul 10 goes to 2' ); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-05-09', $frequency), 3, 'May 9 still 3' ); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1973-05-10', $frequency), 4, 'May 10 goes to 4' ); + + # TEST CASE - 4 issue per 1 months + $subscription = { + periodicity => $freq_4i_1m, + firstacquidate => '1972-02-22', + countissuesperunit => 1, + }; + $frequency = GetSubscriptionFrequency($freq_4i_1m); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-02-28', $frequency), 1, 'Feb 28 still 1' ); + $subscription->{countissuesperunit} = 2; + is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-02-29', $frequency), 2, 'Feb 29 goes to 2' ); + $subscription->{countissuesperunit} = 4; + is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-21', $frequency), 4, 'Mar 21 still 4' ); + $subscription->{countissuesperunit} = 1; + is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-22', $frequency), 5, 'Mar 22 goes to 5' ); + }; -$issueNumber = - C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-01', $frequency ); -is( $issueNumber, 1 ); -$issueNumber = - C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-02', $frequency ); -is( $issueNumber, 1 ); -$issueNumber = - C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-03', $frequency ); -is( $issueNumber, 2 ); -$issueNumber = - C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-04', $frequency ); -is( $issueNumber, 2 ); -$issueNumber = - C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-05', $frequency ); -is( $issueNumber, 3 ); - -# TEST CASE - 1 issue per week, no irregularity -$frequency = { - description => "one issue per week", - unit => 'week', - issuesperunit => 1, - unitsperissue => 1, + +subtest 'Tests for weekly frequencies' => sub { + plan tests => 4; + + # First add a few frequencies + my $freq_1i_7w = AddSubscriptionFrequency({ + description => "1 issue per 7 weeks", + unit => 'week', + issuesperunit => 1, + unitsperissue => 7, + }); + my $freq_3i_1w = AddSubscriptionFrequency({ + description => "3 issues per week", + unit => 'week', + issuesperunit => 3, + unitsperissue => 1, + }); + + # TEST CASE - 1 issue per 7 weeks + my $subscription = { + periodicity => $freq_1i_7w, + firstacquidate => '1972-02-10', + countissuesperunit => 1, + }; + my $frequency = GetSubscriptionFrequency($freq_1i_7w); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-29', $frequency), 1, 'Mar 29 still 1' ); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-30', $frequency), 2, 'Mar 30 goes to 2' ); + + # TEST CASE - 3 issue per 1 week + $subscription = { + periodicity => $freq_3i_1w, + firstacquidate => '1972-02-03', + countissuesperunit => 1, + }; + $subscription->{countissuesperunit} = 3; + $frequency = GetSubscriptionFrequency($freq_3i_1w); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-02-09', $frequency), 3, 'Feb 9 still 3' ); + $subscription->{countissuesperunit} = 1; + is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-02-10', $frequency), 4, 'Feb 10 goes to 4' ); }; -$subscription = { - firstacquidate => '1970-01-01', - irregularity => '', - countissuesperunit => 1, + +subtest 'Tests for dayly frequencies' => sub { + plan tests => 4; + + # First add a few frequencies + my $freq_1i_12d = AddSubscriptionFrequency({ + description => "1 issue per 12 days", + unit => 'day', + issuesperunit => 1, + unitsperissue => 12, + }); + my $freq_3i_1d = AddSubscriptionFrequency({ + description => "3 issues per day", + unit => 'day', + issuesperunit => 3, + unitsperissue => 1, + }); + + # TEST CASE - 1 issue per 12 days + my $subscription = { + periodicity => $freq_1i_12d, + firstacquidate => '1972-03-16', + countissuesperunit => 1, + }; + my $frequency = GetSubscriptionFrequency($freq_1i_12d); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-27', $frequency), 1, 'Mar 27 still 1' ); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-03-28', $frequency), 2, 'Mar 28 goes to 2' ); + + # TEST CASE - 3 issue per day + $subscription = { + periodicity => $freq_3i_1d, + firstacquidate => '1972-04-23', + countissuesperunit => 1, + }; + $subscription->{countissuesperunit} = 3; + $frequency = GetSubscriptionFrequency($freq_3i_1d); + is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-05-01', $frequency), 27, 'May 1 still 27' ); + $subscription->{countissuesperunit} = 1; + is( C4::Serials::GetFictiveIssueNumber($subscription, '1972-05-02', $frequency), 28, 'May 2 goes to 28' ); }; -$issueNumber = - C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-01', $frequency ); -is( $issueNumber, 1 ); -$issueNumber = - C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-02', $frequency ); -is( $issueNumber, 1 ); -$issueNumber = - C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-08', $frequency ); -is( $issueNumber, 2 ); -$issueNumber = - C4::Serials::GetFictiveIssueNumber( $subscription, '1970-01-15', $frequency ); -is( $issueNumber, 3 ); - -$dbh->rollback; + +$schema->storage->txn_rollback; -- 2.7.4
- - - [% FOREACH comment IN subscription.comments %] - [% END %] -