Bug 15878 - Updated unit tests for hbyymmincr barcodes
[koha.git] / t / db_dependent / Letters.t
index 32b760e..ed5c972 100644 (file)
@@ -18,7 +18,7 @@
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
-use Test::More tests => 58;
+use Test::More tests => 72;
 use Test::MockModule;
 use Test::Warn;
 
@@ -36,30 +36,37 @@ $module->mock(
 
 use_ok('C4::Context');
 use_ok('C4::Members');
-use_ok('C4::Branch');
 use_ok('C4::Acquisition');
 use_ok('C4::Biblio');
 use_ok('C4::Bookseller');
 use_ok('C4::Letters');
 use t::lib::Mocks;
+use t::lib::TestBuilder;
+use Koha::Database;
 use Koha::DateUtils qw( dt_from_string output_pref );
+use Koha::Acquisition::Order;
+use Koha::Acquisition::Bookseller;
+use Koha::Libraries;
+my $schema = Koha::Database->schema;
+$schema->storage->txn_begin();
 
+my $builder = t::lib::TestBuilder->new;
 my $dbh = C4::Context->dbh;
-
-# Start transaction
-$dbh->{AutoCommit} = 0;
 $dbh->{RaiseError} = 1;
 
 $dbh->do(q|DELETE FROM letter|);
 $dbh->do(q|DELETE FROM message_queue|);
 $dbh->do(q|DELETE FROM message_transport_types|);
 
+my $library = $builder->build({
+    source => 'Branch',
+});
 my $date = dt_from_string;
 my $borrowernumber = AddMember(
     firstname    => 'Jane',
     surname      => 'Smith',
     categorycode => 'PT',
-    branchcode   => 'CPL',
+    branchcode   => $library->{branchcode},
     dateofbirth  => $date,
 );
 
@@ -130,12 +137,22 @@ is(
     'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)'
 );
 
+# ResendMessage
+my $resent = C4::Letters::ResendMessage($messages->[0]->{message_id});
+my $message = C4::Letters::GetMessage( $messages->[0]->{message_id});
+is( $resent, 1, 'The message should have been resent' );
+is($message->{status},'pending', 'ResendMessage sets status to pending correctly (bug 12426)');
+$resent = C4::Letters::ResendMessage($messages->[0]->{message_id});
+is( $resent, 0, 'The message should not have been resent again' );
+$resent = C4::Letters::ResendMessage();
+is( $resent, undef, 'ResendMessage should return undef if not message_id given' );
+
 # GetLetters
 my $letters = C4::Letters::GetLetters();
 is( @$letters, 0, 'GetLetters returns the correct number of letters' );
 
 my $title = q|<<branches.branchname>> - <<status>>|;
-my $content = q|Dear <<borrowers.firstname>> <<borrowers.surname>>,
+my $content = q{Dear <<borrowers.firstname>> <<borrowers.surname>>,
 According to our current records, you have items that are overdue.Your library does not charge late fines, but please return or renew them at the branch below as soon as possible.
 
 <<branches.branchname>>
@@ -149,20 +166,20 @@ The following item(s) is/are currently <<status>>:
 Thank-you for your prompt attention to this matter.
 Don't forget your date of birth: <<borrowers.dateofbirth>>.
 Look at this wonderful biblio timestamp: <<biblio.timestamp>>.
-|;
+};
 
-$dbh->do( q|INSERT INTO letter(branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES ('CPL','my module','my code','my name',1,?,?,'email')|, undef, $title, $content );
+$dbh->do( q|INSERT INTO letter(branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES (?,'my module','my code','my name',1,?,?,'email')|, undef, $library->{branchcode}, $title, $content );
 $letters = C4::Letters::GetLetters();
 is( @$letters, 1, 'GetLetters returns the correct number of letters' );
-is( $letters->[0]->{branchcode}, 'CPL', 'GetLetters gets the branch code correctly' );
+is( $letters->[0]->{branchcode}, $library->{branchcode}, 'GetLetters gets the branch code correctly' );
 is( $letters->[0]->{module}, 'my module', 'GetLetters gets the module correctly' );
 is( $letters->[0]->{code}, 'my code', 'GetLetters gets the code correctly' );
 is( $letters->[0]->{name}, 'my name', 'GetLetters gets the name correctly' );
 
 
 # getletter
-my $letter = C4::Letters::getletter('my module', 'my code', 'CPL', 'email');
-is( $letter->{branchcode}, 'CPL', 'GetLetters gets the branch code correctly' );
+my $letter = C4::Letters::getletter('my module', 'my code', $library->{branchcode}, 'email');
+is( $letter->{branchcode}, $library->{branchcode}, 'GetLetters gets the branch code correctly' );
 is( $letter->{module}, 'my module', 'GetLetters gets the module correctly' );
 is( $letter->{code}, 'my code', 'GetLetters gets the code correctly' );
 is( $letter->{name}, 'my name', 'GetLetters gets the name correctly' );
@@ -171,6 +188,21 @@ is( $letter->{title}, $title, 'GetLetters gets the title correctly' );
 is( $letter->{content}, $content, 'GetLetters gets the content correctly' );
 is( $letter->{message_transport_type}, 'email', 'GetLetters gets the message type correctly' );
 
+# Regression test for Bug 14206
+$dbh->do( q|INSERT INTO letter(branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES ('FFL','my module','my code','my name',1,?,?,'print')|, undef, $title, $content );
+my $letter14206_a = C4::Letters::getletter('my module', 'my code', 'FFL' );
+is( $letter14206_a->{message_transport_type}, 'print', 'Bug 14206 - message_transport_type not passed, correct mtt detected' );
+my $letter14206_b = C4::Letters::getletter('my module', 'my code', 'FFL', 'print');
+is( $letter14206_b->{message_transport_type}, 'print', 'Bug 14206 - message_transport_type passed, correct mtt detected'  );
+
+# test for overdue_notices.pl
+my $overdue_rules = {
+    letter1         => 'my code',
+};
+my $i = 1;
+my $branchcode = 'FFL';
+my $letter14206_c = C4::Letters::getletter('my module', $overdue_rules->{"letter$i"}, $branchcode);
+is( $letter14206_c->{message_transport_type}, 'print', 'Bug 14206 - correct mtt detected for call from overdue_notices.pl' );
 
 # addalert
 my $type = 'my type';
@@ -213,11 +245,11 @@ is( @$alerts, 0, 'delalert removes an alert' );
 t::lib::Mocks::mock_preference('OPACBaseURL', 'http://thisisatest.com');
 
 my $sms_content = 'This is a SMS for an <<status>>';
-$dbh->do( q|INSERT INTO letter(branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES ('CPL','my module','my code','my name',1,'my title',?,'sms')|, undef, $sms_content );
+$dbh->do( q|INSERT INTO letter(branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES (?,'my module','my code','my name',1,'my title',?,'sms')|, undef, $library->{branchcode}, $sms_content );
 
 my $tables = {
     borrowers => $borrowernumber,
-    branches => 'CPL',
+    branches => $library->{branchcode},
     biblio => $biblionumber,
 };
 my $substitute = {
@@ -235,19 +267,19 @@ my $repeat = [
 ];
 my $prepared_letter = GetPreparedLetter((
     module      => 'my module',
-    branchcode  => 'CPL',
+    branchcode  => $library->{branchcode},
     letter_code => 'my code',
     tables      => $tables,
     substitute  => $substitute,
     repeat      => $repeat,
 ));
-my $branch = GetBranchDetail('CPL');
-my $my_title_letter = qq|$branch->{branchname} - $substitute->{status}|;
+my $retrieved_library = Koha::Libraries->find($library->{branchcode});
+my $my_title_letter = $retrieved_library->branchname . qq| - $substitute->{status}|;
 my $my_content_letter = qq|Dear Jane Smith,
 According to our current records, you have items that are overdue.Your library does not charge late fines, but please return or renew them at the branch below as soon as possible.
 
-$branch->{branchname}
-$branch->{branchaddress1}
+|.$retrieved_library->branchname.qq|
+|.$retrieved_library->branchaddress1.qq|
 URL: http://thisisatest.com
 
 The following item(s) is/are currently $substitute->{status}:
@@ -258,12 +290,13 @@ The following item(s) is/are currently $substitute->{status}:
 Thank-you for your prompt attention to this matter.
 Don't forget your date of birth: | . output_pref({ dt => $date, dateonly => 1 }) . q|.
 Look at this wonderful biblio timestamp: | . output_pref({ dt => $date }) . ".\n";
+
 is( $prepared_letter->{title}, $my_title_letter, 'GetPreparedLetter returns the title correctly' );
 is( $prepared_letter->{content}, $my_content_letter, 'GetPreparedLetter returns the content correctly' );
 
 $prepared_letter = GetPreparedLetter((
     module                 => 'my module',
-    branchcode             => 'CPL',
+    branchcode             => $library->{branchcode},
     letter_code            => 'my code',
     tables                 => $tables,
     substitute             => $substitute,
@@ -273,8 +306,48 @@ $prepared_letter = GetPreparedLetter((
 $my_content_letter = qq|This is a SMS for an $substitute->{status}|;
 is( $prepared_letter->{content}, $my_content_letter, 'GetPreparedLetter returns the content correctly' );
 
+$dbh->do(q{INSERT INTO letter (module, code, name, title, content) VALUES ('test_date','TEST_DATE','Test dates','Test dates','This one only contains the date: <<biblio.timestamp | dateonly>>.');});
+$prepared_letter = GetPreparedLetter((
+    module                 => 'test_date',
+    branchcode             => '',
+    letter_code            => 'test_date',
+    tables                 => $tables,
+    substitute             => $substitute,
+    repeat                 => $repeat,
+));
+is( $prepared_letter->{content}, q|This one only contains the date: | . output_pref({ dt => $date, dateonly => 1 }) . q|.|, 'dateonly test 1' );
+
+$dbh->do(q{UPDATE letter SET content = 'And also this one:<<timestamp | dateonly>>.' WHERE code = 'test_date';});
+$prepared_letter = GetPreparedLetter((
+    module                 => 'test_date',
+    branchcode             => '',
+    letter_code            => 'test_date',
+    tables                 => $tables,
+    substitute             => $substitute,
+    repeat                 => $repeat,
+));
+is( $prepared_letter->{content}, q|This one only contains the date: | . output_pref({ dt => $date, dateonly => 1 }) . q|.|, 'dateonly test 2' );
+
+$dbh->do(q{UPDATE letter SET content = 'And also this one:<<timestamp|dateonly >>.' WHERE code = 'test_date';});
+$prepared_letter = GetPreparedLetter((
+    module                 => 'test_date',
+    branchcode             => '',
+    letter_code            => 'test_date',
+    tables                 => $tables,
+    substitute             => $substitute,
+    repeat                 => $repeat,
+));
+is( $prepared_letter->{content}, q|This one only contains the date: | . output_pref({ dt => $date, dateonly => 1 }) . q|.|, 'dateonly test 3' );
+
 $dbh->do(q{INSERT INTO letter (module, code, name, title, content) VALUES ('claimacquisition','TESTACQCLAIM','Acquisition Claim','Item Not Received','<<aqbooksellers.name>>|<<aqcontacts.name>>|<order>Ordernumber <<aqorders.ordernumber>> (<<biblio.title>>) (<<aqorders.quantity>> ordered)</order>');});
 
+# Test that _parseletter doesn't modify its parameters bug 15429
+{
+    my $values = { dateexpiry => '2015-12-13', };
+    C4::Letters::_parseletter($prepared_letter, 'borrowers', $values);
+    is( $values->{dateexpiry}, '2015-12-13', "_parseletter doesn't modify its parameters" );
+}
+
 my $booksellerid = C4::Bookseller::AddBookseller(
     {
         name => "my vendor",
@@ -295,7 +368,6 @@ my $budgetid = C4::Budgets::AddBudget({
     budget_name => "budget_name_test_letters",
 });
 
-my $ordernumber;
 my $bib = MARC::Record->new();
 if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
     $bib->append_fields(
@@ -308,14 +380,15 @@ if (C4::Context->preference('marcflavour') eq 'UNIMARC') {
 }
 
 ($biblionumber, $biblioitemnumber) = AddBiblio($bib, '');
-$ordernumber = C4::Acquisition::NewOrder(
+my $order = Koha::Acquisition::Order->new(
     {
         basketno => $basketno,
         quantity => 1,
         biblionumber => $biblionumber,
         budget_id => $budgetid,
     }
-);
+)->insert;
+my $ordernumber = $order->{ordernumber};
 
 C4::Acquisition::CloseBasket( $basketno );
 my $err;
@@ -325,11 +398,15 @@ warning_like {
     "SendAlerts prints a warning";
 is($err->{'error'}, 'no_email', "Trying to send an alert when there's no e-mail results in an error");
 
-my $bookseller = C4::Bookseller::GetBookSellerFromId($booksellerid);
-$bookseller->{'contacts'}->[0]->email('testemail@mydomain.com');
+my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
+$bookseller->contacts->[0]->email('testemail@mydomain.com');
 C4::Bookseller::ModBookseller($bookseller);
-$bookseller = C4::Bookseller::GetBookSellerFromId($booksellerid);
+$bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
 
+# Ensure that the preference 'LetterLog' is set to logging
+t::lib::Mocks::mock_preference( 'LetterLog', 'on' );
+
+{
 warning_is {
     $err = SendAlerts( 'claimacquisition', [ $ordernumber ], 'TESTACQCLAIM' ) }
     "Fake sendmail",
@@ -337,6 +414,44 @@ warning_is {
 
 is($err, 1, "Successfully sent claim");
 is($mail{'To'}, 'testemail@mydomain.com', "mailto correct in sent claim");
-is($mail{'Message'}, 'my vendor|John Smith|<order>Ordernumber ' . $ordernumber . ' (Silence in the library) (1 ordered)</order>', 'Claim notice text constructed successfully');
+is($mail{'Message'}, 'my vendor|John Smith|Ordernumber ' . $ordernumber . ' (Silence in the library) (1 ordered)', 'Claim notice text constructed successfully');
+}
 
-$dbh->rollback;
+{
+use C4::Serials;
+
+my $notes = 'notes';
+my $internalnotes = 'intnotes';
+$dbh->do(q|UPDATE subscription_numberpatterns SET numberingmethod='No. {X}' WHERE id=1|);
+my $subscriptionid = NewSubscription(
+     undef,      "",     undef, undef, undef, $biblionumber,
+    '2013-01-01', 1, undef, undef,  undef,
+    undef,      undef,  undef, undef, undef, undef,
+    1,          $notes,undef, '2013-01-01', undef, 1,
+    undef,       undef,  0,    $internalnotes,  0,
+    undef, undef, 0,          undef,         '2013-12-31', 0
+);
+$dbh->do(q{INSERT INTO letter (module, code, name, title, content) VALUES ('serial','RLIST','Serial issue notification','Serial issue notification','<<biblio.title>>,<<subscription.subscriptionid>>,<<serial.serialseq>>');});
+my ($serials_count, @serials) = GetSerials($subscriptionid);
+my $serial = $serials[0];
+
+my $borrowernumber = AddMember(
+    firstname    => 'John',
+    surname      => 'Smith',
+    categorycode => 'PT',
+    branchcode   => $library->{branchcode},
+    dateofbirth  => $date,
+    email        => 'john.smith@test.de',
+);
+my $alert_id = C4::Letters::addalert($borrowernumber, 'issue', $subscriptionid);
+
+
+my $err2;
+warning_is {
+$err2 = SendAlerts( 'issue', $serial->{serialid}, 'RLIST' ) }
+    "Fake sendmail",
+    "SendAlerts is using the mocked sendmail routine";
+is($err2, "", "Successfully sent serial notification");
+is($mail{'To'}, 'john.smith@test.de', "mailto correct in sent serial notification");
+is($mail{'Message'}, 'Silence in the library,'.$subscriptionid.',No. 0', 'Serial notification text constructed successfully');
+}