Bug 17932: Unit tests
[koha.git] / t / db_dependent / Koha / Object.t
blobfd5d89b0a3cd219f95b30070d6f0645c8418d340
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 use Test::More tests => 8;
21 use Test::Warn;
23 use C4::Context;
24 use Koha::Database;
25 use Koha::DateUtils qw( dt_from_string );
27 use Scalar::Util qw( isvstring );
29 use t::lib::TestBuilder;
31 BEGIN {
32 use_ok('Koha::Object');
33 use_ok('Koha::Patron');
36 my $schema = Koha::Database->new->schema;
37 $schema->storage->txn_begin;
39 my $builder = t::lib::TestBuilder->new();
41 my $categorycode = $schema->resultset('Category')->first()->categorycode();
42 my $branchcode = $schema->resultset('Branch')->first()->branchcode();
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" );
57 $object->store();
58 is( $object->is_changed(), 0, "Object no longer marked as changed after being stored" );
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" );
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 );
78 my $borrowernumber = $object->borrowernumber;
79 my $patron = $schema->resultset('Borrower')->find( $borrowernumber );
80 is( $patron->surname(), "Test Surname", "Object found in database" );
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" );
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 );
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' );
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;
108 dt_from_string( $patron->dateexpiry ),
109 dt_from_string->truncate( to => 'day' ),
110 'discard_changes should refresh the object'
114 subtest 'TO_JSON tests' => sub {
116 plan tests => 5;
118 my $borrowernumber = $builder->build(
119 { source => 'Borrower',
120 value => { lost => 1,
121 gonenoaddress => 0 } })->{borrowernumber};
123 my $patron = Koha::Patrons->find($borrowernumber);
124 my $lost = $patron->TO_JSON()->{lost};
125 my $gonenoaddress = $patron->TO_JSON->{gonenoaddress};
127 ok( $lost->isa('Mojo::JSON::_Bool'), 'Boolean attribute type is correct' );
128 is( $lost, 1, 'Boolean attribute value is correct (true)' );
130 ok( $gonenoaddress->isa('Mojo::JSON::_Bool'), 'Boolean attribute type is correct' );
131 is( $gonenoaddress, 0, 'Boolean attribute value is correct (false)' );
133 ok( !isvstring($patron->borrowernumber), 'Integer values are not coded as strings' );