Bug 21281: Correct t/db_dependent/Creators/Lib.t failures
[koha.git] / t / db_dependent / Labels / t_Batch.t
1 #!/usr/bin/perl
2 #
3 # Copyright 2007 Foundations Bible College.
4 # Copyright 2013 BibLibre
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
17 #
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>.
20
21 use Modern::Perl;
22
23 use Test::More tests => 24;
24 use MARC::Record;
25 use MARC::Field;
26
27 use t::lib::TestBuilder;
28
29 use C4::Context;
30 use C4::Biblio;
31 use C4::Items;
32 use Koha::Libraries;
33
34 BEGIN {
35     use_ok('C4::Labels::Batch');
36 }
37
38 # Start transaction
39 my $dbh = C4::Context->dbh;
40 $dbh->{AutoCommit} = 0;
41 $dbh->{RaiseError} = 1;
42
43 my $builder = t::lib::TestBuilder->new;
44 $builder->build({ source => 'Branch', value => { branchcode => 'CPL' } })
45     unless Koha::Libraries->find('CPL');
46
47 my $sth = $dbh->prepare('SELECT branchcode FROM branches b LIMIT 0,1');
48 $sth->execute();
49 my $branch_code = $sth->fetchrow_hashref()->{'branchcode'};
50 diag sprintf('Database returned the following error: %s', $sth->errstr) if $sth->errstr;
51
52 my $expected_batch = {
53         creator         => 'Labels',
54         items           => [],
55         branch_code     => $branch_code,
56         batch_stat      => 0,   # False if any data has changed and the db has not been updated
57     };
58
59 my $batch = 0;
60 my $item_number = 0;
61
62 # Testing Batch->new() method.
63 ok($batch = C4::Labels::Batch->new(branch_code => $branch_code), "C4::Labels::Batch-->new() succeeds");
64 my $batch_id = $batch->get_attr('batch_id');
65 $expected_batch->{'batch_id'} = $batch_id;
66 is_deeply($batch, $expected_batch, "New batch object is correct.");
67
68 # Testing Batch->get_attr() method.
69 foreach my $key (keys %{$expected_batch}) {
70     if (ref($expected_batch->{$key}) eq 'ARRAY') {
71         ok(ref($expected_batch->{$key}) eq ref($batch->get_attr($key)), "Batch->get_attr() SUCCESS on attribute $key.");
72     }
73     else {
74         ok($expected_batch->{$key} eq $batch->get_attr($key), "Batch->get_attr() SUCCESS on attribute $key.");
75     }
76 }
77
78 # Testing Batch->add_item() method.
79 # Create the item
80 my ( $f_holdingbranch, $sf_holdingbranch ) = GetMarcFromKohaField( 'items.holdingbranch' );
81 my ( $f_homebranch, $sf_homebranch ) = GetMarcFromKohaField( 'items.homebranch' );
82 is( $f_holdingbranch, $f_homebranch, "items information should be in the same field" );
83 my $field = $f_holdingbranch;
84
85 my $record = MARC::Record->new();
86 $record->append_fields(
87     MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
88     MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
89     MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
90     MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
91     MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
92     MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
93     MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
94     MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
95     MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
96     MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
97 );
98 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
99 my @iteminfo = C4::Items::AddItemBatchFromMarc( $record, $biblionumber, $biblioitemnumber, '' );
100
101 my $itemnumbers = $iteminfo[0];
102
103 for my $itemnumber ( @$itemnumbers ) {
104     ok($batch->add_item($itemnumber) eq 0 , "Batch->add_item() success.");
105 }
106 $batch_id = $batch->get_attr('batch_id');
107
108 # Testing Batch->retrieve() method.
109 ok(my $saved_batch = C4::Labels::Batch->retrieve(batch_id => $batch_id), "Batch->retrieve() success.");
110 is_deeply($saved_batch, $batch, "Batch object retrieved correctly" );
111
112 # Testing Batch->remove_item() method.
113 my $itemnumber = @$itemnumbers[0];
114 ok($batch->remove_item($itemnumber) eq 0, "Batch->remove_item() success.");
115
116 my $updated_batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
117 is_deeply($updated_batch, $batch, "Updated batch object is correct.");
118
119 # Testing Batch->delete() method.
120 my $del_results = $batch->delete();
121 ok($del_results eq 0, "Batch->delete() success.");
122
123 $dbh->rollback;