Bug 19743: Unit Tests
[srvgit] / t / db_dependent / Letters / TemplateToolkit.t
index 95973ed..c4f4e82 100644 (file)
@@ -19,7 +19,7 @@
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
-use Test::More tests => 17;
+use Test::More tests => 18;
 use Test::Warn;
 
 use MARC::Record;
@@ -88,7 +88,7 @@ my $hold = Koha::Hold->new(
     }
 )->store();
 
-my $news         = Koha::NewsItem->new()->store();
+my $news         = Koha::NewsItem->new({ title => 'a news title', content => 'a news content'})->store();
 my $serial       = Koha::Serial->new()->store();
 my $subscription = Koha::Subscription->new()->store();
 my $suggestion   = Koha::Suggestion->new()->store();
@@ -286,7 +286,7 @@ $prepared_letter = GetPreparedLetter(
 is( $prepared_letter->{content}, $modification->id(), 'Patron modification object used correctly' );
 
 subtest 'regression tests' => sub {
-    plan tests => 6;
+    plan tests => 8;
 
     my $library = $builder->build( { source => 'Branch' } );
     my $patron  = $builder->build( { source => 'Borrower' } );
@@ -555,8 +555,8 @@ You have [% count %] items due
 EOF
 
         reset_template( { template => $template, code => $code, module => 'circulation' } );
-        my $letter_for_item1 = C4::Reserves::ReserveSlip( $library->{branchcode}, $patron->{borrowernumber}, $biblio1->{biblionumber} );
-        my $letter_for_item2 = C4::Reserves::ReserveSlip( $library->{branchcode}, $patron->{borrowernumber}, $biblio2->{biblionumber} );
+        my $letter_for_item1 = C4::Reserves::ReserveSlip( { branchcode => $library->{branchcode}, borrowernumber => $patron->{borrowernumber}, biblionumber => $biblio1->{biblionumber} } );
+        my $letter_for_item2 = C4::Reserves::ReserveSlip( { branchcode => $library->{branchcode}, borrowernumber => $patron->{borrowernumber}, biblionumber => $biblio2->{biblionumber} } );
 
         my $tt_template = <<EOF;
 <h5>Date: [% today | \$KohaDates with_hours => 1 %]</h5>
@@ -589,8 +589,8 @@ EOF
 EOF
 
         reset_template( { template => $tt_template, code => $code, module => 'circulation' } );
-        my $tt_letter_for_item1 = C4::Reserves::ReserveSlip( $library->{branchcode}, $patron->{borrowernumber}, $biblio1->{biblionumber} );
-        my $tt_letter_for_item2 = C4::Reserves::ReserveSlip( $library->{branchcode}, $patron->{borrowernumber}, $biblio2->{biblionumber} );
+        my $tt_letter_for_item1 = C4::Reserves::ReserveSlip( { branchcode => $library->{branchcode}, borrowernumber => $patron->{borrowernumber}, biblionumber => $biblio1->{biblionumber} } );
+        my $tt_letter_for_item2 = C4::Reserves::ReserveSlip( { branchcode => $library->{branchcode}, borrowernumber => $patron->{borrowernumber}, biblionumber => $biblio2->{biblionumber} } );
 
         is( $tt_letter_for_item1->{content}, $letter_for_item1->{content}, );
         is( $tt_letter_for_item2->{content}, $letter_for_item2->{content}, );
@@ -600,6 +600,8 @@ EOF
         plan tests => 2;
 
         my $code = 'ISSUESLIP';
+        my $now = dt_from_string;
+        my $one_minute_ago = dt_from_string->subtract( minutes => 1 );
 
         my $branchcode = $library->{branchcode};
 
@@ -647,10 +649,12 @@ EOF
 
         reset_template( { template => $template, code => $code, module => 'circulation' } );
 
-        C4::Circulation::AddIssue( $patron, $item1->{barcode} ); # Add a first checkout
+        my $checkout = C4::Circulation::AddIssue( $patron, $item1->{barcode} ); # Add a first checkout
+        $checkout->set_columns( { timestamp => $now, issuedate => $one_minute_ago } )->update; # FIXME $checkout is a Koha::Schema::Result::Issues, must be a Koha::Checkout
         my $first_slip = C4::Members::IssueSlip( $branchcode, $patron->{borrowernumber} );
 
-        C4::Circulation::AddIssue( $patron, $item2->{barcode} ); # Add a second checkout
+        $checkout = C4::Circulation::AddIssue( $patron, $item2->{barcode} ); # Add a second checkout
+        $checkout->set_columns( { timestamp => $now, issuedate => $now } )->update;
         my $yesterday = dt_from_string->subtract( days => 1 );
         C4::Circulation::AddIssue( $patron, $item3->{barcode}, $yesterday ); # Add an overdue
         my $second_slip = C4::Members::IssueSlip( $branchcode, $patron->{borrowernumber} );
@@ -705,10 +709,12 @@ EOF
 
         reset_template( { template => $tt_template, code => $code, module => 'circulation' } );
 
-        C4::Circulation::AddIssue( $patron, $item1->{barcode} ); # Add a first checkout
+        $checkout = C4::Circulation::AddIssue( $patron, $item1->{barcode} ); # Add a first checkout
+        $checkout->set_columns( { timestamp => $now, issuedate => $one_minute_ago } )->update;
         my $first_tt_slip = C4::Members::IssueSlip( $branchcode, $patron->{borrowernumber} );
 
-        C4::Circulation::AddIssue( $patron, $item2->{barcode} ); # Add a second checkout
+        $checkout = C4::Circulation::AddIssue( $patron, $item2->{barcode} ); # Add a second checkout
+        $checkout->set_columns( { timestamp => $now, issuedate => $now } )->update;
         C4::Circulation::AddIssue( $patron, $item3->{barcode}, $yesterday ); # Add an overdue
         my $second_tt_slip = C4::Members::IssueSlip( $branchcode, $patron->{borrowernumber} );
 
@@ -717,7 +723,267 @@ EOF
 
         is( $first_tt_slip->{content}, $first_slip->{content}, );
         is( $second_tt_slip->{content}, $second_slip->{content}, );
+
+        # Cleanup
+        AddReturn( $item1->{barcode} );
+        AddReturn( $item2->{barcode} );
+        AddReturn( $item3->{barcode} );
+    };
+
+    subtest 'ODUE|items.content|item' => sub {
+        plan tests => 1;
+
+        my $code = 'ODUE';
+
+        my $branchcode = $library->{branchcode};
+
+        # historic syntax
+        # FIXME items.fine does not work with TT notices
+        # See bug 17976
+        # <item> should contain Fine: <<items.fine>></item>
+        my $template = <<EOF;
+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>>
+<<branches.branchaddress1>>
+<<branches.branchaddress2>> <<branches.branchaddress3>>
+Phone: <<branches.branchphone>>
+Fax: <<branches.branchfax>>
+Email: <<branches.branchemail>>
+
+If you have registered a password with the library, and you have a renewal available, you may renew online. If an item becomes more than 30 days overdue, you will be unable to use your library card until the item is returned.
+
+The following item(s) is/are currently overdue:
+
+<item>"<<biblio.title>>" by <<biblio.author>>, <<items.itemcallnumber>>, Barcode: <<items.barcode>></item>
+
+<<items.content>>
+
+Thank-you for your prompt attention to this matter.
+
+<<branches.branchname>> Staff
+EOF
+
+        reset_template( { template => $template, code => $code, module => 'circulation' } );
+
+        my $yesterday = dt_from_string->subtract( days => 1 );
+        my $two_days_ago = dt_from_string->subtract( days => 2 );
+        my $issue1 = C4::Circulation::AddIssue( $patron, $item1->{barcode} ); # Add a first checkout
+        my $issue2 = C4::Circulation::AddIssue( $patron, $item2->{barcode}, $yesterday ); # Add an first overdue
+        my $issue3 = C4::Circulation::AddIssue( $patron, $item3->{barcode}, $two_days_ago ); # Add an second overdue
+        $issue1 = Koha::Checkout->_new_from_dbic( $issue1 )->unblessed; # ->unblessed should be enough but AddIssue does not return a Koha::Checkout object
+        $issue2 = Koha::Checkout->_new_from_dbic( $issue2 )->unblessed;
+        $issue3 = Koha::Checkout->_new_from_dbic( $issue3 )->unblessed;
+
+        # For items.content
+        my @item_fields = qw( date_due title barcode author itemnumber );
+        my $items_content = C4::Letters::get_item_content( { item => { %$item1, %$biblio1, %$issue1 }, item_content_fields => \@item_fields, dateonly => 1 } );
+          $items_content .= C4::Letters::get_item_content( { item => { %$item2, %$biblio2, %$issue2 }, item_content_fields => \@item_fields, dateonly => 1 } );
+          $items_content .= C4::Letters::get_item_content( { item => { %$item3, %$biblio3, %$issue3 }, item_content_fields => \@item_fields, dateonly => 1 } );
+
+        my @items = ( $item1, $item2, $item3 );
+        my $letter = C4::Overdues::parse_overdues_letter(
+            {
+                letter_code => $code,
+                borrowernumber => $patron->{borrowernumber},
+                branchcode  => $library->{branchcode},
+                items       => \@items,
+                substitute  => {
+                    bib                    => $library->{branchname},
+                    'items.content'        => $items_content,
+                    count                  => scalar( @items ),
+                    message_transport_type => 'email',
+                }
+            }
+        );
+
+        # Cleanup
+        AddReturn( $item1->{barcode} );
+        AddReturn( $item2->{barcode} );
+        AddReturn( $item3->{barcode} );
+
+
+        # historic syntax
+        my $tt_template = <<EOF;
+Dear [% borrower.firstname %] [% borrower.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.
+
+[% branch.branchname %]
+[% branch.branchaddress1 %]
+[% branch.branchaddress2 %] [% branch.branchaddress3 %]
+Phone: [% branch.branchphone %]
+Fax: [% branch.branchfax %]
+Email: [% branch.branchemail %]
+
+If you have registered a password with the library, and you have a renewal available, you may renew online. If an item becomes more than 30 days overdue, you will be unable to use your library card until the item is returned.
+
+The following item(s) is/are currently overdue:
+
+[% FOREACH overdue IN overdues %]
+[%~ SET item = overdue.item ~%]
+"[% item.biblio.title %]" by [% item.biblio.author %], [% item.itemcallnumber %], Barcode: [% item.barcode %]
+[% END %]
+[% FOREACH overdue IN overdues %]
+[%~ SET item = overdue.item ~%]
+[% overdue.date_due | \$KohaDates %]\t[% item.biblio.title %]\t[% item.barcode %]\t[% item.biblio.author %]\t[% item.itemnumber %]
+[% END %]
+
+Thank-you for your prompt attention to this matter.
+
+[% branch.branchname %] Staff
+EOF
+
+        reset_template( { template => $tt_template, code => $code, module => 'circulation' } );
+
+        C4::Circulation::AddIssue( $patron, $item1->{barcode} ); # Add a first checkout
+        C4::Circulation::AddIssue( $patron, $item2->{barcode}, $yesterday ); # Add an first overdue
+        C4::Circulation::AddIssue( $patron, $item3->{barcode}, $two_days_ago ); # Add an second overdue
+
+        my $tt_letter = C4::Overdues::parse_overdues_letter(
+            {
+                letter_code => $code,
+                borrowernumber => $patron->{borrowernumber},
+                branchcode  => $library->{branchcode},
+                items       => \@items,
+                substitute  => {
+                    bib                    => $library->{branchname},
+                    'items.content'        => $items_content,
+                    count                  => scalar( @items ),
+                    message_transport_type => 'email',
+                }
+            }
+        );
+
+        is( $tt_letter->{content}, $letter->{content}, );
     };
+
+    subtest 'Bug 19743 - Header and Footer should be updated on each item for checkin / checkout / renewal notices' => sub {
+        plan tests => 8;
+
+        my $checkout_code = 'CHECKOUT';
+        my $checkin_code = 'CHECKIN';
+
+        my $dbh = C4::Context->dbh;
+        $dbh->do("DELETE FROM letter");
+        $dbh->do("DELETE FROM issues");
+        $dbh->do("DELETE FROM message_queue");
+
+        # Enable notification for CHECKOUT - Things are hardcoded here but should work with default data
+        $dbh->do(q|INSERT INTO borrower_message_preferences( borrowernumber, message_attribute_id ) VALUES ( ?, ? )|, undef, $patron->{borrowernumber}, 6 );
+        my $borrower_message_preference_id = $dbh->last_insert_id(undef, undef, "borrower_message_preferences", undef);
+        $dbh->do(q|INSERT INTO borrower_message_transport_preferences( borrower_message_preference_id, message_transport_type) VALUES ( ?, ? )|, undef, $borrower_message_preference_id, 'email' );
+        # Enable notification for CHECKIN - Things are hardcoded here but should work with default data
+        $dbh->do(q|INSERT INTO borrower_message_preferences( borrowernumber, message_attribute_id ) VALUES ( ?, ? )|, undef, $patron->{borrowernumber}, 5 );
+        $borrower_message_preference_id = $dbh->last_insert_id(undef, undef, "borrower_message_preferences", undef);
+        $dbh->do(q|INSERT INTO borrower_message_transport_preferences( borrower_message_preference_id, message_transport_type) VALUES ( ?, ? )|, undef, $borrower_message_preference_id, 'email' );
+
+        my $checkout_template = q|
+<<branches.branchname>>
+----
+----
+|;
+        reset_template( { template => $checkout_template, code => $checkout_code, module => 'circulation' } );
+        my $checkin_template = q[
+<<branches.branchname>>
+----
+----
+];
+        reset_template( { template => $checkin_template, code => $checkin_code, module => 'circulation' } );
+
+        my $issue = C4::Circulation::AddIssue( $patron, $item1->{barcode} );
+        my $first_checkout_letter = Koha::Notice::Messages->search( {}, { order_by => { -desc => 'message_id' } } )->next;
+
+        my $library_object = Koha::Libraries->find( $issue->branchcode );
+        my $old_branchname = $library_object->branchname;
+        my $new_branchname = "Kyle M Hall Memorial Library";
+
+        # Change branch name for second checkout notice
+        $library_object->branchname($new_branchname);
+        $library_object->store();
+
+        C4::Circulation::AddIssue( $patron, $item2->{barcode} );
+        my $second_checkout_letter = Koha::Notice::Messages->search( {}, { order_by => { -desc => 'message_id' } } )->next;
+
+        # Restore old name for first checkin notice
+        $library_object->branchname( $old_branchname );
+        $library_object->store();
+
+        AddReturn( $item1->{barcode} );
+        my $first_checkin_letter = Koha::Notice::Messages->search( {}, { order_by => { -desc => 'message_id' } } )->next;
+
+        # Change branch name for second checkin notice
+        $library_object->branchname($new_branchname);
+        $library_object->store();
+
+        AddReturn( $item2->{barcode} );
+        my $second_checkin_letter = Koha::Notice::Messages->search( {}, { order_by => { -desc => 'message_id' } } )->next;
+
+        # Restore old name for first TT checkout notice
+        $library_object->branchname( $old_branchname );
+        $library_object->store();
+
+        Koha::Notice::Messages->delete;
+
+        # TT syntax
+        $checkout_template = q|
+[% branch.branchname %]
+----
+----
+|;
+        reset_template( { template => $checkout_template, code => $checkout_code, module => 'circulation' } );
+        $checkin_template = q[
+[% branch.branchname %]
+----
+----
+];
+        reset_template( { template => $checkin_template, code => $checkin_code, module => 'circulation' } );
+
+        C4::Circulation::AddIssue( $patron, $item1->{barcode} );
+        my $first_checkout_tt_letter = Koha::Notice::Messages->search( {}, { order_by => { -desc => 'message_id' } } )->next;
+
+        # Change branch name for second checkout notice
+        $library_object->branchname($new_branchname);
+        $library_object->store();
+
+        C4::Circulation::AddIssue( $patron, $item2->{barcode} );
+        my $second_checkout_tt_letter = Koha::Notice::Messages->search( {}, { order_by => { -desc => 'message_id' } } )->next;
+
+        # Restore old name for first checkin notice
+        $library_object->branchname( $old_branchname );
+        $library_object->store();
+
+        AddReturn( $item1->{barcode} );
+        my $first_checkin_tt_letter = Koha::Notice::Messages->search( {}, { order_by => { -desc => 'message_id' } } )->next;
+#
+        # Change branch name for second checkin notice
+        $library_object->branchname($new_branchname);
+        $library_object->store();
+
+        AddReturn( $item2->{barcode} );
+        my $second_checkin_tt_letter = Koha::Notice::Messages->search( {}, { order_by => { -desc => 'message_id' } } )->next;
+
+        my $first_letter = qq[
+$old_branchname
+];
+        my $second_letter = qq[
+$new_branchname
+];
+
+
+        is( $first_checkout_letter->content, $first_letter, 'Verify first checkout letter' );
+        is( $second_checkout_letter->content, $second_letter, 'Verify second checkout letter' );
+        is( $first_checkin_letter->content, $first_letter, 'Verify first checkin letter'  );
+        is( $second_checkin_letter->content, $second_letter, 'Verify second checkin letter' );
+
+        is( $first_checkout_tt_letter->content, $first_letter, 'Verify TT first checkout letter' );
+        is( $second_checkout_tt_letter->content, $second_letter, 'Verify TT second checkout letter' );
+        is( $first_checkin_tt_letter->content, $first_letter, 'Verify TT first checkin letter'  );
+        is( $second_checkin_tt_letter->content, $second_letter, 'Verify TT second checkin letter' );
+    };
+
 };
 
 subtest 'loops' => sub {
@@ -752,6 +1018,46 @@ subtest 'loops' => sub {
     };
 };
 
+subtest 'add_tt_filters' => sub {
+    plan tests => 1;
+    my $code   = "TEST";
+    my $module = "TEST";
+
+    my $patron = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value => { surname => "with_punctuation_" }
+        }
+    );
+    my $biblio = $builder->build_object(
+        { class => 'Koha::Biblios', value => { title => "with_punctuation_" } }
+    );
+    my $biblioitem = $builder->build_object(
+        {
+            class => 'Koha::Biblioitems',
+            value => {
+                biblionumber => $biblio->biblionumber,
+                isbn         => "with_punctuation_"
+            }
+        }
+    );
+
+    my $template = q|patron=[% borrower.surname %];biblio=[% biblio.title %];biblioitems=[% biblioitem.isbn %]|;
+    reset_template( { template => $template, code => $code, module => $module } );
+    my $letter = GetPreparedLetter(
+        module      => $module,
+        letter_code => $code,
+        tables      => {
+            borrowers   => $patron->borrowernumber,
+            biblio      => $biblio->biblionumber,
+            biblioitems => $biblioitem->biblioitemnumber
+        }
+    );
+    my $expected_letter = q|patron=with_punctuation_;biblio=with_punctuation;biblioitems=with_punctuation|;
+    is( $letter->{content}, $expected_letter, "Pre-processing should call TT plugin to remove punctuation if table is biblio or biblioitems");
+};
+
+
 sub reset_template {
     my ( $params ) = @_;
     my $template   = $params->{template};