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