- [% INCLUDE 'serials-toolbar.inc' mana_id = mana_id %]
+ [% INCLUDE 'serials-toolbar.inc' %]
Subscription for [% bibliotitle%] [% IF closed %](closed)[% END %]
[% IF ( abouttoexpire ) %]
--- a/koha-tmpl/intranet-tmpl/prog/js/subscription-add.js
+++ a/koha-tmpl/intranet-tmpl/prog/js/subscription-add.js
@@ -393,7 +393,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({
@@ -404,7 +404,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"]],
@@ -419,10 +419,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 );
@@ -455,10 +455,7 @@ function mana_search() {
mana_increment($("#selectedcomment").val(), 'resource_comment', 'nb', -1);
});
- }).fail(function(result){
- console.log( msg );
- console.log( longmsg );
- });
+ })
}
function mana_comment( target_id, manamsg, resource_type ){
@@ -473,7 +470,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",
})
@@ -554,10 +551,6 @@ function mana_use(mana_id){
});
}
-function removeDisabledAttr() {
- $('select:disabled').removeAttr('disabled');
-}
-
$(document).ready(function() {
mana_search();
$("#displayexample").hide();
@@ -699,4 +692,7 @@ $(document).ready(function() {
e.preventDefault();
testPredictionPattern();
});
+ $('#save-subscription').on("click", function(e){
+ $('select:disabled').removeAttr('disabled');
+ });
});
--- a/reports/guided_reports.pl
+++ a/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'),
);
}
@@ -672,7 +672,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{
--- a/serials/subscription-add.pl
+++ a/serials/subscription-add.pl
@@ -289,35 +289,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'),
@@ -328,15 +302,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');
@@ -378,9 +344,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' } );
@@ -404,7 +367,7 @@ sub redirect_add_subscription {
$skip_serialseq, $itemtype, $previousitemtype, $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' } );
@@ -434,8 +397,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');
--- a/serials/subscription-detail.pl
+++ a/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};
}
--- a/svc/mana/addvaluetofield
+++ a/svc/mana/addvaluetofield
@@ -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;
--- a/svc/mana/search
+++ a/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(
{
--- a/svc/mana/share
+++ a/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: $@ )";
--- a/svc/mana/use
+++ a/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;
--- a/t/db_dependent/Koha/SharedContent.t
+++ a/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;
--- a/t/db_dependent/Koha/Subscription/Numberpatterns.t
+++ a/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;
--- a/t/db_dependent/Serials/GetFictiveIssueNumber.t
+++ a/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;
--