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

(-)a/t/db_dependent/Koha/Patron.t (-52 / +128 lines)
Lines 368-374 subtest 'is_superlibrarian() tests' => sub { Link Here
368
};
368
};
369
369
370
subtest 'extended_attributes' => sub {
370
subtest 'extended_attributes' => sub {
371
371
    plan tests => 14;
372
    plan tests => 14;
373
372
    my $schema = Koha::Database->new->schema;
374
    my $schema = Koha::Database->new->schema;
373
    $schema->storage->txn_begin;
375
    $schema->storage->txn_begin;
374
376
Lines 395-404 subtest 'extended_attributes' => sub { Link Here
395
397
396
    my $attribute_type3 = $builder->build_object({ class => 'Koha::Patron::Attribute::Types' });
398
    my $attribute_type3 = $builder->build_object({ class => 'Koha::Patron::Attribute::Types' });
397
399
398
    my $deleted_attribute_type = $builder->build_object({ class => 'Koha::Patron::Attribute::Types' });
399
    my $deleted_attribute_type_code = $deleted_attribute_type->code;
400
    $deleted_attribute_type->delete;
401
402
    my $new_library = $builder->build( { source => 'Branch' } );
400
    my $new_library = $builder->build( { source => 'Branch' } );
403
    my $attribute_type_limited = Koha::Patron::Attribute::Type->new(
401
    my $attribute_type_limited = Koha::Patron::Attribute::Type->new(
404
        { code => 'my code3', description => 'my description3' } )->store;
402
        { code => 'my code3', description => 'my description3' } )->store;
Lines 427-436 subtest 'extended_attributes' => sub { Link Here
427
        {
425
        {
428
            attribute => 'my attribute limited 2',
426
            attribute => 'my attribute limited 2',
429
            code => $attribute_type_limited->code(),
427
            code => $attribute_type_limited->code(),
430
        },
431
        {
432
            attribute => 'my nonexistent attribute 2',
433
            code => $deleted_attribute_type_code,
434
        }
428
        }
435
    ];
429
    ];
436
430
Lines 441-450 subtest 'extended_attributes' => sub { Link Here
441
    $patron_1->extended_attributes->filter_by_branch_limitations->delete;
435
    $patron_1->extended_attributes->filter_by_branch_limitations->delete;
442
    $patron_2->extended_attributes->filter_by_branch_limitations->delete;
436
    $patron_2->extended_attributes->filter_by_branch_limitations->delete;
443
    $patron_1->extended_attributes($attributes_for_1);
437
    $patron_1->extended_attributes($attributes_for_1);
444
438
    $patron_2->extended_attributes($attributes_for_2);
445
    warning_like {
446
        $patron_2->extended_attributes($attributes_for_2);
447
    } [ qr/a foreign key constraint fails/ ], 'nonexistent attribute should have not exploded but print a warning';
448
439
449
    my $extended_attributes_for_1 = $patron_1->extended_attributes;
440
    my $extended_attributes_for_1 = $patron_1->extended_attributes;
450
    is( $extended_attributes_for_1->count, 3, 'There should be 3 attributes now for patron 1');
441
    is( $extended_attributes_for_1->count, 3, 'There should be 3 attributes now for patron 1');
Lines 452-516 subtest 'extended_attributes' => sub { Link Here
452
    my $extended_attributes_for_2 = $patron_2->extended_attributes;
443
    my $extended_attributes_for_2 = $patron_2->extended_attributes;
453
    is( $extended_attributes_for_2->count, 2, 'There should be 2 attributes now for patron 2');
444
    is( $extended_attributes_for_2->count, 2, 'There should be 2 attributes now for patron 2');
454
445
455
    my $attribute_12 = $extended_attributes_for_2->search({ code => $attribute_type1->code });
446
    my $attribute_12 = $extended_attributes_for_2->search({ code => $attribute_type1->code })->next;
456
    is( $attribute_12->next->attribute, 'my attribute12', 'search by code should return the correct attribute' );
447
    is( $attribute_12->attribute, 'my attribute12', 'search by code should return the correct attribute' );
457
448
458
    $attribute_12 = $patron_2->get_extended_attribute( $attribute_type1->code );
449
    $attribute_12 = $patron_2->get_extended_attribute( $attribute_type1->code );
459
    is( $attribute_12->attribute, 'my attribute12', 'Koha::Patron->get_extended_attribute should return the correct attribute value' );
450
    is( $attribute_12->attribute, 'my attribute12', 'Koha::Patron->get_extended_attribute should return the correct attribute value' );
460
451
461
    warning_is {
462
        $extended_attributes_for_2 = $patron_2->extended_attributes->merge_with(
463
            [
464
                {
465
                    attribute => 'my attribute12 XXX',
466
                    code      => $attribute_type1->code(),
467
                },
468
                {
469
                    attribute => 'my nonexistent attribute 2',
470
                    code      => $deleted_attribute_type_code,
471
                },
472
                {
473
                    attribute => 'my attribute 3', # Adding a new attribute using merge_with
474
                    code      => $attribute_type3->code,
475
                },
476
            ]
477
        );
478
    }
479
    "Cannot merge element: unrecognized code = '$deleted_attribute_type_code'",
480
    "Trying to merge_with using a nonexistent attribute code should display a warning";
481
482
    is( @$extended_attributes_for_2, 3, 'There should be 3 attributes now for patron 3');
483
    my $expected_attributes_for_2 = [
452
    my $expected_attributes_for_2 = [
484
        {
453
        {
485
            code      => $attribute_type1->code(),
454
            code      => $attribute_type1->code(),
486
            attribute => 'my attribute12 XXX',
455
            attribute => 'my attribute12',
487
        },
456
        },
488
        {
457
        {
489
            code      => $attribute_type_limited->code(),
458
            code      => $attribute_type_limited->code(),
490
            attribute => 'my attribute limited 2',
459
            attribute => 'my attribute limited 2',
491
        },
460
        }
492
        {
493
            attribute => 'my attribute 3',
494
            code      => $attribute_type3->code,
495
        },
496
    ];
461
    ];
497
    # Sorting them by code
462
    # Sorting them by code
498
    $expected_attributes_for_2 = [ sort { $a->{code} cmp $b->{code} } @$expected_attributes_for_2 ];
463
    $expected_attributes_for_2 = [ sort { $a->{code} cmp $b->{code} } @$expected_attributes_for_2 ];
464
    my @extended_attributes_for_2 = $extended_attributes_for_2->as_list;
499
465
500
    is_deeply(
466
    is_deeply(
501
        [
467
        [
502
            {
468
            {
503
                code      => $extended_attributes_for_2->[0]->{code},
469
                code      => $extended_attributes_for_2[0]->code,
504
                attribute => $extended_attributes_for_2->[0]->{attribute}
470
                attribute => $extended_attributes_for_2[0]->attribute
505
            },
506
            {
507
                code      => $extended_attributes_for_2->[1]->{code},
508
                attribute => $extended_attributes_for_2->[1]->{attribute}
509
            },
471
            },
510
            {
472
            {
511
                code      => $extended_attributes_for_2->[2]->{code},
473
                code      => $extended_attributes_for_2[1]->code,
512
                attribute => $extended_attributes_for_2->[2]->{attribute}
474
                attribute => $extended_attributes_for_2[1]->attribute
513
            },
475
            }
514
        ],
476
        ],
515
        $expected_attributes_for_2
477
        $expected_attributes_for_2
516
    );
478
    );
Lines 537-541 subtest 'extended_attributes' => sub { Link Here
537
    #$limited_value = $patron_1->get_extended_attribute( $attribute_type_limited->code );
499
    #$limited_value = $patron_1->get_extended_attribute( $attribute_type_limited->code );
538
    #is( $limited_value, undef, );
500
    #is( $limited_value, undef, );
539
501
502
    subtest 'non-repeatable attributes tests' => sub {
503
504
        plan tests => 3;
505
506
        my $patron = $builder->build_object({ class => 'Koha::Patrons' });
507
        my $attribute_type = $builder->build_object(
508
            {
509
                class => 'Koha::Patron::Attribute::Types',
510
                value => { repeatable => 0 }
511
            }
512
        );
513
514
        is( $patron->extended_attributes->count, 0, 'Patron has no extended attributes' );
515
516
        throws_ok
517
            {
518
                $patron->extended_attributes(
519
                    [
520
                        { code => $attribute_type->code, attribute => 'a' },
521
                        { code => $attribute_type->code, attribute => 'b' }
522
                    ]
523
                );
524
            }
525
            'Koha::Exceptions::Patron::Attribute::NonRepeatable',
526
            'Exception thrown on non-repeatable attribute';
527
528
        is( $patron->extended_attributes->count, 0, 'Extended attributes storing rolled back' );
529
    };
530
531
    subtest 'unique attributes tests' => sub {
532
533
        plan tests => 5;
534
535
        my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
536
        my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
537
538
        my $attribute_type_1 = $builder->build_object(
539
            {
540
                class => 'Koha::Patron::Attribute::Types',
541
                value => { unique => 1 }
542
            }
543
        );
544
545
        my $attribute_type_2 = $builder->build_object(
546
            {
547
                class => 'Koha::Patron::Attribute::Types',
548
                value => { unique => 0 }
549
            }
550
        );
551
552
        is( $patron_1->extended_attributes->count, 0, 'patron_1 has no extended attributes' );
553
        is( $patron_2->extended_attributes->count, 0, 'patron_2 has no extended attributes' );
554
555
        $patron_1->extended_attributes(
556
            [
557
                { code => $attribute_type_1->code, attribute => 'a' },
558
                { code => $attribute_type_2->code, attribute => 'a' }
559
            ]
560
        );
561
562
        throws_ok
563
            {
564
                $patron_2->extended_attributes(
565
                    [
566
                        { code => $attribute_type_1->code, attribute => 'a' },
567
                        { code => $attribute_type_2->code, attribute => 'a' }
568
                    ]
569
                );
570
            }
571
            'Koha::Exceptions::Patron::Attribute::UniqueIDConstraint',
572
            'Exception thrown on unique attribute';
573
574
        is( $patron_1->extended_attributes->count, 2, 'Extended attributes stored' );
575
        is( $patron_2->extended_attributes->count, 0, 'Extended attributes storing rolled back' );
576
    };
577
578
    subtest 'invalid type attributes tests' => sub {
579
580
        plan tests => 3;
581
582
        my $patron = $builder->build_object({ class => 'Koha::Patrons' });
583
584
        my $attribute_type_1 = $builder->build_object(
585
            {
586
                class => 'Koha::Patron::Attribute::Types',
587
                value => { repeatable => 0 }
588
            }
589
        );
590
591
        my $attribute_type_2 = $builder->build_object(
592
            {
593
                class => 'Koha::Patron::Attribute::Types'
594
            }
595
        );
596
597
        my $type_2 = $attribute_type_2->code;
598
        $attribute_type_2->delete;
599
600
        is( $patron->extended_attributes->count, 0, 'Patron has no extended attributes' );
601
602
        throws_ok
603
            {
604
                $patron->extended_attributes(
605
                    [
606
                        { code => $attribute_type_1->code, attribute => 'a' },
607
                        { code => $attribute_type_2->code, attribute => 'b' }
608
                    ]
609
                );
610
            }
611
            'Koha::Exceptions::Patron::Attribute::InvalidType',
612
            'Exception thrown on invalid attribute type';
613
614
        is( $patron->extended_attributes->count, 0, 'Extended attributes storing rolled back' );
615
    };
616
540
    $schema->storage->txn_rollback;
617
    $schema->storage->txn_rollback;
541
};
618
};
542
- 

Return to bug 27857