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

(-)a/t/db_dependent/Koha/Patrons.t (-3 / +114 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 21;
22
use Test::More tests => 22;
23
use Test::Warn;
23
use Test::Warn;
24
use DateTime;
24
use DateTime;
25
25
Lines 52-57 my $new_patron_1 = Koha::Patron->new( Link Here
52
        surname => 'surname for patron1',
52
        surname => 'surname for patron1',
53
        firstname => 'firstname for patron1',
53
        firstname => 'firstname for patron1',
54
        userid => 'a_nonexistent_userid_1',
54
        userid => 'a_nonexistent_userid_1',
55
        flags => 1, # Is superlibrarian
55
    }
56
    }
56
)->store;
57
)->store;
57
my $new_patron_2  = Koha::Patron->new(
58
my $new_patron_2  = Koha::Patron->new(
Lines 65-71 my $new_patron_2 = Koha::Patron->new( Link Here
65
)->store;
66
)->store;
66
67
67
C4::Context->_new_userenv('xxx');
68
C4::Context->_new_userenv('xxx');
68
C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Midway Public Library', '', '', '');
69
set_logged_in_user( $new_patron_1 );
69
70
70
is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
71
is( Koha::Patrons->search->count, $nb_of_patrons + 2, 'The 2 patrons should have been added' );
71
72
Lines 879-884 subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub { Link Here
879
880
880
    Koha::Patrons->find( $anonymous->{borrowernumber})->delete;
881
    Koha::Patrons->find( $anonymous->{borrowernumber})->delete;
881
    Koha::Patrons->find( $userenv_patron->{borrowernumber})->delete;
882
    Koha::Patrons->find( $userenv_patron->{borrowernumber})->delete;
883
884
    # Reset IndependentBranches for further tests
885
    t::lib::Mocks::mock_preference('IndependentBranches', 0);
886
};
887
888
subtest 'libraries_where_can_see_patrons + can_see_patron_infos + search_limited' => sub {
889
    plan tests => 3;
890
891
    # group1
892
    #   + library_11
893
    #   + library_12
894
    # group2
895
    #   + library21
896
    my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
897
    my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
898
    my $library_11 = $builder->build( { source => 'Branch' } );
899
    my $library_12 = $builder->build( { source => 'Branch' } );
900
    my $library_21 = $builder->build( { source => 'Branch' } );
901
    $library_11 = Koha::Libraries->find( $library_11->{branchcode} );
902
    $library_12 = Koha::Libraries->find( $library_12->{branchcode} );
903
    $library_21 = Koha::Libraries->find( $library_21->{branchcode} );
904
    Koha::Library::Group->new(
905
        { branchcode => $library_11->branchcode, parent_id => $group_1->id } )->store;
906
    Koha::Library::Group->new(
907
        { branchcode => $library_12->branchcode, parent_id => $group_1->id } )->store;
908
    Koha::Library::Group->new(
909
        { branchcode => $library_21->branchcode, parent_id => $group_2->id } )->store;
910
911
    my $sth = C4::Context->dbh->prepare(q|INSERT INTO user_permissions( borrowernumber, module_bit, code ) VALUES (?, 4, ?)|); # 4 for borrowers
912
    # 2 patrons from library_11 (group1)
913
    # patron_11_1 see patron's infos from outside its group
914
    # Setting flags => undef to not be considered as superlibrarian
915
    my $patron_11_1 = $builder->build({ source => 'Borrower', value => { branchcode => $library_11->branchcode, flags => undef, }});
916
    $patron_11_1 = Koha::Patrons->find( $patron_11_1->{borrowernumber} );
917
    $sth->execute( $patron_11_1->borrowernumber, 'edit_borrowers' );
918
    $sth->execute( $patron_11_1->borrowernumber, 'view_borrower_infos_from_any_libraries' );
919
    # patron_11_2 can only see patron's info from its group
920
    my $patron_11_2 = $builder->build({ source => 'Borrower', value => { branchcode => $library_11->branchcode, flags => undef, }});
921
    $patron_11_2 = Koha::Patrons->find( $patron_11_2->{borrowernumber} );
922
    $sth->execute( $patron_11_2->borrowernumber, 'edit_borrowers' );
923
    # 1 patron from library_12 (group1)
924
    my $patron_12 = $builder->build({ source => 'Borrower', value => { branchcode => $library_12->branchcode, flags => undef, }});
925
    $patron_12 = Koha::Patrons->find( $patron_12->{borrowernumber} );
926
    # 1 patron from library_21 (group2) can only see patron's info from its group
927
    my $patron_21 = $builder->build({ source => 'Borrower', value => { branchcode => $library_21->branchcode, flags => undef, }});
928
    $patron_21 = Koha::Patrons->find( $patron_21->{borrowernumber} );
929
    $sth->execute( $patron_21->borrowernumber, 'edit_borrowers' );
930
931
    # Pfiou, we can start now!
932
    subtest 'libraries_where_can_see_patrons' => sub {
933
        plan tests => 3;
934
935
        my @branchcodes;
936
937
        set_logged_in_user( $patron_11_1 );
938
        @branchcodes = $patron_11_1->libraries_where_can_see_patrons;
939
        is_deeply( \@branchcodes, [], q|patron_11_1 has view_borrower_infos_from_any_libraries => No restriction| );
940
941
        set_logged_in_user( $patron_11_2 );
942
        @branchcodes = $patron_11_2->libraries_where_can_see_patrons;
943
        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| );
944
945
        set_logged_in_user( $patron_21 );
946
        @branchcodes = $patron_21->libraries_where_can_see_patrons;
947
        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| );
948
    };
