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

(-)a/Koha/Object.pm (-3 / +3 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)
Lines 597-603 sub to_api { Link Here
597
        }
597
        }
598
    }
598
    }
599
599
600
    $json_object->{_authorised_values} = $avs
600
    $json_object->{_str} = $avs
601
      if $params->{av_expand};
601
      if $params->{av_expand};
602
602
603
    # Make sure we duplicate the $params variable to avoid
603
    # Make sure we duplicate the $params variable to avoid
(-)a/t/db_dependent/Koha/Object.t (-41 / +93 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 => {
438
                    category => $category->category_name,
439
                    lib      => 'AR (Argentina)',
440
                    lib_opac => 'Argentina',
441
                }
442
            }
443
        );
444
        my $france = $builder->build_object(
445
            {   class => 'Koha::AuthorisedValues',
446
                value => {
447
                    category => $category->category_name,
448
                    lib      => 'FR (France)',
449
                    lib_opac => 'France',
450
                }
451
            }
452
        );
453
454
        my $city_mock = Test::MockModule->new('Koha::City');
455
        $city_mock->mock(
456
            'api_av_mapping',
457
            sub {
458
                my ( $self, $params ) = @_;
459
460
                my $av = Koha::AuthorisedValues->find(
461
                    {
462
                        authorised_value => $self->city_country,
463
                        category         => $category->category_name,
464
                    }
465
                );
466
467
                return {
468
                    city_country => {
469
                        category => $av->category,
470
                        str      => ( $params->{public} ) ? $av->lib_opac : $av->lib,
471
                        type     => 'av',
472
                    }
473
                };
474
            }
475
        );
476
        $city_mock->mock( 'public_read_list', sub { return [ 'city_id', 'city_country', 'city_name', 'city_state' ] } );
477
478
        my $cordoba = $builder->build_object(
479
            {   class => 'Koha::Cities',
480
                value => { city_country => $argentina->authorised_value, city_name => 'Cordoba' }
481
            }
482
        );
483
        my $marseille = $builder->build_object(
484
            {   class => 'Koha::Cities',
485
                value => { city_country => $france->authorised_value, city_name => 'Marseille' }
486
            }
487
        );
488
489
        my $mobj = $marseille->to_api( { av_expand => 1, public => 1 } );
490
        my $cobj = $cordoba->to_api( { av_expand => 1, public => 0 } );
491
492
        ok( exists $mobj->{_str}, '_str exists for Marseille' );
493
        ok( exists $cobj->{_str}, '_str exists for Córdoba' );
494
495
        is_deeply(
496
            $mobj->{_str}->{country},
497
            {
498
                category => $category->category_name,
499
                str      => $france->lib_opac,
500
                type     => 'av',
501
            },
502
            'Authorised value for country expanded'
503
        );
504
        is_deeply(
505
            $cobj->{_str}->{country},
506
            {
507
                category => $category->category_name,
508
                str      => $argentina->lib,
509
                type     => 'av'
510
            },
511
            'Authorised value for country expanded'
512
        );
513
514
        $schema->storage->txn_rollback;
515
    };
516
426
    $schema->storage->txn_rollback;
517
    $schema->storage->txn_rollback;
