Bug 14334: Remove AutoCommit from tests
[koha.git] / t / db_dependent / Circulation / OfflineOperation.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use C4::Circulation;
5
6 use Koha::Database;
7 use Koha::DateUtils qw( dt_from_string output_pref );
8 use Koha::Library;
9
10 use Test::More tests => 7;
11
12 BEGIN {
13     use_ok('C4::Circulation');
14 }
15 can_ok(
16     'C4::Circulation',
17     qw(
18       AddOfflineOperation
19       GetOfflineOperation
20       GetOfflineOperations
21       DeleteOfflineOperation
22       )
23 );
24
25 my $schema = Koha::Database->new->schema;
26 $schema->storage->txn_begin;
27 my $dbh = C4::Context->dbh;
28
29 $dbh->do(q|DELETE FROM issues|);
30 $dbh->do(q|DELETE FROM borrowers|);
31 $dbh->do(q|DELETE FROM items|);
32 $dbh->do(q|DELETE FROM branches|);
33 $dbh->do(q|DELETE FROM pending_offline_operations|);
34
35 #Add branch
36 my $samplebranch1 = {
37     branchcode     => 'SAB1',
38     branchname     => 'Sample Branch',
39     branchaddress1 => 'sample adr1',
40     branchaddress2 => 'sample adr2',
41     branchaddress3 => 'sample adr3',
42     branchzip      => 'sample zip',
43     branchcity     => 'sample city',
44     branchstate    => 'sample state',
45     branchcountry  => 'sample country',
46     branchphone    => 'sample phone',
47     branchfax      => 'sample fax',
48     branchemail    => 'sample email',
49     branchurl      => 'sample url',
50     branchip       => 'sample ip',
51     branchprinter  => undef,
52     opac_info      => 'sample opac',
53 };
54 Koha::Library->new($samplebranch1)->store;
55
56 my $now = dt_from_string->truncate( to => 'minute' );
57
58 #Begin Tests
59 #Test AddOfflineOperation
60 is(
61     AddOfflineOperation(
62         'User1', $samplebranch1->{branchcode},
63         $now, 'Action1', 'CODE', 'Cardnumber1', 10
64     ),
65     'Added.',
66     "OfflineOperation has been added"
67 );
68 my $offline_id =
69   $dbh->last_insert_id( undef, undef, 'pending_offline_operations', undef );
70
71 #Test GetOfflineOperations
72 is_deeply(
73     GetOfflineOperation($offline_id),
74     {
75         operationid => $offline_id,
76         userid      => 'User1',
77         branchcode  => $samplebranch1->{branchcode},
78         # FIXME sounds like we need a 'timestamp' dateformat
79         timestamp   => output_pref({ dt => $now, dateformat => 'iso', dateonly => 0 }) . ':00',
80         action      => 'Action1',
81         barcode     => 'CODE',
82         cardnumber  => 'Cardnumber1',
83         amount      => '10.000000'
84     },
85     "GetOffline returns offlineoperation's informations"
86 );
87 is( GetOfflineOperation(), undef,
88     'GetOfflineOperation without parameters returns undef' );
89 is( GetOfflineOperation(-1), undef,
90     'GetOfflineOperation with wrong parameters returns undef' );
91
92 #Test GetOfflineOperations
93 #TODO later: test GetOfflineOperations
94 # Actually we cannot mock C4::Context->userenv in unit tests
95
96 #Test DeleteOfflineOperation
97 is( DeleteOfflineOperation($offline_id),
98     'Deleted.', 'Offlineoperation has been deleted' );
99
100 #is (DeleteOfflineOperation(), undef, 'DeleteOfflineOperation without id returns undef');
101 #is (DeleteOfflineOperation(-1),undef, 'DeleteOfflineOperation with a wrong id returns undef');#FIXME