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

(-)a/Koha/Patron/Attribute.pm (+51 lines)
Lines 21-29 use Koha::Database; Link Here
21
use Koha::Exceptions::Patron::Attribute;
21
use Koha::Exceptions::Patron::Attribute;
22
use Koha::Patron::Attribute::Types;
22
use Koha::Patron::Attribute::Types;
23
use Koha::AuthorisedValues;
23
use Koha::AuthorisedValues;
24
use Koha::Cache::Memory::Lite;
25
use C4::Log qw( logaction );
26
use JSON qw( to_json );
24
27
25
use base qw(Koha::Object);
28
use base qw(Koha::Object);
26
29
30
our %deleted_attributes;
31
27
=head1 NAME
32
=head1 NAME
28
33
29
Koha::Patron::Attribute - Koha Patron Attribute Object class
34
Koha::Patron::Attribute - Koha Patron Attribute Object class
Lines 55-60 sub store { Link Here
55
    Koha::Exceptions::Patron::Attribute::UniqueIDConstraint->throw( attribute => $self )
60
    Koha::Exceptions::Patron::Attribute::UniqueIDConstraint->throw( attribute => $self )
56
        unless $self->unique_ok();
61
        unless $self->unique_ok();
57
62
63
    # Only log for non-repeatable attributes
64
    if (!$self->type->repeatable) {
65
        my $change;
66
        if ( exists $deleted_attributes{$self->_deleted_attribute_key} ) {
67
            my $previous_attribute = $deleted_attributes{$self->_deleted_attribute_key};
68
            if ( $previous_attribute->attribute ne $self->attribute ) {
69
                $change = {
70
                    before => $previous_attribute->attribute,
71
                    after  => $self->attribute
72
                };
73
            }
74
            delete $deleted_attributes{$self->_deleted_attribute_key};
75
        } else {
76
            $change = {
77
                before => "",
78
                after  => $self->attribute
79
            };
80
        }
81
        if (defined $change) {
82
            logaction(
83
                "MEMBERS",
84
                "MODIFY",
85
                $self->borrowernumber,
86
                "Patron attribute " . $self->code . ": " . to_json($change, { pretty => 1, canonical => 1 })
87
            );
88
        }
89
    }
58
    return $self->SUPER::store();
90
    return $self->SUPER::store();
59
}
91
}
60
92
Lines 189-194 sub unique_ok { Link Here
189
    return $ok;
221
    return $ok;
190
}
222
}
191
223
224
=head3 delete
225
226
Overloaded I<store> method to temporarily stash deleted attribute for action log.
227
228
=cut
229
230
sub delete {
231
    my ($self) = @_;
232
    if (!$self->type->repeatable) {
233
        $deleted_attributes{$self->_deleted_attribute_key} = $self;
234
    }
235
    $self->SUPER::delete();
236
}
237
192
=head2 Internal methods
238
=head2 Internal methods
193
239
194
=head3 _type
240
=head3 _type
Lines 199-202 sub _type { Link Here
199
    return 'BorrowerAttribute';
245
    return 'BorrowerAttribute';
200
}
246
}
201
247
248
sub _deleted_attribute_key {
249
    my ($self) = @_;
250
    return join(':', $self->borrowernumber, $self->code);
251
}
252
202
1;
253
1;
(-)a/t/db_dependent/Koha/Patron/Attribute.t (-2 / +89 lines)
Lines 18-25 Link Here
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
use JSON qw( to_json );
21
22
22
use Test::More tests => 3;
23
use Test::More tests => 4;
23
24
24
use t::lib::TestBuilder;
25
use t::lib::TestBuilder;
25
use Test::Exception;
26
use Test::Exception;
Lines 27-32 use Test::Exception; Link Here
27
use Koha::Database;
28
use Koha::Database;
28
use Koha::Patron::Attribute;
29
use Koha::Patron::Attribute;
29
use Koha::Patron::Attributes;
30
use Koha::Patron::Attributes;
31
use Koha::ActionLogs;
30
32
31
my $schema  = Koha::Database->new->schema;
33
my $schema  = Koha::Database->new->schema;
32
my $builder = t::lib::TestBuilder->new;
34
my $builder = t::lib::TestBuilder->new;
Lines 404-406 subtest 'merge_and_replace_with' => sub { Link Here
404
    $schema->storage->txn_rollback;
406
    $schema->storage->txn_rollback;
405
407
406
};
408
};
407
- 
409
410
subtest 'action log tests' => sub {
411
    plan tests => 3;
412
    my $schema = Koha::Database->new->schema;
413
    $schema->storage->txn_begin;
414
415
    my $patron = $builder->build_object({ class=> 'Koha::Patrons' });
416
    my $attribute_type = $builder->build_object(
417
        {
418
            class => 'Koha::Patron::Attribute::Types',
419
            value  => { repeatable => 0 }
420
        }
421
    );
422
423
    my $attributes = [
424
        {
425
            attribute => 'Foo',
426
            code => $attribute_type->code,
427
        }
428
    ];
429
    $patron->extended_attributes($attributes);
430
431
    my $change = {
432
        before => "",
433
        after  => 'Foo',
434
    };
435
    my $info = "Patron attribute " . $attribute_type->code . ": " . to_json($change, { pretty => 1, canonical => 1 });
436
    my $action_logs = Koha::ActionLogs->search(
437
        {
438
            module => "MEMBERS",
439
            action => "MODIFY",
440
            object => $patron->borrowernumber,
441
            info => $info
442
        }
443
    );
444
    is(
445
        $action_logs->count,
446
        1,
447
        'An action log entry has been created when adding patron attribute'
448
    );
449
450
    $patron->extended_attributes($attributes);
451
    $action_logs = Koha::ActionLogs->search(
452
        {
453
            module => "MEMBERS",
454
            action => "MODIFY",
455
            object => $patron->borrowernumber,
456
            info => $info
457
        }
458
    );
459
    is(
460
        $action_logs->count,
461
        1,
462
        'No additional action log entry has been created when updating patron attribute with same value'
463
    );
464
465
    $attributes = [
466
        {
467
            attribute => 'Bar',
468
            code => $attribute_type->code,
469
        }
470
    ];
471
    $patron->extended_attributes($attributes);
472
473
    $change = {
474
        before => 'Foo',
475
        after  => 'Bar'
476
    };
477
    $info = "Patron attribute " . $attribute_type->code . ": " . to_json($change, { pretty => 1, canonical => 1 });
478
    $action_logs = Koha::ActionLogs->search(
479
        {
480
            module => "MEMBERS",
481
            action => "MODIFY",
482
            object => $patron->borrowernumber,
483
            info => $info
484
        }
485
    );
486
    is(
487
        $action_logs->count,
488
        1,
489
        'New action log entry has been created when updating patron attribute with different value'
490
    );
491
492
    $schema->storage->txn_rollback;
493
};
494

Return to bug 26744