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

(-)a/Koha/Objects.pm (-5 / +12 lines)
Lines 20-25 package Koha::Objects; Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Carp;
22
use Carp;
23
use List::MoreUtils qw( none );
23
24
24
use Koha::Database;
25
use Koha::Database;
25
26
Lines 70-88 sub _new_from_dbic { Link Here
70
71
71
=head3 Koha::Objects->find();
72
=head3 Koha::Objects->find();
72
73
73
my $object = Koha::Objects->find($id);
74
Similar to DBIx::Class::ResultSet->find this method accepts:
74
my $object = Koha::Objects->find( { keypart1 => $keypart1, keypart2 => $keypart2 } );
75
    \%columns_values | @pk_values, { key => $unique_constraint, %attrs }?
76
Strictly speaking, columns_values should only refer to columns under an
77
unique constraint.
78
79
my $object = Koha::Objects->find( { col1 => $val1, col2 => $val2 } );
80
my $object = Koha::Objects->find( $id );
81
my $object = Koha::Objects->find( $idpart1, $idpart2, $attrs ); # composite PK
75
82
76
=cut
83
=cut
77
84
78
sub find {
85
sub find {
79
    my ( $self, $id ) = @_;
86
    my ( $self, @pars ) = @_;
80
87
81
    croak 'Cannot use "->find" in list context' if wantarray;
88
    croak 'Cannot use "->find" in list context' if wantarray;
82
89
83
    return unless defined($id);
90
    return if !@pars || none { defined($_) } @pars;
84
91
85
    my $result = $self->_resultset()->find($id);
92
    my $result = $self->_resultset()->find( @pars );
86
93
87
    return unless $result;
94
    return unless $result;
88
95
(-)a/t/db_dependent/Koha/Objects.t (-7 / +37 lines)
Lines 19-33 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
<<<<<<< HEAD
22
use Test::More tests => 12;
23
use Test::More tests => 10;
24
=======
25
use Test::More tests => 14;
26
>>>>>>> 5a26041... Bug 18539: Forbid list context calls for Koha::Objects->find
27
use Test::Warn;
23
use Test::Warn;
28
24
29
use Koha::Authority::Types;
25
use Koha::Authority::Types;
30
use Koha::Cities;
26
use Koha::Cities;
27
use Koha::IssuingRules;
31
use Koha::Patron::Category;
28
use Koha::Patron::Category;
32
use Koha::Patron::Categories;
29
use Koha::Patron::Categories;
33
use Koha::Patrons;
30
use Koha::Patrons;
Lines 48-60 my $borrowernumber_exists = grep { /^borrowernumber$/ } @columns; Link Here
48
is( $borrowernumber_exists, 1, 'Koha::Objects->columns should return the table columns' );
45
is( $borrowernumber_exists, 1, 'Koha::Objects->columns should return the table columns' );
49
46
50
subtest 'find' => sub {
47
subtest 'find' => sub {
51
    plan tests => 2;
48
    plan tests => 4;
52
    my $patron = $builder->build({source => 'Borrower'});
49
    my $patron = $builder->build({source => 'Borrower'});
53
    my $patron_object = Koha::Patrons->find( $patron->{borrowernumber} );
50
    my $patron_object = Koha::Patrons->find( $patron->{borrowernumber} );
54
    is( $patron_object->borrowernumber, $patron->{borrowernumber}, '->find should return the correct object' );
51
    is( $patron_object->borrowernumber, $patron->{borrowernumber}, '->find should return the correct object' );
55
52
56
    eval { my @patrons = Koha::Patrons->find( $patron->{borrowernumber} ); };
53
    eval { my @patrons = Koha::Patrons->find( $patron->{borrowernumber} ); };
57
    like( $@, qr|^Cannot use "->find" in list context|, "->find should not be called in list context to avoid side-effects" );
54
    like( $@, qr|^Cannot use "->find" in list context|, "->find should not be called in list context to avoid side-effects" );
55
56
    # Test sending undef to find; should not generate a warning
57
    warning_is { $patron = Koha::Patrons->find( undef ); }
58
        "", "Sending undef does not trigger a DBIx warning";
59
    warning_is { $patron = Koha::Patrons->find( undef, undef ); }
60
        "", "Sending two undefs does not trigger a DBIx warning too";
58
};
61
};
59
62
60
subtest 'update' => sub {
63
subtest 'update' => sub {
Lines 112-117 subtest 'new' => sub { Link Here
112
    Koha::Patron::Categories->find($a_cat_code)->delete;
115
    Koha::Patron::Categories->find($a_cat_code)->delete;
113
};
116
};
114
117
118
subtest 'find' => sub {
119
    plan tests => 5;
120
121
    # check find on a single PK
122
    my $patron = $builder->build({ source => 'Borrower' });
123
    is( Koha::Patrons->find($patron->{borrowernumber})->surname,
124
        $patron->{surname}, "Checking an arbitrary patron column after find"
125
    );
126
    # check find with unique column
127
    my $obj = Koha::Patrons->find($patron->{cardnumber}, { key => 'cardnumber' });
128
    is( $obj->borrowernumber, $patron->{borrowernumber},
129
        'Find with unique column and key specified' );
130
    # check find with an additional where clause in the attrs hash
131
    # we do not expect to find something now
132
    is( Koha::Patrons->find(
133
        $patron->{borrowernumber},
134
        { where => { surname => { '!=', $patron->{surname} }}},
135
    ), undef, 'Additional where clause in find call' );
136
137
    # check find with a composite FK
138
    my $rule = $builder->build({ source => 'Issuingrule' });
139
    my @pk = ( $rule->{branchcode}, $rule->{categorycode}, $rule->{itemtype} );
140
    is( ref(Koha::IssuingRules->find(@pk)), "Koha::IssuingRule",
141
        'Find returned a Koha object for composite primary key' );
142
143
    is( Koha::Patrons->find(), undef, 'Find returns undef if no params passed' );
144
};
145
115
subtest 'search_related' => sub {
146
subtest 'search_related' => sub {
116
    plan tests => 8;
147
    plan tests => 8;
117
    my $builder   = t::lib::TestBuilder->new;
148
    my $builder   = t::lib::TestBuilder->new;
118
- 

Return to bug 18361