#!/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 . use Modern::Perl; use C4::Context; use C4::Members; use C4::Members::AttributeTypes; use Test::More tests => 60; use_ok('C4::Members::Attributes'); my $dbh = C4::Context->dbh; $dbh->{AutoCommit} = 0; $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 $borrowernumber = AddMember( firstname => 'my firstname', surname => 'my surname', categorycode => 'S', branchcode => 'CPL', ); my $attribute_type1 = C4::Members::AttributeTypes->new('my code1', 'my description1'); $attribute_type1->unique_id(1); my $attribute_type2 = C4::Members::AttributeTypes->new('my code2', 'my description2'); $attribute_type2->opac_display(1); $attribute_type2->staff_searchable(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' ); $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 $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', } ]; 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, 2, '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, 1); is( @$borrower_attributes, 1, 'GetBorrowerAttributes returns the correct number of borrower attributes with the filter opac_only' ); is( $borrower_attributes->[0]->{code}, $attributes->[1]->{code}, 'GetBorrowerAttributes returns the correct code' ); is( $borrower_attributes->[0]->{description}, $attribute_type2->description(), 'GetBorrowerAttributes returns the correct description' ); is( $borrower_attributes->[0]->{value}, $attributes->[1]->{value}, 'GetBorrowerAttributes returns the correct value' ); is( $borrower_attributes->[0]->{password}, $attributes->[1]->{password}, 'GetBorrowerAttributes returns the correct password' ); 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, 2, '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, 2, 'DeleteBorrowerAttribute without arguments deletes nothing' ); C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber); $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber); is( @$borrower_attributes, 2, 'DeleteBorrowerAttribute without the attribute deletes nothing' ); C4::Members::Attributes::DeleteBorrowerAttribute(undef, $attribute); $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber); is( @$borrower_attributes, 2, 'DeleteBorrowerAttribute with a undef borrower number deletes nothing' ); C4::Members::Attributes::DeleteBorrowerAttribute($borrowernumber, $attribute); $borrower_attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber); is( @$borrower_attributes, 1, '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, 0, 'DeleteBorrowerAttribute deletes a borrower attribute' ); $dbh->rollback;