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

(-)a/t/db_dependent/Koha/Patrons.t (-2 / +112 lines)
Lines 54-59 my $new_patron_1 = Koha::Patron->new( Link Here
54
        surname => 'surname for patron1',
54
        surname => 'surname for patron1',
55
        firstname => 'firstname for patron1',
55
        firstname => 'firstname for patron1',
56
        userid => 'a_nonexistent_userid_1',
56
        userid => 'a_nonexistent_userid_1',
57
        flags => 1, # Is superlibrarian
57
    }
58
    }
58
)->store;
59
)->store;
59
my $new_patron_2  = Koha::Patron->new(
60
my $new_patron_2  = Koha::Patron->new(
Lines 67-73 my $new_patron_2 = Koha::Patron->new( Link Here
67
)->store;
68
)->store;
68
69
69
C4::Context->_new_userenv('xxx');
70
C4::Context->_new_userenv('xxx');
70
C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', '');
71
set_logged_in_user( $new_patron_1 );
71
72
72
is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
73
is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
73
74
Lines 933-938 subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub { Link Here
933
934
934
    Koha::Patrons->find( $anonymous->{borrowernumber})->delete;
935
    Koha::Patrons->find( $anonymous->{borrowernumber})->delete;
935
    Koha::Patrons->find( $userenv_patron->{borrowernumber})->delete;
936
    Koha::Patrons->find( $userenv_patron->{borrowernumber})->delete;
937
938
    # Reset IndependentBranches for further tests
939
    t::lib::Mocks::mock_preference('IndependentBranches', 0);
940
};
941
942
subtest 'libraries_where_can_see_patrons + can_see_patron_infos + search_limited' => sub {
943
    plan tests => 3;
944
945
    # group1
946
    #   + library_11
947
    #   + library_12
948
    # group2
949
    #   + library21
950
    my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
951
    my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
952
    my $library_11 = $builder->build( { source => 'Branch' } );
953
    my $library_12 = $builder->build( { source => 'Branch' } );
954
    my $library_21 = $builder->build( { source => 'Branch' } );
955
    $library_11 = Koha::Libraries->find( $library_11->{branchcode} );
956
    $library_12 = Koha::Libraries->find( $library_12->{branchcode} );
957
    $library_21 = Koha::Libraries->find( $library_21->{branchcode} );
958
    Koha::Library::Group->new(
959
        { branchcode => $library_11->branchcode, parent_id => $group_1->id } )->store;
960
    Koha::Library::Group->new(
961
        { branchcode => $library_12->branchcode, parent_id => $group_1->id } )->store;
962
    Koha::Library::Group->new(
963
        { branchcode => $library_21->branchcode, parent_id => $group_2->id } )->store;
964
965
    my $sth = C4::Context->dbh->prepare(q|INSERT INTO user_permissions( borrowernumber, module_bit, code ) VALUES (?, 4, ?)|); # 4 for borrowers
966
    # 2 patrons from library_11 (group1)
967
    # patron_11_1 see patron's infos from outside its group
968
    # Setting flags => undef to not be considered as superlibrarian
969
    my $patron_11_1 = $builder->build({ source => 'Borrower', value => { branchcode => $library_11->branchcode, flags => undef, }});
970
    $patron_11_1 = Koha::Patrons->find( $patron_11_1->{borrowernumber} );
971
    $sth->execute( $patron_11_1->borrowernumber, 'edit_borrowers' );
972
    $sth->execute( $patron_11_1->borrowernumber, 'view_borrower_infos_from_any_libraries' );
973
    # patron_11_2 can only see patron's info from its group
974
    my $patron_11_2 = $builder->build({ source => 'Borrower', value => { branchcode => $library_11->branchcode, flags => undef, }});
975
    $patron_11_2 = Koha::Patrons->find( $patron_11_2->{borrowernumber} );
976
    $sth->execute( $patron_11_2->borrowernumber, 'edit_borrowers' );
977
    # 1 patron from library_12 (group1)
978
    my $patron_12 = $builder->build({ source => 'Borrower', value => { branchcode => $library_12->branchcode, flags => undef, }});
979
    $patron_12 = Koha::Patrons->find( $patron_12->{borrowernumber} );
980
    # 1 patron from library_21 (group2) can only see patron's info from its group
981
    my $patron_21 = $builder->build({ source => 'Borrower', value => { branchcode => $library_21->branchcode, flags => undef, }});
982
    $patron_21 = Koha::Patrons->find( $patron_21->{borrowernumber} );
983
    $sth->execute( $patron_21->borrowernumber, 'edit_borrowers' );
984
985
    # Pfiou, we can start now!
986
    subtest 'libraries_where_can_see_patrons' => sub {
987
        plan tests => 3;
988
989
        my @branchcodes;
990
991
        set_logged_in_user( $patron_11_1 );
992
        @branchcodes = $patron_11_1->libraries_where_can_see_patrons;
993
        is_deeply( \@branchcodes, [], q|patron_11_1 has view_borrower_infos_from_any_libraries => No restriction| );
994
995
        set_logged_in_user( $patron_11_2 );
996
        @branchcodes = $patron_11_2->libraries_where_can_see_patrons;
997
        is_deeply( \@branchcodes, [ sort ( $library_11->branchcode, $library_12->branchcode ) ], q|patron_11_2 has not view_borrower_infos_from_any_libraries => Can only see patron's from its group| );
998
999
        set_logged_in_user( $patron_21 );
1000
        @branchcodes = $patron_21->libraries_where_can_see_patrons;
1001
        is_deeply( \@branchcodes, [$library_21->branchcode], q|patron_21 has not view_borrower_infos_from_any_libraries => Can only see patron's from its group| );
1002
    };
1003
    subtest 'can_see_patron_infos' => sub {
1004
        plan tests => 6;
1005
1006
        set_logged_in_user( $patron_11_1 );
1007
        is( $patron_11_1->can_see_patron_infos( $patron_11_2 ), 1, q|patron_11_1 can see patron_11_2, from its library| );
1008
        is( $patron_11_1->can_see_patron_infos( $patron_12 ),   1, q|patron_11_1 can see patron_12, from its group| );
1009
        is( $patron_11_1->can_see_patron_infos( $patron_21 ),   1, q|patron_11_1 can see patron_11_2, from another group| );
1010
1011
        set_logged_in_user( $patron_11_2 );
1012
        is( $patron_11_2->can_see_patron_infos( $patron_11_1 ), 1, q|patron_11_2 can see patron_11_1, from its library| );
1013
        is( $patron_11_2->can_see_patron_infos( $patron_12 ),   1, q|patron_11_2 can see patron_12, from its group| );
1014
        is( $patron_11_2->can_see_patron_infos( $patron_21 ),   0, q|patron_11_2 can NOT see patron_21, from another group| );
1015
    };
1016
    subtest 'search_limited' => sub {
1017
        plan tests => 6;
1018
1019
        set_logged_in_user( $patron_11_1 );
1020
        my $total_number_of_patrons = $nb_of_patrons + 6; # 2 created before + 4 for these subtests
1021
        is( Koha::Patrons->search->count, $total_number_of_patrons, 'Non-limited search should return all patrons');
1022
        is( Koha::Patrons->search_limited->count, $total_number_of_patrons, 'patron_11_1 is allowed to see all patrons' );
1023
1024
        set_logged_in_user( $patron_11_2 );
1025
        is( Koha::Patrons->search->count, $total_number_of_patrons, 'Non-limited search should return all patrons');
1026
        is( Koha::Patrons->search_limited->count, 3, 'patron_12_1 is not allowed to see patrons from other groups, only patron_11_1, patron_11_2 and patron_12' );
1027
1028
        set_logged_in_user( $patron_21 );
1029
        is( Koha::Patrons->search->count, $total_number_of_patrons, 'Non-limited search should return all patrons');
1030
        is( Koha::Patrons->search_limited->count, 1, 'patron_21 is not allowed to see patrons from other groups, only himself' );
1031
    };
1032
    $patron_11_1->delete;
1033
    $patron_11_2->delete;
1034
    $patron_12->delete;
1035
    $patron_21->delete;
936
};
1036
};
937
1037
938
subtest 'account_locked' => sub {
1038
subtest 'account_locked' => sub {
Lines 960-962 is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have delete Link Here
960
1060
961
$schema->storage->txn_rollback;
1061
$schema->storage->txn_rollback;
962
1062
963
- 
1063
# TODO Move to t::lib::Mocks and reuse it!
1064
sub set_logged_in_user {
1065
    my ($patron) = @_;
1066
    C4::Context->set_userenv(
1067
        $patron->borrowernumber, $patron->userid,
1068
        $patron->cardnumber,     'firstname',
1069
        'surname',               $patron->library->branchcode,
1070
        'Midway Public Library', $patron->flags,
1071
        '',                      ''
1072
    );
1073
}

Return to bug 18403