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

(-)a/t/db_dependent/Koha/Object.t (-44 / +22 lines)
Lines 29-34 use Koha::Database; Link Here
29
use Koha::DateUtils qw( dt_from_string );
29
use Koha::DateUtils qw( dt_from_string );
30
use Koha::Libraries;
30
use Koha::Libraries;
31
use Koha::Patrons;
31
use Koha::Patrons;
32
use Koha::ApiKeys;
32
33
33
use Scalar::Util qw( isvstring );
34
use Scalar::Util qw( isvstring );
34
use Try::Tiny;
35
use Try::Tiny;
Lines 239-259 subtest "Test update method" => sub { Link Here
239
240
240
subtest 'store() tests' => sub {
241
subtest 'store() tests' => sub {
241
242
242
    plan tests => 10;
243
    plan tests => 7;
244
245
    # Using Koha::ApiKey to test Koha::Object>-store
246
    # Simple object with foreign keys and unique key
243
247
244
    $schema->storage->txn_begin;
248
    $schema->storage->txn_begin;
245
249
246
    # Create a category to make sure its ID doesn't exist on the DB
250
    # Create a patron to make sure its ID doesn't exist on the DB
247
    my $category = $builder->build_object({ class => 'Koha::Patron::Categories' });
251
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
248
    my $category_id = $category->id;
252
    my $patron_id = $patron->id;
249
    $category->delete;
253
    $patron->delete;
250
254
251
    my $patron = Koha::Patron->new({ categorycode => $category_id });
255
    my $api_key = Koha::ApiKey->new({ patron_id => $patron_id });
252
256
253
    my $print_error = $schema->storage->dbh->{PrintError};
257
    my $print_error = $schema->storage->dbh->{PrintError};
254
    $schema->storage->dbh->{PrintError} = 0; # FIXME This does not longer work - because of the transaction in Koha::Patron->store?
258
    $schema->storage->dbh->{PrintError} = 0; # FIXME This does not longer work - because of the transaction in Koha::Patron->store?
255
    throws_ok
259
    throws_ok
256
        { $patron->store }
260
        { $api_key->store }
257
        'Koha::Exceptions::Object::FKConstraint',
261
        'Koha::Exceptions::Object::FKConstraint',
258
        'Exception is thrown correctly';
262
        'Exception is thrown correctly';
259
    is(
263
    is(
Lines 263-307 subtest 'store() tests' => sub { Link Here
263
    );
267
    );
264
    is(
268
    is(
265
        $@->broken_fk,
269
        $@->broken_fk,
266
        'categorycode',
270
        'patron_id',
267
        'Exception field is correct'
271
        'Exception field is correct'
268
    );
272
    );
269
273
270
    my $library = $builder->build_object({ class => 'Koha::Libraries' });
274
    $patron = $builder->build_object({ class => 'Koha::Patrons' });
271
    $category   = $builder->build_object({ class => 'Koha::Patron::Categories' });
275
    $api_key = $builder->build_object({ class => 'Koha::ApiKeys' });
272
    $patron     = $builder->build_object({ class => 'Koha::Patrons' });
273
274
    my $new_patron = Koha::Patron->new({
275
        branchcode   => $library->id,
276
        cardnumber   => $patron->cardnumber,
277
        categorycode => $category->id
278
    });
279
280
    throws_ok
281
        { $new_patron->store }
282
        'Koha::Exceptions::Object::DuplicateID',
283
        'Exception is thrown correctly';
284
285
    is(
286
        $@->message,
287
        'Duplicate ID',
288
        'Exception message is correct'
289
    );
290
291
    is(
292
       $@->duplicate_id,
293
       'cardnumber',
294
       'Exception field is correct'
295
    );
296
276
297
    $new_patron = Koha::Patron->new({
277
    my $new_api_key = Koha::ApiKey->new({
298
        branchcode   => $library->id,
278
        patron_id => $patron_id,
299
        userid       => $patron->userid,
279
        secret => $api_key->secret,
300
        categorycode => $category->id
301
    });
280
    });
302
281
303
    throws_ok
282
    throws_ok
304
        { $new_patron->store }
283
        { $new_api_key->store }
305
        'Koha::Exceptions::Object::DuplicateID',
284
        'Koha::Exceptions::Object::DuplicateID',
306
        'Exception is thrown correctly';
285
        'Exception is thrown correctly';
307
286
Lines 313-328 subtest 'store() tests' => sub { Link Here
313
292
314
    is(
293
    is(
315
       $@->duplicate_id,
294
       $@->duplicate_id,
316
       'userid',
295
       'secret',
317
       'Exception field is correct'
296
       'Exception field is correct'
318
    );
297
    );
319
298
320
    $schema->storage->dbh->{PrintError} = $print_error;
299
    $schema->storage->dbh->{PrintError} = $print_error;
321
300
322
    # Successful test
301
    # Successful test
323
    $patron->set({ firstname => 'Manuel' });
302
    $api_key->set({ secret => 'Manuel' });
324
    my $ret = $patron->store;
303
    my $ret = $api_key->store;
325
    is( ref($ret), 'Koha::Patron', 'store() returns the object on success' );
304
    is( ref($ret), 'Koha::ApiKey', 'store() returns the object on success' );
326
305
327
    $schema->storage->txn_rollback;
306
    $schema->storage->txn_rollback;
328
};
307
};
329
- 

Return to bug 20287