Bug 17828: (QA followup) Add ->type and reuse it
authorTomas Cohen Arazi <tomascohen@theke.io>
Mon, 27 Mar 2017 15:47:14 +0000 (12:47 -0300)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 31 Mar 2017 14:36:48 +0000 (14:36 +0000)
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Koha/Patron/Attribute.pm
t/db_dependent/Koha/Patron/Attributes.t

index fe62dfa..52eb9c1 100644 (file)
@@ -79,6 +79,21 @@ sub opac_editable {
     return Koha::Patron::Attribute::Types->find( $self->code )->opac_editable;
 }
 
+=head3 type
+
+    my $attribute_type = $attribute->type;
+
+Returns a C<Koha::Patron::Attribute::Type> object corresponding to the current patron attribute
+
+=cut
+
+sub type {
+
+    my $self = shift;
+
+    return Koha::Patron::Attribute::Types->find( $self->code );
+}
+
 =head2 Internal methods
 
 =head3 _check_repeatable
@@ -93,7 +108,7 @@ sub _check_repeatable {
 
     my $self = shift;
 
-    if ( !Koha::Patron::Attribute::Types->find( $self->code )->repeatable ) {
+    if ( !$self->type->repeatable ) {
         my $attr_count
             = Koha::Database->new->schema->resultset( $self->_type )->search(
             {   borrowernumber => $self->borrowernumber,
@@ -119,7 +134,7 @@ sub _check_unique_id {
 
     my $self = shift;
 
-    if ( Koha::Patron::Attribute::Types->find( $self->code )->unique_id ) {
+    if ( $self->type->unique_id ) {
         my $unique_count
             = Koha::Database->new->schema->resultset( $self->_type )
             ->search( { code => $self->code, attribute => $self->attribute } )
index 36b04df..cbb02b9 100644 (file)
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 4;
+use Test::More tests => 5;
 
 use t::lib::TestBuilder;
 use Test::Exception;
@@ -235,4 +235,44 @@ subtest 'opac_editable() tests' => sub {
     $schema->storage->txn_rollback;
 };
 
+subtest 'type() tests' => sub {
+
+    plan tests => 4;
+
+    $schema->storage->txn_begin;
+
+    my $patron
+        = $builder->build( { source => 'Borrower' } )->{borrowernumber};
+    my $attr_type = $builder->build( { source => 'BorrowerAttributeType' } );
+    my $attribute = Koha::Patron::Attribute->new(
+        {   borrowernumber => $patron,
+            code           => $attr_type->{code},
+            attribute      => $patron
+        }
+    );
+
+    my $attribute_type = $attribute->type;
+
+    is( ref($attribute_type),
+        'Koha::Patron::Attribute::Type',
+        '->type returns a Koha::Patron::Attribute::Type object'
+    );
+
+    is( $attribute_type->code,
+        $attr_type->{code},
+        '->type returns the right Koha::Patron::Attribute::Type object' );
+
+    is( $attribute_type->opac_editable,
+        $attr_type->{opac_editable},
+        '->type returns the right Koha::Patron::Attribute::Type object'
+    );
+
+    is( $attribute_type->opac_display,
+        $attr_type->{opac_display},
+        '->type returns the right Koha::Patron::Attribute::Type object'
+    );
+
+    $schema->storage->txn_rollback;
+};
+
 1;