|
Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# Copyright 2016 Koha Development team |
| 4 |
# |
| 5 |
# This file is part of Koha |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use Test::More tests => 2; |
| 23 |
|
| 24 |
use t::lib::TestBuilder; |
| 25 |
use Test::Exception; |
| 26 |
|
| 27 |
use Koha::Database; |
| 28 |
use Koha::Patron::Attribute; |
| 29 |
use Koha::Patron::Attributes; |
| 30 |
|
| 31 |
my $schema = Koha::Database->new->schema; |
| 32 |
my $builder = t::lib::TestBuilder->new; |
| 33 |
|
| 34 |
subtest 'store() tests' => sub { |
| 35 |
|
| 36 |
plan tests => 2; |
| 37 |
|
| 38 |
subtest 'repeatable attributes tests' => sub { |
| 39 |
|
| 40 |
plan tests => 5; |
| 41 |
|
| 42 |
$schema->storage->txn_begin; |
| 43 |
|
| 44 |
my $patron = $builder->build( { source => 'Borrower' } )->{borrowernumber}; |
| 45 |
my $attribute_type_1 = $builder->build( |
| 46 |
{ source => 'BorrowerAttributeType', |
| 47 |
value => { repeatable => 1 } |
| 48 |
} |
| 49 |
); |
| 50 |
Koha::Patron::Attribute->new( |
| 51 |
{ borrowernumber => $patron, |
| 52 |
code => $attribute_type_1->{code}, |
| 53 |
attribute => 'Foo' |
| 54 |
} |
| 55 |
)->store; |
| 56 |
Koha::Patron::Attribute->new( |
| 57 |
{ borrowernumber => $patron, |
| 58 |
code => $attribute_type_1->{code}, |
| 59 |
attribute => 'Bar' |
| 60 |
} |
| 61 |
)->store; |
| 62 |
my $attr_count |
| 63 |
= Koha::Patron::Attributes->search( |
| 64 |
{ borrowernumber => $patron, code => $attribute_type_1->{code} } ) |
| 65 |
->count; |
| 66 |
is( $attr_count, 2, |
| 67 |
'2 repeatable attributes stored and retrieved correcctly' ); |
| 68 |
|
| 69 |
my $attribute_type_2 = $builder->build( |
| 70 |
{ source => 'BorrowerAttributeType', |
| 71 |
value => { repeatable => 0 } |
| 72 |
} |
| 73 |
); |
| 74 |
|
| 75 |
Koha::Patron::Attribute->new( |
| 76 |
{ borrowernumber => $patron, |
| 77 |
code => $attribute_type_2->{code}, |
| 78 |
attribute => 'Foo' |
| 79 |
} |
| 80 |
)->store; |
| 81 |
throws_ok { |
| 82 |
Koha::Patron::Attribute->new( |
| 83 |
{ borrowernumber => $patron, |
| 84 |
code => $attribute_type_2->{code}, |
| 85 |
attribute => 'Bar' |
| 86 |
} |
| 87 |
)->store; |
| 88 |
} |
| 89 |
'Koha::Exceptions::Patron::Attribute::NonRepeatable', |
| 90 |
'Exception thrown trying to store more than one non-repeatable attribute'; |
| 91 |
|
| 92 |
is( |
| 93 |
"$@", |
| 94 |
"Tried to add more than one non-repeatable attributes. code=" |
| 95 |
. $attribute_type_2->{code} |
| 96 |
. " attribute=Bar", |
| 97 |
'Exception stringified correctly, attribute passed correctly' |
| 98 |
); |
| 99 |
|
| 100 |
my $attributes = Koha::Patron::Attributes->search( |
| 101 |
{ borrowernumber => $patron, code => $attribute_type_2->{code} } ); |
| 102 |
is( $attributes->count, 1, '1 non-repeatable attribute stored' ); |
| 103 |
is( $attributes->next->attribute, |
| 104 |
'Foo', 'Non-repeatable attribute remains unchanged' ); |
| 105 |
|
| 106 |
$schema->storage->txn_rollback; |
| 107 |
}; |
| 108 |
|
| 109 |
subtest 'unique_id attributes tests' => sub { |
| 110 |
|
| 111 |
plan tests => 5; |
| 112 |
|
| 113 |
$schema->storage->txn_begin; |
| 114 |
|
| 115 |
my $patron_1 = $builder->build( { source => 'Borrower' } )->{borrowernumber}; |
| 116 |
my $patron_2 = $builder->build( { source => 'Borrower' } )->{borrowernumber}; |
| 117 |
|
| 118 |
my $attribute_type_1 = $builder->build( |
| 119 |
{ source => 'BorrowerAttributeType', |
| 120 |
value => { unique_id => 0 } |
| 121 |
} |
| 122 |
); |
| 123 |
Koha::Patron::Attribute->new( |
| 124 |
{ borrowernumber => $patron_1, |
| 125 |
code => $attribute_type_1->{code}, |
| 126 |
attribute => 'Foo' |
| 127 |
} |
| 128 |
)->store; |
| 129 |
Koha::Patron::Attribute->new( |
| 130 |
{ borrowernumber => $patron_2, |
| 131 |
code => $attribute_type_1->{code}, |
| 132 |
attribute => 'Bar' |
| 133 |
} |
| 134 |
)->store; |
| 135 |
my $attr_count |
| 136 |
= Koha::Patron::Attributes->search( |
| 137 |
{ code => $attribute_type_1->{code} } ) |
| 138 |
->count; |
| 139 |
is( $attr_count, 2, |
| 140 |
'2 non-unique attributes stored and retrieved correcctly' ); |
| 141 |
|
| 142 |
my $attribute_type_2 = $builder->build( |
| 143 |
{ source => 'BorrowerAttributeType', |
| 144 |
value => { unique_id => 1 } |
| 145 |
} |
| 146 |
); |
| 147 |
|
| 148 |
Koha::Patron::Attribute->new( |
| 149 |
{ borrowernumber => $patron_1, |
| 150 |
code => $attribute_type_2->{code}, |
| 151 |
attribute => 'Foo' |
| 152 |
} |
| 153 |
)->store; |
| 154 |
throws_ok { |
| 155 |
Koha::Patron::Attribute->new( |
| 156 |
{ borrowernumber => $patron_2, |
| 157 |
code => $attribute_type_2->{code}, |
| 158 |
attribute => 'Foo' |
| 159 |
} |
| 160 |
)->store; |
| 161 |
} |
| 162 |
'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint', |
| 163 |
'Exception thrown trying to store more than one unique attribute'; |
| 164 |
|
| 165 |
is( |
| 166 |
"$@", |
| 167 |
"Your action breaks a unique constraint on the attribute. code=" |
| 168 |
. $attribute_type_2->{code} |
| 169 |
. " attribute=Foo", |
| 170 |
'Exception stringified correctly, attribute passed correctly' |
| 171 |
); |
| 172 |
|
| 173 |
my $attributes = Koha::Patron::Attributes->search( |
| 174 |
{ borrowernumber => $patron_1, code => $attribute_type_2->{code} } ); |
| 175 |
is( $attributes->count, 1, '1 unique attribute stored' ); |
| 176 |
is( $attributes->next->attribute, |
| 177 |
'Foo', 'unique attribute remains unchanged' ); |
| 178 |
|
| 179 |
$schema->storage->txn_rollback; |
| 180 |
}; |
| 181 |
}; |
| 182 |
|
| 183 |
subtest 'type() tests' => sub { |
| 184 |
|
| 185 |
plan tests => 4; |
| 186 |
|
| 187 |
$schema->storage->txn_begin; |
| 188 |
|
| 189 |
my $patron |
| 190 |
= $builder->build( { source => 'Borrower' } )->{borrowernumber}; |
| 191 |
my $attr_type = $builder->build( { source => 'BorrowerAttributeType' } ); |
| 192 |
my $attribute = Koha::Patron::Attribute->new( |
| 193 |
{ borrowernumber => $patron, |
| 194 |
code => $attr_type->{code}, |
| 195 |
attribute => $patron |
| 196 |
} |
| 197 |
); |
| 198 |
|
| 199 |
my $attribute_type = $attribute->type; |
| 200 |
|
| 201 |
is( ref($attribute_type), |
| 202 |
'Koha::Patron::Attribute::Type', |
| 203 |
'->type returns a Koha::Patron::Attribute::Type object' |
| 204 |
); |
| 205 |
|
| 206 |
is( $attribute_type->code, |
| 207 |
$attr_type->{code}, |
| 208 |
'->type returns the right Koha::Patron::Attribute::Type object' ); |
| 209 |
|
| 210 |
is( $attribute_type->opac_editable, |
| 211 |
$attr_type->{opac_editable}, |
| 212 |
'->type returns the right Koha::Patron::Attribute::Type object' |
| 213 |
); |
| 214 |
|
| 215 |
is( $attribute_type->opac_display, |
| 216 |
$attr_type->{opac_display}, |
| 217 |
'->type returns the right Koha::Patron::Attribute::Type object' |
| 218 |
); |
| 219 |
|
| 220 |
$schema->storage->txn_rollback; |
| 221 |
}; |