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 => 33;
22
use Test::More tests => 34;
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 1574-1576 subtest '->set_password' => sub { Link Here
1574
1574
1575
    $schema->storage->txn_rollback;
1575
    $schema->storage->txn_rollback;
1576
};
1576
};
1577
- 
1577
1578
subtest 'libraries_where_can_edit_items + can_edit_item' => sub {
1579
    plan tests => 2;
1580
1581
    $schema->storage->txn_begin;
1582
    my $dbh = $schema->storage->dbh;
1583
1584
    $dbh->do("DELETE FROM library_groups");
1585
1586
    # group1
1587
    #   library_1A
1588
    #   library_1B
1589
    # group2
1590
    #   library_2A
1591
    my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1', ft_limit_item_editing => 1 } )->store;
1592
    my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2', ft_limit_item_editing => 1 } )->store;
1593
    my $library_1A = $builder->build( { source => 'Branch' } );
1594
    my $library_1B = $builder->build( { source => 'Branch' } );
1595
    my $library_2A = $builder->build( { source => 'Branch' } );
1596
    $library_1A = Koha::Libraries->find( $library_1A->{branchcode} );
1597
    $library_1B = Koha::Libraries->find( $library_1B->{branchcode} );
1598
    $library_2A = Koha::Libraries->find( $library_2A->{branchcode} );
1599
    Koha::Library::Group->new( { branchcode => $library_1A->branchcode, parent_id => $group_1->id } )->store;
1600
    Koha::Library::Group->new( { branchcode => $library_1B->branchcode, parent_id => $group_1->id } )->store;
1601
    Koha::Library::Group->new( { branchcode => $library_2A->branchcode, parent_id => $group_2->id } )->store;
1602
1603
    my $sth = C4::Context->dbh->prepare(q|INSERT INTO user_permissions( borrowernumber, module_bit, code ) VALUES (?, 9, ?)|); # 9 for editcatalogue
1604
    # 2 patrons from library_1A (group1)
1605
    # patron_1A_1 see patron's infos from outside its group
1606
    # Setting flags => undef to not be considered as superlibrarian
1607
    my $patron_1A_1 = $builder->build({ source => 'Borrower', value => { branchcode => $library_1A->branchcode, flags => undef, }});
1608
    $patron_1A_1 = Koha::Patrons->find( $patron_1A_1->{borrowernumber} );
1609
    $sth->execute( $patron_1A_1->borrowernumber, 'edit_items' );
1610
    $sth->execute( $patron_1A_1->borrowernumber, 'edit_any_item' );
1611
    # patron_1A_2 can only see patron's info from its group
1612
    my $patron_1A_2 = $builder->build({ source => 'Borrower', value => { branchcode => $library_1A->branchcode, flags => undef, }});
1613
    $patron_1A_2 = Koha::Patrons->find( $patron_1A_2->{borrowernumber} );
1614
    $sth->execute( $patron_1A_2->borrowernumber, 'edit_items' );
1615
    # 1 patron from library_1B (group1)
1616
    my $patron_1B = $builder->build({ source => 'Borrower', value => { branchcode => $library_1B->branchcode, flags => undef, }});
1617
    $patron_1B = Koha::Patrons->find( $patron_1B->{borrowernumber} );
1618
    # 1 patron from library_2A (group2) can only see patron's info from its group
1619
    my $patron_2A = $builder->build({ source => 'Borrower', value => { branchcode => $library_2A->branchcode, flags => undef, }});
1620
    $patron_2A = Koha::Patrons->find( $patron_2A->{borrowernumber} );
1621
    $sth->execute( $patron_2A->borrowernumber, 'edit_items' );
1622
1623
    subtest 'libraries_where_can_edit_items' => sub {
1624
        plan tests => 3;
1625
1626
        my @branchcodes;
1627
1628
        @branchcodes = $patron_1A_1->libraries_where_can_edit_items;
1629
        is_deeply( \@branchcodes, [], "patron_1A_1 has edit_any_item => No restrictions" );
1630
1631
        @branchcodes = $patron_1A_2->libraries_where_can_edit_items;
1632
        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" );
1633
1634
        @branchcodes = $patron_2A->libraries_where_can_edit_items;
1635
        is_deeply( \@branchcodes, [$library_2A->branchcode], "patron_2A doesn't have edit_any_item => Can only see patron's from its group" );
1636
    };
1637
1638
    subtest 'can_edit_item' => sub {
1639
        plan tests => 6;
1640
1641
        t::lib::Mocks::mock_userenv({ patron => $patron_1A_1 });
1642
        is( $patron_1A_1->can_edit_item( $library_1A->id ), 1, "patron_1A_1 can see patron_1A_2, from its library" );
1643
        is( $patron_1A_1->can_edit_item( $library_1B->id ),   1, "patron_1A_1 can see patron_1B, from its group" );
1644
        is( $patron_1A_1->can_edit_item( $library_2A->id ),   1, "patron_1A_1 can see patron_1A_2, from another group" );
1645
1646
        t::lib::Mocks::mock_userenv({ patron => $patron_1A_2 });
1647
        is( $patron_1A_2->can_edit_item( $library_1A->id ),   1, "patron_1A_2 can see patron_1A_1, from its library" );
1648
        is( $patron_1A_2->can_edit_item( $library_1B->id ),   1, "patron_1A_2 can see patron_1B, from its group" );
1649
        is( $patron_1A_2->can_edit_item( $library_2A->id ),   0, "patron_1A_2 can NOT see patron_2A, from another group" );
1650
    };
1651
1652
    $schema->storage->txn_rollback;
1653
};

Return to bug 20256