3 # Copyright 2007 Foundations Bible College.
4 # Copyright 2013 BibLibre
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 use Test::More tests => 24;
31 use_ok('C4::Labels::Batch');
35 my $dbh = C4::Context->dbh;
36 $dbh->{AutoCommit} = 0;
37 $dbh->{RaiseError} = 1;
39 my $sth = $dbh->prepare('SELECT branchcode FROM branches b LIMIT 0,1');
41 my $branch_code = $sth->fetchrow_hashref()->{'branchcode'};
42 diag sprintf('Database returned the following error: %s', $sth->errstr) if $sth->errstr;
44 my $expected_batch = {
47 branch_code => $branch_code,
48 batch_stat => 0, # False if any data has changed and the db has not been updated
54 # Testing Batch->new() method.
55 ok($batch = C4::Labels::Batch->new(branch_code => $branch_code), "C4::Labels::Batch-->new() succeeds");
56 my $batch_id = $batch->get_attr('batch_id');
57 $expected_batch->{'batch_id'} = $batch_id;
58 is_deeply($batch, $expected_batch, "New batch object is correct.");
60 # Testing Batch->get_attr() method.
61 foreach my $key (keys %{$expected_batch}) {
62 if (ref($expected_batch->{$key}) eq 'ARRAY') {
63 ok(ref($expected_batch->{$key}) eq ref($batch->get_attr($key)), "Batch->get_attr() SUCCESS on attribute $key.");
66 ok($expected_batch->{$key} eq $batch->get_attr($key), "Batch->get_attr() SUCCESS on attribute $key.");
70 # Testing Batch->add_item() method.
72 my ( $f_holdingbranch, $sf_holdingbranch ) = GetMarcFromKohaField( 'items.holdingbranch' );
73 my ( $f_homebranch, $sf_homebranch ) = GetMarcFromKohaField( 'items.homebranch' );
74 is( $f_holdingbranch, $f_homebranch, "items information should be in the same field" );
75 my $field = $f_holdingbranch;
77 my $record = MARC::Record->new();
78 $record->append_fields(
79 MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
80 MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
81 MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
82 MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
83 MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
84 MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
85 MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
86 MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
87 MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
88 MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
90 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
91 my @iteminfo = C4::Items::AddItemBatchFromMarc( $record, $biblionumber, $biblioitemnumber, '' );
93 my $itemnumbers = $iteminfo[0];
95 for my $itemnumber ( @$itemnumbers ) {
96 ok($batch->add_item($itemnumber) eq 0 , "Batch->add_item() success.");
98 $batch_id = $batch->get_attr('batch_id');
100 # Testing Batch->retrieve() method.
101 ok(my $saved_batch = C4::Labels::Batch->retrieve(batch_id => $batch_id), "Batch->retrieve() success.");
102 is_deeply($saved_batch, $batch, "Batch object retrieved correctly" );
104 # Testing Batch->remove_item() method.
105 my $itemnumber = @$itemnumbers[0];
106 ok($batch->remove_item($itemnumber) eq 0, "Batch->remove_item() success.");
108 my $updated_batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
109 is_deeply($updated_batch, $batch, "Updated batch object is correct.");
111 # Testing Batch->delete() method.
112 my $del_results = $batch->delete();
113 ok($del_results eq 0, "Batch->delete() success.");