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

(-)a/t/db_dependent/Koha/Patron/Attribute/Types.t (-3 / +161 lines)
Lines 20-26 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::NoWarnings;
22
use Test::NoWarnings;
23
use Test::More tests => 8;
23
use Test::More tests => 9;
24
24
25
use t::lib::TestBuilder;
25
use t::lib::TestBuilder;
26
use t::lib::Mocks;
26
use t::lib::Mocks;
Lines 432-438 subtest 'search_with_library_limits() tests' => sub { Link Here
432
    my $object_code_4 =
432
    my $object_code_4 =
433
        $builder->build_object( { class => 'Koha::Patron::Attribute::Types', value => { code => 'code_4' } } );
433
        $builder->build_object( { class => 'Koha::Patron::Attribute::Types', value => { code => 'code_4' } } );
434
434
435
    is( Koha::Patron::Attribute::Types->search()->count, 4, 'Three objects created' );
435
    is( Koha::Patron::Attribute::Types->search()->count, 4, 'Four objects created' );
436
436
437
    my $branch_1 = $builder->build( { source => 'Branch' } )->{branchcode};
437
    my $branch_1 = $builder->build( { source => 'Branch' } )->{branchcode};
438
    my $branch_2 = $builder->build( { source => 'Branch' } )->{branchcode};
438
    my $branch_2 = $builder->build( { source => 'Branch' } )->{branchcode};
Lines 466-468 subtest 'search_with_library_limits() tests' => sub { Link Here
466
    $schema->storage->txn_rollback;
466
    $schema->storage->txn_rollback;
467
};
467
};
468
468
469
- 
469
subtest 'pattron_attributes_form tests' => sub {
470
471
    plan tests => 12;
472
473
    $schema->storage->txn_begin;
474
475
    my $branch_1 = $builder->build_object( { class => 'Koha::Libraries' } );
476
    my $branch_2 = $builder->build_object( { class => 'Koha::Libraries' } );
477
478
    my $start_count = Koha::Patron::Attribute::Types->search()->count;
479
480
    my $object_code_1 =
481
        $builder->build_object( { class => 'Koha::Patron::Attribute::Types', value => { code => 'code_1' } } );
482
    my $object_code_2 =
483
        $builder->build_object( { class => 'Koha::Patron::Attribute::Types', value => { code => 'code_2' } } );
484
    my $object_code_3 =
485
        $builder->build_object( { class => 'Koha::Patron::Attribute::Types', value => { code => 'code_3' } } );
486
    my $object_code_4 =
487
        $builder->build_object( { class => 'Koha::Patron::Attribute::Types', value => { code => 'code_4' } } );
488
489
    is( Koha::Patron::Attribute::Types->search()->count, $start_count + 4, 'Four objects created' );
490
491
    $object_code_1->library_limits( [ $branch_1->branchcode ] );
492
    $object_code_2->library_limits( [ $branch_2->branchcode ] );
493
    $object_code_3->library_limits( [ $branch_1->branchcode, $branch_2->branchcode ] );
494
495
    my $patron_1 =
496
        $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $branch_1->branchcode } } );
497
    my $patron_2 =
498
        $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $branch_2->branchcode } } );
499
    my $attribute_p1_1 = $builder->build_object(
500
        {
501
            class => 'Koha::Patron::Attributes',
502
            value => {
503
                borrowernumber => $patron_1->borrowernumber,
504
                code           => $object_code_1->code,
505
                attribute      => '1_1',
506
            }
507
        }
508
    );
509
    my $attribute_p1_3 = $builder->build_object(
510
        {
511
            class => 'Koha::Patron::Attributes',
512
            value => {
513
                borrowernumber => $patron_1->borrowernumber,
514
                code           => $object_code_3->code,
515
                attribute      => '1_3',
516
            }
517
        }
518
    );
519
    my $attribute_p1_4 = $builder->build_object(
520
        {
521
            class => 'Koha::Patron::Attributes',
522
            value => {
523
                borrowernumber => $patron_1->borrowernumber,
524
                code           => $object_code_4->code,
525
                attribute      => '1_4',
526
            }
527
        }
528
    );
529
    my $attribute_p2_2 = $builder->build_object(
530
        {
531
            class => 'Koha::Patron::Attributes',
532
            value => {
533
                borrowernumber => $patron_2->borrowernumber,
534
                code           => $object_code_2->code,
535
                attribute      => '2_2',
536
            }
537
        }
538
    );
