|
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', 'Group member with limit raised exception'; |
| 52 |
$value = { units => 0, belongs_to => undef }; |
| 53 |
throws_ok { $limit02->set($value)->store } 'Koha::Exceptions::DomainLimit::EmptyLimitData', 'Not possible to store 0 units'; |
| 54 |
is( $@->parameter, 'units', 'Correct column name passed in exception' ); |
| 55 |
$value = { domain_limit => undef, units => undef, unit_type => undef, belongs_to => $limit02->id + 1 }; |
| 56 |
throws_ok { local *STDERR; open STDERR, '>', '/dev/null'; $limit02->set($value)->store } 'Koha::Exceptions::Object::FKConstraint', 'Trigger FK constraint for bad group id'; |
| 57 |
$value = { domain_limit => undef, units => undef, unit_type => undef, belongs_to => $limit02->id }; |
| 58 |
throws_ok { $limit02->set($value)->store } 'Koha::Exceptions::DomainLimit::NoSelfChaining', 'Not possible to store id in belongs_to'; |
| 59 |
}; |
| 60 |
|
| 61 |
subtest 'search_with_group_domain, group_domain' => sub { |
| 62 |
plan tests => 6; |
| 63 |
my $limit01 = $builder->build_object( { class => 'Koha::MailDomainLimits' } ); |
| 64 |
my $value = { domain_limit => undef, units => undef, unit_type => undef, belongs_to => $limit01->id }; |
| 65 |
my $limit02 = $builder->build_object( { class => 'Koha::MailDomainLimits', value => $value } ); |
| 66 |
my $limit03 = $builder->build_object( { class => 'Koha::MailDomainLimits' } ); |
| 67 |
|
| 68 |
# Check group_domain with and without search_with |
| 69 |
is( $limit02->_result->has_column_loaded('group_domain'), 0, 'No additional column' ); |
| 70 |
is( $limit02->group_domain, $limit01->domain_name, 'Check group domain name via relation' ); |
| 71 |
my $rs_plus = Koha::MailDomainLimits->search_with_group_domain; |
| 72 |
my $limit02_plus = $rs_plus->find( $limit02->id ); |
| 73 |
is( $limit02_plus->_result->has_column_loaded('group_domain'), 1, 'Includes additional column' ); |
| 74 |
is( $limit02_plus->group_domain, $limit01->domain_name, 'Check group domain name from search_with' ); |
| 75 |
$limit02_plus->belongs_to( $limit03->id )->store->discard_changes; |
| 76 |
is( $limit02_plus->_result->has_column_loaded('group_domain'), 0, 'Column lost by discarding changes' ); |
| 77 |
is( $limit02_plus->group_domain, $limit03->domain_name, 'Check group domain name again after store (via relation)' ); |
| 78 |
}; |
| 79 |
|
| 80 |
$schema->storage->txn_rollback; |