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

(-)a/t/db_dependent/Koha/Patrons.t (-2 / +75 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 44;
22
use Test::More tests => 45;
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 2458-2464 subtest 'filter_by_amount_owed' => sub { Link Here
2458
    is( $filtered->_resultset->as_subselect_rs->count, 1,
2458
    is( $filtered->_resultset->as_subselect_rs->count, 1,
2459
"filter_by_amount_owed({ more_than => 6.00, library => $library2->{branchcode} }) found one patron"
2459
"filter_by_amount_owed({ more_than => 6.00, library => $library2->{branchcode} }) found one patron"
2460
    );
2460
    );
2461
};
2462
2463
subtest 'libraries_where_can_edit_items + can_edit_item' => sub {
2464
    plan tests => 2;
2465
2466
    $schema->storage->txn_begin;
2467
    my $dbh = $schema->storage->dbh;
2468
2469
    $dbh->do("DELETE FROM library_groups");
2470
2471
    # group1
2472
    #   library_1A
2473
    #   library_1B
2474
    # group2
2475
    #   library_2A
2476
    my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1', ft_limit_item_editing => 1 } )->store;
2477
    my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2', ft_limit_item_editing => 1 } )->store;
2478
    my $library_1A = $builder->build( { source => 'Branch' } );
2479
    my $library_1B = $builder->build( { source => 'Branch' } );
2480
    my $library_2A = $builder->build( { source => 'Branch' } );
2481
    $library_1A = Koha::Libraries->find( $library_1A->{branchcode} );
2482
    $library_1B = Koha::Libraries->find( $library_1B->{branchcode} );
2483
    $library_2A = Koha::Libraries->find( $library_2A->{branchcode} );
2484
    Koha::Library::Group->new( { branchcode => $library_1A->branchcode, parent_id => $group_1->id } )->store;
2485
    Koha::Library::Group->new( { branchcode => $library_1B->branchcode, parent_id => $group_1->id } )->store;
2486
    Koha::Library::Group->new( { branchcode => $library_2A->branchcode, parent_id => $group_2->id } )->store;
2487
2488
    my $sth = C4::Context->dbh->prepare(q|INSERT INTO user_permissions( borrowernumber, module_bit, code ) VALUES (?, 9, ?)|); # 9 for editcatalogue
2489
    # 2 patrons from library_1A (group1)
2490
    # patron_1A_1 see patron's infos from outside its group
2491
    # Setting flags => undef to not be considered as superlibrarian
2492
    my $patron_1A_1 = $builder->build({ source => 'Borrower', value => { branchcode => $library_1A->branchcode, flags => undef, }});
2493
    $patron_1A_1 = Koha::Patrons->find( $patron_1A_1->{borrowernumber} );
2494
    $sth->execute( $patron_1A_1->borrowernumber, 'edit_items' );
2495
    $sth->execute( $patron_1A_1->borrowernumber, 'edit_any_item' );
2496
    # patron_1A_2 can only see patron's info from its group
2497
    my $patron_1A_2 = $builder->build({ source => 'Borrower', value => { branchcode => $library_1A->branchcode, flags => undef, }});
2498
    $patron_1A_2 = Koha::Patrons->find( $patron_1A_2->{borrowernumber} );
2499
    $sth->execute( $patron_1A_2->borrowernumber, 'edit_items' );
2500
    # 1 patron from library_1B (group1)
2501
    my $patron_1B = $builder->build({ source => 'Borrower', value => { branchcode => $library_1B->branchcode, flags => undef, }});
2502
    $patron_1B = Koha::Patrons->find( $patron_1B->{borrowernumber} );
2503
    # 1 patron from library_2A (group2) can only see patron's info from its group
2504
    my $patron_2A = $builder->build({ source => 'Borrower', value => { branchcode => $library_2A->branchcode, flags => undef, }});
2505
    $patron_2A = Koha::Patrons->find( $patron_2A->{borrowernumber} );
2506
    $sth->execute( $patron_2A->borrowernumber, 'edit_items' );
2507
2508
    subtest 'libraries_where_can_edit_items' => sub {
2509
        plan tests => 3;
2461
2510
2511
        my @branchcodes;
2512
2513
        @branchcodes = $patron_1A_1->libraries_where_can_edit_items;
2514
        is_deeply( \@branchcodes, [], "patron_1A_1 has edit_any_item => No restrictions" );
2515
2516
        @branchcodes = $patron_1A_2->libraries_where_can_edit_items;
2517
        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" );
2518
2519
        @branchcodes = $patron_2A->libraries_where_can_edit_items;
2520
        is_deeply( \@branchcodes, [$library_2A->branchcode], "patron_2A doesn't have edit_any_item => Can only see patron's from its group" );
2521
    };
2522
2523
    subtest 'can_edit_item' => sub {
2524
        plan tests => 6;
2525
2526
        t::lib::Mocks::mock_userenv({ patron => $patron_1A_1 });
2527
        is( $patron_1A_1->can_edit_item( $library_1A->id ), 1, "patron_1A_1 can see patron_1A_2, from its library" );
2528
        is( $patron_1A_1->can_edit_item( $library_1B->id ),   1, "patron_1A_1 can see patron_1B, from its group" );
2529
        is( $patron_1A_1->can_edit_item( $library_2A->id ),   1, "patron_1A_1 can see patron_1A_2, from another group" );
2530
2531
        t::lib::Mocks::mock_userenv({ patron => $patron_1A_2 });
2532
        is( $patron_1A_2->can_edit_item( $library_1A->id ),   1, "patron_1A_2 can see patron_1A_1, from its library" );
2533
        is( $patron_1A_2->can_edit_item( $library_1B->id ),   1, "patron_1A_2 can see patron_1B, from its group" );
2534
        is( $patron_1A_2->can_edit_item( $library_2A->id ),   0, "patron_1A_2 can NOT see patron_2A, from another group" );
2535
    };
2462
};
2536
};
2463
2537
2464
subtest 'filter_by_have_permission' => sub {
2538
subtest 'filter_by_have_permission' => sub {
2465
- 

Return to bug 20256