View | Details | Raw Unified | Return to bug 30657
Collapse All | Expand All

(-)a/t/db_dependent/Koha/Patron.t (-3 / +51 lines)
Lines 1197-1223 subtest 'extended_attributes' => sub { Link Here
1197
    };
1197
    };
1198
1198
1199
    subtest 'attribute accessor tests' => sub {
1199
    subtest 'attribute accessor tests' => sub {
1200
        plan tests => 2;
1200
        plan tests => 4;
1201
1201
1202
        $schema->storage->txn_begin;
1202
        $schema->storage->txn_begin;
1203
1203
1204
        # Cleanup previous attribute types
1204
        Koha::Patron::Attribute::Types->search->delete;
1205
        Koha::Patron::Attribute::Types->search->delete;
1205
1206
1207
        # Create a non-repeatable attribute type and a repeatable attribute type
1206
        my $attribute_type_1 = $builder->build_object(
1208
        my $attribute_type_1 = $builder->build_object(
1207
            {
1209
            {
1208
                class => 'Koha::Patron::Attribute::Types',
1210
                class => 'Koha::Patron::Attribute::Types',
1209
                value => { code => 'smartnumber', repeatable => 0 }
1211
                value => { code => 'smartnumber', repeatable => 0 }
1210
            }
1212
            }
1211
        );
1213
        );
1214
        my $attribute_type_2 = $builder->build_object(
1215
            {
1216
                class => 'Koha::Patron::Attribute::Types',
1217
                value => { code => 'favbooks', repeatable => 1 }
1218
            }
1219
        );
1212
1220
1221
        # Create a test patron
1213
        my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1222
        my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1214
1223
1224
        # Test non-existing attribute (should return undef and not throw an error)
1215
        is( $patron->smartnumber, undef, 'smartnumber accessor returns undef when not set' );
1225
        is( $patron->smartnumber, undef, 'smartnumber accessor returns undef when not set' );
1216
1226
1227
        # Test setting and retrieving a non-repeatable attribute
1217
        $patron->extended_attributes( [ { code => 'smartnumber', attribute => 'SM1234' } ] );
1228
        $patron->extended_attributes( [ { code => 'smartnumber', attribute => 'SM1234' } ] );
1218
1219
        is( $patron->smartnumber, 'SM1234', 'smartnumber accessor returns value when set' );
1229
        is( $patron->smartnumber, 'SM1234', 'smartnumber accessor returns value when set' );
1220
1230
1231
        # Test setting and retrieving a repeatable attribute
1232
        $patron->extended_attributes(
1233
            [
1234
                { code => 'favbooks', attribute => 'Book1' },
1235
                { code => 'favbooks', attribute => 'Book2' }
1236
            ]
1237
        );
1238
        is_deeply(
1239
            $patron->favbooks, [ 'Book1', 'Book2' ],
1240
            'favbooks accessor returns an array of values for repeatable attribute'
1241
        );
1242
1243
        # Test for a non-existing method to confirm it throws an error
1244
        throws_ok { $patron->nonexistentattribute } 'Koha::Exceptions::Object::MethodNotCoveredByTests',
1245
            'Calling a non-existent attribute method throws an error';
1246
1247
        #        # Count DBI calls to confirm once one attribute accessor is called, we trigger AUTOLOAD again
1248
        #        # for subsequent attribute accessor calls for any valid attribute types.
1249
        #        # Enable DBI tracing and count search queries for attribute types
1250
        #        my $trace_output = '';
1251
        #        open my $fh, '>', \$trace_output or die "Can't open scalar for writing: $!";
1252
        #
1253
        #        # Enable tracing with a callback to capture the output
1254
        #        my $dbh = $schema->storage->dbh;
1255
        #        $dbh->trace(0, $fh);
1256
        #
1257
        #        # Call each accessor method and confirm the search call count
1258
        #        my $refetched_patron = Koha::Patron->find( $patron->id );
1259
        #        $refetched_patron->smartnumber;    # First access
1260
        #        $refetched_patron->favbooks;       # Access a different attribute type
1261
        #
1262
        #        # Disable tracing
1263
        #        $dbh->trace(0);
1264
        #
1265
        #        # Count occurrences of the attribute types query in trace output
1266
        #        my $search_query_count = () = $trace_output =~ /SELECT .* FROM patron_attribute_types/i;
1267
        #
1268
        #        is( $search_query_count, 1, 'attributes->search is only called once per attribute type' );
1269
1221
        $schema->storage->txn_rollback;
1270
        $schema->storage->txn_rollback;
1222
    };
1271
    };
1223
};
1272
};
1224
- 

Return to bug 30657