Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
use Modern::Perl; |
4 |
|
5 |
use Test::More tests => 7; |
6 |
|
7 |
use C4::Serials; |
8 |
use C4::Serials::RoutingLists; |
9 |
|
10 |
use C4::Biblio; |
11 |
use C4::Members; |
12 |
|
13 |
use Koha::Database; |
14 |
|
15 |
my $dbh = C4::Context->dbh; |
16 |
$dbh->{AutoCommit} = 0; |
17 |
|
18 |
my $record = MARC::Record->new; |
19 |
my $field = C4::Context->preference('marcflavour') eq 'UNIMARC' |
20 |
? MARC::Field->new(200, '', '', a => 'Title') |
21 |
: MARC::Field->new(245, '', '', a => 'Title'); |
22 |
$record->append_fields($field); |
23 |
my ($biblionumber) = AddBiblio($record, ''); |
24 |
|
25 |
my $subscriptionid = C4::Serials::NewSubscription(undef, '', (undef)x3, |
26 |
$biblionumber, '2015-04-23', (undef)x10, '', '', (undef)x6, 0, '', 0, |
27 |
undef, undef, 0, undef, undef, 0); |
28 |
|
29 |
my $schema = Koha::Database->new->schema; |
30 |
my $categorycode = $schema->resultset('Category')->search->first->categorycode; |
31 |
my $branchcode = $schema->resultset('Branch')->search->first->branchcode; |
32 |
|
33 |
my $borrowernumber1 = C4::Members::AddMember( |
34 |
surname => 'Doe', |
35 |
firstname => 'John', |
36 |
categorycode => $categorycode, |
37 |
branchcode => $branchcode, |
38 |
); |
39 |
my $borrowernumber2 = C4::Members::AddMember( |
40 |
surname => 'Smith', |
41 |
firstname => 'Jane', |
42 |
categorycode => $categorycode, |
43 |
branchcode => $branchcode, |
44 |
); |
45 |
|
46 |
subtest 'AddRoutingList' => sub { |
47 |
plan tests => 9; |
48 |
|
49 |
my $routinglistid; |
50 |
|
51 |
$routinglistid = C4::Serials::RoutingLists::AddRoutingList; |
52 |
ok(not (defined $routinglistid), |
53 |
'AddRoutingList with bad parameters should return undef'); |
54 |
|
55 |
$routinglistid = C4::Serials::RoutingLists::AddRoutingList(-1); |
56 |
ok(not (defined $routinglistid), |
57 |
'AddRoutingList with bad parameters should return undef'); |
58 |
|
59 |
$routinglistid = C4::Serials::RoutingLists::AddRoutingList($subscriptionid); |
60 |
ok(not (defined $routinglistid), |
61 |
'AddRoutingList with bad parameters should return undef'); |
62 |
|
63 |
$routinglistid = C4::Serials::RoutingLists::AddRoutingList($subscriptionid, ''); |
64 |
ok(not (defined $routinglistid), |
65 |
'AddRoutingList with bad parameters should return undef'); |
66 |
|
67 |
$routinglistid = C4::Serials::RoutingLists::AddRoutingList($subscriptionid, 'Title'); |
68 |
ok((defined $routinglistid), |
69 |
'AddRoutingList with good parameters should return an id'); |
70 |
|
71 |
my $routinglist = C4::Serials::RoutingLists::GetRoutingList($routinglistid); |
72 |
is($routinglist->{routinglistid}, $routinglistid, |
73 |
'new routing list id is ok'); |
74 |
is($routinglist->{subscriptionid}, $subscriptionid, |
75 |
'new routing list subscriptionid is ok'); |
76 |
is($routinglist->{title}, |
77 |
'Title', 'new routing list title is ok'); |
78 |
ok(not (defined $routinglist->{notes}), |
79 |
'new routing list has not notes'); |
80 |
}; |
81 |
|
82 |
subtest 'ModRoutingList' => sub { |
83 |
plan tests => 5; |
84 |
|
85 |
my ($routinglist, $expected); |
86 |
|
87 |
my $routinglistid = C4::Serials::RoutingLists::AddRoutingList($subscriptionid, 'Title'); |
88 |
|
89 |
C4::Serials::RoutingLists::ModRoutingList($routinglistid); |
90 |
$routinglist = C4::Serials::RoutingLists::GetRoutingList($routinglistid); |
91 |
$expected = { |
92 |
routinglistid => $routinglistid, |
93 |
subscriptionid => $subscriptionid, |
94 |
title => 'Title', |
95 |
notes => undef, |
96 |
}; |
97 |
is_deeply($routinglist, $expected, 'routing list is as expected'); |
98 |
|
99 |
C4::Serials::RoutingLists::ModRoutingList($routinglistid, undef, 'Title 2'); |
100 |
$routinglist = C4::Serials::RoutingLists::GetRoutingList($routinglistid); |
101 |
$expected = { |
102 |
routinglistid => $routinglistid, |
103 |
subscriptionid => $subscriptionid, |
104 |
title => 'Title 2', |
105 |
notes => undef, |
106 |
}; |
107 |
is_deeply($routinglist, $expected, 'routing list is as expected'); |
108 |
|
109 |
C4::Serials::RoutingLists::ModRoutingList($routinglistid, undef, undef, 'notes'); |
110 |
$routinglist = C4::Serials::RoutingLists::GetRoutingList($routinglistid); |
111 |
$expected = { |
112 |
routinglistid => $routinglistid, |
113 |
subscriptionid => $subscriptionid, |
114 |
title => 'Title 2', |
115 |
notes => 'notes', |
116 |
}; |
117 |
is_deeply($routinglist, $expected, 'routing list is as expected'); |
118 |
|
119 |
C4::Serials::RoutingLists::ModRoutingList($routinglistid, undef, undef, |
120 |
undef, $borrowernumber1, $borrowernumber2); |
121 |
$routinglist = C4::Serials::RoutingLists::GetRoutingList($routinglistid); |
122 |
$expected = { |
123 |
routinglistid => $routinglistid, |
124 |
subscriptionid => $subscriptionid, |
125 |
title => 'Title 2', |
126 |
notes => 'notes', |
127 |
borrowers => [$borrowernumber1, $borrowernumber2], |
128 |
}; |
129 |
is_deeply($routinglist, $expected, 'routing list is as expected'); |
130 |
|
131 |
C4::Serials::RoutingLists::ModRoutingList($routinglistid); |
132 |
$routinglist = C4::Serials::RoutingLists::GetRoutingList($routinglistid); |
133 |
$expected = { |
134 |
routinglistid => $routinglistid, |
135 |
subscriptionid => $subscriptionid, |
136 |
title => 'Title 2', |
137 |
notes => 'notes', |
138 |
}; |
139 |
is_deeply($routinglist, $expected, 'routing list is as expected'); |
140 |
}; |
141 |
|
142 |
subtest 'DelRoutingList' => sub { |
143 |
plan tests => 1; |
144 |
|
145 |
my $routinglist; |
146 |
|
147 |
my $routinglistid = C4::Serials::RoutingLists::AddRoutingList($subscriptionid, 'Title'); |
148 |
C4::Serials::RoutingLists::DelRoutingList($routinglistid); |
149 |
$routinglist = C4::Serials::RoutingLists::GetRoutingList($routinglistid); |
150 |
ok(not (defined $routinglist), 'routing list is deleted'); |
151 |
}; |
152 |
|
153 |
subtest 'GetRoutingList' => sub { |
154 |
plan tests => 3; |
155 |
|
156 |
my $routinglist; |
157 |
|
158 |
$routinglist = C4::Serials::RoutingLists::GetRoutingList; |
159 |
ok (not (defined $routinglist), 'GetRoutingList() returns undef'); |
160 |
|
161 |
$routinglist = C4::Serials::RoutingLists::GetRoutingList(-1); |
162 |
ok (not (defined $routinglist), 'GetRoutingList(-1) returns undef'); |
163 |
|
164 |
my $routinglistid = C4::Serials::RoutingLists::AddRoutingList($subscriptionid, 'Title'); |
165 |
$routinglist = C4::Serials::RoutingLists::GetRoutingList($routinglistid); |
166 |
ok ((defined $routinglist), 'GetRoutingList($routinglistid) returns undef'); |
167 |
}; |
168 |
|
169 |
subtest 'GetRoutingLists' => sub { |
170 |
plan tests => 4; |
171 |
|
172 |
my (@routinglists, $routinglistid); |
173 |
|
174 |
# Remove routing lists from previous tests |
175 |
@routinglists = C4::Serials::RoutingLists::GetRoutingLists($subscriptionid); |
176 |
foreach my $routinglist (@routinglists) { |
177 |
C4::Serials::RoutingLists::DelRoutingList($routinglist->{routinglistid}); |
178 |
} |
179 |
|
180 |
@routinglists = C4::Serials::RoutingLists::GetRoutingLists; |
181 |
is(scalar @routinglists, 0, 'GetRoutingLists() returns an empty list'); |
182 |
|
183 |
@routinglists = C4::Serials::RoutingLists::GetRoutingLists($subscriptionid); |
184 |
is(scalar @routinglists, 0, |
185 |
'GetRoutingLists($subscriptionid) returns an empty list'); |
186 |
|
187 |
$routinglistid = C4::Serials::RoutingLists::AddRoutingList($subscriptionid, 'list 1'); |
188 |
C4::Serials::RoutingLists::ModRoutingList($routinglistid, undef, undef, |
189 |
undef, $borrowernumber1); |
190 |
$routinglistid = C4::Serials::RoutingLists::AddRoutingList($subscriptionid, 'list 2'); |
191 |
C4::Serials::RoutingLists::ModRoutingList($routinglistid, undef, undef, |
192 |
undef, $borrowernumber1); |
193 |
@routinglists = C4::Serials::RoutingLists::GetRoutingLists($subscriptionid); |
194 |
is(scalar @routinglists, 2, |
195 |
'GetRoutingLists($subscriptionid) returns all routing lists'); |
196 |
is_deeply($routinglists[0]->{borrowers}, [$borrowernumber1], |
197 |
'GetRoutingLists returns borrower list for each routing list'); |
198 |
}; |
199 |
|
200 |
subtest 'GetRoutingListsCount' => sub { |
201 |
plan tests => 3; |
202 |
|
203 |
my $count; |
204 |
|
205 |
# Remove routing lists from previous tests |
206 |
my @routinglists = C4::Serials::RoutingLists::GetRoutingLists($subscriptionid); |
207 |
foreach my $routinglist (@routinglists) { |
208 |
C4::Serials::RoutingLists::DelRoutingList($routinglist->{routinglistid}); |
209 |
} |
210 |
|
211 |
$count = C4::Serials::RoutingLists::GetRoutingListsCount; |
212 |
is($count, undef, 'GetRoutingListsCount() returns undef'); |
213 |
|
214 |
$count = C4::Serials::RoutingLists::GetRoutingListsCount($subscriptionid); |
215 |
is($count, 0, 'GetRoutingListsCount($subscriptionid) returns 0'); |
216 |
|
217 |
C4::Serials::RoutingLists::AddRoutingList($subscriptionid, 'list 1'); |
218 |
C4::Serials::RoutingLists::AddRoutingList($subscriptionid, 'list 2'); |
219 |
$count = C4::Serials::RoutingLists::GetRoutingListsCount($subscriptionid); |
220 |
is($count, 2, 'GetRoutingListsCount($subscriptionid) returns 2'); |
221 |
}; |
222 |
|
223 |
subtest 'GetRoutingListAsCSV' => sub { |
224 |
plan tests => 1; |
225 |
|
226 |
my $routinglistid = C4::Serials::RoutingLists::AddRoutingList( |
227 |
$subscriptionid, 'list 1'); |
228 |
|
229 |
C4::Serials::RoutingLists::ModRoutingList($routinglistid, undef, undef, |
230 |
'notes', $borrowernumber1, $borrowernumber2); |
231 |
|
232 |
my $csv = C4::Serials::RoutingLists::GetRoutingListAsCSV($routinglistid); |
233 |
my $expected = |
234 |
q|"Subscription title","Routing list",Surname,Firstname,Notes| . "\n" |
235 |
. q|Title,"list 1",Doe,John,notes| . "\n" |
236 |
. q|Title,"list 1",Smith,Jane,notes| . "\n"; |
237 |
is($csv, $expected, 'csv is as expected'); |
238 |
}; |
239 |
|
240 |
$dbh->rollback; |