|
Line 0
Link Here
|
| 0 |
- |
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 => 5; |
| 23 |
|
| 24 |
use t::lib::TestBuilder; |
| 25 |
use t::lib::Mocks; |
| 26 |
use Test::Exception; |
| 27 |
|
| 28 |
use C4::Context; |
| 29 |
use Koha::Database; |
| 30 |
use Koha::Patron::Attribute::Type; |
| 31 |
use Koha::Patron::Attribute::Types; |
| 32 |
|
| 33 |
my $schema = Koha::Database->new->schema; |
| 34 |
my $dbh = C4::Context->dbh; |
| 35 |
my $builder = t::lib::TestBuilder->new; |
| 36 |
|
| 37 |
subtest 'new() tests' => sub { |
| 38 |
|
| 39 |
plan tests => 2; |
| 40 |
|
| 41 |
$schema->storage->txn_begin; |
| 42 |
|
| 43 |
# Cleanup before running the tests |
| 44 |
Koha::Patron::Attribute::Types->search()->delete(); |
| 45 |
|
| 46 |
my $attribute_type = Koha::Patron::Attribute::Type->new( |
| 47 |
{ code => 'code', |
| 48 |
description => 'description', |
| 49 |
repeatable => 0 |
| 50 |
} |
| 51 |
)->store(); |
| 52 |
|
| 53 |
is( Koha::Patron::Attribute::Types->search()->count, |
| 54 |
1, 'Only one object created' ); |
| 55 |
|
| 56 |
my $cateogory_code |
| 57 |
= $builder->build( { source => 'Category' } )->{categorycode}; |
| 58 |
|
| 59 |
my $attribute_type_with_category = Koha::Patron::Attribute::Type->new( |
| 60 |
{ code => 'code_2', |
| 61 |
description => 'description', |
| 62 |
category_code => $cateogory_code, |
| 63 |
repeatable => 0 |
| 64 |
} |
| 65 |
)->store(); |
| 66 |
|
| 67 |
is( Koha::Patron::Attribute::Types->search()->count, |
| 68 |
2, 'Two objects created' ); |
| 69 |
|
| 70 |
$schema->storage->txn_rollback; |
| 71 |
}; |
| 72 |
|
| 73 |
subtest 'library_limits() tests' => sub { |
| 74 |
|
| 75 |
plan tests => 5; |
| 76 |
|
| 77 |
$schema->storage->txn_begin; |
| 78 |
|
| 79 |
# Cleanup before running the tests |
| 80 |
Koha::Patron::Attribute::Types->search()->delete(); |
| 81 |
|
| 82 |
my $attribute_type = Koha::Patron::Attribute::Type->new( |
| 83 |
{ code => 'code', |
| 84 |
description => 'description', |
| 85 |
repeatable => 0 |
| 86 |
} |
| 87 |
)->store(); |
| 88 |
|
| 89 |
my $library = $builder->build( { source => 'Branch' } )->{branchcode}; |
| 90 |
|
| 91 |
my $library_limits = $attribute_type->library_limits(); |
| 92 |
is_deeply( $library_limits, [], |
| 93 |
'No branch limitations defined for attribute type' ); |
| 94 |
|
| 95 |
my $print_error = $dbh->{PrintError}; |
| 96 |
$dbh->{PrintError} = 0; |
| 97 |
|
| 98 |
throws_ok { |
| 99 |
$library_limits = $attribute_type->library_limits( ['fake'] ); |
| 100 |
} |
| 101 |
'Koha::Exceptions::CannotAddLibraryLimit', |
| 102 |
'Exception thrown on single invalid branchcode'; |
| 103 |
|
| 104 |
throws_ok { |
| 105 |
$library_limits |
| 106 |
= $attribute_type->library_limits( [ 'fake', $library ] ); |
| 107 |
} |
| 108 |
'Koha::Exceptions::CannotAddLibraryLimit', |
| 109 |
'Exception thrown on invalid branchcode present'; |
| 110 |
|
| 111 |
$dbh->{PrintError} = $print_error; |
| 112 |
|
| 113 |
$library_limits = $attribute_type->library_limits( [$library] ); |
| 114 |
is_deeply( $library_limits, [1], 'Library limits correctly set' ); |
| 115 |
|
| 116 |
my $another_library |
| 117 |
= $builder->build( { source => 'Branch' } )->{branchcode}; |
| 118 |
|
| 119 |
$library_limits |
| 120 |
= $attribute_type->library_limits( [ $library, $another_library ] ); |
| 121 |
is_deeply( $library_limits, [ 1, 1 ], 'Library limits correctly set' ); |
| 122 |
|
| 123 |
$schema->storage->txn_rollback; |
| 124 |
}; |
| 125 |
|
| 126 |
subtest 'add_library_limit() tests' => sub { |
| 127 |
|
| 128 |
plan tests => 3; |
| 129 |
|
| 130 |
$schema->storage->txn_begin; |
| 131 |
|
| 132 |
# Cleanup before running the tests |
| 133 |
Koha::Patron::Attribute::Types->search()->delete(); |
| 134 |
|
| 135 |
my $attribute_type = Koha::Patron::Attribute::Type->new( |
| 136 |
{ code => 'code', |
| 137 |
description => 'description', |
| 138 |
repeatable => 0 |
| 139 |
} |
| 140 |
)->store(); |
| 141 |
|
| 142 |
throws_ok { $attribute_type->add_library_limit() } |
| 143 |
'Koha::Exceptions::MissingParameter', |
| 144 |
'branchcode parameter is mandatory'; |
| 145 |
|
| 146 |
my $library = $builder->build( { source => 'Branch' } )->{branchcode}; |
| 147 |
is( $attribute_type->add_library_limit($library), |
| 148 |
1, 'Library limit successfully added' ); |
| 149 |
|
| 150 |
my $print_error = $dbh->{PrintError}; |
| 151 |
$dbh->{PrintError} = 0; |
| 152 |
|
| 153 |
throws_ok { |
| 154 |
$attribute_type->add_library_limit('fake'); |
| 155 |
} |
| 156 |
'Koha::Exceptions::CannotAddLibraryLimit', |
| 157 |
'Exception thrown on invalid branchcode'; |
| 158 |
|
| 159 |
$dbh->{PrintError} = $print_error; |
| 160 |
|
| 161 |
$schema->storage->txn_rollback; |
| 162 |
}; |
| 163 |
|
| 164 |
subtest 'del_library_limit() tests' => sub { |
| 165 |
|
| 166 |
plan tests => 4; |
| 167 |
|
| 168 |
$schema->storage->txn_begin; |
| 169 |
|
| 170 |
# Cleanup before running the tests |
| 171 |
Koha::Patron::Attribute::Types->search()->delete(); |
| 172 |
|
| 173 |
my $attribute_type = Koha::Patron::Attribute::Type->new( |
| 174 |
{ code => 'code', |
| 175 |
description => 'description', |
| 176 |
repeatable => 0 |
| 177 |
} |
| 178 |
)->store(); |
| 179 |
|
| 180 |
throws_ok { $attribute_type->del_library_limit() } |
| 181 |
'Koha::Exceptions::MissingParameter', |
| 182 |
'branchcode parameter is mandatory'; |
| 183 |
|
| 184 |
my $library = $builder->build( { source => 'Branch' } )->{branchcode}; |
| 185 |
$attribute_type->add_library_limit($library); |
| 186 |
|
| 187 |
is( $attribute_type->del_library_limit($library), |
| 188 |
1, 'Library limit successfully deleted' ); |
| 189 |
|
| 190 |
my $print_error = $dbh->{PrintError}; |
| 191 |
$dbh->{PrintError} = 0; |
| 192 |
|
| 193 |
throws_ok { |
| 194 |
$attribute_type->del_library_limit($library); |
| 195 |
} |
| 196 |
'Koha::Exceptions::ObjectNotFound', |
| 197 |
'Exception thrown on non-existent library limit'; |
| 198 |
|
| 199 |
throws_ok { |
| 200 |
$attribute_type->del_library_limit('fake'); |
| 201 |
} |
| 202 |
'Koha::Exceptions::ObjectNotFound', |
| 203 |
'Exception thrown on non-existent library limit'; |
| 204 |
|
| 205 |
$dbh->{PrintError} = $print_error; |
| 206 |
|
| 207 |
$schema->storage->txn_rollback; |
| 208 |
}; |
| 209 |
|
| 210 |
subtest 'replace_library_limits() tests' => sub { |
| 211 |
|
| 212 |
plan tests => 6; |
| 213 |
|
| 214 |
$schema->storage->txn_begin; |
| 215 |
|
| 216 |
# Cleanup before running the tests |
| 217 |
Koha::Patron::Attribute::Types->search()->delete(); |
| 218 |
|
| 219 |
my $attribute_type = Koha::Patron::Attribute::Type->new( |
| 220 |
{ code => 'code', |
| 221 |
description => 'description', |
| 222 |
repeatable => 0 |
| 223 |
} |
| 224 |
)->store(); |
| 225 |
|
| 226 |
is_deeply( $attribute_type->replace_library_limits( [] ), |
| 227 |
[], 'Replacing with empty array returns an empty array as expected' ); |
| 228 |
|
| 229 |
is_deeply( $attribute_type->library_limits(), |
| 230 |
[], 'Replacing with empty array yields no library limits' ); |
| 231 |
|
| 232 |
my $library_1 = $builder->build({ source => 'Branch'})->{branchcode}; |
| 233 |
my $library_2 = $builder->build({ source => 'Branch'})->{branchcode}; |
| 234 |
|
| 235 |
is_deeply( $attribute_type->replace_library_limits( [$library_1] ), |
| 236 |
[ 1 ], 'Successfully adds a single library limit' ); |
| 237 |
|
| 238 |
is_deeply( $attribute_type->library_limits(), |
| 239 |
[ $library_1 ], 'Library limit correctly set' ); |
| 240 |
|
| 241 |
is_deeply( $attribute_type->replace_library_limits( [$library_1, $library_2] ), |
| 242 |
[ 1, 1 ], 'Successfully adds two library limit' ); |
| 243 |
|
| 244 |
is_deeply( $attribute_type->library_limits(), |
| 245 |
[ $library_1, $library_2 ], 'Library limit correctly set' ); |
| 246 |
|
| 247 |
$schema->storage->txn_rollback; |
| 248 |
}; |
| 249 |
|
| 250 |
1; |