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

(-)a/Koha/Patron.pm (-1 / +10 lines)
Lines 62-68 our $RESULTSET_PATRON_ID_MAPPING = { Link Here
62
    Aqbudget             => 'budget_owner_id',
62
    Aqbudget             => 'budget_owner_id',
63
    Aqbudgetborrower     => 'borrowernumber',
63
    Aqbudgetborrower     => 'borrowernumber',
64
    ArticleRequest       => 'borrowernumber',
64
    ArticleRequest       => 'borrowernumber',
65
    BorrowerAttribute    => 'borrowernumber',
66
    BorrowerDebarment    => 'borrowernumber',
65
    BorrowerDebarment    => 'borrowernumber',
67
    BorrowerFile         => 'borrowernumber',
66
    BorrowerFile         => 'borrowernumber',
68
    BorrowerModification => 'borrowernumber',
67
    BorrowerModification => 'borrowernumber',
Lines 629-634 sub merge_with { Link Here
629
            # Unbless for safety, the patron will end up being deleted
628
            # Unbless for safety, the patron will end up being deleted
630
            $results->{merged}->{$patron_id}->{patron} = $patron->unblessed;
629
            $results->{merged}->{$patron_id}->{patron} = $patron->unblessed;
631
630
631
            my $attributes = $patron->extended_attributes;
632
            my $new_attributes = [
633
                map { { code => $_->code, attribute => $_->attribute } }
634
                    $attributes->as_list
635
            ];
636
            $attributes->delete; # We need to delete before trying to merge them to prevent exception on unique and repeatable
637
            for my $attribute ( @$new_attributes ) {
638
                $self->add_extended_attribute($attribute);
639
            }
640
632
            while (my ($r, $field) = each(%$RESULTSET_PATRON_ID_MAPPING)) {
641
            while (my ($r, $field) = each(%$RESULTSET_PATRON_ID_MAPPING)) {
633
                my $rs = $schema->resultset($r)->search({ $field => $patron_id });
642
                my $rs = $schema->resultset($r)->search({ $field => $patron_id });
634
                $results->{merged}->{ $patron_id }->{updated}->{$r} = $rs->count();
643
                $results->{merged}->{ $patron_id }->{updated}->{$r} = $rs->count();
(-)a/t/db_dependent/Koha/Patrons.t (-2 / +113 lines)
Lines 1606-1612 subtest 'BorrowersLog tests' => sub { Link Here
1606
$schema->storage->txn_rollback;
1606
$schema->storage->txn_rollback;
1607
1607
1608
subtest 'Test Koha::Patrons::merge' => sub {
1608
subtest 'Test Koha::Patrons::merge' => sub {
1609
    plan tests => 113;
1609
    plan tests => 110;
1610
1610
1611
    my $schema = Koha::Database->new()->schema();
1611
    my $schema = Koha::Database->new()->schema();
1612
1612
Lines 1657-1662 subtest 'Test Koha::Patrons::merge' => sub { Link Here
1657
    is( $results, undef, "Anonymous patron cannot have other patrons merged into it" );
1657
    is( $results, undef, "Anonymous patron cannot have other patrons merged into it" );
1658
    is( Koha::Patrons->search( { borrowernumber => $keeper->id } )->count, 1, "Patron from attempted merge with AnonymousPatron still exists" );
1658
    is( Koha::Patrons->search( { borrowernumber => $keeper->id } )->count, 1, "Patron from attempted merge with AnonymousPatron still exists" );
1659
1659
1660
    subtest 'extended attributes' => sub {
1661
        plan tests => 5;
1662
1663
        my $keep_patron =
1664
          $builder->build_object( { class => 'Koha::Patrons' } );
1665
        my $merge_patron =
1666
          $builder->build_object( { class => 'Koha::Patrons' } );
1667
1668
        my $attribute_type_normal_1 = $builder->build_object(
1669
            {
1670
                class => 'Koha::Patron::Attribute::Types',
1671
                value => { repeatable => 0, unique_id => 0 }
1672
            }
1673
        );
1674
        my $attribute_type_normal_2 = $builder->build_object(
1675
            {
1676
                class => 'Koha::Patron::Attribute::Types',
1677
                value => { repeatable => 0, unique_id => 0 }
1678
            }
1679
        );
1680
1681
        my $attribute_type_repeatable = $builder->build_object(
1682
            {
1683
                class => 'Koha::Patron::Attribute::Types',
1684
                value => { repeatable => 1, unique_id => 0 }
1685
            }
1686
        );
1687
1688
        my $attr_keep = [
1689
            {
1690
                code      => $attribute_type_normal_1->code,
1691
                attribute => 'from attr 1'
1692
            },
1693
            {
1694
                code      => $attribute_type_repeatable->code,
1695
                attribute => 'from attr repeatable'
1696
            }
1697
        ];
1698
1699
        my $attr_merge = [
1700
            {
1701
                code      => $attribute_type_normal_2->code,
1702
                attribute => 'to attr 2'
1703
            },
1704
            {
1705
                code      => $attribute_type_repeatable->code,
1706
                attribute => 'to attr repeatable'
1707
            },
1708
        ];
1709
1710
        $keep_patron->extended_attributes($attr_keep);
1711
        $merge_patron->extended_attributes($attr_merge);
1712
1713
        $keep_patron->merge_with( [ $merge_patron->borrowernumber ] );
1714
        my $merged_attributes = $keep_patron->extended_attributes;
1715
        is( $merged_attributes->count, 4 );
1716
1717
        sub compare_attributes {
1718
            my ( $got, $expected, $code ) = @_;
1719
1720
            is_deeply(
1721
                [
1722
                    sort $got->search( { code => $code } )
1723
                      ->get_column('attribute')
1724
                ],
1725
                $expected
1726
            );
1727
        }
1728
        compare_attributes(
1729
            $merged_attributes,
1730
            ['from attr 1'],
1731
            $attribute_type_normal_1->code
1732
        );
1733
        compare_attributes(
1734
            $merged_attributes,
1735
            ['to attr 2'],
1736
            $attribute_type_normal_2->code
1737
        );
1738
        compare_attributes(
1739
            $merged_attributes,
1740
            [ 'from attr repeatable', 'to attr repeatable' ],
1741
            $attribute_type_repeatable->code
1742
        );
1743
1744
        # Cleanup
1745
        $keep_patron->delete;
1746
        $merge_patron->delete;
1747
1748
        # Recreate but expect an exception because 2 "normal" attributes will be in the resulting patron
1749
        $keep_patron =
1750
          $builder->build_object( { class => 'Koha::Patrons' } );
1751
        $merge_patron =
1752
          $builder->build_object( { class => 'Koha::Patrons' } );
1753
1754
        $keep_patron->extended_attributes($attr_keep);
1755
        $merge_patron->extended_attributes(
1756
            [
1757
                @$attr_merge,
1758
                {
1759
                    code      => $attribute_type_normal_1->code,
1760
                    attribute => 'yet another attribute for non-repeatable'
1761
                }
1762
            ]
1763
        );
1764
1765
        throws_ok {
1766
            $keep_patron->merge_with( [ $merge_patron->borrowernumber ] );
1767
        }
1768
        'Koha::Exceptions::Patron::Attribute::NonRepeatable',
1769
            'Exception thrown trying to merge several non-repeatable attributes';
1770
    };
1771
1660
    t::lib::Mocks::mock_preference( 'AnonymousPatron', '' );
1772
    t::lib::Mocks::mock_preference( 'AnonymousPatron', '' );
1661
    $schema->storage->txn_rollback;
1773
    $schema->storage->txn_rollback;
1662
};
1774
};
1663
- 

Return to bug 28217