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

(-)a/t/db_dependent/Koha/Patrons.t (-2 / +78 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 40;
22
use Test::More tests => 41;
23
use Test::Warn;
23
use Test::Warn;
24
use Test::Exception;
24
use Test::Exception;
25
use Test::MockModule;
25
use Test::MockModule;
Lines 1970-1972 subtest 'anonymize' => sub { Link Here
1970
    is( $patron2->firstname, undef, 'First name patron2 cleared' );
1970
    is( $patron2->firstname, undef, 'First name patron2 cleared' );
1971
};
1971
};
1972
$schema->storage->txn_rollback;
1972
$schema->storage->txn_rollback;
1973
- 
1973
1974
subtest 'libraries_where_can_edit_items + can_edit_item' => sub {
1975
    plan tests => 2;
1976
1977
    $schema->storage->txn_begin;
1978
    my $dbh = $schema->storage->dbh;
1979
1980
    $dbh->do("DELETE FROM library_groups");
1981
1982
    # group1
1983
    #   library_1A
1984
    #   library_1B
1985
    # group2
1986
    #   library_2A
1987
    my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1', ft_limit_item_editing => 1 } )->store;
1988
    my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2', ft_limit_item_editing => 1 } )->store;
1989
    my $library_1A = $builder->build( { source => 'Branch' } );
1990
    my $library_1B = $builder->build( { source => 'Branch' } );
1991
    my $library_2A = $builder->build( { source => 'Branch' } );
1992
    $library_1A = Koha::Libraries->find( $library_1A->{branchcode} );
1993
    $library_1B = Koha::Libraries->find( $library_1B->{branchcode} );
1994
    $library_2A = Koha::Libraries->find( $library_2A->{branchcode} );
1995
    Koha::Library::Group->new( { branchcode => $library_1A->branchcode, parent_id => $group_1->id } )->store;
1996
    Koha::Library::Group->new( { branchcode => $library_1B->branchcode, parent_id => $group_1->id } )->store;
1997
    Koha::Library::Group->new( { branchcode => $library_2A->branchcode, parent_id => $group_2->id } )->store;
1998
1999
    my $sth = C4::Context->dbh->prepare(q|INSERT INTO user_permissions( borrowernumber, module_bit, code ) VALUES (?, 9, ?)|); # 9 for editcatalogue
2000
    # 2 patrons from library_1A (group1)
2001
    # patron_1A_1 see patron's infos from outside its group
2002
    # Setting flags => undef to not be considered as superlibrarian
2003
    my $patron_1A_1 = $builder->build({ source => 'Borrower', value => { branchcode => $library_1A->branchcode, flags => undef, }});
2004
    $patron_1A_1 = Koha::Patrons->find( $patron_1A_1->{borrowernumber} );
2005
    $sth->execute( $patron_1A_1->borrowernumber, 'edit_items' );
2006
    $sth->execute( $patron_1A_1->borrowernumber, 'edit_any_item' );
2007
    # patron_1A_2 can only see patron's info from its group
2008
    my $patron_1A_2 = $builder->build({ source => 'Borrower', value => { branchcode => $library_1A->branchcode, flags => undef, }});
2009
    $patron_1A_2 = Koha::Patrons->find( $patron_1A_2->{borrowernumber} );
2010
    $sth->execute( $patron_1A_2->borrowernumber, 'edit_items' );
2011
    # 1 patron from library_1B (group1)
2012
    my $patron_1B = $builder->build({ source => 'Borrower', value => { branchcode => $library_1B->branchcode, flags => undef, }});
2013
    $patron_1B = Koha::Patrons->find( $patron_1B->{borrowernumber} );
2014
    # 1 patron from library_2A (group2) can only see patron's info from its group
2015
    my $patron_2A = $builder->build({ source => 'Borrower', value => { branchcode => $library_2A->branchcode, flags => undef, }});
2016
    $patron_2A = Koha::Patrons->find( $patron_2A->{borrowernumber} );
2017
    $sth->execute( $patron_2A->borrowernumber, 'edit_items' );
2018
2019
    subtest 'libraries_where_can_edit_items' => sub {
2020
        plan tests => 3;
2021
2022
        my @branchcodes;
2023
2024
        @branchcodes = $patron_1A_1->libraries_where_can_edit_items;
2025
        is_deeply( \@branchcodes, [], "patron_1A_1 has edit_any_item => No restrictions" );
2026
2027
        @branchcodes = $patron_1A_2->libraries_where_can_edit_items;
2028
        is_deeply( \@branchcodes, [ sort ( $library_1A->branchcode, $library_1B->branchcode ) ], "patron_1A_2 doesn't have edit_any_item => Can only edit items from its group" );
2029
2030
        @branchcodes = $patron_2A->libraries_where_can_edit_items;
2031
        is_deeply( \@branchcodes, [$library_2A->branchcode], "patron_2A doesn't have edit_any_item => Can only see patron's from its group" );
2032
    };
2033
2034
    subtest 'can_edit_item' => sub {
2035
        plan tests => 6;
2036
2037
        t::lib::Mocks::mock_userenv({ patron => $patron_1A_1 });
2038
        is( $patron_1A_1->can_edit_item( $library_1A->id ), 1, "patron_1A_1 can see patron_1A_2, from its library" );
2039
        is( $patron_1A_1->can_edit_item( $library_1B->id ),   1, "patron_1A_1 can see patron_1B, from its group" );
2040
        is( $patron_1A_1->can_edit_item( $library_2A->id ),   1, "patron_1A_1 can see patron_1A_2, from another group" );
2041
2042
        t::lib::Mocks::mock_userenv({ patron => $patron_1A_2 });
2043
        is( $patron_1A_2->can_edit_item( $library_1A->id ),   1, "patron_1A_2 can see patron_1A_1, from its library" );
2044
        is( $patron_1A_2->can_edit_item( $library_1B->id ),   1, "patron_1A_2 can see patron_1B, from its group" );
2045
        is( $patron_1A_2->can_edit_item( $library_2A->id ),   0, "patron_1A_2 can NOT see patron_2A, from another group" );
2046
    };
2047
2048
    $schema->storage->txn_rollback;
2049
};

Return to bug 20256