From 408eaa42d9a0479b0f65395c162c5bf2e64653f1 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Fri, 29 Mar 2019 14:58:00 -0400 Subject: [PATCH] Bug 20256: Add unit tests Signed-off-by: Bob Bennhoff - CLiC --- t/db_dependent/Koha/Patrons.t | 75 ++++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Patrons.t b/t/db_dependent/Koha/Patrons.t index 4f434adc3c..ac2a518584 100644 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -2201,6 +2201,79 @@ subtest 'extended_attributes' => sub { ## Do we need a filtered? #$limited_value = $patron_1->get_extended_attribute( $attribute_type_limited->code ); #is( $limited_value, undef, ); +}; - $schema->storage->txn_rollback; +subtest 'libraries_where_can_edit_items + can_edit_item' => sub { + plan tests => 2; + + $schema->storage->txn_begin; + my $dbh = $schema->storage->dbh; + + $dbh->do("DELETE FROM library_groups"); + + # group1 + # library_1A + # library_1B + # group2 + # library_2A + my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1', ft_limit_item_editing => 1 } )->store; + my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2', ft_limit_item_editing => 1 } )->store; + my $library_1A = $builder->build( { source => 'Branch' } ); + my $library_1B = $builder->build( { source => 'Branch' } ); + my $library_2A = $builder->build( { source => 'Branch' } ); + $library_1A = Koha::Libraries->find( $library_1A->{branchcode} ); + $library_1B = Koha::Libraries->find( $library_1B->{branchcode} ); + $library_2A = Koha::Libraries->find( $library_2A->{branchcode} ); + Koha::Library::Group->new( { branchcode => $library_1A->branchcode, parent_id => $group_1->id } )->store; + Koha::Library::Group->new( { branchcode => $library_1B->branchcode, parent_id => $group_1->id } )->store; + Koha::Library::Group->new( { branchcode => $library_2A->branchcode, parent_id => $group_2->id } )->store; + + my $sth = C4::Context->dbh->prepare(q|INSERT INTO user_permissions( borrowernumber, module_bit, code ) VALUES (?, 9, ?)|); # 9 for editcatalogue + # 2 patrons from library_1A (group1) + # patron_1A_1 see patron's infos from outside its group + # Setting flags => undef to not be considered as superlibrarian + my $patron_1A_1 = $builder->build({ source => 'Borrower', value => { branchcode => $library_1A->branchcode, flags => undef, }}); + $patron_1A_1 = Koha::Patrons->find( $patron_1A_1->{borrowernumber} ); + $sth->execute( $patron_1A_1->borrowernumber, 'edit_items' ); + $sth->execute( $patron_1A_1->borrowernumber, 'edit_any_item' ); + # patron_1A_2 can only see patron's info from its group + my $patron_1A_2 = $builder->build({ source => 'Borrower', value => { branchcode => $library_1A->branchcode, flags => undef, }}); + $patron_1A_2 = Koha::Patrons->find( $patron_1A_2->{borrowernumber} ); + $sth->execute( $patron_1A_2->borrowernumber, 'edit_items' ); + # 1 patron from library_1B (group1) + my $patron_1B = $builder->build({ source => 'Borrower', value => { branchcode => $library_1B->branchcode, flags => undef, }}); + $patron_1B = Koha::Patrons->find( $patron_1B->{borrowernumber} ); + # 1 patron from library_2A (group2) can only see patron's info from its group + my $patron_2A = $builder->build({ source => 'Borrower', value => { branchcode => $library_2A->branchcode, flags => undef, }}); + $patron_2A = Koha::Patrons->find( $patron_2A->{borrowernumber} ); + $sth->execute( $patron_2A->borrowernumber, 'edit_items' ); + + subtest 'libraries_where_can_edit_items' => sub { + plan tests => 3; + + my @branchcodes; + + @branchcodes = $patron_1A_1->libraries_where_can_edit_items; + is_deeply( \@branchcodes, [], "patron_1A_1 has edit_any_item => No restrictions" ); + + @branchcodes = $patron_1A_2->libraries_where_can_edit_items; + 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" ); + + @branchcodes = $patron_2A->libraries_where_can_edit_items; + is_deeply( \@branchcodes, [$library_2A->branchcode], "patron_2A doesn't have edit_any_item => Can only see patron's from its group" ); + }; + + subtest 'can_edit_item' => sub { + plan tests => 6; + + t::lib::Mocks::mock_userenv({ patron => $patron_1A_1 }); + is( $patron_1A_1->can_edit_item( $library_1A->id ), 1, "patron_1A_1 can see patron_1A_2, from its library" ); + is( $patron_1A_1->can_edit_item( $library_1B->id ), 1, "patron_1A_1 can see patron_1B, from its group" ); + is( $patron_1A_1->can_edit_item( $library_2A->id ), 1, "patron_1A_1 can see patron_1A_2, from another group" ); + + t::lib::Mocks::mock_userenv({ patron => $patron_1A_2 }); + is( $patron_1A_2->can_edit_item( $library_1A->id ), 1, "patron_1A_2 can see patron_1A_1, from its library" ); + is( $patron_1A_2->can_edit_item( $library_1B->id ), 1, "patron_1A_2 can see patron_1B, from its group" ); + is( $patron_1A_2->can_edit_item( $library_2A->id ), 0, "patron_1A_2 can NOT see patron_2A, from another group" ); + }; }; -- 2.24.1 (Apple Git-126)