Bug 19403: Prevent Circulation.t to fail randomly
[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 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 = $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
44 my $expected_batch = {
45         creator         => 'Labels',
46         items           => [],
47         branch_code     => $branch_code,
48         batch_stat      => 0,   # False if any data has changed and the db has not been updated
49     };
50
51 my $batch = 0;
52 my $item_number = 0;
53
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.");
59
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.");
64     }
65     else {
66         ok($expected_batch->{$key} eq $batch->get_attr($key), "Batch->get_attr() SUCCESS on attribute $key.");
67     }
68 }
69
70 # Testing Batch->add_item() method.
71 # Create the item
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;
76
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' ),
89 );
90 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
91 my @iteminfo = C4::Items::AddItemBatchFromMarc( $record, $biblionumber, $biblioitemnumber, '' );
92
93 my $itemnumbers = $iteminfo[0];
94
95 for my $itemnumber ( @$itemnumbers ) {
96     ok($batch->add_item($itemnumber) eq 0 , "Batch->add_item() success.");
97 }
98 $batch_id = $batch->get_attr('batch_id');
99
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" );
103
104 # Testing Batch->remove_item() method.
105 my $itemnumber = @$itemnumbers[0];
106 ok($batch->remove_item($itemnumber) eq 0, "Batch->remove_item() success.");
107
108 my $updated_batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
109 is_deeply($updated_batch, $batch, "Updated batch object is correct.");
110
111 # Testing Batch->delete() method.
112 my $del_results = $batch->delete();
113 ok($del_results eq 0, "Batch->delete() success.");
114
115 $dbh->rollback;