Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# Copyright 2023 Rijksmuseum, 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 |
use Test::More tests => 3; |
22 |
use Test::Exception; |
23 |
|
24 |
use t::lib::TestBuilder; |
25 |
use t::lib::Mocks; |
26 |
|
27 |
use Koha::MailDomainLimits; |
28 |
use Koha::Database; |
29 |
|
30 |
my $builder = t::lib::TestBuilder->new; |
31 |
my $schema = Koha::Database->new->schema; |
32 |
|
33 |
$schema->storage->txn_begin; |
34 |
|
35 |
subtest 'Some trivial tests' => sub { |
36 |
plan tests => 3; |
37 |
|
38 |
my $count = Koha::MailDomainLimits->count; |
39 |
my $limit01 = $builder->build_object( { class => 'Koha::MailDomainLimits' } ); |
40 |
is( Koha::MailDomainLimits->count, $count + 1, 'Check if record has been added' ); |
41 |
is( $limit01->belongs_to, undef, 'Check default for belongs_to' ); |
42 |
$limit01->delete; |
43 |
is( Koha::MailDomainLimits->count, $count, 'Back to original count' ); |
44 |
}; |
45 |
|
46 |
subtest 'store' => sub { |
47 |
plan tests => 5; |
48 |
my $limit01 = $builder->build_object( { class => 'Koha::MailDomainLimits' } ); |
49 |
my $limit02 = $builder->build_object( { class => 'Koha::MailDomainLimits' } ); |
50 |
my $value = { domain_limit => 1, belongs_to => $limit01->id }; |
51 |
throws_ok { $limit02->set($value)->store } 'Koha::Exceptions::DomainLimit::MemberWithLimit', |
52 |
'Group member with limit raised exception'; |
53 |
$value = { units => 0, belongs_to => undef }; |
54 |
throws_ok { $limit02->set($value)->store } 'Koha::Exceptions::DomainLimit::EmptyLimitData', |
55 |
'Not possible to store 0 units'; |
56 |
is( $@->parameter, 'units', 'Correct column name passed in exception' ); |
57 |
$value = { domain_limit => undef, units => undef, unit_type => undef, belongs_to => $limit02->id + 1 }; |
58 |
throws_ok { local *STDERR; open STDERR, '>', '/dev/null'; $limit02->set($value)->store } |
59 |
'Koha::Exceptions::Object::FKConstraint', 'Trigger FK constraint for bad group id'; |
60 |
$value = { domain_limit => undef, units => undef, unit_type => undef, belongs_to => $limit02->id }; |
61 |
throws_ok { $limit02->set($value)->store } 'Koha::Exceptions::DomainLimit::NoSelfChaining', |
62 |
'Not possible to store id in belongs_to'; |
63 |
}; |
64 |
|
65 |
subtest 'search_with_group_domain, group_domain' => sub { |
66 |
plan tests => 6; |
67 |
my $limit01 = $builder->build_object( { class => 'Koha::MailDomainLimits' } ); |
68 |
my $value = { domain_limit => undef, units => undef, unit_type => undef, belongs_to => $limit01->id }; |
69 |
my $limit02 = $builder->build_object( { class => 'Koha::MailDomainLimits', value => $value } ); |
70 |
my $limit03 = $builder->build_object( { class => 'Koha::MailDomainLimits' } ); |
71 |
|
72 |
# Check group_domain with and without search_with |
73 |
is( $limit02->_result->has_column_loaded('group_domain'), 0, 'No additional column' ); |
74 |
is( $limit02->group_domain, $limit01->domain_name, 'Check group domain name via relation' ); |
75 |
my $rs_plus = Koha::MailDomainLimits->search_with_group_domain; |
76 |
my $limit02_plus = $rs_plus->find( $limit02->id ); |
77 |
is( $limit02_plus->_result->has_column_loaded('group_domain'), 1, 'Includes additional column' ); |
78 |
is( $limit02_plus->group_domain, $limit01->domain_name, 'Check group domain name from search_with' ); |
79 |
$limit02_plus->belongs_to( $limit03->id )->store->discard_changes; |
80 |
is( $limit02_plus->_result->has_column_loaded('group_domain'), 0, 'Column lost by discarding changes' ); |
81 |
is( |
82 |
$limit02_plus->group_domain, $limit03->domain_name, |
83 |
'Check group domain name again after store (via relation)' |
84 |
); |
85 |
}; |
86 |
|
87 |
$schema->storage->txn_rollback; |