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

(-)a/Koha/Patron/Modification.pm (+40 lines)
Lines 1-6 Link Here
1
package Koha::Patron::Modification;
1
package Koha::Patron::Modification;
2
2
3
# Copyright ByWater Solutions 2014
3
# Copyright ByWater Solutions 2014
4
# Copyright Koha-Suomi Oy 2016
4
#
5
#
5
# This file is part of Koha.
6
# This file is part of Koha.
6
#
7
#
Lines 22-27 use Modern::Perl; Link Here
22
use Carp;
23
use Carp;
23
24
24
use Koha::Database;
25
use Koha::Database;
26
use Koha::Exceptions;
27
use Koha::Patrons;
28
25
use Koha::Exceptions::Patron::Modification;
29
use Koha::Exceptions::Patron::Modification;
26
use Koha::Patron::Attribute;
30
use Koha::Patron::Attribute;
27
use Koha::Patron::Attributes;
31
use Koha::Patron::Attributes;
Lines 94-99 sub store { Link Here
94
        };
98
        };
95
    }
99
    }
96
100
101
    my $patron = Koha::Patrons->find( $self->borrowernumber );
102
    if ( $patron ) {
103
        my %columns = map { $_ => 1 } Koha::Patrons->columns;
104
105
        my $changes_made = 0;
106
        foreach my $column ( keys %{$self->unblessed} ) {
107
            if (exists $columns{$column}) {
108
                if ($patron->$column ne $self->$column) {
109
                    $changes_made = 1;
110
                    last;
111
                }
112
                # Set the changes for patron without commiting - this will be used
113
                # for validating the given parameters via Koha::Patron.
114
                # Do not ->store the patron!
115
                $patron->$column($self->$column);
116
            }
117
        }
118
        my $extended_attributes = try { from_json( $self->extended_attributes ) };
119
        if (!$changes_made && $extended_attributes) {
120
            my %codes = map { $_->{code} => $_->{value} } @{$extended_attributes};
121
            foreach my $code (keys %codes) {
122
                delete $codes{$code} if Koha::Patron::Attributes->search({
123
                        borrowernumber => $patron->borrowernumber,
124
                        code           => $code,
125
                        attribute      => $codes{$code}
126
                    })->count > 0;
127
            }
128
            $changes_made = 1 if keys %codes > 0;
129
        }
130
        Koha::Exceptions::NoChanges->throw(
131
            error => "No changes have been made",
132
        ) unless $changes_made;
133
        # Validate changes via Koha::Patron
134
        $patron->_validate;
135
    }
136
97
    return $self->SUPER::store();
137
    return $self->SUPER::store();
98
}
138
}
99
139
(-)a/t/db_dependent/Koha/Patron/Modifications.t (-4 / +35 lines)
Lines 97-103 subtest 'new() tests' => sub { Link Here
97
97
98
subtest 'store( extended_attributes ) tests' => sub {
98
subtest 'store( extended_attributes ) tests' => sub {
99
99
100
    plan tests => 4;
100
    plan tests => 6;
101
101
102
    $schema->storage->txn_begin;
102
    $schema->storage->txn_begin;
103
103
Lines 106-113 subtest 'store( extended_attributes ) tests' => sub { Link Here
106
    my $patron
106
    my $patron
107
        = $builder->build( { source => 'Borrower' } )->{borrowernumber};
107
        = $builder->build( { source => 'Borrower' } )->{borrowernumber};
108
    my $verification_token = md5_hex( time().{}.rand().{}.$$ );
108
    my $verification_token = md5_hex( time().{}.rand().{}.$$ );
109
    my $valid_json_text    = '[{"code":"CODE","value":"VALUE"}]';
109
    $builder->build(
110
    my $invalid_json_text  = '[{"code":"CODE";"value":"VALUE"}]';
110
        { source => 'BorrowerAttributeType', value => { code => 'CODE_1' } }
111
    );
112
    $builder->build(
113
        { source => 'BorrowerAttributeType', value => { code => 'CODE_2' } }
114
    );
115
    my $valid_json_text    = '[{"code":"CODE_1","value":"VALUE"}]';
116
    my $invalid_json_text  = '[{"code":"CODE_2";"value":"VALUE"}]';
111
117
112
    Koha::Patron::Modification->new(
118
    Koha::Patron::Modification->new(
113
        {   verification_token  => $verification_token,
119
        {   verification_token  => $verification_token,
Lines 142-147 subtest 'store( extended_attributes ) tests' => sub { Link Here
142
148
143
    is( $@, 'The passed extended_attributes is not valid JSON' );
149
    is( $@, 'The passed extended_attributes is not valid JSON' );
144
150
151
    Koha::Patrons->find($patron)->surname('Hall')->store;
152
    throws_ok {
153
        Koha::Patron::Modification->new(
154
            {   verification_token  => $verification_token,
155
                borrowernumber      => $patron,
156
                surname             => 'Hall',
157
            }
158
        )->store();
159
    }
160
    'Koha::Exceptions::NoChanges',
161
        'Trying to create a modification request without changing anything'
162
        .' raises exception';
163
164
    $patron_modification->approve;
165
    throws_ok {
166
        Koha::Patron::Modification->new(
167
            {   verification_token  => $verification_token,
168
                borrowernumber      => $patron,
169
                extended_attributes => $valid_json_text,
170
            }
171
        )->store();
172
    }
173
    'Koha::Exceptions::NoChanges',
174
        'Trying to create a modification request without changing anything'
175
        .' raises exception';
176
145
    $schema->storage->txn_rollback;
177
    $schema->storage->txn_rollback;
146
};
178
};
147
179
148
- 

Return to bug 16330