539
    my $attribute_p2_3 = $builder->build_object(
540
        {
541
            class => 'Koha::Patron::Attributes',
542
            value => {
543
                borrowernumber => $patron_2->borrowernumber,
544
                code           => $object_code_3->code,
545
                attribute      => '2_3',
546
            }
547
        }
548
    );
549
    my $attribute_p2_4 = $builder->build_object(
550
        {
551
            class => 'Koha::Patron::Attributes',
552
            value => {
553
                borrowernumber => $patron_2->borrowernumber,
554
                code           => $object_code_4->code,
555
                attribute      => '2_4',
556
            }
557
        }
558
    );
559
    my $extended_patron_attributes_1 = $patron_1->extended_attributes->unblessed;
560
    my $extended_patron_attributes_2 = $patron_2->extended_attributes->unblessed;
561
562
    my $param_values;
563
    my $C4_template = Test::MockModule->new('C4::Templates');
564
    $C4_template->mock(
565
        'param',
566
        sub {
567
            my ( $self, $param, $values ) = @_;
568
            $param_values = $values;
569
        }
570
    );
571
    my $template = C4::Templates->new( 'intranet', 'intranet', 'about.tt' );
572
    t::lib::Mocks::mock_userenv( { branchcode => $branch_1->branchcode } );
573
    Koha::Patron::Attribute::Types::patron_attributes_form( $template, $extended_patron_attributes_1, 'not_duplicate' );
574
    my $seen_attributes = 0;
575
    foreach my $param_value ( @{$param_values} ) {
576
        if ( $param_value->{items}[0]->{class} eq $object_code_1->class ) {
577
            is( $attribute_p1_1->attribute, $param_value->{items}[0]->{value}, "Got the expected value" );
578
            $seen_attributes++;
579
        }
580
        if ( $param_value->{items}[0]->{class} eq $object_code_3->class ) {
581
            is( $attribute_p1_3->attribute, $param_value->{items}[0]->{value}, "Got the expected value" );
582
            $seen_attributes++;
583
        }
584
        if ( $param_value->{items}[0]->{class} eq $object_code_4->class ) {
585
            is( $attribute_p1_4->attribute, $param_value->{items}[0]->{value}, "Got the expected value" );
586
            $seen_attributes++;
587
        }
588
    }
589
    is( $seen_attributes, 3, "Found all three expected values" );
590
591
    t::lib::Mocks::mock_userenv( { branchcode => $branch_2->branchcode } );
592
    Koha::Patron::Attribute::Types::patron_attributes_form( $template, $extended_patron_attributes_1, 'not_duplicate' );
593
    $seen_attributes = 0;
594
    foreach my $param_value ( @{$param_values} ) {
595
        if ( $param_value->{items}[0]->{class} eq $object_code_1->class ) {
596
            $seen_attributes++;    # We don't expect this one, branch limited
597
        }
598
        if ( $param_value->{items}[0]->{class} eq $object_code_3->class ) {
599
            $seen_attributes++;
600
            is( $attribute_p1_3->attribute, $param_value->{items}[0]->{value}, "Got the expected value" );
601
        }
602
        if ( $param_value->{items}[0]->{class} eq $object_code_4->class ) {
603
            $seen_attributes++;
604
            is( $attribute_p1_4->attribute, $param_value->{items}[0]->{value}, "Got the expected value" );
605
        }
606
    }
607
    is( $seen_attributes, 2, "We get only 2 values when from a different branch" );
608
609
    Koha::Patron::Attribute::Types::patron_attributes_form( $template, $extended_patron_attributes_2, 'not_duplicate' );
610
    $seen_attributes = 0;
611
    foreach my $param_value ( @{$param_values} ) {
612
        if ( $param_value->{items}[0]->{class} eq $object_code_2->class ) {
613
            is( $attribute_p2_2->attribute, $param_value->{items}[0]->{value}, "Got the expected value" );
614
            $seen_attributes++;
615
        }
616
        if ( $param_value->{items}[0]->{class} eq $object_code_3->class ) {
617
            $seen_attributes++;
618
            is( $attribute_p2_3->attribute, $param_value->{items}[0]->{value}, "Got the expected value" );
619
        }
620
        if ( $param_value->{items}[0]->{class} eq $object_code_4->class ) {
621
            $seen_attributes++;
622
            is( $attribute_p2_4->attribute, $param_value->{items}[0]->{value}, "Got the expected value" );
623
        }
624
    }
625
    is( $seen_attributes, 3, "We get all 3 expected values" );
626
627
};

Return to bug 39971