949
    subtest 'can_see_patron_infos' => sub {
950
        plan tests => 6;
951
952
        set_logged_in_user( $patron_11_1 );
953
        is( $patron_11_1->can_see_patron_infos( $patron_11_2 ), 1, q|patron_11_1 can see patron_11_2, from its library| );
954
        is( $patron_11_1->can_see_patron_infos( $patron_12 ),   1, q|patron_11_1 can see patron_12, from its group| );
955
        is( $patron_11_1->can_see_patron_infos( $patron_21 ),   1, q|patron_11_1 can see patron_11_2, from another group| );
956
957
        set_logged_in_user( $patron_11_2 );
958
        is( $patron_11_2->can_see_patron_infos( $patron_11_1 ), 1, q|patron_11_2 can see patron_11_1, from its library| );
959
        is( $patron_11_2->can_see_patron_infos( $patron_12 ),   1, q|patron_11_2 can see patron_12, from its group| );
960
        is( $patron_11_2->can_see_patron_infos( $patron_21 ),   0, q|patron_11_2 can NOT see patron_21, from another group| );
961
    };
962
    subtest 'search_limited' => sub {
963
        plan tests => 6;
964
965
        set_logged_in_user( $patron_11_1 );
966
        my $total_number_of_patrons = $nb_of_patrons + 6; # 2 created before + 4 for these subtests
967
        is( Koha::Patrons->search->count, $total_number_of_patrons, 'Non-limited search should return all patrons');
968
        is( Koha::Patrons->search_limited->count, $total_number_of_patrons, 'patron_11_1 is allowed to see all patrons' );
969
970
        set_logged_in_user( $patron_11_2 );
971
        is( Koha::Patrons->search->count, $total_number_of_patrons, 'Non-limited search should return all patrons');
972
        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' );
973
974
        set_logged_in_user( $patron_21 );
975
        is( Koha::Patrons->search->count, $total_number_of_patrons, 'Non-limited search should return all patrons');
976
        is( Koha::Patrons->search_limited->count, 1, 'patron_21 is not allowed to see patrons from other groups, only himself' );
977
    };
978
    $patron_11_1->delete;
979
    $patron_11_2->delete;
980
    $patron_12->delete;
981
    $patron_21->delete;
882
};
982
};
883
983
884
$retrieved_patron_1->delete;
984
$retrieved_patron_1->delete;
Lines 886-889 is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have delete Link Here
886
986
887
$schema->storage->txn_rollback;
987
$schema->storage->txn_rollback;
888
988
989
# TODO Move to t::lib::Mocks and reuse it!
990
sub set_logged_in_user {
991
    my ($patron) = @_;
992
    C4::Context->set_userenv(
993
        $patron->borrowernumber, $patron->userid,
994
        $patron->cardnumber,     'firstname',
995
        'surname',               $patron->library->branchcode,
996
        'Midway Public Library', $patron->flags,
997
        '',                      ''
998
    );
999
}
1000
889
1;
1001
1;
890
- 

Return to bug 18403