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

(-)a/t/db_dependent/Koha/Patron.t (-1 / +175 lines)
Lines 19-26 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 6;
22
use Test::More tests => 7;
23
use Test::Exception;
23
use Test::Exception;
24
use Test::Warn;
24
25
25
use Koha::Database;
26
use Koha::Database;
26
use Koha::DateUtils qw(dt_from_string);
27
use Koha::DateUtils qw(dt_from_string);
Lines 365-367 subtest 'is_superlibrarian() tests' => sub { Link Here
365
366
366
    $schema->storage->txn_rollback;
367
    $schema->storage->txn_rollback;
367
};
368
};
369
370
subtest 'extended_attributes' => sub {
371
    plan tests => 14;
372
    my $schema = Koha::Database->new->schema;
373
    $schema->storage->txn_begin;
374
375
    my $patron_1 = $builder->build_object({class=> 'Koha::Patrons'});
376
    my $patron_2 = $builder->build_object({class=> 'Koha::Patrons'});
377
378
    t::lib::Mocks::mock_userenv({ patron => $patron_1 });
379
380
    my $attribute_type1 = Koha::Patron::Attribute::Type->new(
381
        {
382
            code        => 'my code1',
383
            description => 'my description1',
384
            unique_id   => 1
385
        }
386
    )->store;
387
    my $attribute_type2 = Koha::Patron::Attribute::Type->new(
388
        {
389
            code             => 'my code2',
390
            description      => 'my description2',
391
            opac_display     => 1,
392
            staff_searchable => 1
393
        }
394
    )->store;
395
396
    my $attribute_type3 = $builder->build_object({ class => 'Koha::Patron::Attribute::Types' });
397
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' } );
403
    my $attribute_type_limited = Koha::Patron::Attribute::Type->new(
404
        { code => 'my code3', description => 'my description3' } )->store;
405
    $attribute_type_limited->library_limits( [ $new_library->{branchcode} ] );
406
407
    my $attributes_for_1 = [
408
        {
409
            attribute => 'my attribute1',
410
            code => $attribute_type1->code(),
411
        },
412
        {
413
            attribute => 'my attribute2',
414
            code => $attribute_type2->code(),
415
        },
416
        {
417
            attribute => 'my attribute limited',
418
            code => $attribute_type_limited->code(),
419
        }
420
    ];
421
422
    my $attributes_for_2 = [
423
        {
424
            attribute => 'my attribute12',
425
            code => $attribute_type1->code(),
426
        },
427
        {
428
            attribute => 'my attribute limited 2',
429
            code => $attribute_type_limited->code(),
430
        },
431
        {
432
            attribute => 'my nonexistent attribute 2',
433
            code => $deleted_attribute_type_code,
434
        }
435
    ];
436
437
    my $extended_attributes = $patron_1->extended_attributes;
438
    is( ref($extended_attributes), 'Koha::Patron::Attributes', 'Koha::Patron->extended_attributes must return a Koha::Patron::Attribute set' );
439
    is( $extended_attributes->count, 0, 'There should not be attribute yet');
440
441
    $patron_1->extended_attributes->filter_by_branch_limitations->delete;
442
    $patron_2->extended_attributes->filter_by_branch_limitations->delete;
443
    $patron_1->extended_attributes($attributes_for_1);
444
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
449
    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');
451
452
    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');
454
455
    my $attribute_12 = $extended_attributes_for_2->search({ code => $attribute_type1->code });
456
    is( $attribute_12->next->attribute, 'my attribute12', 'search by code should return the correct attribute' );
457
458
    $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' );
460
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 = [
484
        {
485
            code      => $attribute_type1->code(),
486
            attribute => 'my attribute12 XXX',
487
        },
488
        {
489
            code      => $attribute_type_limited->code(),
490
            attribute => 'my attribute limited 2',
491
        },
492
        {
493
            attribute => 'my attribute 3',
494
            code      => $attribute_type3->code,
495
        },
496
    ];
497
    # Sorting them by code
498
    $expected_attributes_for_2 = [ sort { $a->{code} cmp $b->{code} } @$expected_attributes_for_2 ];
499
500
    is_deeply(
501
        [
502
            {
503
                code      => $extended_attributes_for_2->[0]->{code},
504
                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
            },
510
            {
511
                code      => $extended_attributes_for_2->[2]->{code},
512
                attribute => $extended_attributes_for_2->[2]->{attribute}
513
            },
514
        ],
515
        $expected_attributes_for_2
516
    );
517
518
    # TODO - What about multiple? POD explains the problem
519
    my $non_existent = $patron_2->get_extended_attribute( 'not_exist' );
520
    is( $non_existent, undef, 'Koha::Patron->get_extended_attribute must return undef if the attribute does not exist' );
521
522
    # Test branch limitations
523
    t::lib::Mocks::mock_userenv({ patron => $patron_2 });
524
    # Return all
525
    $extended_attributes_for_1 = $patron_1->extended_attributes;
526
    is( $extended_attributes_for_1->count, 3, 'There should be 2 attributes for patron 1, the limited one should be returned');
527
528
    # Return filtered
529
    $extended_attributes_for_1 = $patron_1->extended_attributes->filter_by_branch_limitations;
530
    is( $extended_attributes_for_1->count, 2, 'There should be 2 attributes for patron 1, the limited one should be returned');
531
532
    # Not filtered
533
    my $limited_value = $patron_1->get_extended_attribute( $attribute_type_limited->code );
534
    is( $limited_value->attribute, 'my attribute limited', );
535
536
    ## Do we need a filtered?
537
    #$limited_value = $patron_1->get_extended_attribute( $attribute_type_limited->code );
538
    #is( $limited_value, undef, );
539
540
    $schema->storage->txn_rollback;
541
};
(-)a/t/db_dependent/Koha/Patrons.t (-175 / +1 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 41;
22
use Test::More tests => 40;
23
use Test::Warn;
23
use Test::Warn;
24
use Test::Exception;
24
use Test::Exception;
25
use Test::MockModule;
25
use Test::MockModule;
Lines 1968-2143 subtest 'anonymize' => sub { Link Here
1968
    is( $patron2->firstname, undef, 'First name patron2 cleared' );
1968
    is( $patron2->firstname, undef, 'First name patron2 cleared' );
1969
};
1969
};
1970
$schema->storage->txn_rollback;
1970
$schema->storage->txn_rollback;
1971
1972
subtest 'extended_attributes' => sub {
1973
    plan tests => 14;
1974
    my $schema = Koha::Database->new->schema;
1975
    $schema->storage->txn_begin;
1976
1977
    my $patron_1 = $builder->build_object({class=> 'Koha::Patrons'});
1978
    my $patron_2 = $builder->build_object({class=> 'Koha::Patrons'});
1979
1980
    t::lib::Mocks::mock_userenv({ patron => $patron_1 });
1981
1982
    my $attribute_type1 = Koha::Patron::Attribute::Type->new(
1983
        {
1984
            code        => 'my code1',
1985
            description => 'my description1',
1986
            unique_id   => 1
1987
        }
1988
    )->store;
1989
    my $attribute_type2 = Koha::Patron::Attribute::Type->new(
1990
        {
1991
            code             => 'my code2',
1992
            description      => 'my description2',
1993
            opac_display     => 1,
1994
            staff_searchable => 1
1995
        }
1996
    )->store;
1997
1998
    my $attribute_type3 = $builder->build_object({ class => 'Koha::Patron::Attribute::Types' });
1999
2000
    my $deleted_attribute_type = $builder->build_object({ class => 'Koha::Patron::Attribute::Types' });
2001
    my $deleted_attribute_type_code = $deleted_attribute_type->code;
2002
    $deleted_attribute_type->delete;
2003
2004
    my $new_library = $builder->build( { source => 'Branch' } );
2005
    my $attribute_type_limited = Koha::Patron::Attribute::Type->new(
2006
        { code => 'my code3', description => 'my description3' } )->store;
2007
    $attribute_type_limited->library_limits( [ $new_library->{branchcode} ] );
2008
2009
    my $attributes_for_1 = [
2010
        {
2011
            attribute => 'my attribute1',
2012
            code => $attribute_type1->code(),
2013
        },
2014
        {
2015
            attribute => 'my attribute2',
2016
            code => $attribute_type2->code(),
2017
        },
2018
        {
2019
            attribute => 'my attribute limited',
2020
            code => $attribute_type_limited->code(),
2021
        }
2022
    ];
2023
2024
    my $attributes_for_2 = [
2025
        {
2026
            attribute => 'my attribute12',
2027
            code => $attribute_type1->code(),
2028
        },
2029
        {
2030
            attribute => 'my attribute limited 2',
2031
            code => $attribute_type_limited->code(),
2032
        },
2033
        {
2034
            attribute => 'my nonexistent attribute 2',
2035
            code => $deleted_attribute_type_code,
2036
        }
2037
    ];
2038
2039
    my $extended_attributes = $patron_1->extended_attributes;
2040
    is( ref($extended_attributes), 'Koha::Patron::Attributes', 'Koha::Patron->extended_attributes must return a Koha::Patron::Attribute set' );
2041
    is( $extended_attributes->count, 0, 'There should not be attribute yet');
2042
2043
    $patron_1->extended_attributes->filter_by_branch_limitations->delete;
2044
    $patron_2->extended_attributes->filter_by_branch_limitations->delete;
2045
    $patron_1->extended_attributes($attributes_for_1);
2046
2047
    warning_like {
2048
        $patron_2->extended_attributes($attributes_for_2);
2049
    } [ qr/a foreign key constraint fails/ ], 'nonexistent attribute should have not exploded but print a warning';
2050
2051
    my $extended_attributes_for_1 = $patron_1->extended_attributes;
2052
    is( $extended_attributes_for_1->count, 3, 'There should be 3 attributes now for patron 1');
2053
2054
    my $extended_attributes_for_2 = $patron_2->extended_attributes;
2055
    is( $extended_attributes_for_2->count, 2, 'There should be 2 attributes now for patron 2');
2056
2057
    my $attribute_12 = $extended_attributes_for_2->search({ code => $attribute_type1->code });
2058
    is( $attribute_12->next->attribute, 'my attribute12', 'search by code should return the correct attribute' );
2059
2060
    $attribute_12 = $patron_2->get_extended_attribute( $attribute_type1->code );
2061
    is( $attribute_12->attribute, 'my attribute12', 'Koha::Patron->get_extended_attribute should return the correct attribute value' );
2062
2063
    warning_is {
2064
        $extended_attributes_for_2 = $patron_2->extended_attributes->merge_with(
2065
            [
2066
                {
2067
                    attribute => 'my attribute12 XXX',
2068
                    code      => $attribute_type1->code(),
2069
                },
2070
                {
2071
                    attribute => 'my nonexistent attribute 2',
2072
                    code      => $deleted_attribute_type_code,
2073
                },
2074
                {
2075
                    attribute => 'my attribute 3', # Adding a new attribute using merge_with
2076
                    code      => $attribute_type3->code,
2077
                },
2078
            ]
2079
        );
2080
    }
2081
    "Cannot merge element: unrecognized code = '$deleted_attribute_type_code'",
2082
    "Trying to merge_with using a nonexistent attribute code should display a warning";
2083
2084
    is( @$extended_attributes_for_2, 3, 'There should be 3 attributes now for patron 3');
2085
    my $expected_attributes_for_2 = [
2086
        {
2087
            code      => $attribute_type1->code(),
2088
            attribute => 'my attribute12 XXX',
2089
        },
2090
        {
2091
            code      => $attribute_type_limited->code(),
2092
            attribute => 'my attribute limited 2',
2093
        },
2094
        {
2095
            attribute => 'my attribute 3',
2096
            code      => $attribute_type3->code,
2097
        },
2098
    ];
2099
    # Sorting them by code
2100
    $expected_attributes_for_2 = [ sort { $a->{code} cmp $b->{code} } @$expected_attributes_for_2 ];
2101
2102
    is_deeply(
2103
        [
2104
            {
2105
                code      => $extended_attributes_for_2->[0]->{code},
2106
                attribute => $extended_attributes_for_2->[0]->{attribute}
2107
            },
2108
            {
2109
                code      => $extended_attributes_for_2->[1]->{code},
2110
                attribute => $extended_attributes_for_2->[1]->{attribute}
2111
            },
2112
            {
2113
                code      => $extended_attributes_for_2->[2]->{code},
2114
                attribute => $extended_attributes_for_2->[2]->{attribute}
2115
            },
2116
        ],
2117
        $expected_attributes_for_2
2118
    );
2119
2120
    # TODO - What about multiple? POD explains the problem
2121
    my $non_existent = $patron_2->get_extended_attribute( 'not_exist' );
2122
    is( $non_existent, undef, 'Koha::Patron->get_extended_attribute must return undef if the attribute does not exist' );
2123
2124
    # Test branch limitations
2125
    t::lib::Mocks::mock_userenv({ patron => $patron_2 });
2126
    # Return all
2127
    $extended_attributes_for_1 = $patron_1->extended_attributes;
2128
    is( $extended_attributes_for_1->count, 3, 'There should be 2 attributes for patron 1, the limited one should be returned');
2129
2130
    # Return filtered
2131
    $extended_attributes_for_1 = $patron_1->extended_attributes->filter_by_branch_limitations;
2132
    is( $extended_attributes_for_1->count, 2, 'There should be 2 attributes for patron 1, the limited one should be returned');
2133
2134
    # Not filtered
2135
    my $limited_value = $patron_1->get_extended_attribute( $attribute_type_limited->code );
2136
    is( $limited_value->attribute, 'my attribute limited', );
2137
2138
    ## Do we need a filtered?
2139
    #$limited_value = $patron_1->get_extended_attribute( $attribute_type_limited->code );
2140
    #is( $limited_value, undef, );
2141
2142
    $schema->storage->txn_rollback;
2143
};
2144
- 

Return to bug 27857