Lines 17-23
Link Here
|
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
|
19 |
|
20 |
use Test::More tests => 1; |
20 |
use Test::More tests => 3; |
21 |
|
21 |
|
22 |
use Koha::Database; |
22 |
use Koha::Database; |
23 |
use Koha::BackgroundJobs; |
23 |
use Koha::BackgroundJobs; |
Lines 54-56
subtest 'enqueue() tests' => sub {
Link Here
|
54 |
|
54 |
|
55 |
$schema->storage->txn_rollback; |
55 |
$schema->storage->txn_rollback; |
56 |
}; |
56 |
}; |
57 |
- |
57 |
|
|
|
58 |
subtest 'marc_record_contains_item_data tests' => sub { |
59 |
plan tests => 6; |
60 |
|
61 |
# Mock the MARC21 flavor preference |
62 |
t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' ); |
63 |
|
64 |
# Test with no item fields |
65 |
my $record = MARC::Record->new(); |
66 |
is( |
67 |
Koha::BackgroundJob::BatchUpdateBiblio::marc_record_contains_item_data($record), 0, |
68 |
'Returns 0 with no item fields' |
69 |
); |
70 |
|
71 |
# Test with one item field |
72 |
$record->append_fields( MARC::Field->new( '952', ' ', ' ', 'a' => 'test' ) ); |
73 |
is( |
74 |
Koha::BackgroundJob::BatchUpdateBiblio::marc_record_contains_item_data($record), 1, |
75 |
'Returns 1 with one item field' |
76 |
); |
77 |
|
78 |
# Test with multiple item fields |
79 |
$record->append_fields( MARC::Field->new( '952', ' ', ' ', 'a' => 'test2' ) ); |
80 |
is( |
81 |
Koha::BackgroundJob::BatchUpdateBiblio::marc_record_contains_item_data($record), 2, |
82 |
'Returns 2 with two item fields' |
83 |
); |
84 |
|
85 |
# Mock the UNIMARC flavor preference |
86 |
t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' ); |
87 |
|
88 |
# Test with no item fields |
89 |
$record = MARC::Record->new(); |
90 |
is( |
91 |
Koha::BackgroundJob::BatchUpdateBiblio::marc_record_contains_item_data($record), 0, |
92 |
'Returns 0 with no item fields' |
93 |
); |
94 |
|
95 |
# Test with one item field |
96 |
$record->append_fields( MARC::Field->new( '995', ' ', ' ', 'a' => 'test' ) ); |
97 |
is( |
98 |
Koha::BackgroundJob::BatchUpdateBiblio::marc_record_contains_item_data($record), 1, |
99 |
'Returns 1 with one item field' |
100 |
); |
101 |
|
102 |
# Test with multiple item fields |
103 |
$record->append_fields( MARC::Field->new( '995', ' ', ' ', 'a' => 'test2' ) ); |
104 |
is( |
105 |
Koha::BackgroundJob::BatchUpdateBiblio::marc_record_contains_item_data($record), 2, |
106 |
'Returns 2 with two item fields' |
107 |
); |
108 |
}; |
109 |
|
110 |
subtest 'can_add_item_from_marc_record tests' => sub { |
111 |
plan tests => 7; |
112 |
$schema->storage->txn_begin; |
113 |
|
114 |
t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' ); |
115 |
|
116 |
# Set item-level_itypes preference for testing |
117 |
t::lib::Mocks::mock_preference( 'item-level_itypes', 1 ); |
118 |
|
119 |
# Tests for can_add_item_from_marc_record with all fields |
120 |
subtest 'can_add_item_from_marc_record with all fields' => sub { |
121 |
plan tests => 2; |
122 |
|
123 |
my $record = MARC::Record->new(); |
124 |
$record->append_fields( |
125 |
MARC::Field->new( |
126 |
'952', ' ', ' ', |
127 |
'a' => 'CENTRAL', # homebranch |
128 |
'b' => 'CENTRAL', # holdingbranch |
129 |
'y' => 'BOOK' # itype |
130 |
) |
131 |
); |
132 |
|
133 |
my ( $result, $error ) = Koha::BackgroundJob::BatchUpdateBiblio::can_add_item_from_marc_record($record); |
134 |
is( $result, 1, 'Returns 1 when all required fields exist' ); |
135 |
is( $error, undef, 'No error message when successful' ); |
136 |
}; |
137 |
|
138 |
# Test missing holdingbranch |
139 |
subtest 'can_add_item_from_marc_record missing holdingbranch' => sub { |
140 |
plan tests => 2; |
141 |
|
142 |
my $record = MARC::Record->new(); |
143 |
$record->append_fields( |
144 |
MARC::Field->new( |
145 |
'952', ' ', ' ', |
146 |
'a' => 'CENTRAL', # homebranch |
147 |
'y' => 'BOOK' # itype |
148 |
# No 'b' for holdingbranch |
149 |
) |
150 |
); |
151 |
|
152 |
my ( $result, $error ) = Koha::BackgroundJob::BatchUpdateBiblio::can_add_item_from_marc_record($record); |
153 |
is( $result, 0, 'Returns 0 when holdingbranch is missing' ); |
154 |
is( $error, 'No holdingbranch subfield found', 'Correct error message for missing holdingbranch' ); |
155 |
}; |
156 |
|
157 |
# Test missing homebranch |
158 |
subtest 'can_add_item_from_marc_record missing homebranch' => sub { |
159 |
plan tests => 2; |
160 |
|
161 |
my $record = MARC::Record->new(); |
162 |
$record->append_fields( |
163 |
MARC::Field->new( |
164 |
'952', ' ', ' ', |
165 |
'b' => 'CENTRAL', # holdingbranch |
166 |
'y' => 'BOOK' # itype |
167 |
# No 'a' for homebranch |
168 |
) |
169 |
); |
170 |
|
171 |
my ( $result, $error ) = Koha::BackgroundJob::BatchUpdateBiblio::can_add_item_from_marc_record($record); |
172 |
is( $result, 0, 'Returns 0 when homebranch is missing' ); |
173 |
is( $error, 'No homebranch subfield found', 'Correct error message for missing homebranch' ); |
174 |
}; |
175 |
|
176 |
# Test missing itemtype |
177 |
subtest 'can_add_item_from_marc_record missing itemtype' => sub { |
178 |
plan tests => 2; |
179 |
|
180 |
my $record = MARC::Record->new(); |
181 |
$record->append_fields( |
182 |
MARC::Field->new( |
183 |
'952', ' ', ' ', |
184 |
'a' => 'CENTRAL', # homebranch |
185 |
'b' => 'CENTRAL' # holdingbranch |
186 |
# No 'y' for itemtype |
187 |
) |
188 |
); |
189 |
|
190 |
my ( $result, $error ) = Koha::BackgroundJob::BatchUpdateBiblio::can_add_item_from_marc_record($record); |
191 |
is( $result, 0, 'Returns 0 when itemtype is missing' ); |
192 |
is( $error, 'No itemtype subfield found', 'Correct error message for missing itemtype' ); |
193 |
}; |
194 |
|
195 |
# Test with bib-level itemtypes |
196 |
subtest 'can_add_item_from_marc_record with bib-level itemtypes' => sub { |
197 |
plan tests => 2; |
198 |
|
199 |
# Change preference to use bib-level itemtypes |
200 |
t::lib::Mocks::mock_preference( 'item-level_itypes', 0 ); |
201 |
|
202 |
my $record = MARC::Record->new(); |
203 |
$record->append_fields( |
204 |
MARC::Field->new( |
205 |
'952', ' ', ' ', |
206 |
'a' => 'CENTRAL', # homebranch |
207 |
'b' => 'CENTRAL', # holdingbranch |
208 |
) |
209 |
); |
210 |
|
211 |
$record->append_fields( |
212 |
MARC::Field->new( |
213 |
'942', ' ', ' ', |
214 |
'c' => 'BOOK', # itemtype |
215 |
) |
216 |
); |
217 |
|
218 |
my ( $result, $error ) = Koha::BackgroundJob::BatchUpdateBiblio::can_add_item_from_marc_record($record); |
219 |
is( $result, 1, 'Returns 1 when using bib-level itemtypes' ); |
220 |
is( $error, undef, 'No error message when successful' ); |
221 |
|
222 |
# Reset to item-level itemtypes |
223 |
t::lib::Mocks::mock_preference( 'item-level_itypes', 1 ); |
224 |
}; |
225 |
|
226 |
# Test with multiple item fields |
227 |
subtest 'can_add_item_from_marc_record with multiple item fields' => sub { |
228 |
plan tests => 2; |
229 |
|
230 |
my $record = MARC::Record->new(); |
231 |
|
232 |
# First field missing itemtype |
233 |
$record->append_fields( |
234 |
MARC::Field->new( |
235 |
'952', ' ', ' ', |
236 |
'a' => 'CENTRAL', # homebranch |
237 |
'b' => 'CENTRAL' # holdingbranch |
238 |
# No itemtype |
239 |
) |
240 |
); |
241 |
|
242 |
# Second field is complete |
243 |
$record->append_fields( |
244 |
MARC::Field->new( |
245 |
'952', ' ', ' ', |
246 |
'a' => 'EAST', # homebranch |
247 |
'b' => 'EAST', # holdingbranch |
248 |
'y' => 'DVD' # itemtype |
249 |
) |
250 |
); |
251 |
|
252 |
my ( $result, $error ) = Koha::BackgroundJob::BatchUpdateBiblio::can_add_item_from_marc_record($record); |
253 |
is( $result, 1, 'Returns 1 when at least one item field is complete' ); |
254 |
is( $error, undef, 'No error message when successful' ); |
255 |
}; |
256 |
|
257 |
# Test mixed valid/invalid fields |
258 |
subtest 'can_add_item_from_marc_record with mix of valid/invalid fields' => sub { |
259 |
plan tests => 2; |
260 |
|
261 |
my $record = MARC::Record->new(); |
262 |
|
263 |
# Add a non-item field that shouldn't be considered |
264 |
$record->append_fields( |
265 |
MARC::Field->new( |
266 |
'245', '1', '0', |
267 |
'a' => 'Title', |
268 |
'b' => 'Subtitle' |
269 |
) |
270 |
); |
271 |
|
272 |
# Add a valid item field |
273 |
$record->append_fields( |
274 |
MARC::Field->new( |
275 |
'952', ' ', ' ', |
276 |
'a' => 'CENTRAL', # homebranch |
277 |
'b' => 'CENTRAL', # holdingbranch |
278 |
'y' => 'BOOK' # itemtype |
279 |
) |
280 |
); |
281 |
|
282 |
my ( $result, $error ) = Koha::BackgroundJob::BatchUpdateBiblio::can_add_item_from_marc_record($record); |
283 |
is( $result, 1, 'Returns 1 with mix of item and non-item fields' ); |
284 |
is( $error, undef, 'No error message when successful' ); |
285 |
}; |
286 |
|
287 |
$schema->storage->txn_rollback; |
288 |
}; |