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

(-)a/t/db_dependent/Koha/Object/JSONField.t (-1 / +104 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2022 Koha Development team
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use utf8;
23
24
use Test::More tests => 1;
25
use Test::Exception;
26
27
use Koha::Auth::Identity::Providers;
28
29
use t::lib::TestBuilder;
30
31
my $schema  = Koha::Database->new->schema;
32
my $builder = t::lib::TestBuilder->new;
33
34
subtest 'decode_json_field() and set_encoded_json_field() tests' => sub {
35
36
    plan tests => 16;
37
38
    $schema->storage->txn_begin;
39
40
    my $idp = $builder->build_object({ class => 'Koha::Auth::Identity::Providers' });
41
42
    my $data = { some => 'data' };
43
44
    $idp->set_encoded_json_field({ data => $data, field => 'config' });
45
46
    is_deeply( $idp->_json->decode($idp->config), $data, 'decode what we sent' );
47
    is_deeply( $idp->decode_json_field({ field => 'config' }), $data, 'check with decode_json_field' );
48
49
    # Let's get some Unicode stuff into the game
50
    $data = { favorite_Chinese => [ '葑', '癱' ], latin_dancing => [ '¢', '¥', 'á', 'û' ] };
51
    $idp->set_encoded_json_field({ data => $data, field => 'config' })->store;
52
53
    $idp->discard_changes; # refresh
54
    is_deeply( $idp->decode_json_field({ field => 'config' }), $data, 'Deep compare with Unicode data' );
55
    # To convince you even more
56
    is( ord( $idp->decode_json_field({ field => 'config' })->{favorite_Chinese}->[0] ), 33873, 'We still found Unicode \x8451' );
57
    is( ord( $idp->decode_json_field({ field => 'config' })->{latin_dancing}->[0] ), 162, 'We still found the equivalent of Unicode \x00A2' );
58
59
    # Testing with sending encoded data (which we normally shouldn't do)
60
    my $utf8_data;
61
    foreach my $k ( 'favorite_Chinese', 'latin_dancing' ) {
62
        foreach my $c ( @{$data->{$k}} ) {
63
            push @{$utf8_data->{$k}}, Encode::encode('UTF-8', $c);
64
        }
65
    }
66
    $idp->set_encoded_json_field({ data => $utf8_data, field => 'config' })->store;
67
    $idp->discard_changes; # refresh
68
    is_deeply( $idp->decode_json_field({ field => 'config' }), $utf8_data, 'Deep compare with utf8_data' );
69
    # Need more evidence?
70
    is( ord( $idp->decode_json_field({ field => 'config' })->{favorite_Chinese}->[0] ), 232, 'We still found a UTF8 encoded byte' ); # ord does not need substr here
71
72
    # pathological use cases
73
74
    throws_ok
75
        { $idp->set_encoded_json_field({ data => $data }); }
76
        'Koha::Exceptions::MissingParameter',
77
        'Exception thrown on missing parameter';
78
    is( $@->parameter, 'field', 'Correct parameter reported (field)' );
79
80
    throws_ok
81
        { $idp->set_encoded_json_field({ data => $data, field => undef }); }
82
        'Koha::Exceptions::MissingParameter',
83
        'Exception thrown on missing parameter';
84
    is( $@->parameter, 'field', 'Correct parameter reported (field)' );
85
86
    throws_ok
87
        { $idp->set_encoded_json_field({ field => 'something' }); }
88
        'Koha::Exceptions::MissingParameter',
89
        'Exception thrown on missing parameter';
90
    is( $@->parameter, 'data', 'Correct parameter reported (data)' );
91
92
    $idp->set_encoded_json_field({ data => undef, field => 'config' });
93
    is( $idp->config, undef, 'undef is undef' );
94
95
    # set invalid data
96
    $idp->config('{');
97
    throws_ok
98
        { $idp->decode_json_field({ field => 'config' }) }
99
        'Koha::Exceptions::Object::BadValue',
100
        'Exception thrown';
101
    like( "$@", qr/Error reading JSON data/ );
102
103
    $schema->storage->txn_rollback;
104
};

Return to bug 32370