Bug 10692: add unit tests for C4::Circulation routines about transfers
[koha_fer] / t / db_dependent / Circulation_transfers.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use C4::Biblio;
5 use C4::Context;
6 use C4::Items;
7 use C4::Branch;
8 use C4::Circulation;
9 use Koha::DateUtils;
10 use DateTime::Duration;
11
12 use Test::More tests => 12;
13
14 BEGIN {
15     use_ok('C4::Circulation');
16 }
17 can_ok(
18     'C4::Circulation',
19     qw(
20       CreateBranchTransferLimit
21       DeleteBranchTransferLimits
22       DeleteTransfer
23       GetTransfers
24       GetTransfersFromTo
25       )
26 );
27
28 #Start transaction
29 my $dbh = C4::Context->dbh;
30 $dbh->{RaiseError} = 1;
31 $dbh->{AutoCommit} = 0;
32
33 $dbh->do(q|DELETE FROM issues|);
34 $dbh->do(q|DELETE FROM borrowers|);
35 $dbh->do(q|DELETE FROM items|);
36 $dbh->do(q|DELETE FROM branches|);
37 $dbh->do(q|DELETE FROM branch_transfer_limits|);
38 $dbh->do(q|DELETE FROM branchtransfers|);
39
40 #Add sample datas
41 #Add branches
42 my $samplebranch1 = {
43     add            => 1,
44     branchcode     => 'SAB1',
45     branchname     => 'Sample Branch',
46     branchaddress1 => 'sample adr1',
47     branchaddress2 => 'sample adr2',
48     branchaddress3 => 'sample adr3',
49     branchzip      => 'sample zip',
50     branchcity     => 'sample city',
51     branchstate    => 'sample state',
52     branchcountry  => 'sample country',
53     branchphone    => 'sample phone',
54     branchfax      => 'sample fax',
55     branchemail    => 'sample email',
56     branchurl      => 'sample url',
57     branchip       => 'sample ip',
58     branchprinter  => undef,
59     opac_info      => 'sample opac',
60 };
61 my $samplebranch2 = {
62     add            => 1,
63     branchcode     => 'SAB2',
64     branchname     => 'Sample Branch2',
65     branchaddress1 => 'sample adr1_2',
66     branchaddress2 => 'sample adr2_2',
67     branchaddress3 => 'sample adr3_2',
68     branchzip      => 'sample zip2',
69     branchcity     => 'sample city2',
70     branchstate    => 'sample state2',
71     branchcountry  => 'sample country2',
72     branchphone    => 'sample phone2',
73     branchfax      => 'sample fax2',
74     branchemail    => 'sample email2',
75     branchurl      => 'sample url2',
76     branchip       => 'sample ip2',
77     branchprinter  => undef,
78     opac_info      => 'sample opac2',
79 };
80 ModBranch($samplebranch1);
81 ModBranch($samplebranch2);
82
83 #Add biblio and items
84 my $record = MARC::Record->new();
85 $record->append_fields(
86     MARC::Field->new( '952', '0', '0', a => $samplebranch1->{branchcode} ) );
87 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '', );
88
89 my @sampleitem1 = C4::Items::AddItem(
90     {
91         barcode        => 1,
92         itemcallnumber => 'callnumber1',
93         homebranch     => $samplebranch1->{branchcode},
94         holdingbranch  => $samplebranch1->{branchcode}
95     },
96     $biblionumber
97 );
98 my $item_id1    = $sampleitem1[2];
99 my @sampleitem2 = C4::Items::AddItem(
100     {
101         barcode        => 2,
102         itemcallnumber => 'callnumber2',
103         homebranch     => $samplebranch1->{branchcode},
104         holdingbranch  => $samplebranch1->{branchcode}
105     },
106     $biblionumber
107 );
108 my $item_id2 = $sampleitem2[2];
109
110 #Add transfers
111 ModItemTransfer(
112     $item_id1,
113     $samplebranch1->{branchcode},
114     $samplebranch2->{branchcode}
115 );
116 ModItemTransfer(
117     $item_id2,
118     $samplebranch1->{branchcode},
119     $samplebranch2->{branchcode}
120 );
121
122 #Begin Tests
123 #Test CreateBranchTransferLimit
124 is(
125     CreateBranchTransferLimit(
126         $samplebranch2->{branchcode},
127         $samplebranch1->{branchcode}, 'CODE'
128     ),
129     1,
130     "A Branch TransferLimit has been added"
131 );
132 #FIXME :The following test should pass but doesn't because currently the routine CreateBranchTransferLimit returns nothing
133 #is(CreateBranchTransferLimit(),undef,"Without parameters CreateBranchTransferLimit returns undef");
134
135 #Test GetTransfers
136 my $dt_today = dt_from_string( undef, 'sql', undef );
137 my $today = $dt_today->strftime("%Y-%m-%d %H:%M:%S");
138
139 my @transfers = GetTransfers($item_id1);
140 is_deeply(
141     \@transfers,
142     [ $today, $samplebranch1->{branchcode}, $samplebranch2->{branchcode} ],
143     "Transfers of the item1"
144 );    #NOTE: Only the first transfer is returned
145 @transfers = GetTransfers;
146 is_deeply( \@transfers, [],
147     "GetTransfers without params returns an empty array" );
148 @transfers = GetTransfers(-1);
149 is_deeply( \@transfers, [],
150     "GetTransfers with a wrong item id returns an empty array" );
151
152 #Test GetTransfersFromTo
153 my @transferfrom1to2 = GetTransfersFromTo( $samplebranch1->{branchcode},
154     $samplebranch2->{branchcode} );
155 is_deeply(
156     \@transferfrom1to2,
157     [
158         {
159             itemnumber => $item_id1,
160             datesent   => $today,
161             frombranch => $samplebranch1->{branchcode}
162         },
163         {
164             itemnumber => $item_id2,
165             datesent   => $today,
166             frombranch => $samplebranch1->{branchcode}
167         }
168     ],
169     "Item1 and Item2 has been transfered from branch1 to branch2"
170 );
171 my @transferto = GetTransfersFromTo( undef, $samplebranch2->{branchcode} );
172 is_deeply( \@transferto, [],
173     "GetTransfersfromTo without frombranch returns an empty array" );
174 my @transferfrom = GetTransfersFromTo( $samplebranch1->{branchcode} );
175 is_deeply( \@transferfrom, [],
176     "GetTransfersfromTo without tobranch returns an empty array" );
177 @transferfrom = GetTransfersFromTo();
178 is_deeply( \@transferfrom, [],
179     "GetTransfersfromTo without params returns an empty array" );
180
181 #Test DeleteBranchTransferLimits
182 is(
183     C4::Circulation::DeleteBranchTransferLimits( $samplebranch1->{branchcode} ),
184     1,
185     "A Branch TransferLimit has been deleted"
186 );
187 #FIXME :The following test should pass but doesn't because currently the routine DeleteBranchTransferLimit returns nothin
188 #is(C4::Circulation::DeleteBranchTransferLimits(),undef,"Without parameters DeleteBranchTransferLimit returns undef");
189
190 #Test DeleteTransfer
191 is( C4::Circulation::DeleteTransfer($item_id1),
192     1, "A the item1's transfer has been deleted" );
193 #FIXME :The following tests should pass but don't because currently the routine DeleteTransfer returns nothing
194 #is(C4::Circulation::DeleteTransfer(),undef,"Without itemid DeleteTransfer returns undef");
195 #is(C4::Circulation::DeleteTransfer(-1),0,"with a wrong itemid DeleteTranfer returns 0");
196
197 #End transaction
198 $dbh->rollback;