View | Details | Raw Unified | Return to bug 17712
Collapse All | Expand All

(-)a/Koha/Biblio/Availability.pm (+132 lines)
Line 0 Link Here
1
package Koha::Biblio::Availability;
2
3
# Copyright Koha-Suomi Oy 2016
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
use Scalar::Util qw(looks_like_number);
22
23
use base qw(Koha::Availability);
24
25
use Koha::Exceptions;
26
use Koha::Exceptions::Biblio;
27
use Koha::Exceptions::Patron;
28
29
=head1 NAME
30
31
Koha::Biblio::Availability - Koha Biblio Availability object class
32
33
=head1 SYNOPSIS
34
35
Parent class for different types of biblio availabilities.
36
37
=head1 DESCRIPTION
38
39
=head2 Class Methods
40
41
This class is for storing biblio availability information. It is a subclass of
42
Koha::Availability. For more documentation on usage, see Koha::Availability.
43
44
=cut
45
46
=head3 new
47
48
my $availability = Koha::Biblio::Availability->new({
49
    biblionumber => 123
50
});
51
52
REQUIRED PARAMETERS:
53
    biblio (Koha::Biblio) / biblionumber
54
55
OPTIONAL PARAMETERS:
56
    patron (Koha::Patron) / borrowernumber
57
58
Creates a new Koha::Biblio::Availability object.
59
60
=cut
61
62
sub new {
63
    my $class = shift;
64
    my ($params) = @_;
65
    my $self = $class->SUPER::new(@_);
66
67
    $self->{'biblio'} = undef;
68
    $self->{'patron'} = undef;
69
70
    # ARRAYref of Koha::Item::Availability objects
71
    # ...for available items
72
    $self->{'item_availabilities'} = [];
73
    # ...for unavailabile items
74
    $self->{'item_unavailabilities'} = [];
75
76
    if (exists $params->{'biblio'}) {
77
        unless (ref($params->{'biblio'}) eq 'Koha::Biblio') {
78
            Koha::Exceptions::BadParameter->throw(
79
                error => 'Parameter must be a Koha::Biblio object.',
80
                parameter => 'biblio',
81
            );
82
        }
83
        $self->biblio($params->{'biblio'});
84
    } elsif (exists $params->{'biblionumber'}) {
85
        unless (looks_like_number($params->{'biblionumber'})) {
86
            Koha::Exceptions::BadParameter->throw(
87
                error => 'Parameter must be a numeric value.',
88
                parameter => 'biblionumber',
89
            );
90
        }
91
        $self->biblio(Koha::Biblios->find($params->{'biblionumber'}));
92
        unless ($self->biblio) {
93
            Koha::Exceptions::Biblio::NotFound->throw(
94
                error => 'Biblio not found.',
95
                biblionumber => $params->{'biblionumber'},
96
            );
97
        }
98
    } else {
99
        Koha::Exceptions::MissingParameter->throw(
100
            error => "Missing one of parameters 'biblionumber, 'biblio'.",
101
            parameter => ["biblionumber", "biblio"],
102
        );
103
    }
104
105
    if (exists $params->{'patron'}) {
106
        unless (ref($params->{'patron'}) eq 'Koha::Patron') {
107
            Koha::Exceptions::BadParameter->throw(
108
                error => 'Parameter must be a Koha::Patron object.',
109
                parameter => 'patron',
110
            );
111
        }
112
        $self->patron($params->{'patron'});
113
    } elsif (exists $params->{'borrowernumber'}) {
114
        unless (looks_like_number($params->{'borrowernumber'})) {
115
            Koha::Exceptions::BadParameter->throw(
116
                error => 'Parameter must be a numeric value.',
117
                parameter => 'borrowernumber',
118
            );
119
        }
120
        $self->patron(Koha::Patrons->find($params->{'borrowernumber'}));
121
        unless ($self->patron) {
122
            Koha::Exceptions::Patron::NotFound->throw(
123
                error => 'Patron not found.',
124
                borrowernumber => $params->{'borrowernumber'},
125
            );
126
        }
127
    }
128
129
    return $self;
130
}
131
132
1;
(-)a/Koha/Item/Availability.pm (+126 lines)
Line 0 Link Here
1
package Koha::Item::Availability;
2
3
# Copyright Koha-Suomi Oy 2016
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
use Scalar::Util qw(looks_like_number);
22
23
use base qw(Koha::Availability);
24
25
use Koha::Exceptions;
26
use Koha::Exceptions::Item;
27
use Koha::Exceptions::Patron;
28
29
=head1 NAME
30
31
Koha::Item::Availability - Koha Item Availability object class
32
33
=head1 SYNOPSIS
34
35
Parent class for different types of item availabilities.
36
37
=head1 DESCRIPTION
38
39
=head2 Class Methods
40
41
This class is for storing item availability information. It is a subclass of
42
Koha::Availability. For more documentation on usage, see Koha::Availability.
43
44
=cut
45
46
=head3 new
47
48
my $availability = Koha::Item::Availability->new({
49
    itemnumber => 123
50
});
51
52
REQUIRED PARAMETERS:
53
    item (Koha::Item) / itemnumber
