Bug 29754: Unit test
[koha.git] / t / db_dependent / www / search_utf8.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use utf8;
21 use Test::More; #See plan tests => \d+ below
22 use Test::WWW::Mechanize;
23 use Data::Dumper;
24 use XML::Simple;
25 use File::Basename qw(dirname );
26 use POSIX;
27 use Encode;
28 use URI::Escape;
29
30 use C4::Context;
31
32 use t::lib::Mocks::Zebra;
33
34 my $testdir = File::Spec->rel2abs( dirname(__FILE__) );
35 # global variables that will be used when forking
36
37 my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21';
38
39 my $file1 =
40   $marcflavour eq 'UNIMARC'
41   ? "$testdir/data/unimarcutf8record.mrc"
42   : "$testdir/data/marc21utf8record.mrc";
43
44 my $file2 =
45   $marcflavour eq 'UNIMARC'
46   ? "$testdir/data/unimarclatin1utf8rec.mrc"
47   : "$testdir/data/marc21latin1utf8rec.mrc";
48
49 my $file3 =
50   $marcflavour eq 'UNIMARC'
51   ? "$testdir/data/unimarcutf8supprec.mrc"
52   : "$testdir/data/marc21utf8supprec.mrc";
53
54 our $intranet = $ENV{KOHA_INTRANET_URL};
55 our $opac     = $ENV{KOHA_OPAC_URL};
56
57 # test KOHA_INTRANET_URL is set
58 if ( not $intranet ) {
59    plan skip_all => "Tests skip. You must set env. variable KOHA_INTRANET_URL to do tests\n";
60 }
61 # test KOHA_OPAC_URL is set
62 elsif ( not $opac ) {
63    plan skip_all => "Tests skip. You must set env. variable KOHA_OPAC_URL to do tests\n";
64 }
65 else {
66     plan tests => 89;
67 }
68
69 $intranet =~ s#/$##;
70 $opac     =~ s#/$##;
71
72 my $mock_zebra = t::lib::Mocks::Zebra->new(
73     {
74         intranet  => $intranet,
75         opac      => $opac,
76         koha_conf => $ENV{KOHA_CONF},
77     }
78 );
79
80 #-------------------------------- Test with greek and corean chars;
81 # launch the zebra saerch process
82 $mock_zebra->launch_zebra;
83 if ( not defined $mock_zebra->{zebra_pid} ) {
84     plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n";
85 }
86 # launch the zebra index process
87 $mock_zebra->launch_indexer;
88 if ( not defined $mock_zebra->{indexer_pid} ) {
89     plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
90 }
91
92 our $agent = Test::WWW::Mechanize->new( autocheck => 1 );
93 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' );
94 $agent->form_name('loginform');
95 $agent->field( 'userid', $ENV{KOHA_PASS} );
96 $agent->field( 'password', $ENV{KOHA_USER} );
97 $agent->field( 'branch',   '' );
98 $agent->click_ok( '', 'login to staff interface' );
99
100 my $batch_id = $mock_zebra->load_records_ui($file1);
101 my $utf8_reg1 = qr/学協会. μμ/;
102 test_search('Αθήνα', 'deuteros', $utf8_reg1);
103 $mock_zebra->clean_records($batch_id);
104 $mock_zebra->cleanup;
105
106 #--------------------------------- Test with only utf-8 chars in the latin-1 range;
107 $mock_zebra->launch_zebra;
108 if ( not defined $mock_zebra->{zebra_pid} ) {
109     plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n";
110 }
111 $mock_zebra->launch_indexer;
112 if ( not defined $mock_zebra->{indexer_pid} ) {
113     plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
114 }
115 $batch_id = $mock_zebra->load_records_ui($file2);
116 my $utf8_reg2 = qr/Tòmas/;
117 test_search('Ramòn', 'Tòmas',$utf8_reg2);
118 $mock_zebra->clean_records($batch_id);
119 $mock_zebra->cleanup;
120
121 #--------------------------------- Test with supplementary utf-8 chars;
122 $mock_zebra->launch_zebra;
123 if ( not defined $mock_zebra->{zebra_pid} ) {
124     plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n";
125 }
126 $mock_zebra->launch_indexer;
127 if ( not defined $mock_zebra->{indexer_pid} ) {
128     plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
129 }
130 $batch_id = $mock_zebra->load_records_ui($file3);
131 my $utf8_reg3 = qr/😀/;
132 test_search("𠻺tomasito𠻺", 'A tiny record', $utf8_reg3);
133 $mock_zebra->clean_records($batch_id);
134 $mock_zebra->cleanup;
135
136 sub test_search{
137     my ( $publisher, $search_key, $utf8_reg ) = @_;
138
139     # --------------------------------- TEST INTRANET SEARCH
140
141     $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/search.pl" , "got search on intranet");
142     $agent->form_number(5);
143     $agent->field('idx', 'kw');
144     $agent->field('q', $search_key);
145     $agent->click();
146     my $intra_text = $agent->text() ;
147
148     $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/search.pl" , "got search on intranet");
149     $agent->form_number(5);
150     $agent->field('idx', 'kw');
151     $agent->field('q', $publisher);
152     $agent->click();
153     $intra_text = $agent->text();
154
155     my $expected_base = q|search.pl\?advsearch=1&idx=kw&q=| . uri_escape_utf8( $publisher );
156     $agent->base_like(qr|$expected_base|, );
157
158     ok ( ( length(Encode::encode('UTF-8', $intra_text)) != length($intra_text) ) , 'UTF-8 are multi-byte. Good') ;
159     ok ($intra_text =~  $utf8_reg, 'UTF-8 chars are correctly present. Good');
160     # -------------------------------------------------- TEST ON OPAC
161
162     $agent->get_ok( "$opac" , "got opac");
163     $agent->form_name('searchform');
164     $agent->field( 'q',   $search_key );
165     $agent->field( 'idx',   '' );
166     $agent->click( );
167     my $opac_text = $agent->text() ;
168
169     $agent->get_ok( "$opac" , "got opac");
170     $agent->form_name('searchform');
171     $agent->field('q', $publisher);
172     $agent->field( 'idx',   '' );
173     $agent->click();
174     $opac_text = $agent->text();
175
176     $expected_base = q|opac-search.pl\?(idx=&)?q=| . uri_escape_utf8( $publisher );
177     $agent->base_like(qr|$expected_base|, );
178     # Test added on BZ 14909 in addition to making the empty idx= optional
179     # in the previous regex
180     $agent->base_unlike( qr|idx=\w+|, 'Base does not contain an idx' );
181
182     ok ( ( length(Encode::encode('UTF-8', $opac_text)) != length($opac_text) ) , 'UTF-8 are multi-byte. Good') ;
183     ok ($opac_text =~  $utf8_reg, 'UTF-8 chars are correctly present. Good');
184
185 }
186
187 END {
188     $mock_zebra->cleanup;
189 };
190