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