Lines 18-32
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; |
|
|
26 |
use t::lib::Mocks; |
25 |
use Test::Exception; |
27 |
use Test::Exception; |
26 |
|
28 |
|
27 |
use Koha::Database; |
29 |
use Koha::Database; |
28 |
use Koha::Patron::Attribute; |
30 |
use Koha::Patron::Attribute; |
29 |
use Koha::Patron::Attributes; |
31 |
use Koha::Patron::Attributes; |
|
|
32 |
use Koha::ActionLogs; |
30 |
|
33 |
|
31 |
my $schema = Koha::Database->new->schema; |
34 |
my $schema = Koha::Database->new->schema; |
32 |
my $builder = t::lib::TestBuilder->new; |
35 |
my $builder = t::lib::TestBuilder->new; |
Lines 519-521
subtest 'merge_and_replace_with' => sub {
Link Here
|
519 |
$schema->storage->txn_rollback; |
522 |
$schema->storage->txn_rollback; |
520 |
|
523 |
|
521 |
}; |
524 |
}; |
522 |
- |
525 |
|
|
|
526 |
subtest 'action log tests' => sub { |
527 |
plan tests => 11; |
528 |
my $schema = Koha::Database->new->schema; |
529 |
$schema->storage->txn_begin; |
530 |
|
531 |
my $get_info = sub { |
532 |
my ( $before, $after, $code, $repeatable ) = @_; |
533 |
my $change = { |
534 |
before => $before, |
535 |
after => $after |
536 |
}; |
537 |
if ($repeatable) { |
538 |
while ( my ( $k, $v ) = each %{$change} ) { |
539 |
if ( ref $v eq 'ARRAY' ) { |
540 |
$change->{$k} = [ sort @{$v} ]; |
541 |
} else { |
542 |
$change->{$k} = $v ? [$v] : []; |
543 |
} |
544 |
} |
545 |
} |
546 |
return "Patron attribute " . $code . ": " . to_json( $change, { pretty => 1, canonical => 1 } ); |
547 |
}; |
548 |
|
549 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
550 |
my $attribute_type = $builder->build_object( |
551 |
{ |
552 |
class => 'Koha::Patron::Attribute::Types', |
553 |
value => { repeatable => 0 } |
554 |
} |
555 |
); |
556 |
|
557 |
t::lib::Mocks::mock_preference( 'BorrowersLog', 0 ); |
558 |
my $attributes = [ |
559 |
{ |
560 |
attribute => 'Foo', |
561 |
code => $attribute_type->code, |
562 |
} |
563 |
]; |
564 |
$patron->extended_attributes($attributes); |
565 |
|
566 |
my $info = $get_info->( '', 'Foo', $attribute_type->code ); |
567 |
my $action_logs = Koha::ActionLogs->search( |
568 |
{ |
569 |
module => "MEMBERS", |
570 |
action => "MODIFY", |
571 |
object => $patron->borrowernumber, |
572 |
info => $info |
573 |
} |
574 |
); |
575 |
is( |
576 |
$action_logs->count, |
577 |
0, |
578 |
'No action log entry has been created when adding patron attribute if BorrowersLog syspref disabled' |
579 |
); |
580 |
|
581 |
t::lib::Mocks::mock_preference( 'BorrowersLog', 1 ); |
582 |
my $current_action_logs_count; |
583 |
my $repeatable_text; |
584 |
for my $repeatable ( 0, 1 ) { |
585 |
$repeatable_text = $repeatable ? ' repeatable' : ''; |
586 |
|
587 |
$patron->extended_attributes( [] ); |
588 |
|
589 |
$attribute_type = $builder->build_object( |
590 |
{ |
591 |
class => 'Koha::Patron::Attribute::Types', |
592 |
value => { repeatable => $repeatable } |
593 |
} |
594 |
); |
595 |
$attributes = [ |
596 |
{ |
597 |
attribute => 'Foo', |
598 |
code => $attribute_type->code, |
599 |
} |
600 |
]; |
601 |
|
602 |
$patron->extended_attributes($attributes); |
603 |
$info = $get_info->( '', 'Foo', $attribute_type->code, $repeatable ); |
604 |
$action_logs = Koha::ActionLogs->search( |
605 |
{ |
606 |
module => "MEMBERS", |
607 |
action => "MODIFY", |
608 |
object => $patron->borrowernumber, |
609 |
info => $info |
610 |
} |
611 |
); |
612 |
is( |
613 |
$action_logs->count, |
614 |
1, |
615 |
"An action log entry has been created when adding$repeatable_text patron attribute" |
616 |
); |
617 |
|
618 |
$current_action_logs_count = Koha::ActionLogs->search( |
619 |
{ |
620 |
module => "MEMBERS", |
621 |
action => "MODIFY", |
622 |
object => $patron->borrowernumber |
623 |
} |
624 |
)->count; |
625 |
|
626 |
$patron->extended_attributes($attributes); |
627 |
$action_logs = Koha::ActionLogs->search( |
628 |
{ |
629 |
module => "MEMBERS", |
630 |
action => "MODIFY", |
631 |
object => $patron->borrowernumber |
632 |
} |
633 |
); |
634 |
is( |
635 |
$action_logs->count, |
636 |
$current_action_logs_count, |
637 |
"No additional action log entry has been created when updating$repeatable_text patron attribute with same value" |
638 |
); |
639 |
|
640 |
$attributes = [ |
641 |
{ |
642 |
attribute => 'Bar', |
643 |
code => $attribute_type->code, |
644 |
} |
645 |
]; |
646 |
$patron->extended_attributes($attributes); |
647 |
$info = $get_info->( 'Foo', 'Bar', $attribute_type->code, $repeatable ); |
648 |
$action_logs = Koha::ActionLogs->search( |
649 |
{ |
650 |
module => "MEMBERS", |
651 |
action => "MODIFY", |
652 |
object => $patron->borrowernumber, |
653 |
info => $info |
654 |
} |
655 |
); |
656 |
is( |
657 |
$action_logs->count, |
658 |
1, |
659 |
"New action log entry has been created when updating$repeatable_text patron attribute with different value" |
660 |
); |
661 |
|
662 |
$patron->extended_attributes( [] ); |
663 |
$info = $get_info->( 'Bar', '', $attribute_type->code, $repeatable ); |
664 |
$action_logs = Koha::ActionLogs->search( |
665 |
{ |
666 |
module => "MEMBERS", |
667 |
action => "MODIFY", |
668 |
object => $patron->borrowernumber, |
669 |
info => $info |
670 |
} |
671 |
); |
672 |
is( |
673 |
$action_logs->count, |
674 |
1, |
675 |
"New action log entry has been created when deleting$repeatable_text patron attribute value" |
676 |
); |
677 |
} |
678 |
|
679 |
$attributes = [ |
680 |
{ |
681 |
attribute => 'Foo', |
682 |
code => $attribute_type->code, |
683 |
}, |
684 |
{ |
685 |
attribute => 'Bar', |
686 |
code => $attribute_type->code, |
687 |
} |
688 |
]; |
689 |
$patron->extended_attributes($attributes); |
690 |
|
691 |
$info = $get_info->( [], [ 'Foo', 'Bar' ], $attribute_type->code, 1 ); |
692 |
use Data::Dumper; |
693 |
print Dumper($info); |
694 |
$action_logs = Koha::ActionLogs->search( |
695 |
{ |
696 |
module => "MEMBERS", |
697 |
action => "MODIFY", |
698 |
object => $patron->borrowernumber, |
699 |
info => $info |
700 |
} |
701 |
); |
702 |
is( |
703 |
$action_logs->count, |
704 |
1, |
705 |
"New action log entry has been created when updating repeatable patron attribute with multiple values" |
706 |
); |
707 |
|
708 |
$attributes = [ |
709 |
{ |
710 |
attribute => 'Foo', |
711 |
code => $attribute_type->code, |
712 |
}, |
713 |
{ |
714 |
attribute => 'Bar', |
715 |
code => $attribute_type->code, |
716 |
}, |
717 |
{ |
718 |
attribute => 'Baz', |
719 |
code => $attribute_type->code, |
720 |
} |
721 |
]; |
722 |
$patron->extended_attributes($attributes); |
723 |
|
724 |
$info = $get_info->( |
725 |
[ 'Foo', 'Bar' ], |
726 |
[ 'Foo', 'Bar', 'Baz' ], |
727 |
$attribute_type->code, |
728 |
1 |
729 |
); |
730 |
$action_logs = Koha::ActionLogs->search( |
731 |
{ |
732 |
module => "MEMBERS", |
733 |
action => "MODIFY", |
734 |
object => $patron->borrowernumber, |
735 |
info => $info |
736 |
} |
737 |
); |
738 |
is( |
739 |
$action_logs->count, |
740 |
1, |
741 |
"New action log entry has been created when updating repeatable patron attribute with existing multiple values with multiple values" |
742 |
); |
743 |
|
744 |
$schema->storage->txn_rollback; |
745 |
}; |