Koha/t/db_dependent/Members_Attributes.t
Jonathan Druart 4a3404594f Bug 15163: Do not erase patron attributes if limited to another library
The patron attributes displayed on editing a patron are not displayed if
limited to another library.

C4::Members::Attributes::SetBorrowerAttributes will now only delete attributes
the librarian is editing.
SetBorrowerAttributes takes a new $no_branch_limit parameter. If set,
the branch limitations have not effect and all attributes are deleted
(same behavior as before this patch).

Test plan:
1/ Create 2 patron attributes, without branch limitations.
2/ Edit a patron and set a value for these attributes
3/ Limit a patron attributes to a library (one you are not logged in
with).
4/ Edit again the patron.
=> You should not see the limited attributes
5/ Edit the patron attributes and remove the branch limitation
=> Without this patch, it has been removed from the database and is not
displayed anymore.
=> With this patch, you should see it.

Signed-off-by: Jesse Weaver <jweaver@bywatersolutions.com>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

Signed-off-by: Brendan Gallagher brendan@bywatersolutions.com
2016-03-21 16:56:37 +00:00

233 lines
13 KiB
Perl
Executable file

#!/usr/bin/perl
# This file is part of Koha.
#
# Copyright 2014 Biblibre SARL
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use C4::Context;
use C4::Members;
use C4::Members::AttributeTypes;
use Koha::Database;
use t::lib::TestBuilder;
use Test::More tests => 57;
use t::lib::TestBuilder;
use_ok('C4::Members::Attributes');
my $schema = Koha::Database->schema;
$schema->storage->txn_begin;
my $builder = t::lib::TestBuilder->new;
my $dbh = C4::Context->dbh;
$dbh->{RaiseError} = 1;
$dbh->do(q|DELETE FROM issues|);
$dbh->do(q|DELETE FROM borrowers|);
$dbh->do(q|DELETE FROM borrower_attributes|);
$dbh->do(q|DELETE FROM borrower_attribute_types|);
my $library = $builder->build({
source => 'Branch',
});
my $patron = $builder->build(
{ source => 'Borrower',
value => {
firstname => 'my firstname',
surname => 'my surname',
categorycode => 'S',
branchcode => $library->{branchcode},
}
}
);
C4::Context->_new_userenv('DUMMY SESSION');
C4::Context->set_userenv(123, 'userid', 'usercnum', 'First name', 'Surname', $library->{branchcode}, 'My Library', 0);
my $borrowernumber = $patron->{borrowernumber};
my $attribute_type1 = C4::Members::AttributeTypes->new('my code1', 'my description1');
$attribute_type1->unique_id(1);
my $attribute_types = C4::Members::Attributes::GetAttributes();
is( @$attribute_types, 0, 'GetAttributes returns the correct number of attribute types' );
$attribute_type1->store();
$attribute_types = C4::Members::Attributes::GetAttributes();
is( @$attribute_types, 1, 'GetAttributes returns the correct number of attribute types' );
is( $attribute_types->[0], $attribute_type1->code(), 'GetAttributes returns the correct value for code' );
$attribute_types = C4::Members::Attributes::GetAttributes(1);
is( @$attribute_types, 0, 'GetAttributes returns the correct number of attribute types with the filter opac_only' );
my $attribute_type2 = C4::Members::AttributeTypes->new('my code2', 'my description2');
$attribute_type2->opac_display(1);
$attribute_type2->staff_searchable(1);
$attribute_type2->store();
$attribute_types = C4::Members::Attributes::GetAttributes();
is( @$attribute_types, 2, 'GetAttributes returns the correct number of attribute types' );
is( $attribute_types->[1], $attribute_type2->code(), 'GetAttributes returns the correct value for code' );
$attribute_types = C4::Members::Attributes::GetAttributes(1);
is( @$attribute_types, 1, 'GetAttributes returns the correct number of attribute types with the filter opac_only' );
my $new_library = $builder->build( { source => 'Branch' } );
my $attribute_type_limited = C4::Members::AttributeTypes->new('my code3', 'my description3');
$attribute_type_limited->branches([ $new_library->{branchcode} ]);
$attribute_type_limited->store;
my $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
is( @$borrower_attributes, 0, 'GetBorrowerAttributes without the borrower number returns an empty array' );
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
is( @$borrower_attributes, 0, 'GetBorrowerAttributes returns the correct number of borrower attributes' );
my $attributes = [
{
value => 'my attribute1',
code => $attribute_type1->code(),
password => 'my password1',
},
{
value => 'my attribute2',
code => $attribute_type2->code(),
password => 'my password2',
},
{
value => 'my attribute limited',
code => $attribute_type_limited->code(),
}
];
my $set_borrower_attributes = C4::Members::Attributes::SetBorrowerAttributes();
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
is( @$borrower_attributes, 0, 'SetBorrowerAttributes without arguments does not add borrower attributes' );
$set_borrower_attributes = C4::Members::Attributes::SetBorrowerAttributes($borrowernumber);
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
is( @$borrower_attributes, 0, 'SetBorrowerAttributes without the attributes does not add borrower attributes' );
$set_borrower_attributes = C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $attributes);
is( $set_borrower_attributes, 1, 'SetBorrowerAttributes returns the success code' );
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes();
is( @$borrower_attributes, 0, 'GetBorrowerAttributes without the borrower number returns an empty array' );
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
is( @$borrower_attributes, 3, 'GetBorrowerAttributes returns the correct number of borrower attributes' );
is( $borrower_attributes->[0]->{code}, $attributes->[0]->{code}, 'SetBorrowerAttributes stores the correct code correctly' );
is( $borrower_attributes->[0]->{description}, $attribute_type1->description(), 'SetBorrowerAttributes stores the field description correctly' );
is( $borrower_attributes->[0]->{value}, $attributes->[0]->{value}, 'SetBorrowerAttributes stores the field value correctly' );
is( $borrower_attributes->[0]->{password}, $attributes->[0]->{password}, 'SetBorrowerAttributes stores the field password correctly' );
is( $borrower_attributes->[1]->{code}, $attributes->[1]->{code}, 'SetBorrowerAttributes stores the field code correctly' );
is( $borrower_attributes->[1]->{description}, $attribute_type2->description(), 'SetBorrowerAttributes stores the field description correctly' );
is( $borrower_attributes->[1]->{value}, $attributes->[1]->{value}, 'SetBorrowerAttributes stores the field value correctly' );
is( $borrower_attributes->[1]->{password}, $attributes->[1]->{password}, 'SetBorrowerAttributes stores the field password correctly' );
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
is( @$borrower_attributes, 3, 'GetBorrowerAttributes returns the correct number of borrower attributes' );
$attributes = [
{
value => 'my attribute1',
code => $attribute_type1->code(),
password => 'my password1',
},
{
value => 'my attribute2',
code => $attribute_type2->code(),
password => 'my password2',
}
];
C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $attributes);
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
is( @$borrower_attributes, 3, 'SetBorrowerAttributes should not have removed the attributes limited to another branch' );
# TODO This is not implemented yet
#$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber, undef, 'branch_limited');
#is( @$borrower_attributes, 2, 'GetBorrowerAttributes returns the correct number of borrower attributes filtered on library' );
my $attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue();
is( $attribute_value, undef, 'GetBorrowerAttributeValue without arguments returns undef' );
$attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber);
is( $attribute_value, undef, 'GetBorrowerAttributeValue without the attribute code returns undef' );
$attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue(undef, $attributes->[0]->{code});
is( $attribute_value, undef, 'GetBorrowerAttributeValue with a undef borrower number returns undef' );
$attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, 'my invalid code');
is( $attribute_value, undef, 'GetBorrowerAttributeValue with an invalid code retuns undef' );
$attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attributes->[0]->{code});
is( $attribute_value, $attributes->[0]->{value}, 'GetBorrowerAttributeValue returns the correct attribute value' );
$attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attributes->[1]->{code});
is( $attribute_value, $attributes->[1]->{value}, 'GetBorrowerAttributeValue returns the correct attribute value' );
my $attribute = {
attribute => 'my attribute3',
code => $attribute_type1->code(),
password => 'my password3',
};
C4::Members::Attributes::UpdateBorrowerAttribute($borrowernumber, $attribute);
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
is( @$borrower_attributes, 3, 'UpdateBorrowerAttribute does not change the number of borrower attributes' );
is( $borrower_attributes->[0]->{code}, $attribute->{code}, 'UpdateBorrowerAttribute updates the field code correctly' );
is( $borrower_attributes->[0]->{description}, $attribute_type1->description(), 'UpdateBorrowerAttribute updates the field description correctly' );
is( $borrower_attributes->[0]->{value}, $attribute->{attribute}, 'UpdateBorrowerAttribute updates the field value correctly' );
is( $borrower_attributes->[0]->{password}, $attribute->{password}, 'UpdateBorrowerAttributes updates the field password correctly' );
my $check_uniqueness = C4::Members::Attributes::CheckUniqueness();
is( $check_uniqueness, 0, 'CheckUniqueness without arguments returns false' );
$check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code});
is( $check_uniqueness, 1, 'CheckUniqueness with a valid argument code returns true' );
$check_uniqueness = C4::Members::Attributes::CheckUniqueness(undef, $attribute->{attribute});
is( $check_uniqueness, 0, 'CheckUniqueness without the argument code returns false' );
$check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code');
is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code returns false' );
$attribute_value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attributes->[1]->{code});
$check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code', $attribute->{attribute});
is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code returns fale' );
$check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code}, 'new value');
is( $check_uniqueness, 1, 'CheckUniqueness with a new value returns true' );
$check_uniqueness = C4::Members::Attributes::CheckUniqueness('my invalid code', 'new value');
is( $check_uniqueness, 0, 'CheckUniqueness with an invalid argument code and a new value returns false' );
$check_uniqueness = C4::Members::Attributes::CheckUniqueness($attributes->[1]->{code}, $attributes->[1]->{value});
is( $check_uniqueness, 1, 'CheckUniqueness with an attribute unique_id=0 returns true' );
$check_uniqueness = C4::Members::Attributes::CheckUniqueness($attribute->{code}, $attribute->{attribute});
is( $check_uniqueness, '', 'CheckUniqueness returns false' );
my $borrower_numbers = C4::Members::Attributes::SearchIdMatchingAttribute('attribute1');
is( @$borrower_numbers, 0, 'SearchIdMatchingAttribute searchs only in attributes with staff_searchable=1' );
for my $attr( split(' ', $attributes->[1]->{value}) ) {
$borrower_numbers = C4::Members::Attributes::SearchIdMatchingAttribute($attr);
is( $borrower_numbers->[0], $borrowernumber, 'SearchIdMatchingAttribute returns the borrower numbers matching' );
}
C4::Members::Attributes::DeleteBorrowerAttribute();
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
is( @$borrower_attributes, 3, 'DeleteBorrowerAttribute without arguments deletes nothing' );
C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber);
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
is( @$borrower_attributes, 3, 'DeleteBorrowerAttribute without the attribute deletes nothing' );
C4::Members::Attributes::DeleteBorrowerAttribute(undef, $attribute);
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
is( @$borrower_attributes, 3, 'DeleteBorrowerAttribute with a undef borrower number deletes nothing' );
C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber, $attribute);
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
is( @$borrower_attributes, 2, 'DeleteBorrowerAttribute deletes a borrower attribute' );
is( $borrower_attributes->[0]->{code}, $attributes->[1]->{code}, 'DeleteBorrowerAttribute deletes the correct entry');
is( $borrower_attributes->[0]->{description}, $attribute_type2->description(), 'DeleteBorrowerAttribute deletes the correct entry');
is( $borrower_attributes->[0]->{value}, $attributes->[1]->{value}, 'DeleteBorrowerAttribute deletes the correct entry');
is( $borrower_attributes->[0]->{password}, $attributes->[1]->{password}, 'DeleteBorrowerAttribute deletes the correct entry');
C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber, $attributes->[1]);
$borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
is( @$borrower_attributes, 1, 'DeleteBorrowerAttribute deletes a borrower attribute' );