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

(-)a/Koha/Patron/Message/Preference.pm (-10 / +15 lines)
Lines 325-359 sub validate { Link Here
325
        }
325
        }
326
    }
326
    }
327
327
328
    my $attr;
328
    my $attr = Koha::Patron::Message::Attributes->find(
329
    if ($self->days_in_advance || $self->wants_digest) {
329
        $self->message_attribute_id
330
        $attr = Koha::Patron::Message::Attributes->find(
330
    );
331
            $self->message_attribute_id
331
    unless ($attr) {
332
        Koha::Exceptions::BadParameter->throw(
333
            error => 'Message attribute with id '.$self->message_attribute_id
334
            .' not found',
335
            parameter => 'message_attribute_id'
332
        );
336
        );
333
    }
337
    }
334
    if ($self->days_in_advance) {
338
    if (defined $self->days_in_advance) {
335
        if ($attr && $attr->takes_days == 0) {
339
        if ($attr && $attr->takes_days == 0) {
336
            Koha::Exceptions::BadParameter->throw(
340
            Koha::Exceptions::BadParameter->throw(
337
                error => 'days_in_advance cannot be defined for '.
341
                error => 'days_in_advance cannot be defined for '.
338
                $attr->message_name . ' .',
342
                $attr->message_name . '.',
339
                parameter => 'days_in_advance',
343
                parameter => 'days_in_advance',
340
            );
344
            );
341
        }
345
        }
342
        elsif ($self->days_in_advance < 0 || $self->days_in_advance > 30) {
346
        elsif ($self->days_in_advance < 0 || $self->days_in_advance > 30) {
343
            Koha::Exceptions::BadParameter->throw(
347
            Koha::Exceptions::BadParameter->throw(
344
                error => 'days_in_advance has to be a value between 0-30 for '.
348
                error => 'days_in_advance has to be a value between 0-30 for '.
345
                $attr->message_name . ' .',
349
                $attr->message_name . '.',
346
                parameter => 'days_in_advance',
350
                parameter => 'days_in_advance',
347
            );
351
            );
348
        }
352
        }
349
    }
353
    }
350
    if ($self->wants_digest) {
354
    if (defined $self->wants_digest) {
351
        my $transports = Koha::Patron::Message::Transports->search({
355
        my $transports = Koha::Patron::Message::Transports->search({
352
            message_attribute_id => $self->message_attribute_id,
356
            message_attribute_id => $self->message_attribute_id,
353
            is_digest            => 1,
357
            is_digest            => $self->wants_digest,
354
        });
358
        });
355
        Koha::Exceptions::BadParameter->throw(
359
        Koha::Exceptions::BadParameter->throw(
356
            error => 'Digest not available for '.$attr->message_name.' .',
360
            error => (!$self->wants_digest ? 'No d' : 'D').'igest not available '.
361
            'for '.$attr->message_name.'.',
357
            parameter => 'wants_digest',
362
            parameter => 'wants_digest',
358
        ) if $transports->count == 0;
363
        ) if $transports->count == 0;
359
    }
364
    }
