Bug 19185: Fix regressions.t
[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 $koha_conf = $ENV{KOHA_CONF};
38 my $xml       = XMLin($koha_conf);
39
40 my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21';
41
42 my $file1 =
43   $marcflavour eq 'UNIMARC'
44   ? "$testdir/data/unimarcutf8record.mrc"
45   : "$testdir/data/marc21utf8record.mrc";
46
47 my $file2 =
48   $marcflavour eq 'UNIMARC'
49   ? "$testdir/data/unimarclatin1utf8rec.mrc"
50   : "$testdir/data/marc21latin1utf8rec.mrc";
51
52 my $file3 =
53   $marcflavour eq 'UNIMARC'
54   ? "$testdir/data/unimarcutf8supprec.mrc"
55   : "$testdir/data/marc21utf8supprec.mrc";
56
57 my $user     = $ENV{KOHA_USER} || $xml->{config}->{user};
58 my $password = $ENV{KOHA_PASS} || $xml->{config}->{pass};
59 our $intranet = $ENV{KOHA_INTRANET_URL};
60 our $opac     = $ENV{KOHA_OPAC_URL};
61
62 # test KOHA_INTRANET_URL is set
63 if ( not $intranet ) {
64    plan skip_all => "Tests skip. You must set env. variable KOHA_INTRANET_URL to do tests\n";
65 }
66 # test KOHA_OPAC_URL is set
67 elsif ( not $opac ) {
68    plan skip_all => "Tests skip. You must set env. variable KOHA_OPAC_URL to do tests\n";
69 }
70 else {
71     plan tests => 89;
72 }
73
74 $intranet =~ s#/$##;
75 $opac     =~ s#/$##;
76
77 my $mock_zebra = t::lib::Mocks::Zebra->new(
78     {
79         koha_conf => $koha_conf,
80         user      => $user,
81         password  => $password,
82         intranet  => $intranet,
83         opac      => $opac
84     }
85 );
86
87 #-------------------------------- Test with greek and corean chars;
88 # launch the zebra saerch process
89 $mock_zebra->launch_zebra;
90 if ( not defined $mock_zebra->{zebra_pid} ) {
91     plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n";
92 }
93 # launch the zebra index process
94 $mock_zebra->launch_indexer;
95 if ( not defined $mock_zebra->{indexer_pid} ) {
96     plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
97 }
98
99 our $agent = Test::WWW::Mechanize->new( autocheck => 1 );
100 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' );
101 $agent->form_name('loginform');
102 $agent->field( 'password', $password );
103 $agent->field( 'userid',   $user );
104 $agent->field( 'branch',   '' );
105 $agent->click_ok( '', 'login to staff interface' );
106
107 my $batch_id = $mock_zebra->load_records($file1);
108 my $utf8_reg1 = qr/学協会. μμ/;
109 test_search('Αθήνα', 'deuteros', $utf8_reg1);
110 $mock_zebra->clean_records($batch_id);
111 $mock_zebra->cleanup;
112
113 #--------------------------------- Test with only utf-8 chars in the latin-1 range;
114 $mock_zebra->launch_zebra;
115 if ( not defined $mock_zebra->{zebra_pid} ) {
116     plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n";
117 }
118 $mock_zebra->launch_indexer;
119 if ( not defined $mock_zebra->{indexer_pid} ) {
120     plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
121 }
122 $batch_id = $mock_zebra->load_records($file2);
123 my $utf8_reg2 = qr/Tòmas/;
124 test_search('Ramòn', 'Tòmas',$utf8_reg2);
125 $mock_zebra->clean_records($batch_id);
126 $mock_zebra->cleanup;
127
128 #--------------------------------- Test with supplementary utf-8 chars;
129 $mock_zebra->launch_zebra;
130 if ( not defined $mock_zebra->{zebra_pid} ) {
131     plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n";
132 }
133 $mock_zebra->launch_indexer;
134 if ( not defined $mock_zebra->{indexer_pid} ) {
135     plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
136 }
137 $batch_id = $mock_zebra->load_records($file3);
138 my $utf8_reg3 = qr/😀/;
139 test_search("𠻺tomasito𠻺", 'A tiny record', $utf8_reg3);
140 $mock_zebra->clean_records($batch_id);
141 $mock_zebra->cleanup;
142
143 sub test_search{
144     my ( $publisher, $search_key, $utf8_reg ) = @_;
145
146     # --------------------------------- TEST INTRANET SEARCH
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', $search_key);
152     $agent->click();
153     my $intra_text = $agent->text() ;
154
155     $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/search.pl" , "got search on intranet");
156     $agent->form_number(5);
157     $agent->field('idx', 'kw');
158     $agent->field('q', $publisher);
159     $agent->click();
160     $intra_text = $agent->text();
161
162     my $expected_base = q|search.pl\?advsearch=1&idx=kw&q=| . uri_escape_utf8( $publisher );
163     $agent->base_like(qr|$expected_base|, );
164
165     ok ( ( length(Encode::encode('UTF-8', $intra_text)) != length($intra_text) ) , 'UTF-8 are multi-byte. Good') ;
166     ok ($intra_text =~  $utf8_reg, 'UTF-8 chars are correctly present. Good');
167     # -------------------------------------------------- TEST ON OPAC
168
169     $agent->get_ok( "$opac" , "got opac");
170     $agent->form_name('searchform');
171     $agent->field( 'q',   $search_key );
172     $agent->field( 'idx',   '' );
173     $agent->click( );
174     my $opac_text = $agent->text() ;
175
176     $agent->get_ok( "$opac" , "got opac");
177     $agent->form_name('searchform');
178     $agent->field('q', $publisher);
179     $agent->field( 'idx',   '' );
180     $agent->click();
181     $opac_text = $agent->text();
182
183     $expected_base = q|opac-search.pl\?(idx=&)?q=| . uri_escape_utf8( $publisher );
184     $agent->base_like(qr|$expected_base|, );
185     # Test added on BZ 14909 in addition to making the empty idx= optional
186     # in the previous regex
187     $agent->base_unlike( qr|idx=\w+|, 'Base does not contain an idx' );
188
189     ok ( ( length(Encode::encode('UTF-8', $opac_text)) != length($opac_text) ) , 'UTF-8 are multi-byte. Good') ;
190     ok ($opac_text =~  $utf8_reg, 'UTF-8 chars are correctly present. Good');
191
192 }
193
194 END {
195     $mock_zebra->cleanup;
196 };
197