Bug 10333: Labels/t_Batch.t needs to create its own data
[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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use Modern::Perl;
22
23 use Test::More tests => 24;
24 use C4::Context;
25 use MARC::Record;
26 use MARC::Field;
27 use C4::Biblio;
28 use C4::Items;
29
30 BEGIN {
31     use_ok('C4::Labels::Batch');
32 }
33
34 # Start transaction
35 my $dbh = C4::Context->dbh;
36 $dbh->{AutoCommit} = 0;
37 $dbh->{RaiseError} = 1;
38
39 my $sth = C4::Context->dbh->prepare('SELECT branchcode FROM branches b LIMIT 0,1');
40 $sth->execute();
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 = {
44         creator         => 'Labels',
45         items           => [],
46         branch_code     => $branch_code,
47         batch_stat      => 0,   # False if any data has changed and the db has not been updated
48     };
49
50 my $batch = 0;
51 my $item_number = 0;
52
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.";
58
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.";
63     }
64     else {
65         ok($expected_batch->{$key} eq $batch->get_attr($key)) || diag "Batch->get_attr() FAILED on attribute $key.";
66     }
67 }
68
69 diag "Testing Batch->add_item() method.";
70 # Create the item
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;
75
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' ),
88 );
89 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
90 my @iteminfo = C4::Items::AddItemBatchFromMarc( $record, $biblionumber, $biblioitemnumber, '' );
91
92 my $itemnumbers = $iteminfo[0];
93
94 for my $itemnumber ( @$itemnumbers ) {
95     ok($batch->add_item($itemnumber) eq 0 ) || diag "Batch->add_item() FAILED.";
96 }
97
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.";
101
102 diag "Testing Batch->remove_item() method.";
103
104 my $itemnumber = @$itemnumbers[0];
105 ok($batch->remove_item($itemnumber) eq 0) || diag "Batch->remove_item() FAILED.";
106
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.";
109
110 diag "Testing Batch->delete() method.";
111
112 my $del_results = $batch->delete();
113 ok($del_results eq 0) || diag "Batch->delete() FAILED.";
114
115 $dbh->rollback;