View | Details | Raw Unified | Return to bug 34812
Collapse All | Expand All

(-)a/t/Koha.t (-134 lines)
Lines 1-134 Link Here
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 Test::More tests => 4;
20
use Test::MockModule;
21
22
use t::lib::TestBuilder;
23
24
use_ok('C4::Koha', qw( xml_escape GetVariationsOfISBN GetVariationsOfISBNs GetVariationsOfISSN GetVariationsOfISSNs));
25
26
use C4::Context;
27
use Koha::Libraries;
28
29
subtest 'isbn stuff' => sub {
30
    plan tests => 29;
31
32
    my $isbn13 = "9780330356473";
33
    my $isbn13D = "978-0-330-35647-3";
34
    my $isbn10 = "033035647X";
35
    my $isbn10D = "0-330-35647-X";
36
37
    my $undef = undef;
38
    is(xml_escape($undef), '', 'xml_escape() returns empty string on undef input');
39
    my $str = q{'"&<>'};
40
    is(xml_escape($str), '&apos;&quot;&amp;&lt;&gt;&apos;', 'xml_escape() works as expected');
41
    is($str, q{'"&<>'}, '... and does not change input in place');
42
43
    is(C4::Koha::_isbn_cleanup('0-590-35340-3'), '0590353403', '_isbn_cleanup removes hyphens');
44
    is(C4::Koha::_isbn_cleanup('0590353403 (pbk.)'), '0590353403', '_isbn_cleanup removes parenthetical');
45
    is(C4::Koha::_isbn_cleanup('978-0-321-49694-2'), '0321496949', '_isbn_cleanup converts ISBN-13 to ISBN-10');
46
47
    is(C4::Koha::NormalizeISBN({ isbn => '978-0-321-49694-2 (pbk.)', format => 'ISBN-10', strip_hyphens => 1 }), '0321496949', 'Test NormalizeISBN with all features enabled' );
48
49
    my @isbns = qw/ 978-0-321-49694-2 0-321-49694-9 978-0-321-49694-2 0321496949 9780321496942/;
50
    is( join('|', @isbns), join('|', GetVariationsOfISBN('978-0-321-49694-2 (pbk.)')), 'GetVariationsOfISBN returns all variations' );
51
52
    is( join('|', @isbns), join('|', GetVariationsOfISBNs('978-0-321-49694-2 (pbk.)')), 'GetVariationsOfISBNs returns all variations' );
53
54
    my $isbn;
55
    eval {
56
        $isbn = C4::Koha::NormalizeISBN({ isbn => '0788893777 (2 DVD 45th ed)', format => 'ISBN-10', strip_hyphens => 1 });
57
    };
58
    ok($@ eq '', 'NormalizeISBN does not throw exception when parsing invalid ISBN (bug 12243)');
59
    $isbn = C4::Koha::NormalizeISBN({ isbn => '0788893777 (2 DVD 45th ed)', format => 'ISBN-10', strip_hyphens => 1, return_invalid =>1 });
60
    is($isbn, '0788893777 (2 DVD 45th ed)', 'NormalizeISBN returns original string when converting to ISBN10 an ISBN starting with 979 (bug 13167)');
61
62
    eval {
63
        $isbn = C4::Koha::NormalizeISBN({ isbn => '979-10-90085-00-8', format => 'ISBN-10', strip_hyphens => 1 });
64
    };
65
    ok($@ eq '', 'NormalizeISBN does not throw exception when converting to ISBN10 an ISBN starting with 979 (bug 13167)');
66
    ok(!defined $isbn, 'NormalizeISBN returns undef when converting to ISBN10 an ISBN starting with 979 (bug 13167)');
67
68
    @isbns = GetVariationsOfISBNs('abc');
69
    is(@isbns == 1 && $isbns[0] eq 'abc', 1, 'The unaltered version should be returned if invalid');
70
71
    is( C4::Koha::GetNormalizedISBN('9780062059994 (hardcover bdg.) | 0062059998 (hardcover bdg.)'), '0062059998', 'Test GetNormalizedISBN' );
72
    is( C4::Koha::GetNormalizedISBN('9780385753067 (trade) | 0385753063 (trade) | 9780385753074 (lib. bdg.) | 0385753071 (lib. bdg.)'), '0385753063', 'Test GetNormalizedISBN' );
73
    is( C4::Koha::GetNormalizedISBN('9781432829162 (hardcover) | 1432829165 (hardcover)'), '1432829165', 'Test GetNormalizedISBN' );
74
    is( C4::Koha::GetNormalizedISBN('9780062063625 (hardcover) | 9780062063632 | 0062063634'), '0062063626', 'Test GetNormalizedISBN' );
75
    is( C4::Koha::GetNormalizedISBN('9780062059932 (hardback)'), '0062059939', 'Test GetNormalizedISBN' );
76
    is( C4::Koha::GetNormalizedISBN('9780316370318 (hardback) | 9780316376266 (special edition hardcover) | 9780316405454 (international paperback edition)'), '0316370312', 'Test GetNormalizedISBN' );
77
    is( C4::Koha::GetNormalizedISBN('9781595148032 (hbk.) | 1595148035 (hbk.)') , '1595148035', 'Test GetNormalizedISBN' );
78
    is( C4::Koha::GetNormalizedISBN('9780062349859 | 0062349856 | 9780062391308 | 0062391305'), '0062349856', 'Test GetNormalizedISBN' );
79
    is( C4::Koha::GetNormalizedISBN('9781250075345 (hardcover) | 1250075343 (hardcover) | 9781250049872 (trade pbk.) | 1250049873 (trade pbk.)'), '1250075343', 'Test GetNormalizedISBN' );
80
    is( C4::Koha::GetNormalizedISBN('9781250067128 | 125006712X'), '125006712X', 'Test GetNormalizedISBN' );
81
    is( C4::Koha::GetNormalizedISBN('9780373211463 | 0373211465'), '0373211465', 'Test GetNormalizedISBN' );
82
83
    is( C4::Koha::GetNormalizedUPC(), undef, 'GetNormalizedUPC should return undef if no record is passed' );
84
    is( C4::Koha::GetNormalizedISBN(), undef, 'GetNormalizedISBN should return undef if no record and no isbn are passed' );
85
    is( C4::Koha::GetNormalizedEAN(), undef, 'GetNormalizedEAN should return undef if no record and no isbn are passed' );
86
    is( C4::Koha::GetNormalizedOCLCNumber(), undef, 'GetNormalizedOCLCNumber should return undef if no record and no isbn are passed' );
87
};
88
89
subtest 'issn stuff' => sub {
90
    plan tests => 7;
91
92
    is(C4::Koha::NormalizeISSN({ issn => '0024-9319', strip_hyphen => 1 }), '00249319', 'Test NormalizeISSN with all features enabled' );
93
    is(C4::Koha::NormalizeISSN({ issn => '0024-9319', strip_hyphen => 0 }), '0024-9319', 'Test NormalizeISSN with all features enabled' );
94
95
    my @issns = qw/ 0024-9319 00249319 /;
96
    is( join('|', @issns), join('|', GetVariationsOfISSN('0024-9319')), 'GetVariationsOfISSN returns all variations' );
97
    is( join('|', @issns), join('|', GetVariationsOfISSNs('0024-9319')), 'GetVariationsOfISSNs returns all variations' );
98
99
    my $issn;
100
    eval {
101
        $issn = C4::Koha::NormalizeISSN({ issn => '1234-5678', strip_hyphen => 1 });
102
    };
103
    ok($@ eq '', 'NormalizeISSN does not throw exception when parsing invalid ISSN');
104
105
    @issns = GetVariationsOfISSNs('abc');
106
    is($issns[0], 'abc', 'Original ISSN passed through even if invalid');
107
    is(scalar(@issns), 1, 'zero additional variations returned of invalid ISSN');
108
};
109
110
subtest 'getFacets() tests' => sub {
111
    plan tests => 4;
112
113
    my $count = 1;
114
    my $library_module = Test::MockModule->new('Koha::Libraries');
115
    $library_module->mock( 'count', sub { return $count } );
116
117
    is ( Koha::Libraries->search->count, 1, 'There should be only 1 library (singleBranchMode on)' );
118
    my $facets = C4::Koha::getFacets();
119
    is(
120
        scalar( grep { defined $_->{idx} && $_->{idx} eq 'location' } @$facets ),
121
        1,
122
        'location facet present with singleBranchMode on (bug 10078)'
123
    );
124
125
    $count = 3;    # more libraries..
126
    is ( Koha::Libraries->search->count, 3, 'There should be more than 1 library (singleBranchMode off)' );
127
128
    $facets = C4::Koha::getFacets();
129
    is(
130
        scalar( grep { defined $_->{idx} && $_->{idx} eq 'location' } @$facets ),
131
        1,
132
        'location facet present with singleBranchMode off (bug 10078)'
133
    );
134
};
(-)a/t/db_dependent/Koha.t (-19 / +172 lines)
Lines 4-10 Link Here
4
# It requires a working Koha database with the sample data
4
# It requires a working Koha database with the sample data
5
5
6
use Modern::Perl;
6
use Modern::Perl;
7
use Test::More tests => 5;
7
use Test::More tests => 6;
8
use Test::MockModule;
8
use Test::Warn;
9
use Test::Warn;
9
use Test::Deep;
10
use Test::Deep;
10
11
Lines 14-23 use C4::Context; Link Here
14
use Koha::Database;
15
use Koha::Database;
15
use Koha::AuthorisedValue;
16
use Koha::AuthorisedValue;
16
use Koha::AuthorisedValueCategories;
17
use Koha::AuthorisedValueCategories;
18
use Koha::Libraries;
17
19
18
BEGIN {
20
BEGIN {
19
    use_ok('C4::Koha', qw( GetAuthorisedValues GetItemTypesCategorized xml_escape ));
21
    use_ok(
20
    use_ok('C4::Members');
22
        'C4::Koha',
23
        qw( GetAuthorisedValues GetItemTypesCategorized xml_escape GetVariationsOfISBN GetVariationsOfISBNs GetVariationsOfISSN GetVariationsOfISSNs )
24
    );
21
}
25
}
22
26
23
my $schema  = Koha::Database->new->schema;
27
my $schema  = Koha::Database->new->schema;
Lines 155-186 subtest 'Authorized Values Tests' => sub { Link Here
155
159
156
};
160
};
157
161
158
subtest 'ISBN tests' => sub {
162
subtest 'isbn tests' => sub {
159
    plan tests => 6;
163
    plan tests => 29;
160
164
161
    my $isbn13  = "9780330356473";
165
    my $isbn13  = "9780330356473";
162
    my $isbn13D = "978-0-330-35647-3";
166
    my $isbn13D = "978-0-330-35647-3";
163
    my $isbn10  = "033035647X";
167
    my $isbn10  = "033035647X";
164
    my $isbn10D = "0-330-35647-X";
168
    my $isbn10D = "0-330-35647-X";
165
    is( xml_escape(undef), '',
169
166
        'xml_escape() returns empty string on undef input' );
170
    my $undef = undef;
171
    is( xml_escape($undef), '', 'xml_escape() returns empty string on undef input' );
167
    my $str = q{'"&<>'};
172
    my $str = q{'"&<>'};
173
    is( xml_escape($str), '&apos;&quot;&amp;&lt;&gt;&apos;', 'xml_escape() works as expected' );
174
    is( $str,             q{'"&<>'},                         '... and does not change input in place' );
175
176
    is( C4::Koha::_isbn_cleanup('0-590-35340-3'),     '0590353403', '_isbn_cleanup removes hyphens' );
177
    is( C4::Koha::_isbn_cleanup('0590353403 (pbk.)'), '0590353403', '_isbn_cleanup removes parenthetical' );
178
    is( C4::Koha::_isbn_cleanup('978-0-321-49694-2'), '0321496949', '_isbn_cleanup converts ISBN-13 to ISBN-10' );
179
180
    is(
181
        C4::Koha::NormalizeISBN( { isbn => '978-0-321-49694-2 (pbk.)', format => 'ISBN-10', strip_hyphens => 1 } ),
182
        '0321496949', 'Test NormalizeISBN with all features enabled'
183
    );
184
185
    my @isbns = qw/ 978-0-321-49694-2 0-321-49694-9 978-0-321-49694-2 0321496949 9780321496942/;
186
    is(
187
        join( '|', @isbns ), join( '|', GetVariationsOfISBN('978-0-321-49694-2 (pbk.)') ),
188
        'GetVariationsOfISBN returns all variations'
189
    );
190
191
    is(
192
        join( '|', @isbns ), join( '|', GetVariationsOfISBNs('978-0-321-49694-2 (pbk.)') ),
193
        'GetVariationsOfISBNs returns all variations'
194
    );
195
196
    my $isbn;
197
    eval {
198
        $isbn = C4::Koha::NormalizeISBN(
199
            { isbn => '0788893777 (2 DVD 45th ed)', format => 'ISBN-10', strip_hyphens => 1 } );
200
    };
201
    ok( $@ eq '', 'NormalizeISBN does not throw exception when parsing invalid ISBN (bug 12243)' );
202
    $isbn = C4::Koha::NormalizeISBN(
203
        { isbn => '0788893777 (2 DVD 45th ed)', format => 'ISBN-10', strip_hyphens => 1, return_invalid => 1 } );
204
    is(
205
        $isbn, '0788893777 (2 DVD 45th ed)',
206
        'NormalizeISBN returns original string when converting to ISBN10 an ISBN starting with 979 (bug 13167)'
207
    );
208
209
    eval {
210
        $isbn = C4::Koha::NormalizeISBN( { isbn => '979-10-90085-00-8', format => 'ISBN-10', strip_hyphens => 1 } );
211
    };
212
    ok(
213
        $@ eq '',
214
        'NormalizeISBN does not throw exception when converting to ISBN10 an ISBN starting with 979 (bug 13167)'
215
    );
216
    ok( !defined $isbn, 'NormalizeISBN returns undef when converting to ISBN10 an ISBN starting with 979 (bug 13167)' );
217
218
    @isbns = GetVariationsOfISBNs('abc');
219
    is( @isbns == 1 && $isbns[0] eq 'abc', 1, 'The unaltered version should be returned if invalid' );
220
221
    is(
222
        C4::Koha::GetNormalizedISBN('9780062059994 (hardcover bdg.) | 0062059998 (hardcover bdg.)'), '0062059998',
223
        'Test GetNormalizedISBN'
224
    );
225
    is(
226
        C4::Koha::GetNormalizedISBN(
227
            '9780385753067 (trade) | 0385753063 (trade) | 9780385753074 (lib. bdg.) | 0385753071 (lib. bdg.)'),
228
        '0385753063',
229
        'Test GetNormalizedISBN'
230
    );
231
    is(
232
        C4::Koha::GetNormalizedISBN('9781432829162 (hardcover) | 1432829165 (hardcover)'), '1432829165',
233
        'Test GetNormalizedISBN'
234
    );
235
    is(
236
        C4::Koha::GetNormalizedISBN('9780062063625 (hardcover) | 9780062063632 | 0062063634'), '0062063626',
237
        'Test GetNormalizedISBN'
238
    );
239
    is( C4::Koha::GetNormalizedISBN('9780062059932 (hardback)'), '0062059939', 'Test GetNormalizedISBN' );
240
    is(
241
        C4::Koha::GetNormalizedISBN(
242
            '9780316370318 (hardback) | 9780316376266 (special edition hardcover) | 9780316405454 (international paperback edition)'
243
        ),
244
        '0316370312',
245
        'Test GetNormalizedISBN'
246
    );
247
    is(
248
        C4::Koha::GetNormalizedISBN('9781595148032 (hbk.) | 1595148035 (hbk.)'), '1595148035',
249
        'Test GetNormalizedISBN'
250
    );
168
    is(
251
    is(
169
        xml_escape($str),
252
        C4::Koha::GetNormalizedISBN('9780062349859 | 0062349856 | 9780062391308 | 0062391305'), '0062349856',
170
        '&apos;&quot;&amp;&lt;&gt;&apos;',
253
        'Test GetNormalizedISBN'
171
        'xml_escape() works as expected'
172
    );
254
    );
173
    is( $str, q{'"&<>'}, '... and does not change input in place' );
255
    is(
174
    is( C4::Koha::_isbn_cleanup('0-590-35340-3'),
256
        C4::Koha::GetNormalizedISBN(
175
        '0590353403', '_isbn_cleanup removes hyphens' );
257
            '9781250075345 (hardcover) | 1250075343 (hardcover) | 9781250049872 (trade pbk.) | 1250049873 (trade pbk.)'
176
    is( C4::Koha::_isbn_cleanup('0590353403 (pbk.)'),
258
        ),
177
        '0590353403', '_isbn_cleanup removes parenthetical' );
259
        '1250075343',
178
    is( C4::Koha::_isbn_cleanup('978-0-321-49694-2'),
260
        'Test GetNormalizedISBN'
179
        '0321496949', '_isbn_cleanup converts ISBN-13 to ISBN-10' );
261
    );
262
    is( C4::Koha::GetNormalizedISBN('9781250067128 | 125006712X'), '125006712X', 'Test GetNormalizedISBN' );
263
    is( C4::Koha::GetNormalizedISBN('9780373211463 | 0373211465'), '0373211465', 'Test GetNormalizedISBN' );
180
264
265
    is( C4::Koha::GetNormalizedUPC(), undef, 'GetNormalizedUPC should return undef if no record is passed' );
266
    is(
267
        C4::Koha::GetNormalizedISBN(), undef,
268
        'GetNormalizedISBN should return undef if no record and no isbn are passed'
269
    );
270
    is(
271
        C4::Koha::GetNormalizedEAN(), undef,
272
        'GetNormalizedEAN should return undef if no record and no isbn are passed'
273
    );
274
    is(
275
        C4::Koha::GetNormalizedOCLCNumber(), undef,
276
        'GetNormalizedOCLCNumber should return undef if no record and no isbn are passed'
277
    );
278
};
279
280
subtest 'issn stuff' => sub {
281
    plan tests => 7;
282
283
    is(
284
        C4::Koha::NormalizeISSN( { issn => '0024-9319', strip_hyphen => 1 } ), '00249319',
285
        'Test NormalizeISSN with all features enabled'
286
    );
287
    is(
288
        C4::Koha::NormalizeISSN( { issn => '0024-9319', strip_hyphen => 0 } ), '0024-9319',
289
        'Test NormalizeISSN with all features enabled'
290
    );
291
292
    my @issns = qw/ 0024-9319 00249319 /;
293
    is(
294
        join( '|', @issns ), join( '|', GetVariationsOfISSN('0024-9319') ),
295
        'GetVariationsOfISSN returns all variations'
296
    );
297
    is(
298
        join( '|', @issns ), join( '|', GetVariationsOfISSNs('0024-9319') ),
299
        'GetVariationsOfISSNs returns all variations'
300
    );
301
302
    my $issn;
303
    eval { $issn = C4::Koha::NormalizeISSN( { issn => '1234-5678', strip_hyphen => 1 } ); };
304
    ok( $@ eq '', 'NormalizeISSN does not throw exception when parsing invalid ISSN' );
305
306
    @issns = GetVariationsOfISSNs('abc');
307
    is( $issns[0],      'abc', 'Original ISSN passed through even if invalid' );
308
    is( scalar(@issns), 1,     'zero additional variations returned of invalid ISSN' );
309
};
310
311
subtest 'getFacets() tests' => sub {
312
    plan tests => 4;
313
314
    my $count          = 1;
315
    my $library_module = Test::MockModule->new('Koha::Libraries');
316
    $library_module->mock( 'count', sub { return $count } );
317
318
    is( Koha::Libraries->search->count, 1, 'There should be only 1 library (singleBranchMode on)' );
319
    my $facets = C4::Koha::getFacets();
320
    is(
321
        scalar( grep { defined $_->{idx} && $_->{idx} eq 'location' } @$facets ),
322
        1,
323
        'location facet present with singleBranchMode on (bug 10078)'
324
    );
325
326
    $count = 3;    # more libraries..
327
    is( Koha::Libraries->search->count, 3, 'There should be more than 1 library (singleBranchMode off)' );
328
329
    $facets = C4::Koha::getFacets();
330
    is(
331
        scalar( grep { defined $_->{idx} && $_->{idx} eq 'location' } @$facets ),
332
        1,
333
        'location facet present with singleBranchMode off (bug 10078)'
334
    );
181
};
335
};
182
336
183
subtest 'GetItemTypesCategorized test' => sub{
337
subtest 'GetItemTypesCategorized test' => sub {
184
    plan tests => 9;
338
    plan tests => 9;
185
339
186
    my $avc = Koha::AuthorisedValueCategories->find('ITEMTYPECAT');
340
    my $avc = Koha::AuthorisedValueCategories->find('ITEMTYPECAT');
187
- 

Return to bug 34812