|
Lines 19-25
Link Here
|
| 19 |
|
19 |
|
| 20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
| 21 |
|
21 |
|
| 22 |
use Test::More tests => 41; |
22 |
use Test::More tests => 40; |
| 23 |
use Test::Warn; |
23 |
use Test::Warn; |
| 24 |
use Test::Exception; |
24 |
use Test::Exception; |
| 25 |
use Test::MockModule; |
25 |
use Test::MockModule; |
|
Lines 1968-2143
subtest 'anonymize' => sub {
Link Here
|
| 1968 |
is( $patron2->firstname, undef, 'First name patron2 cleared' ); |
1968 |
is( $patron2->firstname, undef, 'First name patron2 cleared' ); |
| 1969 |
}; |
1969 |
}; |
| 1970 |
$schema->storage->txn_rollback; |
1970 |
$schema->storage->txn_rollback; |
| 1971 |
|
|
|
| 1972 |
subtest 'extended_attributes' => sub { |
| 1973 |
plan tests => 14; |
| 1974 |
my $schema = Koha::Database->new->schema; |
| 1975 |
$schema->storage->txn_begin; |
| 1976 |
|
| 1977 |
my $patron_1 = $builder->build_object({class=> 'Koha::Patrons'}); |
| 1978 |
my $patron_2 = $builder->build_object({class=> 'Koha::Patrons'}); |
| 1979 |
|
| 1980 |
t::lib::Mocks::mock_userenv({ patron => $patron_1 }); |
| 1981 |
|
| 1982 |
my $attribute_type1 = Koha::Patron::Attribute::Type->new( |
| 1983 |
{ |
| 1984 |
code => 'my code1', |
| 1985 |
description => 'my description1', |
| 1986 |
unique_id => 1 |
| 1987 |
} |
| 1988 |
)->store; |
| 1989 |
my $attribute_type2 = Koha::Patron::Attribute::Type->new( |
| 1990 |
{ |
| 1991 |
code => 'my code2', |
| 1992 |
description => 'my description2', |
| 1993 |
opac_display => 1, |
| 1994 |
staff_searchable => 1 |
| 1995 |
} |
| 1996 |
)->store; |
| 1997 |
|
| 1998 |
my $attribute_type3 = $builder->build_object({ class => 'Koha::Patron::Attribute::Types' }); |
| 1999 |
|
| 2000 |
my $deleted_attribute_type = $builder->build_object({ class => 'Koha::Patron::Attribute::Types' }); |
| 2001 |
my $deleted_attribute_type_code = $deleted_attribute_type->code; |
| 2002 |
$deleted_attribute_type->delete; |
| 2003 |
|
| 2004 |
my $new_library = $builder->build( { source => 'Branch' } ); |
| 2005 |
my $attribute_type_limited = Koha::Patron::Attribute::Type->new( |
| 2006 |
{ code => 'my code3', description => 'my description3' } )->store; |
| 2007 |
$attribute_type_limited->library_limits( [ $new_library->{branchcode} ] ); |
| 2008 |
|
| 2009 |
my $attributes_for_1 = [ |
| 2010 |
{ |
| 2011 |
attribute => 'my attribute1', |
| 2012 |
code => $attribute_type1->code(), |
| 2013 |
}, |
| 2014 |
{ |
| 2015 |
attribute => 'my attribute2', |
| 2016 |
code => $attribute_type2->code(), |
| 2017 |
}, |
| 2018 |
{ |
| 2019 |
attribute => 'my attribute limited', |
| 2020 |
code => $attribute_type_limited->code(), |
| 2021 |
} |
| 2022 |
]; |
| 2023 |
|
| 2024 |
my $attributes_for_2 = [ |
| 2025 |
{ |
| 2026 |
attribute => 'my attribute12', |
| 2027 |
code => $attribute_type1->code(), |
| 2028 |
}, |
| 2029 |
{ |
| 2030 |
attribute => 'my attribute limited 2', |
| 2031 |
code => $attribute_type_limited->code(), |
| 2032 |
}, |
| 2033 |
{ |
| 2034 |
attribute => 'my nonexistent attribute 2', |
| 2035 |
code => $deleted_attribute_type_code, |
| 2036 |
} |
| 2037 |
]; |
| 2038 |
|
| 2039 |
my $extended_attributes = $patron_1->extended_attributes; |
| 2040 |
is( ref($extended_attributes), 'Koha::Patron::Attributes', 'Koha::Patron->extended_attributes must return a Koha::Patron::Attribute set' ); |
| 2041 |
is( $extended_attributes->count, 0, 'There should not be attribute yet'); |
| 2042 |
|
| 2043 |
$patron_1->extended_attributes->filter_by_branch_limitations->delete; |
| 2044 |
$patron_2->extended_attributes->filter_by_branch_limitations->delete; |
| 2045 |
$patron_1->extended_attributes($attributes_for_1); |
| 2046 |
|
| 2047 |
warning_like { |
| 2048 |
$patron_2->extended_attributes($attributes_for_2); |
| 2049 |
} [ qr/a foreign key constraint fails/ ], 'nonexistent attribute should have not exploded but print a warning'; |
| 2050 |
|
| 2051 |
my $extended_attributes_for_1 = $patron_1->extended_attributes; |
| 2052 |
is( $extended_attributes_for_1->count, 3, 'There should be 3 attributes now for patron 1'); |
| 2053 |
|
| 2054 |
my $extended_attributes_for_2 = $patron_2->extended_attributes; |
| 2055 |
is( $extended_attributes_for_2->count, 2, 'There should be 2 attributes now for patron 2'); |
| 2056 |
|
| 2057 |
my $attribute_12 = $extended_attributes_for_2->search({ code => $attribute_type1->code }); |
| 2058 |
is( $attribute_12->next->attribute, 'my attribute12', 'search by code should return the correct attribute' ); |
| 2059 |
|
| 2060 |
$attribute_12 = $patron_2->get_extended_attribute( $attribute_type1->code ); |
| 2061 |
is( $attribute_12->attribute, 'my attribute12', 'Koha::Patron->get_extended_attribute should return the correct attribute value' ); |
| 2062 |
|
| 2063 |
warning_is { |
| 2064 |
$extended_attributes_for_2 = $patron_2->extended_attributes->merge_with( |
| 2065 |
[ |
| 2066 |
{ |
| 2067 |
attribute => 'my attribute12 XXX', |
| 2068 |
code => $attribute_type1->code(), |
| 2069 |
}, |
| 2070 |
{ |
| 2071 |
attribute => 'my nonexistent attribute 2', |
| 2072 |
code => $deleted_attribute_type_code, |
| 2073 |
}, |
| 2074 |
{ |
| 2075 |
attribute => 'my attribute 3', # Adding a new attribute using merge_with |
| 2076 |
code => $attribute_type3->code, |
| 2077 |
}, |
| 2078 |
] |
| 2079 |
); |
| 2080 |
} |
| 2081 |
"Cannot merge element: unrecognized code = '$deleted_attribute_type_code'", |
| 2082 |
"Trying to merge_with using a nonexistent attribute code should display a warning"; |
| 2083 |
|
| 2084 |
is( @$extended_attributes_for_2, 3, 'There should be 3 attributes now for patron 3'); |
| 2085 |
my $expected_attributes_for_2 = [ |
| 2086 |
{ |
| 2087 |
code => $attribute_type1->code(), |
| 2088 |
attribute => 'my attribute12 XXX', |
| 2089 |
}, |
| 2090 |
{ |
| 2091 |
code => $attribute_type_limited->code(), |
| 2092 |
attribute => 'my attribute limited 2', |
| 2093 |
}, |
| 2094 |
{ |
| 2095 |
attribute => 'my attribute 3', |
| 2096 |
code => $attribute_type3->code, |
| 2097 |
}, |
| 2098 |
]; |
| 2099 |
# Sorting them by code |
| 2100 |
$expected_attributes_for_2 = [ sort { $a->{code} cmp $b->{code} } @$expected_attributes_for_2 ]; |
| 2101 |
|
| 2102 |
is_deeply( |
| 2103 |
[ |
| 2104 |
{ |
| 2105 |
code => $extended_attributes_for_2->[0]->{code}, |
| 2106 |
attribute => $extended_attributes_for_2->[0]->{attribute} |
| 2107 |
}, |
| 2108 |
{ |
| 2109 |
code => $extended_attributes_for_2->[1]->{code}, |
| 2110 |
attribute => $extended_attributes_for_2->[1]->{attribute} |
| 2111 |
}, |
| 2112 |
{ |
| 2113 |
code => $extended_attributes_for_2->[2]->{code}, |
| 2114 |
attribute => $extended_attributes_for_2->[2]->{attribute} |
| 2115 |
}, |
| 2116 |
], |
| 2117 |
$expected_attributes_for_2 |
| 2118 |
); |
| 2119 |
|
| 2120 |
# TODO - What about multiple? POD explains the problem |
| 2121 |
my $non_existent = $patron_2->get_extended_attribute( 'not_exist' ); |
| 2122 |
is( $non_existent, undef, 'Koha::Patron->get_extended_attribute must return undef if the attribute does not exist' ); |
| 2123 |
|
| 2124 |
# Test branch limitations |
| 2125 |
t::lib::Mocks::mock_userenv({ patron => $patron_2 }); |
| 2126 |
# Return all |
| 2127 |
$extended_attributes_for_1 = $patron_1->extended_attributes; |
| 2128 |
is( $extended_attributes_for_1->count, 3, 'There should be 2 attributes for patron 1, the limited one should be returned'); |
| 2129 |
|
| 2130 |
# Return filtered |
| 2131 |
$extended_attributes_for_1 = $patron_1->extended_attributes->filter_by_branch_limitations; |
| 2132 |
is( $extended_attributes_for_1->count, 2, 'There should be 2 attributes for patron 1, the limited one should be returned'); |
| 2133 |
|
| 2134 |
# Not filtered |
| 2135 |
my $limited_value = $patron_1->get_extended_attribute( $attribute_type_limited->code ); |
| 2136 |
is( $limited_value->attribute, 'my attribute limited', ); |
| 2137 |
|
| 2138 |
## Do we need a filtered? |
| 2139 |
#$limited_value = $patron_1->get_extended_attribute( $attribute_type_limited->code ); |
| 2140 |
#is( $limited_value, undef, ); |
| 2141 |
|
| 2142 |
$schema->storage->txn_rollback; |
| 2143 |
}; |
| 2144 |
- |