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

(-)a/Koha/Object.pm (-2 / +2 lines)
Lines 555-562 sub to_api { Link Here
555
555
556
    # coded values handling
556
    # coded values handling
557
    my $avs = {};
557
    my $avs = {};
558
    if ( $params->{av_expand} and $self->can('_fetch_authorised_values') ) {
558
    if ( $params->{av_expand} and $self->can('api_av_mapping') ) {
559
        $avs = $self->_fetch_authorised_values;
559
        $avs = $self->api_av_mapping($params);
560
    }
560
    }
561
561
562
    # Remove forbidden attributes if required (including their coded values)
562
    # Remove forbidden attributes if required (including their coded values)
(-)a/t/db_dependent/Koha/Object.t (-41 / +76 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 22;
20
use Test::More tests => 21;
21
use Test::Exception;
21
use Test::Exception;
22
use Test::Warn;
22
use Test::Warn;
23
use DateTime;
23
use DateTime;
Lines 226-232 subtest 'TO_JSON tests' => sub { Link Here
226
226
227
subtest "to_api() tests" => sub {
227
subtest "to_api() tests" => sub {
228
228
229
    plan tests => 29;
229
    plan tests => 30;
230
230
231
    $schema->storage->txn_begin;
231
    $schema->storage->txn_begin;
232
232
Lines 423-428 subtest "to_api() tests" => sub { Link Here
423
        }
423
        }
424
    };
424
    };
425
425
426
    subtest 'Authorised values expansion' => sub {
427
428
        plan tests => 4;
429
430
        $schema->storage->txn_begin;
431
432
        # new category
433
        my $category = $builder->build_object({ class => 'Koha::AuthorisedValueCategories' });
434
        # add two countries
435
        my $argentina = $builder->build_object(
436
            {   class => 'Koha::AuthorisedValues',
437
                value => { category => $category->category_name, lib => 'AR (Argentina)', lib_opac => 'Argentina' }
438
            }
439
        );
440
        my $france = $builder->build_object(
441
            {   class => 'Koha::AuthorisedValues',
442
                value => { category => $category->category_name, lib => 'FR (France)', lib_opac => 'France' }
443
            }
444
        );
445
446
        my $city_mock = Test::MockModule->new('Koha::City');
447
        $city_mock->mock(
448
            'api_av_mapping',
449
            sub {
450
                my ( $self, $params ) = @_;
451
452
                my $av = Koha::AuthorisedValues->find(
453
                    {
454
                        authorised_value => $self->city_country,
455
                        category         => $category->category_name,
456
                    }
457
                );
458
459
                return {
460
                    city_country => {
461
                        category    => $av->category,
462
                        description => ($params->{public}) ? $av->lib_opac : $av->lib,
463
                    }
464
                };
465
            }
466
        );
467
        $city_mock->mock( 'public_read_list', sub { return [ 'city_id', 'city_country', 'city_name', 'city_state' ] } );
468
469
        my $cordoba = $builder->build_object(
470
            {   class => 'Koha::Cities',
471
                value => { city_country => $argentina->authorised_value, city_name => 'Cordoba' }
472
            }
473
        );
474
        my $marseille = $builder->build_object(
475
            {   class => 'Koha::Cities',
476
                value => { city_country => $france->authorised_value, city_name => 'Marseille' }
477
            }
478
        );
479
480
        my $mobj = $marseille->to_api( { av_expand => 1, public => 1 } );
481
        my $cobj = $cordoba->to_api( { av_expand => 1, public => 0 } );
482
483
        ok( exists $mobj->{_authorised_values}, '_authorised_values exists for Marseille' );
484
        ok( exists $cobj->{_authorised_values}, '_authorised_values exists for Córdoba' );
485
486
        is_deeply(
487
            $mobj->{_authorised_values}->{country},
488
            { category => $category->category_name, description => $france->lib_opac },
489
            'Authorised value for country expanded'
490
        );
491
        is_deeply(
492
            $cobj->{_authorised_values}->{country},
493
            { category => $category->category_name, description => $argentina->lib },
494
            'Authorised value for country expanded'
495
        );
496
497
        $schema->storage->txn_rollback;
498
    };
499
426
    $schema->storage->txn_rollback;
500
    $schema->storage->txn_rollback;
427
};
501
};
428
502
Lines 997-1040 subtest 'messages() and add_message() tests' => sub { Link Here
997
    isnt( $patron->object_messages, undef, '->messages initializes the array if required' );
1071
    isnt( $patron->object_messages, undef, '->messages initializes the array if required' );
998
    is( scalar @{ $patron->object_messages }, 0, '->messages returns an empty arrayref' );
1072
    is( scalar @{ $patron->object_messages }, 0, '->messages returns an empty arrayref' );
999
1073
1000
    $schema->storage->txn_rollback;
1001
};
1002
1003
subtest 'Authorised values expansion' => sub {
1004
    plan tests => 4;
1005
1006
    $schema->storage->txn_begin;
1007
1008
    Koha::AuthorisedValues->search({category => 'Countries'})->delete;
1009
    Koha::AuthorisedValueCategories->search({category_name =>'Countries'})->delete;
1010
1011
    my $cat = $builder->build_object({ class => 'Koha::AuthorisedValueCategories', value => {category_name =>'Countries'} });
1012
    my $fr = $builder->build_object({ class => 'Koha::AuthorisedValues', value => {authorised_value => 'FR', lib=>'France', category=>$cat->category_name} });
1013
    my $us = $builder->build_object({ class => 'Koha::AuthorisedValues', value => {authorised_value => 'US', lib=>'United States of America', category=>$cat->category_name} });
1014
    my $ar = $builder->build_object({ class => 'Koha::AuthorisedValues', value => {authorised_value => 'AR', lib=>'Argentina', category=>$cat->category_name} });
1015
1016
    my $city_class = Test::MockModule->new('Koha::City');
1017
    $city_class->mock( '_fetch_authorised_values',
1018
        sub {
1019
            my ($self) = @_;
1020
            use Koha::AuthorisedValues;
1021
            my $av = Koha::AuthorisedValues->find({authorised_value => $self->city_country, category => 'Countries'});
1022
            return {country => $av->unblessed};
1023
        }
1024
    );
1025
1026
    my $marseille = $builder->build_object({ class => 'Koha::Cities', value => {city_country => 'FR', city_name => 'Marseille'} });
1027
    my $cordoba = $builder->build_object({ class => 'Koha::Cities', value => {city_country => 'AR', city_name => 'Córdoba'} });
1028
1029
    my $mobj = $marseille->to_api({av_expand => 1});
1030
    my $cobj = $cordoba->to_api({av_expand => 1});
1031
1032
    isnt($mobj->{_authorised_values}, undef, '_authorised_values exists for Marseille');
1033
    isnt($cobj->{_authorised_values}, undef, '_authorised_values exists for Córdoba');
1034
1035
    is($mobj->{_authorised_values}->{country}->{lib}, $fr->lib, 'Authorised value for country expanded');
1036
    is($cobj->{_authorised_values}->{country}->{lib}, $ar->lib, 'Authorised value for country expanded');
1037
1038
1039
    $schema->storage->txn_rollback;
1074
    $schema->storage->txn_rollback;
1040
};
1075
};
(-)a/t/db_dependent/Koha/REST/Plugin/Objects.t (-11 / +25 lines)
Lines 670-686 subtest 'objects.find helper with expanded authorised values' => sub { Link Here
670
670
671
    my $city_class = Test::MockModule->new('Koha::City');
671
    my $city_class = Test::MockModule->new('Koha::City');
672
    $city_class->mock(
672
    $city_class->mock(
673
        '_fetch_authorised_values',
673
        'api_av_mapping',
674
        sub {
674
        sub {
675
            my ($self) = @_;
675
            my ($self, $params) = @_;
676
            use Koha::AuthorisedValues;
676
            use Koha::AuthorisedValues;
677
677
            my $av = Koha::AuthorisedValues->find(
678
            my $av = Koha::AuthorisedValues->find(
678
                {
679
                {
679
                    authorised_value => $self->city_country,
680
                    authorised_value => $self->city_country,
680
                    category         => 'Countries'
681
                    category         => 'Countries'
681
                }
682
                }
682
            );
683
            );
683
            return { country => $av->unblessed };
684
685
            return {
686
                city_country => {
687
                    category    => $av->category,
688
                    description => ( $params->{public} ) ? $av->lib_opac : $av->lib,
689
                }
690
            };
684
        }
691
        }
685
    );
692
    );
686
693
Lines 706-712 subtest 'objects.find helper with expanded authorised values' => sub { Link Here
706
    $t->get_ok( '/cities/' . $manuel->cityid => { 'x-koha-av-expand' => 1 } )
713
    $t->get_ok( '/cities/' . $manuel->cityid => { 'x-koha-av-expand' => 1 } )
707
      ->status_is(200)->json_is( '/name' => 'Manuel' )
714
      ->status_is(200)->json_is( '/name' => 'Manuel' )
708
      ->json_has('/_authorised_values')
715
      ->json_has('/_authorised_values')
709
      ->json_is( '/_authorised_values/country/lib' => $ar->lib );
716
      ->json_is( '/_authorised_values/country/description' => $ar->lib );
710
717
711
    $t->get_ok( '/cities/' . $manuel->cityid => { 'x-koha-av-expand' => 0 } )
718
    $t->get_ok( '/cities/' . $manuel->cityid => { 'x-koha-av-expand' => 0 } )
712
      ->status_is(200)->json_is( '/name' => 'Manuel' )
719
      ->status_is(200)->json_is( '/name' => 'Manuel' )
Lines 715-721 subtest 'objects.find helper with expanded authorised values' => sub { Link Here
715
    $t->get_ok( '/cities/' . $manuela->cityid => { 'x-koha-av-expand' => 1 } )
722
    $t->get_ok( '/cities/' . $manuela->cityid => { 'x-koha-av-expand' => 1 } )
716
      ->status_is(200)->json_is( '/name' => 'Manuela' )
723
      ->status_is(200)->json_is( '/name' => 'Manuela' )
717
      ->json_has('/_authorised_values')
724
      ->json_has('/_authorised_values')
718
      ->json_is( '/_authorised_values/country/lib' => $us->lib );
725
      ->json_is( '/_authorised_values/country/description' => $us->lib );
719
726
720
    $schema->storage->txn_rollback;
727
    $schema->storage->txn_rollback;
721
};
728
};
Lines 771-790 subtest 'objects.search helper with expanded authorised values' => sub { Link Here
771
778
772
    my $city_class = Test::MockModule->new('Koha::City');
779
    my $city_class = Test::MockModule->new('Koha::City');
773
    $city_class->mock(
780
    $city_class->mock(
774
        '_fetch_authorised_values',
781
        'api_av_mapping',
775
        sub {
782
        sub {
776
            my ($self) = @_;
783
            my ($self, $params) = @_;
777
            use Koha::AuthorisedValues;
784
            use Koha::AuthorisedValues;
785
778
            my $av = Koha::AuthorisedValues->find(
786
            my $av = Koha::AuthorisedValues->find(
779
                {
787
                {
780
                    authorised_value => $self->city_country,
788
                    authorised_value => $self->city_country,
781
                    category         => 'Countries'
789
                    category         => 'Countries'
782
                }
790
                }
783
            );
791
            );
784
            return { country => $av->unblessed };
792
793
            return {
794
                city_country => {
795
                    category    => $av->category,
796
                    description => ( $params->{public} ) ? $av->lib_opac : $av->lib,
797
                }
798
            };
785
        }
799
        }
786
    );
800
    );
787
801
802
788
    $builder->build_object(
803
    $builder->build_object(
789
        {
804
        {
790
            class => 'Koha::Cities',
805
            class => 'Koha::Cities',
Lines 809-818 subtest 'objects.search helper with expanded authorised values' => sub { Link Here
809
      ->json_has('/0')->json_has('/1')->json_hasnt('/2')
824
      ->json_has('/0')->json_has('/1')->json_hasnt('/2')
810
      ->json_is( '/0/name' => 'Manuel' )
825
      ->json_is( '/0/name' => 'Manuel' )
811
      ->json_has('/0/_authorised_values')
826
      ->json_has('/0/_authorised_values')
812
      ->json_is( '/0/_authorised_values/country/lib' => $ar->lib )
827
      ->json_is( '/0/_authorised_values/country/description' => $ar->lib )
813
      ->json_is( '/1/name' => 'Manuela' )
828
      ->json_is( '/1/name' => 'Manuela' )
814
      ->json_has('/1/_authorised_values')
829
      ->json_has('/1/_authorised_values')
815
      ->json_is( '/1/_authorised_values/country/lib' => $us->lib );
830
      ->json_is( '/1/_authorised_values/country/description' => $us->lib );
816
831
817
    $t->get_ok( '/cities?name=manuel&_per_page=4&_page=1&_match=starts_with' =>
832
    $t->get_ok( '/cities?name=manuel&_per_page=4&_page=1&_match=starts_with' =>
818
          { 'x-koha-av-expand' => 0 } )->status_is(200)
833
          { 'x-koha-av-expand' => 0 } )->status_is(200)
819
- 

Return to bug 26635