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

(-)a/Koha/SearchEngine/Elasticsearch/Search.pm (-2 / +38 lines)
Lines 54-59 use MARC::File::XML; Link Here
54
use MIME::Base64 qw( decode_base64 );
54
use MIME::Base64 qw( decode_base64 );
55
use JSON;
55
use JSON;
56
56
57
use POSIX qw(setlocale LC_COLLATE);
58
use Unicode::Collate::Locale;
59
57
Koha::SearchEngine::Elasticsearch::Search->mk_accessors(qw( store ));
60
Koha::SearchEngine::Elasticsearch::Search->mk_accessors(qw( store ));
58
61
59
=head2 search
62
=head2 search
Lines 453-458 sub max_result_window { Link Here
453
    return $max_result_window;
456
    return $max_result_window;
454
}
457
}
455
458
459
460
=head2 _sort_facets
461
462
    my $facets = _sort_facets($facets);
463
464
Sorts facets using a locale.
465
466
=cut
467
468
sub _sort_facets {
469
    my ( $self, $args ) = @_;
470
    my $facets = $args->{facets};
471
    my $locale = $args->{locale};
472
    if ( !$locale ) {
473
474
        #NOTE: When setlocale is run with only the 1st parameter, it is a "get" not a "set" function.
475
        $locale = setlocale(LC_COLLATE);
476
    }
477
    my $collator = Unicode::Collate::Locale->new( locale => $locale );
478
    if ( $collator && $facets ) {
479
        my @sorted_facets = sort { $collator->cmp( $a->{facet_label_value}, $b->{facet_label_value} ) } @{$facets};
480
        if (@sorted_facets) {
481
            return \@sorted_facets;
482
        }
483
    }
484
485
    #NOTE: If there was a problem, at least return the not sorted facets
486
    return $facets;
487
}
488
489
456
=head2 _convert_facets
490
=head2 _convert_facets
457
491
458
    my $koha_facets = _convert_facets($es_facets);
492
    my $koha_facets = _convert_facets($es_facets);
Lines 532-539 sub _convert_facets { Link Here
532
            };
566
            };
533
        }
567
        }
534
        if ( C4::Context->preference('FacetOrder') eq 'Alphabetical' ) {
568
        if ( C4::Context->preference('FacetOrder') eq 'Alphabetical' ) {
535
            @{ $facet->{facets} } =
569
            my $sorted_facets = $self->_sort_facets( { facets => $facet->{facets} } );
536
                sort { $a->{facet_label_value} cmp $b->{facet_label_value} } @{ $facet->{facets} };
570
            if ($sorted_facets) {
571
                $facet->{facets} = $sorted_facets;
572
            }
537
        }
573
        }
538
        push @facets, $facet if exists $facet->{facets};
574
        push @facets, $facet if exists $facet->{facets};
539
    }
575
    }
(-)a/t/Koha/SearchEngine/Elasticsearch/Search.t (-2 / +68 lines)
Lines 18-31 Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::NoWarnings;
20
use Test::NoWarnings;
21
use Test::More tests => 3;
21
use Test::More tests => 4;
22
use Test::MockModule;
22
use Test::MockModule;
23
use t::lib::Mocks;
23
use t::lib::Mocks;
24
use Encode       qw( encode );
24
use Encode       qw( encode );
25
use MIME::Base64 qw( encode_base64 );
25
use MIME::Base64 qw( encode_base64 );
26
26
27
use utf8;
28
27
use_ok('Koha::SearchEngine::Elasticsearch::Search');
29
use_ok('Koha::SearchEngine::Elasticsearch::Search');
28
30
31
subtest '_sort_facets' => sub {
32
    plan tests => 2;
33
    t::lib::Mocks::mock_preference( 'SearchEngine', 'Elasticsearch' );
34
    my $facets = [
35
        { facet_label_value => 'Mary' },
36
        { facet_label_value => 'Harry' },
37
        { facet_label_value => 'Fairy' },
38
        { facet_label_value => 'Ari' },
39
        { facet_label_value => 'mary' },
40
        { facet_label_value => 'harry' },
41
        { facet_label_value => 'fairy' },
42
        { facet_label_value => 'ari' },
43
        { facet_label_value => 'étienne' },
44
        { facet_label_value => 'Åuthor' },
45
    ];
46
47
    my @normal_sort_facets     = sort { $a->{facet_label_value} cmp $b->{facet_label_value} } @$facets;
48
    my @normal_expected_facets = (
49
        { facet_label_value => 'Ari' },
50
        { facet_label_value => 'Fairy' },
51
        { facet_label_value => 'Harry' },
52
        { facet_label_value => 'Mary' },
53
        { facet_label_value => 'ari' },
54
        { facet_label_value => 'fairy' },
55
        { facet_label_value => 'harry' },
56
        { facet_label_value => 'mary' },
57
        { facet_label_value => 'Åuthor' },
58
        { facet_label_value => 'étienne' },
59
    );
60
61
    #NOTE: stringwise/bytewise is not UTF-8 friendly
62
    is_deeply( \@normal_sort_facets, \@normal_expected_facets, "Perl's built-in sort is stringwise/bytewise." );
63
64
    my $search = Koha::SearchEngine::Elasticsearch::Search->new(
65
        { index => $Koha::SearchEngine::Elasticsearch::AUTHORITIES_INDEX } );
66
67
    #NOTE: The 'default' locale uses the Default Unicode Collation Element Table, which
68
    #is used for the locales of English (en) and French (fr).
69
    my $sorted_facets = $search->_sort_facets( { facets => $facets, locale => 'default' } );
70
    my $expected      = [
71
        { facet_label_value => 'ari' },
72
        { facet_label_value => 'Ari' },
73
        { facet_label_value => 'Åuthor' },
74
        { facet_label_value => 'étienne' },
75
        { facet_label_value => 'fairy' },
76
        { facet_label_value => 'Fairy' },
77
        { facet_label_value => 'harry' },
78
        { facet_label_value => 'Harry' },
79
        { facet_label_value => 'mary' },
80
        { facet_label_value => 'Mary' },
81
    ];
82
    is_deeply( $sorted_facets, $expected, "Facets sorted correctly with default locale" );
83
84
    #NOTE: If "locale" is not provided to _sort_facets, it will look up the LC_COLLATE
85
    #for the local system. This is what allows this function to work well in production.
86
    #However, since LC_COLLATE could vary from system to system running these unit tests,
87
    #we can't test arbitrary locales here.
88
89
    #TODO: It would be great to explicitly use a fi_FI locale for another test, but we cannot guarantee
90
    #that every system has that locale. That said, perhaps we could do some conditional unit
91
    #testing if certain locales are available when running tests. This could allow for the Koha community
92
    #to thoroughly test this functionality, while letting anyone run the unit tests regardless of their
93
    #installed locales.
94
};
95
29
subtest 'search_auth_compat' => sub {
96
subtest 'search_auth_compat' => sub {
30
    plan tests => 7;
97
    plan tests => 7;
31
98
32
- 

Return to bug 36947