Bug 12427: adding unit tests of module C4::Members::Attributes.pm
authorYohann Dufour <dufour.yohann@gmail.com>
Tue, 17 Jun 2014 14:53:14 +0000 (16:53 +0200)
committerTomas Cohen Arazi <tomascohen@gmail.com>
Tue, 24 Jun 2014 12:42:53 +0000 (09:42 -0300)
The subroutines UpdateBorrowerAttribute, SearchIdMatchingAttribute, DeleteBorrowerAttribute, GetBorrowerAttributes, GetAttributes, GetBorrowerAttributeValue, SetBorrowerAttributes and CheckUniqueness of the module C4::Members::Attributes.pm were not tested.

Test plan:
1/ Execute the command: prove t/db_dependent/Members_Attributes.t
2/ The result has to be:
t/db_dependent/Members_Attributes.t .. ok
All tests successful.
Files=1, Tests=60,  2 wallclock secs ( 0.04 usr  0.01 sys +  1.45 cusr  0.08 csys =  1.58 CPU)
Result: PASS

Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>
Test with patrons on db having issues and attributes
New test file, all test pass
No koha-qa errors

Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
t/db_dependent/Members_Attributes.t [new file with mode: 0755]

diff --git a/t/db_dependent/Members_Attributes.t b/t/db_dependent/Members_Attributes.t
new file mode 100755 (executable)
index 0000000..d36a4c0
--- /dev/null
@@ -0,0 +1,199 @@
+#!/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 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 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;