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