(-)a/t/db_dependent/Koha/Patron/Message/Preferences.t (-5 / +51 lines)
Lines 47-52 subtest 'Test Koha::Patron::Message::Preferences' => sub { Link Here
47
    $schema->storage->txn_begin;
47
    $schema->storage->txn_begin;
48
48
49
    my $attribute = build_a_test_attribute();
49
    my $attribute = build_a_test_attribute();
50
    my $letter = build_a_test_letter();
51
    my $mtt = build_a_test_transport_type();
52
    Koha::Patron::Message::Transport->new({
53
        message_attribute_id   => $attribute->message_attribute_id,
54
        message_transport_type => $mtt->message_transport_type,
55
        is_digest              => 0,
56
        letter_module          => $letter->module,
57
        letter_code            => $letter->code,
58
    })->store;
50
59
51
    subtest 'Test for a patron' => sub {
60
    subtest 'Test for a patron' => sub {
52
        plan tests => 3;
61
        plan tests => 3;
Lines 369-375 subtest 'Test adding a new preference with invalid parameters' => sub { Link Here
369
    };
378
    };
370
379
371
    subtest 'Bad parameter' => sub {
380
    subtest 'Bad parameter' => sub {
372
        plan tests => 13;
381
        plan tests => 19;
373
382
374
        $schema->storage->txn_begin;
383
        $schema->storage->txn_begin;
375
384
Lines 417-423 subtest 'Test adding a new preference with invalid parameters' => sub { Link Here
417
            .' was the days_in_advance.');
426
            .' was the days_in_advance.');
418
427
419
        eval { Koha::Patron::Message::Preference->new({
428
        eval { Koha::Patron::Message::Preference->new({
420
                borrowernumber => $patron->{'borrowernumber'},
429
                borrowernumber => $patron->borrowernumber,
421
                message_transport_types => ['nonexistent']
430
                message_transport_types => ['nonexistent']
422
            })->store };
431
            })->store };
423
        is (ref $@, 'Koha::Exceptions::BadParameter',
432
        is (ref $@, 'Koha::Exceptions::BadParameter',
Lines 427-433 subtest 'Test adding a new preference with invalid parameters' => sub { Link Here
427
            .' was the message_transport_type.');
436
            .' was the message_transport_type.');
428
        eval {
437
        eval {
429
            Koha::Patron::Message::Preference->new({
438
            Koha::Patron::Message::Preference->new({
430
                borrowernumber => $patron->{'borrowernumber'},
439
                borrowernumber => $patron->borrowernumber,
431
                message_attribute_id => $attribute->message_attribute_id,
440
                message_attribute_id => $attribute->message_attribute_id,
432
                message_transport_types => ['sms'],
441
                message_transport_types => ['sms'],
433
                wants_digest => 1,
442
                wants_digest => 1,
Lines 440-445 subtest 'Test adding a new preference with invalid parameters' => sub { Link Here
440
        like ($@->error, qr/^Message transport option/, 'Exception s because of given'
449
        like ($@->error, qr/^Message transport option/, 'Exception s because of given'
441
            .' message_transport_type is not a valid option.');
450
            .' message_transport_type is not a valid option.');
442
451
452
        eval {
453
            Koha::Patron::Message::Preference->new({
454
                borrowernumber => $patron->borrowernumber,
455
                message_attribute_id => $attribute->message_attribute_id,
456
                message_transport_types => [],
457
                wants_digest => 1,
458
            })->store };
459
        is (ref $@, 'Koha::Exceptions::BadParameter',
460
            'Adding a message preference with invalid message_transport_type'
461
            .' => Koha::Exceptions::BadParameter');
462
        is ($@->parameter, 'wants_digest', 'The previous exception tells us it'
463
            .' was the wants_digest');
464
        like ($@->error, qr/^Digest not available/, 'Exception s because of given'
465
            .' digest is not available for this transport.');
466
467
        eval {
468
            Koha::Patron::Message::Preference->new({
469
                borrowernumber => $patron->borrowernumber,
470
                message_attribute_id => -1,
471
                message_transport_types => [],
472
            })->store };
473
        is (ref $@, 'Koha::Exceptions::BadParameter',
474
            'Adding a message preference with invalid message_transport_type'
475
            .' => Koha::Exceptions::BadParameter');
476
        is ($@->parameter, 'message_attribute_id', 'The previous exception tells'
477
            .' us it was the message_attribute_id');
478
        like ($@->error, qr/^Message attribute with id -1 not found/, 'Exception '
479
            .' is because of given message attribute id is not found.');
480
443
        $schema->storage->txn_rollback;
481
        $schema->storage->txn_rollback;
444
    };
482
    };
445
483
Lines 449-454 subtest 'Test adding a new preference with invalid parameters' => sub { Link Here
449
        $schema->storage->txn_begin;
487
        $schema->storage->txn_begin;
450
488
451
        my $attribute = build_a_test_attribute();
489
        my $attribute = build_a_test_attribute();
490
        my $letter = build_a_test_letter();
491
        my $mtt = build_a_test_transport_type();
492
        Koha::Patron::Message::Transport->new({
493
            message_attribute_id   => $attribute->message_attribute_id,
494
            message_transport_type => $mtt->message_transport_type,
495
            is_digest              => 0,
496
            letter_module          => $letter->module,
497
            letter_code            => $letter->code,
498
        })->store;
452
        my $patron    = build_a_test_patron();
499
        my $patron    = build_a_test_patron();
453
        my $preference = Koha::Patron::Message::Preference->new({
500
        my $preference = Koha::Patron::Message::Preference->new({
454
            borrowernumber => $patron->borrowernumber,
501
            borrowernumber => $patron->borrowernumber,
Lines 570-576 sub build_a_test_category_preference { Link Here
570
        message_attribute_id => $attr->message_attribute_id,
617
        message_attribute_id => $attr->message_attribute_id,
571
        wants_digest         => $params->{digest} ? 1 : 0,
618
        wants_digest         => $params->{digest} ? 1 : 0,
572
        days_in_advance      => $params->{days_in_advance}
619
        days_in_advance      => $params->{days_in_advance}
573
                                 ? $params->{days_in_advance} : 0,
620
                                 ? $params->{days_in_advance} : undef,
574
    })->store;
621
    })->store;
575
622
576
    Koha::Patron::Message::Transport::Preference->new({
623
    Koha::Patron::Message::Transport::Preference->new({
577
- 

Return to bug 17499