diff --git a/Koha/Checkout.pm b/Koha/Checkout.pm index a63ec71ab8..5656c32bf4 100644 --- a/Koha/Checkout.pm +++ b/Koha/Checkout.pm @@ -222,6 +222,21 @@ sub to_api_mapping { }; } +=head3 public_read_list + +This method returns the list of publicly readable database fields for both API and UI output purposes + +=cut + +sub public_read_list { + return [ + 'checkin_library', 'issue_id', 'borrowernumber', + 'itemnumber', 'date_due', 'branchcode', + 'returndate', 'lastreneweddate', 'issuedate', + 'notedate', 'noteseen' + ]; +} + =head3 claim_returned my $return_claim = $checkout->claim_returned(); diff --git a/t/db_dependent/Koha/Checkout.t b/t/db_dependent/Koha/Checkout.t index 7459cbcbf8..9dc8339744 100755 --- a/t/db_dependent/Koha/Checkout.t +++ b/t/db_dependent/Koha/Checkout.t @@ -77,3 +77,53 @@ subtest 'renewals() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'public_read_list() tests' => sub { + + $schema->storage->txn_begin; + + my @all_attrs = Koha::Checkouts->columns(); + my $public_attrs = + { map { $_ => 1 } @{ Koha::Checkout->public_read_list() } }; + my $mapping = Koha::Checkout->to_api_mapping; + + plan tests => scalar @all_attrs * 2; + + # Create a sample checkout + my $checkout = $builder->build_object( { class => 'Koha::Checkouts' } ); + + my $unprivileged_representation = $checkout->to_api( { public => 1 } ); + my $privileged_representation = $checkout->to_api; + + foreach my $attr (@all_attrs) { + my $mapped = exists $mapping->{$attr} ? $mapping->{$attr} : $attr; + if ( defined($mapped) ) { + ok( + exists $privileged_representation->{$mapped}, + "Attribute '$attr' is present when privileged" + ); + if ( exists $public_attrs->{$attr} ) { + ok( + exists $unprivileged_representation->{$mapped}, + "Attribute '$attr' is present when public" + ); + } else { + ok( + !exists $unprivileged_representation->{$mapped}, + "Attribute '$attr' is not present when public" + ); + } + } else { + ok( + !exists $privileged_representation->{$attr}, + "Unmapped attribute '$attr' is not present when privileged" + ); + ok( + !exists $unprivileged_representation->{$attr}, + "Unmapped attribute '$attr' is not present when public" + ); + } + } + + $schema->storage->txn_rollback; +};