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

(-)a/Koha/Exceptions/Patron/Modification.pm (+4 lines)
Lines 10-15 use Exception::Class ( Link Here
10
    'Koha::Exceptions::Patron::Modification::DuplicateVerificationToken' => {
10
    'Koha::Exceptions::Patron::Modification::DuplicateVerificationToken' => {
11
        isa => 'Koha::Exceptions::Patron::Modification',
11
        isa => 'Koha::Exceptions::Patron::Modification',
12
        description => "The verification token given already exists"
12
        description => "The verification token given already exists"
13
    },
14
    'Koha::Exceptions::Patron::Modification::InvalidData' => {
15
        isa => 'Koha::Exceptions::Patron::Modification',
16
        description => "Some passed data is invalid"
13
    }
17
    }
14
);
18
);
15
19
(-)a/Koha/Patron/Modification.pm (-8 / +65 lines)
Lines 22-30 use Modern::Perl; Link Here
22
use Carp;
22
use Carp;
23
23
24
use Koha::Database;
24
use Koha::Database;
25
26
use Koha::Patron::Modifications;
27
use Koha::Exceptions::Patron::Modification;
25
use Koha::Exceptions::Patron::Modification;
26
use Koha::Patron::Modifications;
27
# TODO: Remove once Koha::Patron::Attribute(s) is implemented
28
use C4::Members::Attributes qw( SetBorrowerAttributes );
29
30
use JSON;
31
use Try::Tiny;
28
32
29
use base qw(Koha::Object);
33
use base qw(Koha::Object);
30
34
Lines 44-59 sub store { Link Here
44
    my ($self) = @_;
48
    my ($self) = @_;
45
49
46
    if ( $self->verification_token ) {
50
    if ( $self->verification_token ) {
47
        if ( Koha::Patron::Modifications->search( { verification_token => $self->verification_token } )->count() ) {
51
        if (Koha::Patron::Modifications->search(
48
            Koha::Exceptions::Patron::Modification::DuplicateVerificationToken->throw(
52
                { verification_token => $self->verification_token }
49
                "Duplicate verification token " . $self->verification_token
53
            )->count()
50
            );
54
            )
55
        {
56
            Koha::Exceptions::Patron::Modification::DuplicateVerificationToken
57
                ->throw(
58
                "Duplicate verification token " . $self->verification_token );
59
        }
60
    }
61
62
    if ( $self->extended_attributes ) {
63
        try {
64
            my $json_parser = JSON->new;
65
            $json_parser->decode( $self->extended_attributes );
51
        }
66
        }
67
        catch {
68
            Koha::Exceptions::Patron::Modification::InvalidData->throw(
69
                'The passed extended_attributes is not valid JSON');
70
        };
52
    }
71
    }
53
72
54
    return $self->SUPER::store();
73
    return $self->SUPER::store();
55
}
74
}
56
75
76
57
=head2 approve
77
=head2 approve
58
78
59
$m->approve();
79
$m->approve();
Lines 70-75 sub approve { Link Here
70
90
71
    delete $data->{timestamp};
91
    delete $data->{timestamp};
72
    delete $data->{verification_token};
92
    delete $data->{verification_token};
93
    delete $data->{extended_attributes};
73
94
74
    foreach my $key ( keys %$data ) {
95
    foreach my $key ( keys %$data ) {
75
        delete $data->{$key} unless ( defined( $data->{$key} ) );
96
        delete $data->{$key} unless ( defined( $data->{$key} ) );
Lines 81-91 sub approve { Link Here
81
102
82
    $patron->set($data);
103
    $patron->set($data);
83
104
84
    if ( $patron->store() ) {
105
    # Take care of extended attributes
85
        return $self->delete();
106
    if ( $self->extended_attributes ) {
107
        our $extended_attributes
108
            = try { decode_json( $self->extended_attributes ) }
109
        catch {
110
            Koha::Exceptions::Patron::Modification::InvalidData->throw(
111
                'The passed extended_attributes is not valid JSON');
112
        };
86
    }
113
    }
114
115
    $self->_result->result_source->schema->txn_do(
116
        sub {
117
            try {
118
                $patron->store();
119
120
                # Take care of extended attributes
121
                if ( $self->extended_attributes ) {
122
                    my $extended_attributes
123
                        = decode_json( $self->extended_attributes );
124
                    SetBorrowerAttributes( $patron->borrowernumber,
125
                        $extended_attributes );
126
                }
127
            }
128
            catch {
129
                if ( $_->isa('DBIx::Class::Exception') ) {
130
                    Koha::Exceptions::Patron::Modification->throw(
131
                        $_->{msg} );
132
                }
133
                else {
134
                    Koha::Exceptions::Patron::Modification->throw($@);
135
                }
136
            };
137
        }
138
    );
139
140
    return $self->delete();
87
}
141
}
88
142
143
144
145
89
=head3 type
146
=head3 type
90
147
91
=cut
148
=cut
(-)a/t/db_dependent/Koha_borrower_modifications.t (-6 / +79 lines)
Lines 1-11 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
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
3
use Modern::Perl;
18
use Modern::Perl;
4
use Test::More tests => 10;
19
5
use Try::Tiny;
20
use Test::More tests => 11;
21
use Test::Exception;
6
22
7
use t::lib::TestBuilder;
23
use t::lib::TestBuilder;
8
24
25
use String::Random qw( random_string );
26
use Try::Tiny;
27
9
use C4::Context;
28
use C4::Context;
10
use C4::Members;
29
use C4::Members;
11
30
Lines 14-20 BEGIN { Link Here
14
    use_ok('Koha::Patron::Modifications');
33
    use_ok('Koha::Patron::Modifications');
15
}
34
}
16
35
17
my $schema = Koha::Database->new->schema;
36
my $schema  = Koha::Database->new->schema;
37
my $builder = t::lib::TestBuilder->new;
38
39
40
subtest 'store( extended_attributes ) tests' => sub {
41
42
    plan tests => 4;
43
44
    $schema->storage->txn_begin;
45
46
    Koha::Patron::Modifications->search->delete;
47
48
    my $patron
49
        = $builder->build( { source => 'Borrower' } )->{borrowernumber};
50
    my $verification_token = random_string("..........");
51
    my $valid_json_text    = '[{"code":"CODE","value":"VALUE"}]';
52
    my $invalid_json_text  = '[{';
53
54
    Koha::Patron::Modification->new(
55
        {   verification_token  => $verification_token,
56
            borrowernumber      => $patron,
57
            surname             => 'Hall',
58
            extended_attributes => $valid_json_text
59
        }
60
    )->store();
61
62
    my $patron_modification
63
        = Koha::Patron::Modifications->search( { borrowernumber => $patron } )
64
        ->next;
65
66
    is( $patron_modification->surname,
67
        'Hall', 'Patron modification correctly stored with valid JSON data' );
68
    is( $patron_modification->extended_attributes,
69
        $valid_json_text,
70
        'Patron modification correctly stored with valid JSON data' );
71
72
    $verification_token = random_string("..........");
73
    throws_ok {
74
        Koha::Patron::Modification->new(
75
            {   verification_token  => $verification_token,
76
                borrowernumber      => $patron,
77
                surname             => 'Hall',
78
                extended_attributes => $invalid_json_text
79
            }
80
        )->store();
81
    }
82
    'Koha::Exceptions::Patron::Modification::InvalidData',
83
        'Trying to store invalid JSON in extended_attributes field raises exception';
84
85
    is( $@, 'The passed extended_attributes is not valid JSON' );
86
87
    $schema->storage->txn_rollback;
88
};
89
90
18
$schema->storage->txn_begin;
91
$schema->storage->txn_begin;
19
92
20
my $dbh = C4::Context->dbh;
93
my $dbh = C4::Context->dbh;
Lines 52-58 my $borrower = Link Here
52
is( $borrower->surname, 'Hall', 'Found modification has matching surname' );
125
is( $borrower->surname, 'Hall', 'Found modification has matching surname' );
53
126
54
## Create new pending modification for a patron
127
## Create new pending modification for a patron
55
my $builder = t::lib::TestBuilder->new;
56
my $borr1 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
128
my $borr1 = $builder->build( { source => 'Borrower' } )->{borrowernumber};
57
129
58
my $m1 = Koha::Patron::Modification->new(
130
my $m1 = Koha::Patron::Modification->new(
Lines 106-109 my $new_borrower = GetMember( borrowernumber => $borr1 ); Link Here
106
ok( $new_borrower->{'surname'} eq 'Hall',
178
ok( $new_borrower->{'surname'} eq 'Hall',
107
    'Test approve() applies modification to borrower' );
179
    'Test approve() applies modification to borrower' );
108
180
109
$dbh->rollback();
181
$schema->storage->txn_rollback;
182
183
1;
110
- 

Return to bug 17767