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 70-75 sub store { Link Here
70
        };
74
        };
71
    }
75
    }
72
76
77
    my $patron = Koha::Patrons->find( $self->borrowernumber );
78
    if ( $patron ) {
79
        my %columns = map { $_ => 1 } Koha::Patrons->columns;
80
81
        my $changes_made = 0;
82
        foreach my $column ( keys %{$self->unblessed} ) {
83
            if (exists $columns{$column}) {
84
                if ($patron->$column ne $self->$column) {
85
                    $changes_made = 1;
86
                    last;
87
                }
88
                # Set the changes for patron without commiting - this will be used
89
                # for validating the given parameters via Koha::Patron.
90
                # Do not ->store the patron!
91
                $patron->$column($self->$column);
92
            }
93
        }
94
        my $extended_attributes = try { from_json( $self->extended_attributes ) };
95
        if (!$changes_made && $extended_attributes) {
96
            my %codes = map { $_->{code} => $_->{value} } @{$extended_attributes};
97
            foreach my $code (keys %codes) {
98
                delete $codes{$code} if Koha::Patron::Attributes->search({
99
                        borrowernumber => $patron->borrowernumber,
100
                        code           => $code,
101
                        attribute      => $codes{$code}
102
                    })->count > 0;
103
            }
104
            $changes_made = 1 if keys %codes > 0;
105
        }
106
        Koha::Exceptions::NoChanges->throw(
107
            error => "No changes have been made",
108
        ) unless $changes_made;
109
        # Validate changes via Koha::Patron
110
        $patron->_validate;
111
    }
112
73
    return $self->SUPER::store();
113
    return $self->SUPER::store();
74
}
114
}
75
115
(-)a/t/db_dependent/Koha/Patron/Modifications.t (-4 / +35 lines)
Lines 85-91 subtest 'new() tests' => sub { Link Here
85
85
86
subtest 'store( extended_attributes ) tests' => sub {
86
subtest 'store( extended_attributes ) tests' => sub {
87
87
88
    plan tests => 4;
88
    plan tests => 6;
89
89
90
    $schema->storage->txn_begin;
90
    $schema->storage->txn_begin;
91
91
Lines 94-101 subtest 'store( extended_attributes ) tests' => sub { Link Here
94
    my $patron
94
    my $patron
95
        = $builder->build( { source => 'Borrower' } )->{borrowernumber};
95
        = $builder->build( { source => 'Borrower' } )->{borrowernumber};
96
    my $verification_token = md5_hex( time().{}.rand().{}.$$ );
96
    my $verification_token = md5_hex( time().{}.rand().{}.$$ );
97
    my $valid_json_text    = '[{"code":"CODE","value":"VALUE"}]';
97
    $builder->build(
98
    my $invalid_json_text  = '[{"code":"CODE";"value":"VALUE"}]';
98
        { source => 'BorrowerAttributeType', value => { code => 'CODE_1' } }
99
    );
100
    $builder->build(
101
        { source => 'BorrowerAttributeType', value => { code => 'CODE_2' } }
102
    );
103
    my $valid_json_text    = '[{"code":"CODE_1","value":"VALUE"}]';
104
    my $invalid_json_text  = '[{"code":"CODE_2";"value":"VALUE"}]';
99
105
100
    Koha::Patron::Modification->new(
106
    Koha::Patron::Modification->new(
101
        {   verification_token  => $verification_token,
107
        {   verification_token  => $verification_token,
Lines 130-135 subtest 'store( extended_attributes ) tests' => sub { Link Here
130
136
131
    is( $@, 'The passed extended_attributes is not valid JSON' );
137
    is( $@, 'The passed extended_attributes is not valid JSON' );
132
138
139
    Koha::Patrons->find($patron)->surname('Hall')->store;
140
    throws_ok {
141
        Koha::Patron::Modification->new(
142
            {   verification_token  => $verification_token,
143
                borrowernumber      => $patron,
144
                surname             => 'Hall',
145
            }
146
        )->store();
147
    }
148
    'Koha::Exceptions::NoChanges',
149
        'Trying to create a modification request without changing anything'
150
        .' raises exception';
151
152
    $patron_modification->approve;
153
    throws_ok {
154
        Koha::Patron::Modification->new(
155
            {   verification_token  => $verification_token,
156
                borrowernumber      => $patron,
157
                extended_attributes => $valid_json_text,
158
            }
159
        )->store();
160
    }
161
    'Koha::Exceptions::NoChanges',
162
        'Trying to create a modification request without changing anything'
163
        .' raises exception';
164
133
    $schema->storage->txn_rollback;
165
    $schema->storage->txn_rollback;
134
};
166
};
135
167
136
- 

Return to bug 16330