Bug 17932: Unit tests
[koha.git] / t / db_dependent / Koha / Object.t
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 => 8;
21 use Test::Warn;
22
23 use C4::Context;
24 use Koha::Database;
25 use Koha::DateUtils qw( dt_from_string );
26
27 use Scalar::Util qw( isvstring );
28
29 use t::lib::TestBuilder;
30
31 BEGIN {
32     use_ok('Koha::Object');
33     use_ok('Koha::Patron');
34 }
35
36 my $schema = Koha::Database->new->schema;
37 $schema->storage->txn_begin;
38
39 my $builder = t::lib::TestBuilder->new();
40
41 my $categorycode = $schema->resultset('Category')->first()->categorycode();
42 my $branchcode = $schema->resultset('Branch')->first()->branchcode();
43
44 subtest 'is_changed' => sub {
45     plan tests => 6;
46     my $object = Koha::Patron->new();
47     $object->categorycode( $categorycode );
48     $object->branchcode( $branchcode );
49     $object->surname("Test Surname");
50     $object->store();
51     is( $object->is_changed(), 0, "Object is unchanged" );
52     $object->surname("Test Surname");
53     is( $object->is_changed(), 0, "Object is still unchanged" );
54     $object->surname("Test Surname 2");
55     is( $object->is_changed(), 1, "Object is changed" );
56
57     $object->store();
58     is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
59
60     $object->set({ firstname => 'Test Firstname' });
61     is( $object->is_changed(), 1, "Object is changed after Set" );
62     $object->store();
63     is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
64 };
65
66 subtest 'in_storage' => sub {
67     plan tests => 6;
68     my $object = Koha::Patron->new();
69     is( $object->in_storage, 0, "Object is not in storage" );
70     $object->categorycode( $categorycode );
71     $object->branchcode( $branchcode );
72     $object->surname("Test Surname");
73     $object->store();
74     is( $object->in_storage, 1, "Object is now stored" );
75     $object->surname("another surname");
76     is( $object->in_storage, 1 );
77
78     my $borrowernumber = $object->borrowernumber;
79     my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
80     is( $patron->surname(), "Test Surname", "Object found in database" );
81
82     $object->delete();
83     $patron = $schema->resultset('Borrower')->find( $borrowernumber );
84     ok( ! $patron, "Object no longer found in database" );
85     is( $object->in_storage, 0, "Object is not in storage" );
86 };
87
88 subtest 'id' => sub {
89     plan tests => 1;
90     my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
91     is( $patron->id, $patron->borrowernumber );
92 };
93
94 subtest 'get_column' => sub {
95     plan tests => 1;
96     my $patron = Koha::Patron->new({categorycode => $categorycode, branchcode => $branchcode })->store;
97     is( $patron->get_column('borrowernumber'), $patron->borrowernumber, 'get_column should retrieve the correct value' );
98 };
99
100 subtest 'discard_changes' => sub {
101     plan tests => 1;
102     my $builder = t::lib::TestBuilder->new;
103     my $patron = $builder->build( { source => 'Borrower' } );
104     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
105     $patron->dateexpiry(dt_from_string);
106     $patron->discard_changes;
107     is(
108         dt_from_string( $patron->dateexpiry ),
109         dt_from_string->truncate( to => 'day' ),
110         'discard_changes should refresh the object'
111     );
112 };
113
114 subtest 'TO_JSON tests' => sub {
115
116     plan tests => 5;
117
118     my $borrowernumber = $builder->build(
119         { source => 'Borrower',
120           value => { lost => 1,
121                      gonenoaddress => 0 } })->{borrowernumber};
122
123     my $patron = Koha::Patrons->find($borrowernumber);
124     my $lost = $patron->TO_JSON()->{lost};
125     my $gonenoaddress = $patron->TO_JSON->{gonenoaddress};
126
127     ok( $lost->isa('Mojo::JSON::_Bool'), 'Boolean attribute type is correct' );
128     is( $lost, 1, 'Boolean attribute value is correct (true)' );
129
130     ok( $gonenoaddress->isa('Mojo::JSON::_Bool'), 'Boolean attribute type is correct' );
131     is( $gonenoaddress, 0, 'Boolean attribute value is correct (false)' );
132
133     ok( !isvstring($patron->borrowernumber), 'Integer values are not coded as strings' );
134 };
135
136
137
138 1;