427
};
518
};
428
519
Lines 997-1040 subtest 'messages() and add_message() tests' => sub { Link Here
997
    isnt( $patron->object_messages, undef, '->messages initializes the array if required' );
1088
    isnt( $patron->object_messages, undef, '->messages initializes the array if required' );
998
    is( scalar @{ $patron->object_messages }, 0, '->messages returns an empty arrayref' );
1089
    is( scalar @{ $patron->object_messages }, 0, '->messages returns an empty arrayref' );
999
1090
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;
1091
    $schema->storage->txn_rollback;
1040
};
1092
};
(-)a/t/db_dependent/Koha/REST/Plugin/Objects.t (-23 / +48 lines)
Lines 621-627 subtest 'objects.search helper, search_limited() tests' => sub { Link Here
621
};
621
};
622
622
623
subtest 'objects.find helper with expanded authorised values' => sub {
623
subtest 'objects.find helper with expanded authorised values' => sub {
624
    plan tests => 14;
624
625
    plan tests => 18;
625
626
626
    $schema->storage->txn_begin;
627
    $schema->storage->txn_begin;
627
628
Lines 670-686 subtest 'objects.find helper with expanded authorised values' => sub { Link Here
670
671
671
    my $city_class = Test::MockModule->new('Koha::City');
672
    my $city_class = Test::MockModule->new('Koha::City');
672
    $city_class->mock(
673
    $city_class->mock(
673
        '_fetch_authorised_values',
674
        'api_av_mapping',
674
        sub {
675
        sub {
675
            my ($self) = @_;
676
            my ($self, $params) = @_;
676
            use Koha::AuthorisedValues;
677
            use Koha::AuthorisedValues;
678
677
            my $av = Koha::AuthorisedValues->find(
679
            my $av = Koha::AuthorisedValues->find(
678
                {
680
                {
679
                    authorised_value => $self->city_country,
681
                    authorised_value => $self->city_country,
680
                    category         => 'Countries'
682
                    category         => 'Countries'
681
                }
683
                }
682
            );
684
            );
683
            return { country => $av->unblessed };
685
686
            return {
687
                city_country => {
688
                    category => $av->category,
689
                    str      => ( $params->{public} ) ? $av->lib_opac : $av->lib,
690
                    type     => 'av',
691
                }
692
            };
684
        }
693
        }
685
    );
694
    );
686
695
Lines 703-728 subtest 'objects.find helper with expanded authorised values' => sub { Link Here
703
        }
712
        }
704
    );
713
    );
705
714
706
    $t->get_ok( '/cities/' . $manuel->cityid => { 'x-koha-av-expand' => 1 } )
715
    $t->get_ok( '/cities/' . $manuel->id => { 'x-koha-av-expand' => 1 } )
707
      ->status_is(200)->json_is( '/name' => 'Manuel' )
716
      ->status_is(200)->json_is( '/name' => 'Manuel' )
708
      ->json_has('/_authorised_values')
717
      ->json_has('/_str')
709
      ->json_is( '/_authorised_values/country/lib' => $ar->lib );
718
      ->json_is( '/_str/country/type'     => 'av' )
719
      ->json_is( '/_str/country/category' => $cat->category_name )
720
      ->json_is( '/_str/country/str'      => $ar->lib );
710
721
711
    $t->get_ok( '/cities/' . $manuel->cityid => { 'x-koha-av-expand' => 0 } )
722
    $t->get_ok( '/cities/' . $manuel->id => { 'x-koha-av-expand' => 0 } )
712
      ->status_is(200)->json_is( '/name' => 'Manuel' )
723
      ->status_is(200)->json_is( '/name' => 'Manuel' )
713
      ->json_hasnt('/_authorised_values');
724
      ->json_hasnt('/_str');
714
725
715
    $t->get_ok( '/cities/' . $manuela->cityid => { 'x-koha-av-expand' => 1 } )
726
    $t->get_ok( '/cities/' . $manuela->id => { 'x-koha-av-expand' => 1 } )
716
      ->status_is(200)->json_is( '/name' => 'Manuela' )
727
      ->status_is(200)->json_is( '/name' => 'Manuela' )
717
      ->json_has('/_authorised_values')
728
      ->json_has('/_str')
718
      ->json_is( '/_authorised_values/country/lib' => $us->lib );
729
      ->json_is( '/_str/country/type'     => 'av' )
730
      ->json_is( '/_str/country/category' => $cat->category_name )
731
      ->json_is( '/_str/country/str'      => $us->lib );
719
732
720
    $schema->storage->txn_rollback;
733
    $schema->storage->txn_rollback;
