3 # This file is part of Koha.
5 # Copyright 2014 - Biblibre SARL
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.
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.
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>.
21 use Test
::More tests
=> 41;
24 use_ok
('t::lib::TestBuilder');
28 my $builder = t
::lib
::TestBuilder
->new();
30 is
( $builder->build(), undef, 'build without arguments returns undef' );
32 my @sources = $builder->schema->sources;
33 my @source_in_failure;
34 for my $source (@sources) {
35 eval { $builder->build( { source
=> $source } ); };
36 push @source_in_failure, $source if $@
;
38 is
( @source_in_failure, 0, 'TestBuilder should be able to create an object for every sources' );
39 if ( @source_in_failure ) {
40 diag
("The following sources have not been generated correctly: " . join ', ', @source_in_failure)
43 my $my_overduerules_transport_type = {
44 message_transport_type
=> {
45 message_transport_type
=> 'my msg_t_t',
49 branchcode
=> 'codeB',
50 categorycode
=> 'codeC',
52 categorycode
=> undef,
54 $my_overduerules_transport_type->{categorycode
} = $my_overduerules_transport_type->{branchcode
};
55 my $overduerules_transport_type = $builder->build({
56 source
=> 'OverduerulesTransportType',
57 value
=> $my_overduerules_transport_type,
60 $overduerules_transport_type->{message_transport_type
},
61 $my_overduerules_transport_type->{message_transport_type
}->{message_transport_type
},
62 'build stores the message_transport_type correctly'
65 $overduerules_transport_type->{letternumber
},
66 $my_overduerules_transport_type->{letternumber
},
67 'build stores the letternumber correctly'
70 $overduerules_transport_type->{branchcode
},
71 $my_overduerules_transport_type->{branchcode
}->{branchcode
},
72 'build stores the branchcode correctly'
75 $overduerules_transport_type->{categorycode
},
76 $my_overduerules_transport_type->{categorycode
}->{categorycode
},
77 'build stores the categorycode correctly'
80 $overduerules_transport_type->{_fk
}->{message_transport_type
}->{message_transport_type
},
81 $my_overduerules_transport_type->{message_transport_type
}->{message_transport_type
},
82 'build stores the foreign key message_transport_type correctly'
85 $overduerules_transport_type->{_fk
}->{branchcode
}->{branchcode
},
86 $my_overduerules_transport_type->{branchcode
}->{branchcode
},
87 'build stores the foreign key branchcode correctly'
90 $overduerules_transport_type->{_fk
}->{categorycode
}->{categorycode
},
91 $my_overduerules_transport_type->{categorycode
}->{categorycode
},
92 'build stores the foreign key categorycode correctly'
95 $overduerules_transport_type->{_fk
}->{branchcode
},
96 $overduerules_transport_type->{_fk
}->{categorycode
},
97 'build links the branchcode and the categorycode correctly'
100 $overduerules_transport_type->{_fk
}->{branchcode
}->{letter2
},
102 'build generates values if they are not given'
105 my $my_user_permission = $t::lib
::TestBuilder
::default_value
->{UserPermission
};
106 my $user_permission = $builder->build({
107 source
=> 'UserPermission',
110 $user_permission->{borrowernumber
},
112 'build generates a borrowernumber correctly'
115 $user_permission->{module_bit
},
116 $my_user_permission->{module_bit
}->{module_bit
}->{bit
},
117 'build stores the default value correctly'
120 $user_permission->{code
},
121 $my_user_permission->{module_bit
}->{code
},
122 'build stores the default value correctly'
125 $user_permission->{borrowernumber
},
126 $user_permission->{_fk
}->{borrowernumber
}->{borrowernumber
},
127 'build links the foreign key correctly'
130 $user_permission->{_fk
}->{borrowernumber
}->{surname
},
131 $my_user_permission->{borrowernumber
}->{surname
},
132 'build stores the foreign key value correctly'
135 $user_permission->{_fk
}->{borrowernumber
}->{address
},
136 $my_user_permission->{borrowernumber
}->{address
},
137 'build stores the foreign key value correctly'
140 $user_permission->{_fk
}->{borrowernumber
}->{city
},
141 $my_user_permission->{borrowernumber
}->{city
},
142 'build stores the foreign key value correctly'
145 $user_permission->{_fk
}->{borrowernumber
}->{_fk
}->{branchcode
}->{branchcode
},
146 $my_user_permission->{borrowernumber
}->{branchcode
}->{branchcode
},
147 'build stores the foreign key value correctly'
150 $user_permission->{_fk
}->{borrowernumber
}->{_fk
}->{branchcode
}->{branchname
},
151 $my_user_permission->{borrowernumber
}->{branchcode
}->{branchname
},
152 'build stores the foreign key value correctly'
155 $user_permission->{_fk
}->{borrowernumber
}->{_fk
}->{categorycode
}->{categorycode
},
156 $my_user_permission->{borrowernumber
}->{categorycode
}->{categorycode
},
157 'build stores the foreign key value correctly'
160 $user_permission->{_fk
}->{borrowernumber
}->{_fk
}->{categorycode
}->{hidelostitems
},
161 $my_user_permission->{borrowernumber
}->{categorycode
}->{hidelostitems
},
162 'build stores the foreign key value correctly'
165 $user_permission->{_fk
}->{borrowernumber
}->{_fk
}->{categorycode
}->{category_type
},
166 $my_user_permission->{borrowernumber
}->{categorycode
}->{category_type
},
167 'build stores the foreign key value correctly'
170 $user_permission->{_fk
}->{borrowernumber
}->{_fk
}->{categorycode
}->{defaultprivacy
},
171 $my_user_permission->{borrowernumber
}->{categorycode
}->{defaultprivacy
},
172 'build stores the foreign key value correctly'
175 $user_permission->{_fk
}->{borrowernumber
}->{privacy
},
176 $my_user_permission->{borrowernumber
}->{privacy
},
177 'build stores the foreign key value correctly'
180 $user_permission->{_fk
}->{module_bit
}->{_fk
}->{module_bit
}->{bit
},
181 $my_user_permission->{module_bit
}->{module_bit
}->{bit
},
182 'build stores the foreign key value correctly'
185 $user_permission->{_fk
}->{module_bit
}->{code
},
186 $my_user_permission->{module_bit
}->{code
},
187 'build stores the foreign key value correctly'
190 $user_permission->{_fk
}->{module_bit
},
191 $user_permission->{_fk
}->{code
},
192 'build links the codes correctly'
195 $user_permission->{_fk
}->{borrowernumber
}->{cardnumber
},
197 'build generates values if they are not given'
200 $user_permission->{_fk
}->{borrowernumber
}->{_fk
}->{branchcode
}->{branchaddress1
},
202 'build generates values if they are not given'
205 $user_permission->{_fk
}->{borrowernumber
}->{_fk
}->{categorycode
}->{description
},
207 'build generates values if they are not given'
210 $user_permission->{_fk
}->{module_bit
}->{description
},
212 'build generates values if they are not given'
215 $user_permission->{_fk
}->{module_bit
}->{_fk
}->{module_bit
}->{flag
},
217 'build generates values if they are not given'
221 my $nb_basket = $builder->schema->resultset('Aqbasket')->search();
222 isnt
( $nb_basket, 0, 'add stores the generated entries correctly' );
223 $builder->clear( { source
=> 'Aqbasket' } );
224 $nb_basket = $builder->schema->resultset('Aqbasket')->search();
225 is
( $nb_basket, 0, 'clear removes all the entries correctly' );
228 my $rs_aqbookseller = $builder->schema->resultset('Aqbookseller');
229 my $bookseller = $builder->build({
230 source
=> 'Aqbookseller',
233 delete $bookseller->{_fk
};
234 my $bookseller_from_db = $rs_aqbookseller->find($bookseller);
235 is
( $bookseller_from_db, undef, 'build with only_fk = 1 does not store the entry' );
236 my $bookseller_result = $rs_aqbookseller->create($bookseller);
237 is
( $bookseller_result->in_storage, 1, 'build with only_fk = 1 creates the foreign keys correctly' );
239 $bookseller = $builder->build({
240 source
=> 'Aqbookseller',
242 ok
( length( $bookseller->{phone
} ) <= 30, 'The length for a generated string should not be longer than the size of the DB field' );
243 delete $bookseller->{_fk
};
244 $bookseller_from_db = $rs_aqbookseller->find($bookseller);
245 is
( $bookseller_from_db->in_storage, 1, 'build without the parameter only_sk stores the entry correctly' );
247 $bookseller = $builder->build({
248 source
=> 'Aqbookseller',
251 delete $bookseller->{_fk
};
252 $bookseller_from_db = $rs_aqbookseller->find($bookseller);
253 is
( $bookseller_from_db->in_storage, 1, 'build with only_fk = 0 stores the entry correctly' );