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

(-)a/Koha/Object.pm (-41 / +13 lines)
Lines 1-6 Link Here
1
package Koha::Object;
1
package Koha::Object;
2
2
3
# Copyright ByWater Solutions 2014
3
# Copyright ByWater Solutions 2014
4
# Copyright 2016 Koha Development Team
4
#
5
#
5
# This file is part of Koha.
6
# This file is part of Koha.
6
#
7
#
Lines 109-139 sub store { Link Here
109
    return $self->_result()->update_or_insert() ? $self : undef;
110
    return $self->_result()->update_or_insert() ? $self : undef;
110
}
111
}
111
112
112
=head3 $object->in_storage();
113
114
Returns true if the object has been previously stored.
115
116
=cut
117
118
sub in_storage {
119
    my ($self) = @_;
120
121
    return $self->_result()->in_storage();
122
}
123
124
=head3 $object->is_changed();
125
126
Returns true if the object has properties that are different from
127
the properties of the object in storage.
128
129
=cut
130
131
sub is_changed {
132
    my ( $self, @columns ) = @_;
133
134
    return $self->_result()->is_changed(@columns);
135
}
136
137
=head3 $object->delete();
113
=head3 $object->delete();
138
114
139
Removes the object from storage.
115
Removes the object from storage.
Lines 193-212 sub set { Link Here
193
    return $self->_result()->set_columns($properties) ? $self : undef;
169
    return $self->_result()->set_columns($properties) ? $self : undef;
194
}
170
}
195
171
196
=head3 $object->id();
197
198
Returns the id of the object if it has one.
199
200
=cut
201
202
sub id {
203
    my ($self) = @_;
204
205
    my ( $id ) = $self->_result()->id();
206
207
    return $id;
208
}
209
210
=head3 $object->unblessed();
172
=head3 $object->unblessed();
211
173
212
Returns an unblessed representation of object.
174
Returns an unblessed representation of object.
Lines 275-282 sub AUTOLOAD { Link Here
275
        }
237
        }
276
    }
238
    }
277
239
278
    carp "No method $method!";
240
    my @known_methods = qw( is_changed id in_storage );
279
    return;
241
242
    carp "The method $method is not covered by tests or does not exist!" and return unless grep {/^$method$/} @known_methods;
243
244
    my $r = eval { $self->_result->$method(@_) };
245
    if ( $@ ) {
246
        carp "No method $method found for " . ref($self) . " " . $@;
247
        return
248
    }
249
    return $r;
280
}
250
}
281
251
282
=head3 _type
252
=head3 _type
Lines 294-299 sub DESTROY { } Link Here
294
264
295
Kyle M Hall <kyle@bywatersolutions.com>
265
Kyle M Hall <kyle@bywatersolutions.com>
296
266
267
Jonathan Druart <jonathan.druart@bugs.koha-community.org>
268
297
=cut
269
=cut
298
270
299
1;
271
1;
(-)a/t/db_dependent/Koha/Object.t (+87 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 5;
21
use Test::Warn;
22
23
use C4::Context;
24
use Koha::Database;
25
26
BEGIN {
27
    use_ok('Koha::Object');
28
    use_ok('Koha::Patron');
29
}
30
31
my $schema = Koha::Database->new->schema;
32
$schema->storage->txn_begin;
33
34
my $categorycode = $schema->resultset('Category')->first()->categorycode();
35
my $branchcode = $schema->resultset('Branch')->first()->branchcode();
36
37
subtest 'is_changed' => sub {
38
    plan tests => 6;
39
    my $object = Koha::Patron->new();
40
    $object->categorycode( $categorycode );
41
    $object->branchcode( $branchcode );
42
    $object->surname("Test Surname");
43
    $object->store();
44
    is( $object->is_changed(), 0, "Object is unchanged" );
45
    $object->surname("Test Surname");
46
    is( $object->is_changed(), 0, "Object is still unchanged" );
47
    $object->surname("Test Surname 2");
48
    is( $object->is_changed(), 1, "Object is changed" );
49
50
    $object->store();
51
    is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
52
53
    $object->set({ firstname => 'Test Firstname' });
54
    is( $object->is_changed(), 1, "Object is changed after Set" );
55
    $object->store();
56
    is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
57
};
58
59
subtest 'in_storage' => sub {
60
    plan tests => 6;
61
    my $object = Koha::Patron->new();
62
    is( $object->in_storage, 0, "Object is not in storage" );
63
    $object->categorycode( $categorycode );
64
    $object->branchcode( $branchcode );
65
    $object->surname("Test Surname");
66
    $object->store();
67
    is( $object->in_storage, 1, "Object is now stored" );
68
    $object->surname("another surname");
69
    is( $object->in_storage, 1 );
70
71
    my $borrowernumber = $object->borrowernumber;
72
    my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
73
    is( $patron->surname(), "Test Surname", "Object found in database" );
74
75
    $object->delete();
76
    $patron = $schema->resultset('Borrower')->find( $borrowernumber );
77
    ok( ! $patron, "Object no longer found in database" );
78
    is( $object->in_storage, 0, "Object is not in storage" );
79
};
80
81
subtest 'id' => sub {
82
    plan tests => 1;
83
    my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
84
    is( $patron->id, $patron->borrowernumber );
85
};
86
87
1;
(-)a/t/db_dependent/Patron.t (-73 lines)
Lines 1-72 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 13;
21
use Test::Warn;
22
23
use C4::Context;
24
use Koha::Database;
25
26
BEGIN {
27
    use_ok('Koha::Object');
28
    use_ok('Koha::Patron');
29
}
30
31
my $schema = Koha::Database->new->schema;
32
$schema->storage->txn_begin;
33
34
my $categorycode = $schema->resultset('Category')->first()->categorycode();
35
my $branchcode = $schema->resultset('Branch')->first()->branchcode();
36
37
my $object = Koha::Patron->new();
38
39
is( $object->in_storage, 0, "Object is not in storage" );
40
41
$object->categorycode( $categorycode );
42
$object->branchcode( $branchcode );
43
$object->surname("Test Surname");
44
$object->store();
45
46
is( $object->in_storage, 1, "Object is now stored" );
47
48
my $borrowernumber = $object->borrowernumber;
49
50
my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
51
is( $patron->surname(), "Test Surname", "Object found in database" );
52
53
is( $object->is_changed(), 0, "Object is unchanged" );
54
$object->surname("Test Surname");
55
is( $object->is_changed(), 0, "Object is still unchanged" );
56
$object->surname("Test Surname 2");
57
is( $object->is_changed(), 1, "Object is changed" );
58
59
$object->store();
60
is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
61
62
$object->set({ firstname => 'Test Firstname' });
63
is( $object->is_changed(), 1, "Object is changed after Set" );
64
$object->store();
65
is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
66
67
$object->delete();
68
$patron = $schema->resultset('Borrower')->find( $borrowernumber );
69
ok( ! $patron, "Object no longer found in database" );
70
is( $object->in_storage, 0, "Object is not in storage" );
71
72
1;
73
- 

Return to bug 17226