Bug 26384: Fix executable flags
[koha.git] / t / db_dependent / Labels / t_Batch.t
blobb74917b514a33818329a19d00c032f57652a51c9
1 #!/usr/bin/perl
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>.
21 use Modern::Perl;
23 use Test::More tests => 25;
24 use MARC::Record;
25 use MARC::Field;
27 use t::lib::TestBuilder;
29 use C4::Context;
30 use C4::Items;
31 use C4::Biblio;
32 use Koha::Database;
33 use Koha::Libraries;
35 BEGIN {
36 use_ok('C4::Labels::Batch');
39 my $schema = Koha::Database->new->schema;
40 $schema->storage->txn_begin;
41 my $dbh = C4::Context->dbh;
43 my $builder = t::lib::TestBuilder->new;
44 $builder->build({ source => 'Branch', value => { branchcode => 'CPL' } })
45 unless Koha::Libraries->find('CPL');
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;
52 my $expected_batch = {
53 description => '',
54 creator => 'Labels',
55 items => [],
56 branch_code => $branch_code,
57 batch_stat => 0, # False if any data has changed and the db has not been updated
60 my $batch = 0;
61 my $item_number = 0;
63 # Testing Batch->new() method.
64 ok($batch = C4::Labels::Batch->new(branch_code => $branch_code), "C4::Labels::Batch-->new() succeeds");
65 my $batch_id = $batch->get_attr('batch_id');
66 $expected_batch->{'batch_id'} = $batch_id;
67 is_deeply($batch, $expected_batch, "New batch object is correct.");
69 # Testing Batch->get_attr() method.
70 foreach my $key (keys %{$expected_batch}) {
71 if (ref($expected_batch->{$key}) eq 'ARRAY') {
72 ok(ref($expected_batch->{$key}) eq ref($batch->get_attr($key)), "Batch->get_attr() SUCCESS on attribute $key.");
74 else {
75 ok($expected_batch->{$key} eq $batch->get_attr($key), "Batch->get_attr() SUCCESS on attribute $key.");
79 # Testing Batch->add_item() method.
80 # Create the item
81 my ( $f_holdingbranch, $sf_holdingbranch ) = GetMarcFromKohaField( 'items.holdingbranch' );
82 my ( $f_homebranch, $sf_homebranch ) = GetMarcFromKohaField( 'items.homebranch' );
83 is( $f_holdingbranch, $f_homebranch, "items information should be in the same field" );
84 my $field = $f_holdingbranch;
86 my $record = MARC::Record->new();
87 $record->append_fields(
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 MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
99 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
100 my @iteminfo = C4::Items::AddItemBatchFromMarc( $record, $biblionumber, $biblioitemnumber, '' );
102 my $itemnumbers = $iteminfo[0];
104 for my $itemnumber ( @$itemnumbers ) {
105 ok($batch->add_item($itemnumber) eq 0 , "Batch->add_item() success.");
107 $batch_id = $batch->get_attr('batch_id');
109 # Testing Batch->retrieve() method.
110 ok(my $saved_batch = C4::Labels::Batch->retrieve(batch_id => $batch_id), "Batch->retrieve() success.");
111 is_deeply($saved_batch, $batch, "Batch object retrieved correctly" );
113 # Testing Batch->remove_item() method.
114 my $itemnumber = @$itemnumbers[0];
115 ok($batch->remove_item($itemnumber) eq 0, "Batch->remove_item() success.");
117 my $updated_batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
118 is_deeply($updated_batch, $batch, "Updated batch object is correct.");
120 # Testing Batch->delete() method.
121 my $del_results = $batch->delete();
122 ok($del_results eq 0, "Batch->delete() success.");