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

(-)a/t/db_dependent/Koha/Patrons.t (-2 / +76 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 42;
22
use Test::More tests => 43;
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 2072-2075 subtest 'queue_notice' => sub { Link Here
2072
    is( Koha::Notice::Messages->search({borrowernumber => $patron->borrowernumber })->count, $counter,"Count of queued notices not increased in test mode");
2072
    is( Koha::Notice::Messages->search({borrowernumber => $patron->borrowernumber })->count, $counter,"Count of queued notices not increased in test mode");
2073
};
2073
};
2074
2074
2075
subtest 'libraries_where_can_edit_items + can_edit_item' => sub {
2076
    plan tests => 2;
2077
2078
    $schema->storage->txn_begin;
2079
    my $dbh = $schema->storage->dbh;
2080
2081
    $dbh->do("DELETE FROM library_groups");
2082
2083
    # group1
2084
    #   library_1A
2085
    #   library_1B
2086
    # group2
2087
    #   library_2A
2088
    my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1', ft_limit_item_editing => 1 } )->store;
2089
    my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2', ft_limit_item_editing => 1 } )->store;
2090
    my $library_1A = $builder->build( { source => 'Branch' } );
2091
    my $library_1B = $builder->build( { source => 'Branch' } );
2092
    my $library_2A = $builder->build( { source => 'Branch' } );
2093
    $library_1A = Koha::Libraries->find( $library_1A->{branchcode} );
2094
    $library_1B = Koha::Libraries->find( $library_1B->{branchcode} );
2095
    $library_2A = Koha::Libraries->find( $library_2A->{branchcode} );
2096
    Koha::Library::Group->new( { branchcode => $library_1A->branchcode, parent_id => $group_1->id } )->store;
2097
    Koha::Library::Group->new( { branchcode => $library_1B->branchcode, parent_id => $group_1->id } )->store;
2098
    Koha::Library::Group->new( { branchcode => $library_2A->branchcode, parent_id => $group_2->id } )->store;
2099
2100
    my $sth = C4::Context->dbh->prepare(q|INSERT INTO user_permissions( borrowernumber, module_bit, code ) VALUES (?, 9, ?)|); # 9 for editcatalogue
2101
    # 2 patrons from library_1A (group1)
2102
    # patron_1A_1 see patron's infos from outside its group
2103
    # Setting flags => undef to not be considered as superlibrarian
2104
    my $patron_1A_1 = $builder->build({ source => 'Borrower', value => { branchcode => $library_1A->branchcode, flags => undef, }});
2105
    $patron_1A_1 = Koha::Patrons->find( $patron_1A_1->{borrowernumber} );
2106
    $sth->execute( $patron_1A_1->borrowernumber, 'edit_items' );
2107
    $sth->execute( $patron_1A_1->borrowernumber, 'edit_any_item' );
2108
    # patron_1A_2 can only see patron's info from its group
2109
    my $patron_1A_2 = $builder->build({ source => 'Borrower', value => { branchcode => $library_1A->branchcode, flags => undef, }});
2110
    $patron_1A_2 = Koha::Patrons->find( $patron_1A_2->{borrowernumber} );
2111
    $sth->execute( $patron_1A_2->borrowernumber, 'edit_items' );
2112
    # 1 patron from library_1B (group1)
2113
    my $patron_1B = $builder->build({ source => 'Borrower', value => { branchcode => $library_1B->branchcode, flags => undef, }});
2114
    $patron_1B = Koha::Patrons->find( $patron_1B->{borrowernumber} );
2115
    # 1 patron from library_2A (group2) can only see patron's info from its group
2116
    my $patron_2A = $builder->build({ source => 'Borrower', value => { branchcode => $library_2A->branchcode, flags => undef, }});
2117
    $patron_2A = Koha::Patrons->find( $patron_2A->{borrowernumber} );
2118
    $sth->execute( $patron_2A->borrowernumber, 'edit_items' );
2119
2120
    subtest 'libraries_where_can_edit_items' => sub {
2121
        plan tests => 3;
2122
2123
        my @branchcodes;
2124
2125
        @branchcodes = $patron_1A_1->libraries_where_can_edit_items;
2126
        is_deeply( \@branchcodes, [], "patron_1A_1 has edit_any_item => No restrictions" );
2127
2128
        @branchcodes = $patron_1A_2->libraries_where_can_edit_items;
2129
        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" );
2130
2131
        @branchcodes = $patron_2A->libraries_where_can_edit_items;
2132
        is_deeply( \@branchcodes, [$library_2A->branchcode], "patron_2A doesn't have edit_any_item => Can only see patron's from its group" );
2133
    };
2134
2135
    subtest 'can_edit_item' => sub {
2136
        plan tests => 6;
2137
2138
        t::lib::Mocks::mock_userenv({ patron => $patron_1A_1 });
2139
        is( $patron_1A_1->can_edit_item( $library_1A->id ), 1, "patron_1A_1 can see patron_1A_2, from its library" );
2140
        is( $patron_1A_1->can_edit_item( $library_1B->id ),   1, "patron_1A_1 can see patron_1B, from its group" );
2141
        is( $patron_1A_1->can_edit_item( $library_2A->id ),   1, "patron_1A_1 can see patron_1A_2, from another group" );
2142
2143
        t::lib::Mocks::mock_userenv({ patron => $patron_1A_2 });
2144
        is( $patron_1A_2->can_edit_item( $library_1A->id ),   1, "patron_1A_2 can see patron_1A_1, from its library" );
2145
        is( $patron_1A_2->can_edit_item( $library_1B->id ),   1, "patron_1A_2 can see patron_1B, from its group" );
2146
        is( $patron_1A_2->can_edit_item( $library_2A->id ),   0, "patron_1A_2 can NOT see patron_2A, from another group" );
2147
    };
2148
};
2149
2075
$schema->storage->txn_rollback;
2150
$schema->storage->txn_rollback;
2076
- 

Return to bug 20256