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

(-)a/t/db_dependent/Koha/Patrons/Import.t (-2 / +111 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 Test::More tests => 160;
21
use Test::More tests => 161;
22
use Test::Warn;
22
use Test::Warn;
23
use Test::Exception;
23
use Encode qw( encode_utf8 );
24
use Encode qw( encode_utf8 );
24
use utf8;
25
use utf8;
25
26
Lines 842-847 subtest 'test_format_dates' => sub { Link Here
842
    is($missing_criticals_2[2]->{lineraw}, $borrowerline, 'Got the expected third lineraw from check_branch_code with bad dates');
843
    is($missing_criticals_2[2]->{lineraw}, $borrowerline, 'Got the expected third lineraw from check_branch_code with bad dates');
843
};
844
};
844
845
846
subtest 'patron_attributes' => sub {
847
848
    plan tests => 4;
849
850
    t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 1);
851
852
    my $unique_attribute_type = $builder->build_object(
853
        {
854
            class => 'Koha::Patron::Attribute::Types',
855
            value => { unique_id=> 1, repeatable => 0 }
856
        }
857
    );
858
    my $repeatable_attribute_type = $builder->build_object(
859
        {
860
            class => 'Koha::Patron::Attribute::Types',
861
            value => { unique_id => 0, repeatable => 1 }
862
        }
863
    );
864
    my $normal_attribute_type = $builder->build_object(
865
        {
866
            class => 'Koha::Patron::Attribute::Types',
867
            value => { unique_id => 0, repeatable => 0 }
868
        }
869
    );
870
    my $non_existent_attribute_type = $builder->build_object(
871
        {
872
            class => 'Koha::Patron::Attribute::Types',
873
        }
874
    );
875
    my $non_existent_attribute_type_code = $non_existent_attribute_type->code;
876
    $non_existent_attribute_type->delete;
877
878
    # attributes is { code => \@attributes }
879
    sub build_csv {
880
        my ($attributes) = @_;
881
882
        my $csv_headers = 'cardnumber,surname,firstname,branchcode,categorycode,patron_attributes';
883
        my @attributes_str = map { my $code = $_; map {  sprintf "%s:%s", $code, $_ } @{ $attributes->{$code} } } keys %$attributes;
884
        my $attributes_str = join ',', @attributes_str;
885
        my $csv_line = sprintf '1042,John,D,MPL,PT,"%s"', $attributes_str;
886
        my $filename = make_csv( $temp_dir, $csv_headers, $csv_line );
887
        open( my $fh, "<:encoding(utf8)", $filename ) or die "cannot open $filename: $!";
888
        return $fh;
889
    }
890
891
    { # Everything good, we create a patron with 3 attributes
892
        my $attributes = {
893
            $unique_attribute_type->code => ['my unique attribute 1'],
894
            $repeatable_attribute_type->code => [ 'my repeatable attribute 1', 'my repeatable attribute 2' ],
895
            $normal_attribute_type->code => ['my normal attribute 1'],
896
        };
897
        my $fh = build_csv({ %$attributes });
898
        my $result = $patrons_import->import_patrons({file => $fh});
899
900
        is( $result->{imported}, 1 );
901
902
        my $patron = Koha::Patrons->find({cardnumber => "1042"});
903
        compare_patron_attributes($patron->extended_attributes->unblessed, { %$attributes } );
904
        $patron->delete;
905
    }
906
907
    {
908
        $builder->build_object(
909
            {
910
                class => 'Koha::Patron::Attributes',
911
                value => { code => $unique_attribute_type->code, attribute => 'unique' }
912
            }
913
        );
914
915
        my $attributes = {
916
            $unique_attribute_type->code => ['unique'],
917
            $normal_attribute_type->code => ['my normal attribute 1']
918
        };
919
        my $fh = build_csv({ %$attributes });
920
921
        throws_ok {
922
            $patrons_import->import_patrons({file => $fh});
923
        }
924
        'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint';
925
926
        my $patron = Koha::Patrons->find({cardnumber => "1042"});
927
        is( $patron, undef );
928
929
    }
930
931
};
932
933
# got is { code => $code, attribute => $attribute }
934
# expected is { $code => \@attributes }
935
sub compare_patron_attributes {
936
    my ( $got, $expected ) = @_;
937
938
    $got = [ map { { code => $_->{code}, attribute => $_->{attribute} } } @$got ];
939
    $expected = [
940
        map {
941
            my $code = $_;
942
            map { { code => $code, attribute => $_ } } @{ $expected->{$code} }
943
          } keys %$expected
944
    ];
945
    for my $v ( $got, $expected ) {
946
        $v = [
947
            sort {
948
                $a->{code} cmp $b->{code} || $a->{attribute} cmp $b->{attribute}
949
            } @$v
950
        ];
951
    }
952
    is_deeply($got, $expected);
953
}
954
845
# ###### Test utility ###########
955
# ###### Test utility ###########
846
sub make_csv {
956
sub make_csv {
847
    my ($temp_dir, @lines) = @_;
957
    my ($temp_dir, @lines) = @_;
848
- 

Return to bug 28220