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 = C4::Context->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;
43 my $expected_batch = {
46 branch_code => $branch_code,
47 batch_stat => 0, # False if any data has changed and the db has not been updated
53 diag "Testing Batch->new() method.";
54 ok($batch = C4::Labels::Batch->new(branch_code => $branch_code)) || diag "Batch->new() FAILED.";
55 my $batch_id = $batch->get_attr('batch_id');
56 $expected_batch->{'batch_id'} = $batch_id;
57 is_deeply($batch, $expected_batch) || diag "New batch object FAILED to verify.";
59 diag "Testing Batch->get_attr() method.";
60 foreach my $key (keys %{$expected_batch}) {
61 if (ref($expected_batch->{$key}) eq 'ARRAY') {
62 ok(ref($expected_batch->{$key}) eq ref($batch->get_attr($key))) || diag "Batch->get_attr() FAILED on attribute $key.";
65 ok($expected_batch->{$key} eq $batch->get_attr($key)) || diag "Batch->get_attr() FAILED on attribute $key.";
69 diag "Testing Batch->add_item() method.";
71 my ( $f_holdingbranch, $sf_holdingbranch ) = GetMarcFromKohaField( 'items.holdingbranch' );
72 my ( $f_homebranch, $sf_homebranch ) = GetMarcFromKohaField( 'items.homebranch' );
73 is( $f_holdingbranch, $f_homebranch, "items information should be in the same field" );
74 my $field = $f_holdingbranch;
76 my $record = MARC::Record->new();
77 $record->append_fields(
78 MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
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' ),
89 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
90 my @iteminfo = C4::Items::AddItemBatchFromMarc( $record, $biblionumber, $biblioitemnumber, '' );
92 my $itemnumbers = $iteminfo[0];
94 for my $itemnumber ( @$itemnumbers ) {
95 ok($batch->add_item($itemnumber) eq 0 ) || diag "Batch->add_item() FAILED.";
98 diag "Testing Batch->retrieve() method.";
99 ok(my $saved_batch = C4::Labels::Batch->retrieve(batch_id => $batch_id)) || diag "Batch->retrieve() FAILED.";
100 is_deeply($saved_batch, $batch) || diag "Retrieved batch object FAILED to verify.";
102 diag "Testing Batch->remove_item() method.";
104 my $itemnumber = @$itemnumbers[0];
105 ok($batch->remove_item($itemnumber) eq 0) || diag "Batch->remove_item() FAILED.";
107 my $updated_batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
108 is_deeply($updated_batch, $batch) || diag "Updated batch object FAILED to verify.";
110 diag "Testing Batch->delete() method.";
112 my $del_results = $batch->delete();
113 ok($del_results eq 0) || diag "Batch->delete() FAILED.";