54
55
OPTIONAL PARAMETERS:
56
    patron (Koha::Patron) / borrowernumber
57
58
Creates a new Koha::Item::Availability object.
59
60
=cut
61
62
sub new {
63
    my $class = shift;
64
    my ($params) = @_;
65
    my $self = $class->SUPER::new(@_);
66
67
    $self->{'item'} = undef;
68
    $self->{'patron'} = undef;
69
70
    if (exists $params->{'item'}) {
71
        unless (ref($params->{'item'}) eq 'Koha::Item') {
72
            Koha::Exceptions::BadParameter->throw(
73
                error => 'Parameter must be a Koha::Item object.',
74
                parameter => 'item',
75
            );
76
        }
77
        $self->item($params->{'item'});
78
    } elsif (exists $params->{'itemnumber'}) {
79
        unless (looks_like_number($params->{'itemnumber'})) {
80
            Koha::Exceptions::BadParameter->throw(
81
                error => 'Parameter must be a numeric value.',
82
                parameter => 'itemnumber',
83
            );
84
        }
85
        $self->item(Koha::Items->find($params->{'itemnumber'}));
86
        unless ($self->item) {
87
            Koha::Exceptions::Item::NotFound->throw(
88
                error => 'Item not found.',
89
                itemnumber => $params->{'itemnumber'},
90
            );
91
        }
92
    } else {
93
        Koha::Exceptions::MissingParameter->throw(
94
            error => "Missing one of parameters 'itemnumber, 'item'.",
95
            parameter => ["itemnumber", "item"],
96
        );
97
    }
98
99
    if (exists $params->{'patron'}) {
100
        unless (ref($params->{'patron'}) eq 'Koha::Patron') {
101
            Koha::Exceptions::BadParameter->throw(
102
                error => 'Parameter must be a Koha::Patron object.',
103
                parameter => 'patron',
104
            );
105
        }
106
        $self->patron($params->{'patron'});
107
    } elsif (exists $params->{'borrowernumber'}) {
108
        unless (looks_like_number($params->{'borrowernumber'})) {
109
            Koha::Exceptions::BadParameter->throw(
110
                error => 'Parameter must be a numeric value.',
111
                parameter => 'borrowernumber',
112
            );
113
        }
114
        $self->patron(Koha::Patrons->find($params->{'borrowernumber'}));
115
        unless ($self->patron) {
116
            Koha::Exceptions::Patron::NotFound->throw(
117
                error => 'Patron not found.',
118
                borrowernumber => $params->{'borrowernumber'},
119
            );
120
        }
121
    }
122
123
    return $self;
124
}
125
126
1;
(-)a/t/db_dependent/Koha/Biblio/Availability.t (+159 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright Koha-Suomi Oy 2016
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 => 14;
22
use t::lib::TestBuilder;
23
24
use Koha::Database;
25
use Koha::Biblios;
26
27
use Koha::Exceptions;
28
29
use_ok('Koha::Biblio::Availability');
30
31
my $schema = Koha::Database->new->schema;
32
$schema->storage->txn_begin;
33
34
my $builder = t::lib::TestBuilder->new;
35
my $biblio = Koha::Biblios->find($builder->build({source => 'Biblio'})->{'biblionumber'});
36
my $patron = Koha::Patrons->find($builder->build({source => 'Borrower'})->{'borrowernumber'});
37
38
subtest 'Check Koha::Biblio::Availability -object default values' => \&check_default_values;
39
40
subtest 'Attempt to instantiate holdability class with valid biblio' => sub {
41
    plan tests => 1;
42
43
    my $availability = Koha::Biblio::Availability->new({ biblio => $biblio });
44
    ok($availability->available, 'When I instantiate class with a valid biblio, then it is available.');
45
};
46
47
subtest 'Attempt to instantiate holdability class with valid biblionumber' => sub {
48
    plan tests => 1;
49
50
    my $availability = Koha::Biblio::Availability->new({ biblionumber => $biblio->biblionumber });
51
    ok($availability->available, 'When I instantiate class with a valid biblionumber, then it is available.');
52
};
53
54
subtest 'Attempt to instantiate holdability class without specifying an biblio' => sub {
55
    plan tests => 2;
56
57
    my $availability = eval{Koha::Biblio::Availability->new};
58
    ok($@, 'When I instantiate class without giving an biblio,'
59
       .' an exception is thrown.');
60
    is(ref($@), 'Koha::Exceptions::MissingParameter',
61
       'Then I see that Koha::Exceptions::MissingParameter is thrown.');
62
};
63
64
subtest 'Attempt to instantiate holdability class with notfound biblionumber' => sub {
65
    plan tests => 2;
66
67
    my $availability = eval{Koha::Biblio::Availability->new({ biblionumber => -9001 })};
68
    ok($@, 'When I instantiate class with valid biblionumber that does not'
69
       .' refer to any stored biblio, an exception is thrown.');
70
    is(ref($@), 'Koha::Exceptions::Biblio::NotFound',
71
       'Then I see that Koha::Exceptions::Biblio::NotFound is thrown.');
72
};
73
74
subtest 'Attempt to instantiate holdability class with invalid biblionumber' => sub {
75
    plan tests => 2;
76
77
    my $availability = eval{Koha::Biblio::Availability->new({ biblionumber => 'lol' })};
78
    ok($@, 'When I instantiate class with invalid biblionumber that is not'
79
       .' a number, an exception is thrown.');
80
    is(ref($@), 'Koha::Exceptions::BadParameter',
81
       'Then I see that Koha::Exceptions::BadParameter is thrown.');
82
};
83
84
subtest 'Attempt to instantiate holdability class with invalid biblio' => sub {
85
    plan tests => 2;
86
87
    my $availability = eval{Koha::Biblio::Availability->new({ biblio => {'nonsense'=>'yeah'} })};
88
    ok($@, 'When I instantiate class with invalid biblio,'
89
       .' an exception is thrown.');
90
    is(ref($@), 'Koha::Exceptions::BadParameter',
91
       'Then I see that Koha::Exceptions::BadParameter is thrown.');
92
};
93
94
subtest 'Attempt to instantiate holdability class with valid patron' => sub {
95
    plan tests => 1;
96
97
    my $availability = Koha::Biblio::Availability->new({ biblio => $biblio, patron => $patron });
98
    ok($availability->available, 'When I instantiate class with a valid patron, then it is available.');
99
};
100
101
subtest 'Attempt to instantiate holdability class with valid borrowernumber' => sub {
102
    plan tests => 1;
103
104
    my $availability = Koha::Biblio::Availability->new({ biblio => $biblio, borrowernumber => $patron->borrowernumber });
105
    ok($availability->available, 'When I instantiate class with a valid borrowernumber, then it is available.');
106
};
107
108
subtest 'Attempt to instantiate holdability class without specifying a patron' => sub {
109
    plan tests => 2;
110
111
    my $availability = eval{Koha::Biblio::Availability->new({ biblio => $biblio })};
112
    is($availability->available, 1, 'When I request availability without specifying patron,'
113
       .' then the biblio is available.');
114
    is(ref($@), '', 'Then holdability can be checked without giving a patron.');
115
};
116
117
subtest 'Attempt to instantiate holdability class with notfound borrowernumber' => sub {
118
    plan tests => 2;
119
120
    my $availability = eval{Koha::Biblio::Availability->new({ biblio => $biblio, borrowernumber => -9001 })};
121
    ok($@, 'When I instantiate class with valid borrowernumber that does not'
122
       .' refer to any stored biblio, an exception is thrown.');
123
    is(ref($@), 'Koha::Exceptions::Patron::NotFound',
124
       'Then I see that Koha::Exceptions::Patron::NotFound is thrown.');
125
};
126
127
subtest 'Attempt to instantiate holdability class with invalid borrowernumber' => sub {
128
    plan tests => 2;
129
130
    my $availability = eval{Koha::Biblio::Availability->new({ biblio => $biblio, borrowernumber => 'lol' })};
131
    ok($@, 'When I instantiate class with invalid borrowernumber that is not'
132
       .' a number, an exception is thrown.');
133
    is(ref($@), 'Koha::Exceptions::BadParameter',
134
       'Then I see that Koha::Exceptions::BadParameter is thrown.');
135
};
136
137
subtest 'Attempt to instantiate holdability class with invalid patron' => sub {
138
    plan tests => 1;
139
140
    my $availability = eval{Koha::Biblio::Availability->new({ biblio => $biblio, patron => $biblio })};
141
    is(ref($@), 'Koha::Exceptions::BadParameter', 'Patron not found');
142
};
143
144
$schema->storage->txn_rollback;
145
146
sub check_default_values {
147
    plan tests => 7;
148
149
    my $availability = Koha::Biblio::Availability->new({ biblio => $biblio});
150
    is($availability->available, 1, 'Koha::Biblio::Availability -object is available.');
151
    is(keys %{$availability->unavailabilities}, 0, 'There are no unavailabilities.');
152
    is(keys %{$availability->confirmations}, 0, 'Nothing needs to be confirmed.');
153
    is(keys %{$availability->notes}, 0, 'There are no additional notes.');
154
    is(ref($availability->biblio), 'Koha::Biblio', 'This availability is related to an biblio.');
155
    is($availability->patron, undef, 'This availability is not related to any patron.');
156
    is($availability->expected_available, undef, 'There is no expectation of future availability');
157
}
158
159
1;
(-)a/t/db_dependent/Koha/Item/Availability.t (-1 / +159 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright Koha-Suomi Oy 2016
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 => 14;
22
use t::lib::TestBuilder;
23
24
use Koha::Database;
25
use Koha::Items;
26
27
use Koha::Exceptions;
28
29
use_ok('Koha::Item::Availability');
30
31
my $schema = Koha::Database->new->schema;
32
$schema->storage->txn_begin;
33
34
my $builder = t::lib::TestBuilder->new;
35
my $item = Koha::Items->find($builder->build({source => 'Item'})->{'itemnumber'});
36
my $patron = Koha::Patrons->find($builder->build({source => 'Borrower'})->{'borrowernumber'});
37
38
subtest 'Check Koha::Item::Availability -object default values' => \&check_default_values;
39
40
subtest 'Attempt to instantiate holdability class with valid item' => sub {
41
    plan tests => 1;
42
43
    my $availability = Koha::Item::Availability->new({ item => $item });
44
    ok($availability->available, 'When I instantiate class with a valid item, then it is available.');
45
};
46
47
subtest 'Attempt to instantiate holdability class with valid itemnumber' => sub {
48
    plan tests => 1;
49
50
    my $availability = Koha::Item::Availability->new({ itemnumber => $item->itemnumber });
51
    ok($availability->available, 'When I instantiate class with a valid itemnumber, then it is available.');
52
};
53
54
subtest 'Attempt to instantiate holdability class without specifying an item' => sub {
55
    plan tests => 2;
56
57
    my $availability = eval{Koha::Item::Availability->new};
58
    ok($@, 'When I instantiate class without giving an item,'
59
       .' an exception is thrown.');
60
    is(ref($@), 'Koha::Exceptions::MissingParameter',
61
       'Then I see that Koha::Exceptions::MissingParameter is thrown.');
62
};
63
64
subtest 'Attempt to instantiate holdability class with notfound itemnumber' => sub {
65
    plan tests => 2;
66
67
    my $availability = eval{Koha::Item::Availability->new({ itemnumber => -9001 })};
68
    ok($@, 'When I instantiate class with valid itemnumber that does not'
69
       .' refer to any stored item, an exception is thrown.');
70
    is(ref($@), 'Koha::Exceptions::Item::NotFound',
71
       'Then I see that Koha::Exceptions::Item::NotFound is thrown.');
72
};
73
74
subtest 'Attempt to instantiate holdability class with invalid itemnumber' => sub {
75
    plan tests => 2;
76
77
    my $availability = eval{Koha::Item::Availability->new({ itemnumber => 'lol' })};
78
    ok($@, 'When I instantiate class with invalid itemnumber that is not'
79
       .' a number, an exception is thrown.');
80
    is(ref($@), 'Koha::Exceptions::BadParameter',
81
       'Then I see that Koha::Exceptions::BadParameter is thrown.');
82
};
83
84
subtest 'Attempt to instantiate holdability class with invalid item' => sub {
85
    plan tests => 2;
86
87
    my $availability = eval{Koha::Item::Availability->new({ item => {'nonsense'=>'yeah'} })};
88
    ok($@, 'When I instantiate class with invalid item,'
89
       .' an exception is thrown.');
90
    is(ref($@), 'Koha::Exceptions::BadParameter',
91
       'Then I see that Koha::Exceptions::BadParameter is thrown.');
92
};
93
94
subtest 'Attempt to instantiate holdability class with valid patron' => sub {
95
    plan tests => 1;
96
97
    my $availability = Koha::Item::Availability->new({ item => $item, patron => $patron });
98
    ok($availability->available, 'When I instantiate class with a valid patron, then it is available.');
99
};
100
101
subtest 'Attempt to instantiate holdability class with valid borrowernumber' => sub {
102
    plan tests => 1;
103
104
    my $availability = Koha::Item::Availability->new({ item => $item, borrowernumber => $patron->borrowernumber });
105
    ok($availability->available, 'When I instantiate class with a valid borrowernumber, then it is available.');
106
};
107
108
subtest 'Attempt to instantiate holdability class without specifying a patron' => sub {
109
    plan tests => 2;
110
111
    my $availability = eval{Koha::Item::Availability->new({ item => $item })};
112
    is($availability->available, 1, 'When I request availability without specifying patron,'
113
       .' then the item is available.');
114
    is(ref($@), '', 'Then holdability can be checked without giving a patron.');
115
};
116
117
subtest 'Attempt to instantiate holdability class with notfound borrowernumber' => sub {
118
    plan tests => 2;
119
120
    my $availability = eval{Koha::Item::Availability->new({ item => $item, borrowernumber => -9001 })};
121
    ok($@, 'When I instantiate class with valid borrowernumber that does not'
122
       .' refer to any stored item, an exception is thrown.');
123
    is(ref($@), 'Koha::Exceptions::Patron::NotFound',
124
       'Then I see that Koha::Exceptions::Patron::NotFound is thrown.');
125
};
126
127
subtest 'Attempt to instantiate holdability class with invalid borrowernumber' => sub {
128
    plan tests => 2;
129
130
    my $availability = eval{Koha::Item::Availability->new({ item => $item, borrowernumber => 'lol' })};
131
    ok($@, 'When I instantiate class with invalid borrowernumber that is not'
132
       .' a number, an exception is thrown.');
133
    is(ref($@), 'Koha::Exceptions::BadParameter',
134
       'Then I see that Koha::Exceptions::BadParameter is thrown.');
135
};
136
137
subtest 'Attempt to instantiate holdability class with invalid patron' => sub {
138
    plan tests => 1;
139
140
    my $availability = eval{Koha::Item::Availability->new({ item => $item, patron => $item })};
141
    is(ref($@), 'Koha::Exceptions::BadParameter', 'Patron not found');
142
};
143
144
$schema->storage->txn_rollback;
145
146
sub check_default_values {
147
    plan tests => 7;
148
149
    my $availability = Koha::Item::Availability->new({ item => $item});
150
    is($availability->available, 1, 'Koha::Item::Availability -object is available.');
151
    is(keys %{$availability->unavailabilities}, 0, 'There are no unavailabilities.');
152
    is(keys %{$availability->confirmations}, 0, 'Nothing needs to be confirmed.');
153
    is(keys %{$availability->notes}, 0, 'There are no additional notes.');
154
    is(ref($availability->item), 'Koha::Item', 'This availability is related to an item.');
155
    is($availability->patron, undef, 'This availability is not related to any patron.');
156
    is($availability->expected_available, undef, 'There is no expectation of future availability');
157
}
158
159
1;

Return to bug 17712