Bug 17600: Standardize our EXPORT_OK
[srvgit] / t / db_dependent / Labels / t_Batch.t
old mode 100644 (file)
new mode 100755 (executable)
index 9fb4a84..61a3710
@@ -1,38 +1,57 @@
 #!/usr/bin/perl
 #
 # Copyright 2007 Foundations Bible College.
+# Copyright 2013 BibLibre
 #
 # This file is part of Koha.
-#       
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# You should have received a copy of the GNU General Public License along with
-# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Test::More tests => 25;
+use MARC::Record;
+use MARC::Field;
 
-use strict;
-use warnings;
+use t::lib::TestBuilder;
 
-use Test::More tests => 22;
 use C4::Context;
-use Data::Dumper;
+use C4::Items qw( AddItemBatchFromMarc );
+use C4::Biblio qw( GetMarcFromKohaField AddBiblio );
+use Koha::Database;
+use Koha::Libraries;
 
 BEGIN {
-    use_ok('C4::Labels::Batch');
+    use_ok('C4::Labels::Batch', qw( save retrieve delete ));
 }
 
-my $sth = C4::Context->dbh->prepare('SELECT branchcode FROM branches b LIMIT 0,1');
+my $schema = Koha::Database->new->schema;
+$schema->storage->txn_begin;
+my $dbh = C4::Context->dbh;
+
+my $builder = t::lib::TestBuilder->new;
+$builder->build({ source => 'Branch', value => { branchcode => 'CPL' } })
+    unless Koha::Libraries->find('CPL');
+
+my $sth = $dbh->prepare('SELECT branchcode FROM branches b LIMIT 0,1');
 $sth->execute();
 my $branch_code = $sth->fetchrow_hashref()->{'branchcode'};
 diag sprintf('Database returned the following error: %s', $sth->errstr) if $sth->errstr;
+
 my $expected_batch = {
+        description     => '',
+        creator         => 'Labels',
         items           => [],
         branch_code     => $branch_code,
         batch_stat      => 0,   # False if any data has changed and the db has not been updated
@@ -41,42 +60,63 @@ my $expected_batch = {
 my $batch = 0;
 my $item_number = 0;
 
-diag "Testing Batch->new() method.";
-ok($batch = C4::Labels::Batch->new(branch_code => $branch_code)) || diag "Batch->new() FAILED.";
+# Testing Batch->new() method.
+ok($batch = C4::Labels::Batch->new(branch_code => $branch_code), "C4::Labels::Batch-->new() succeeds");
 my $batch_id = $batch->get_attr('batch_id');
 $expected_batch->{'batch_id'} = $batch_id;
-is_deeply($batch, $expected_batch) || diag "New batch object FAILED to verify.";
+is_deeply($batch, $expected_batch, "New batch object is correct.");
 
-diag "Testing Batch->get_attr() method.";
+# Testing Batch->get_attr() method.
 foreach my $key (keys %{$expected_batch}) {
     if (ref($expected_batch->{$key}) eq 'ARRAY') {
-        ok(ref($expected_batch->{$key}) eq ref($batch->get_attr($key))) || diag "Batch->get_attr() FAILED on attribute $key.";
+        ok(ref($expected_batch->{$key}) eq ref($batch->get_attr($key)), "Batch->get_attr() SUCCESS on attribute $key.");
     }
     else {
-        ok($expected_batch->{$key} eq $batch->get_attr($key)) || diag "Batch->get_attr() FAILED on attribute $key.";
+        ok($expected_batch->{$key} eq $batch->get_attr($key), "Batch->get_attr() SUCCESS on attribute $key.");
     }
 }
 
-diag "Testing Batch->add_item() method.";
-my $sth1 = C4::Context->dbh->prepare('SELECT itemnumber FROM items LIMIT 0,10');
-$sth1->execute();
-while (my $row = $sth1->fetchrow_hashref()) {
-    diag sprintf('Database returned the following error: %s', $sth1->errstr) if $sth1->errstr;
-    ok($batch->add_item($row->{'itemnumber'}) eq 0 ) || diag "Batch->add_item() FAILED.";
-    $item_number = $row->{'itemnumber'};
+# Testing Batch->add_item() method.
+# Create the item
+my ( $f_holdingbranch, $sf_holdingbranch ) = GetMarcFromKohaField( 'items.holdingbranch' );
+my ( $f_homebranch, $sf_homebranch ) = GetMarcFromKohaField( 'items.homebranch' );
+is( $f_holdingbranch, $f_homebranch, "items information should be in the same field" );
+my $field = $f_holdingbranch;
+
+my $record = MARC::Record->new();
+$record->append_fields(
+    MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
+    MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
+    MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
+    MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
+    MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
+    MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
+    MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
+    MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
+    MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
+    MARC::Field->new( $field, '', '', $sf_homebranch => 'CPL', $sf_holdingbranch => 'CPL' ),
+);
+my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
+my @iteminfo = C4::Items::AddItemBatchFromMarc( $record, $biblionumber, $biblioitemnumber, '' );
+
+my $itemnumbers = $iteminfo[0];
+
+for my $itemnumber ( @$itemnumbers ) {
+    ok($batch->add_item($itemnumber) eq 0 , "Batch->add_item() success.");
 }
+$batch_id = $batch->get_attr('batch_id');
 
-diag "Testing Batch->retrieve() method.";
-ok(my $saved_batch = C4::Labels::Batch->retrieve(batch_id => $batch_id)) || diag "Batch->retrieve() FAILED.";
-is_deeply($saved_batch, $batch) || diag "Retrieved batch object FAILED to verify.";
+# Testing Batch->retrieve() method.
+ok(my $saved_batch = C4::Labels::Batch->retrieve(batch_id => $batch_id), "Batch->retrieve() success.");
+is_deeply($saved_batch, $batch, "Batch object retrieved correctly" );
 
-diag "Testing Batch->remove_item() method.";
+# Testing Batch->remove_item() method.
+my $itemnumber = @$itemnumbers[0];
+ok($batch->remove_item($itemnumber) eq 0, "Batch->remove_item() success.");
 
-ok($batch->remove_item($item_number) eq 0) || diag "Batch->remove_item() FAILED.";
 my $updated_batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
-is_deeply($updated_batch, $batch) || diag "Updated batch object FAILED to verify.";
-
-diag "Testing Batch->delete() method.";
+is_deeply($updated_batch, $batch, "Updated batch object is correct.");
 
+# Testing Batch->delete() method.
 my $del_results = $batch->delete();
-ok($del_results eq 0) || diag "Batch->delete() FAILED.";
+ok($del_results eq 0, "Batch->delete() success.");