721
};
734
};
722
735
723
subtest 'objects.search helper with expanded authorised values' => sub {
736
subtest 'objects.search helper with expanded authorised values' => sub {
724
737
725
    plan tests => 20;
738
    plan tests => 24;
726
739
727
    my $t = Test::Mojo->new;
740
    my $t = Test::Mojo->new;
728
741
Lines 771-790 subtest 'objects.search helper with expanded authorised values' => sub { Link Here
771
784
772
    my $city_class = Test::MockModule->new('Koha::City');
785
    my $city_class = Test::MockModule->new('Koha::City');
773
    $city_class->mock(
786
    $city_class->mock(
774
        '_fetch_authorised_values',
787
        'api_av_mapping',
775
        sub {
788
        sub {
776
            my ($self) = @_;
789
            my ($self, $params) = @_;
777
            use Koha::AuthorisedValues;
790
            use Koha::AuthorisedValues;
791
778
            my $av = Koha::AuthorisedValues->find(
792
            my $av = Koha::AuthorisedValues->find(
779
                {
793
                {
780
                    authorised_value => $self->city_country,
794
                    authorised_value => $self->city_country,
781
                    category         => 'Countries'
795
                    category         => 'Countries'
782
                }
796
                }
783
            );
797
            );
784
            return { country => $av->unblessed };
798
799
            return {
800
                city_country => {
801
                    category => $av->category,
802
                    str      => ( $params->{public} ) ? $av->lib_opac : $av->lib,
803
                    type     => 'av',
804
                }
805
            };
785
        }
806
        }
786
    );
807
    );
787
808
809
788
    $builder->build_object(
810
    $builder->build_object(
789
        {
811
        {
790
            class => 'Koha::Cities',
812
            class => 'Koha::Cities',
Lines 808-824 subtest 'objects.search helper with expanded authorised values' => sub { Link Here
808
          { 'x-koha-av-expand' => 1 } )->status_is(200)
830
          { 'x-koha-av-expand' => 1 } )->status_is(200)
809
      ->json_has('/0')->json_has('/1')->json_hasnt('/2')
831
      ->json_has('/0')->json_has('/1')->json_hasnt('/2')
810
      ->json_is( '/0/name' => 'Manuel' )
832
      ->json_is( '/0/name' => 'Manuel' )
811
      ->json_has('/0/_authorised_values')
833
      ->json_has('/0/_str')
812
      ->json_is( '/0/_authorised_values/country/lib' => $ar->lib )
834
      ->json_is( '/0/_str/country/str'      => $ar->lib )
835
      ->json_is( '/0/_str/country/type'     => 'av' )
836
      ->json_is( '/0/_str/country/category' => $cat->category_name )
813
      ->json_is( '/1/name' => 'Manuela' )
837
      ->json_is( '/1/name' => 'Manuela' )
814
      ->json_has('/1/_authorised_values')
838
      ->json_has('/1/_str')
815
      ->json_is( '/1/_authorised_values/country/lib' => $us->lib );
839
      ->json_is( '/1/_str/country/str' => $us->lib )
840
      ->json_is( '/1/_str/country/type'     => 'av' )
841
      ->json_is( '/1/_str/country/category' => $cat->category_name );
816
842
817
    $t->get_ok( '/cities?name=manuel&_per_page=4&_page=1&_match=starts_with' =>
843
    $t->get_ok( '/cities?name=manuel&_per_page=4&_page=1&_match=starts_with' =>
818
          { 'x-koha-av-expand' => 0 } )->status_is(200)
844
          { 'x-koha-av-expand' => 0 } )->status_is(200)
819
      ->json_has('/0')->json_has('/1')->json_hasnt('/2')
845
      ->json_has('/0')->json_has('/1')->json_hasnt('/2')
820
      ->json_is( '/0/name' => 'Manuel' )->json_hasnt('/0/_authorised_values')
846
      ->json_is( '/0/name' => 'Manuel' )->json_hasnt('/0/_str')
821
      ->json_is( '/1/name' => 'Manuela' )->json_hasnt('/1/_authorised_values');
847
      ->json_is( '/1/name' => 'Manuela' )->json_hasnt('/1/_str');
822
848
823
849
824
    $schema->storage->txn_rollback;
850
    $schema->storage->txn_rollback;
825
- 

Return to bug 26635