From c38709114635d1762324badd9fc4098b5cc6039c 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 --- t/db_dependent/Koha/Patrons.t | 79 ++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Patrons.t b/t/db_dependent/Koha/Patrons.t index baaa07511c..2b1065b6cd 100644 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 33; +use Test::More tests => 34; use Test::Warn; use Test::Exception; use Test::MockModule; @@ -1574,3 +1574,80 @@ subtest '->set_password' => sub { $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" ); + }; + + $schema->storage->txn_rollback; +}; -- 2.20.1 (Apple Git-117)