Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it |
6 |
# under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 3 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
# GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
18 |
use Modern::Perl; |
19 |
use utf8; |
20 |
|
21 |
use Test::More tests => 4; |
22 |
use Test::MockModule; |
23 |
|
24 |
use t::lib::Mocks; |
25 |
use t::lib::TestBuilder; |
26 |
|
27 |
use MARC::Record; |
28 |
|
29 |
use_ok('Koha::Acquisition::Utils'); |
30 |
|
31 |
my $schema = Koha::Database->schema; |
32 |
$schema->storage->txn_begin; |
33 |
my $builder = t::lib::TestBuilder->new; |
34 |
my $dbh = C4::Context->dbh; |
35 |
|
36 |
subtest "GetMarcFieldsToOrderValues" => sub { |
37 |
plan tests => 4; |
38 |
|
39 |
my $record = MARC::Record->new; |
40 |
$record->append_fields( |
41 |
MARC::Field->new( '500', '', '', a => 'Test 1' ), |
42 |
MARC::Field->new( '505', '', '', a => 'Test 2', u => 'http://example.com' ), |
43 |
MARC::Field->new( '520', '', '', a => 'Test 3' ), |
44 |
MARC::Field->new( '541', '', '', a => 'Test 4' ), |
45 |
); |
46 |
|
47 |
my $MarcFieldsToOrder = q{ |
48 |
test1: 500$a |
49 |
test2: 505$a |
50 |
test3: 520$a |
51 |
test4: 541$a |
52 |
}; |
53 |
t::lib::Mocks::mock_preference('MarcFieldsToOrder', $MarcFieldsToOrder); |
54 |
my $data = Koha::Acquisition::Utils::GetMarcFieldsToOrderValues( |
55 |
$record, |
56 |
[ 'test1', 'test2', 'test3', 'test4' ] |
57 |
); |
58 |
|
59 |
is( $data->{test1}, "Test 1", "Got test 1 correctly" ); |
60 |
is( $data->{test2}, "Test 2", "Got test 2 correctly" ); |
61 |
is( $data->{test3}, "Test 3", "Got test 3 correctly" ); |
62 |
is( $data->{test4}, "Test 4", "Got test 4 correctly" ); |
63 |
}; |
64 |
|
65 |
subtest "GetMarcItemFieldsToOrderValues" => sub { |
66 |
plan tests => 13; |
67 |
|
68 |
my $record = MARC::Record->new; |
69 |
$record->append_fields( |
70 |
MARC::Field->new( '500', '', '', a => 'Test 1' ), |
71 |
MARC::Field->new( '505', '', '', a => 'Test 2', u => 'http://example.com' ), |
72 |
MARC::Field->new( '975', '', '', a => 'Test 3', b => "Test 4" ), |
73 |
MARC::Field->new( '975', '', '', a => 'Test 5', b => "Test 6" ), |
74 |
MARC::Field->new( '975', '', '', a => 'Test 7', b => "Test 8" ), |
75 |
MARC::Field->new( '976', '', '', a => 'Test 9', b => "Test 10" ), |
76 |
MARC::Field->new( '976', '', '', a => 'Test 11', b => "Test 12" ), |
77 |
MARC::Field->new( '976', '', '', a => 'Test 13', b => "Test 14" ), |
78 |
); |
79 |
|
80 |
my $MarcItemFieldsToOrder = q{ |
81 |
testA: 975$a |
82 |
testB: 975$b |
83 |
testC: 976$a |
84 |
testD: 976$b |
85 |
}; |
86 |
t::lib::Mocks::mock_preference('MarcItemFieldsToOrder', $MarcItemFieldsToOrder); |
87 |
my $data = Koha::Acquisition::Utils::GetMarcItemFieldsToOrderValues( |
88 |
$record, |
89 |
[ 'testA', 'testB', 'testC', 'testD' ] |
90 |
); |
91 |
|
92 |
is( $data->[0]->{testA}, "Test 3", 'Got first 975$a correctly' ); |
93 |
is( $data->[0]->{testB}, "Test 4", 'Got first 975$b correctly' ); |
94 |
is( $data->[1]->{testA}, "Test 5", 'Got second 975$a correctly' ); |
95 |
is( $data->[1]->{testB}, "Test 6", 'Got second 975$b correctly' ); |
96 |
is( $data->[2]->{testA}, "Test 7", 'Got third 975$a correctly' ); |
97 |
is( $data->[2]->{testB}, "Test 8", 'Got third 975$b correctly' ); |
98 |
|
99 |
is( $data->[0]->{testC}, "Test 9", 'Got first 976$a correctly' ); |
100 |
is( $data->[0]->{testD}, "Test 10", 'Got first 976$b correctly' ); |
101 |
is( $data->[1]->{testC}, "Test 11", 'Got second 976$a correctly' ); |
102 |
is( $data->[1]->{testD}, "Test 12", 'Got second 976$b correctly' ); |
103 |
is( $data->[2]->{testC}, "Test 13", 'Got third 976$a correctly' ); |
104 |
is( $data->[2]->{testD}, "Test 14", 'Got third 976$b correctly' ); |
105 |
|
106 |
# Test with bad record where fields are not one-to-one |
107 |
$record->append_fields( |
108 |
MARC::Field->new( '500', '', '', a => 'Test 1' ), |
109 |
MARC::Field->new( '505', '', '', a => 'Test 2', u => 'http://example.com' ), |
110 |
MARC::Field->new( '975', '', '', a => 'Test 3', b => "Test 4" ), |
111 |
MARC::Field->new( '975', '', '', b => "Test 6" ), |
112 |
MARC::Field->new( '975', '', '', b => 'Test 7' ), |
113 |
MARC::Field->new( '976', '', '', a => 'Test 9', b => "Test 10" ), |
114 |
MARC::Field->new( '976', '', '', a => 'Test 11', b => "Test 12" ), |
115 |
); |
116 |
|
117 |
$data = Koha::Acquisition::Utils::GetMarcItemFieldsToOrderValues( |
118 |
$record, |
119 |
[ 'testA', 'testB', 'testC', 'testD' ] |
120 |
); |
121 |
is( $data, -1, "Got -1 if record fields are not one-to-one"); |
122 |
}; |
123 |
|
124 |
subtest "equal_number_of_fields" => sub { |
125 |
plan tests => 2; |
126 |
|
127 |
my $record = MARC::Record->new; |
128 |
$record->append_fields( |
129 |
MARC::Field->new( '500', '', '', a => 'Test 1' ), |
130 |
MARC::Field->new( '505', '', '', a => 'Test 2', u => 'http://example.com' ), |
131 |
MARC::Field->new( '975', '', '', a => 'Test a', b => "Test b" ), |
132 |
MARC::Field->new( '975', '', '', a => 'Test a', b => "Test b" ), |
133 |
MARC::Field->new( '975', '', '', a => 'Test a', b => "Test b" ), |
134 |
MARC::Field->new( '976', '', '', a => 'Test a', b => "Test b" ), |
135 |
MARC::Field->new( '976', '', '', a => 'Test a', b => "Test b" ), |
136 |
MARC::Field->new( '976', '', '', a => 'Test a', b => "Test b" ), |
137 |
); |
138 |
|
139 |
my $data = Koha::Acquisition::Utils::equal_number_of_fields( [ '975', '976' ], $record ); |
140 |
is( $data, '3', "Got correct number of fields in return value" ); |
141 |
|
142 |
# Test with non-matching field sets |
143 |
$record->append_fields( |
144 |
MARC::Field->new( '500', '', '', a => 'Test 1' ), |
145 |
MARC::Field->new( '505', '', '', a => 'Test 2', u => 'http://example.com' ), |
146 |
MARC::Field->new( '975', '', '', a => 'Test a', b => "Test b" ), |
147 |
MARC::Field->new( '975', '', '', a => 'Test a', b => "Test b" ), |
148 |
MARC::Field->new( '975', '', '', a => 'Test a', b => "Test b" ), |
149 |
MARC::Field->new( '976', '', '', a => 'Test a', b => "Test b" ), |
150 |
MARC::Field->new( '976', '', '', a => 'Test a', b => "Test b" ), |
151 |
); |
152 |
|
153 |
$data = Koha::Acquisition::Utils::equal_number_of_fields( [ '975', '976' ], $record ); |
154 |
is( $data, '-1', "Got -1 in return value" ); |
155 |
}; |
156 |
|
157 |
$schema->storage->txn_rollback; |
158 |
C4::Context->clear_syspref_cache(); |