Bug 19391: (bug 19128 follow-up) Fix failing tests from auth_values_input_www.t
[koha.git] / t / db_dependent / www / auth_values_input_www.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 tests => 33;
22 use Test::WWW::Mechanize;
23 use XML::Simple;
24 use JSON;
25 use File::Basename;
26 use File::Spec;
27 use POSIX;
28 use URI::Escape;
29 use Encode;
30
31 my $testdir = File::Spec->rel2abs( dirname(__FILE__) );
32
33 my $koha_conf = $ENV{KOHA_CONF};
34 my $xml       = XMLin($koha_conf);
35
36 eval{
37     use C4::Context;
38 };
39 if ($@) {
40     plan skip_all => "Tests skip. You must have a working Context\n";
41 }
42
43 my $user     = $ENV{KOHA_USER} || $xml->{config}->{user};
44 my $password = $ENV{KOHA_PASS} || $xml->{config}->{pass};
45 my $intranet = $ENV{KOHA_INTRANET_URL};
46
47 if (not defined $intranet) {
48     plan skip_all => "Tests skip. You must set env. variable KOHA_INTRANET_URL to do tests\n";
49 }
50
51 my $dbh = C4::Context->dbh;
52
53 $intranet =~ s#/$##;
54
55 my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
56 my $jsonresponse;
57 my ($category, $expected_base, $add_form_link_exists, $delete_form_link_exists);
58
59 # -------------------------------------------------- LOGIN
60
61
62 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' );
63 $agent->form_name('loginform');
64 $agent->field( 'password', $password );
65 $agent->field( 'userid',   $user );
66 $agent->field( 'branch',   '' );
67 $agent->click_ok( '', 'login to staff client' );
68 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' );
69
70 #--------------------------------------------------- Test with corean and greek chars
71
72 $category = '学協会μμ';
73 $dbh->do(q|DELETE FROM authorised_values WHERE category = ?|, undef, $category);
74 $dbh->do(q|DELETE FROM authorised_value_categories WHERE category_name = ?|, undef, $category);
75
76 $expected_base = q|authorised_values.pl|;
77 $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl", 'Connect to Authorized values page' );
78 $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl?op=add_form", 'Open to create a new category' );
79 $agent->form_name('Aform');
80 $agent->field('category', $category);
81 $agent->click_ok( '', "Create new AV category " );
82
83 $agent->base_like(qr|$expected_base|, "check base");
84 $add_form_link_exists = 0;
85 for my $link ( $agent->links() ) {
86     if ( $link->url =~ m|authorised_values.pl\?op=add_form&category=| . uri_escape_utf8($category) ) {
87         $add_form_link_exists = 1;
88     }
89 }
90 is( $add_form_link_exists, 1, 'Add a new category button should be displayed');
91 $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl?op=add_form&category=" . uri_escape_utf8($category), 'Open to create a new AV for this category' );
92
93 $agent->form_name('Aform');
94 $agent->field('authorised_value', 'επιμεq');
95 $agent->field('lib_opac', 'autdesc2');
96 $agent->field('lib', 'desc1');
97 $agent->field('branches', '');
98 $agent->click_ok( '', "Create a new value for the category" );
99
100 $agent->base_like(qr|$expected_base|, "check base");
101 $add_form_link_exists = 0;
102 $delete_form_link_exists = 0;
103 my $add_form_re = q|authorised_values.pl\?op=add_form&category=|  . uri_escape_utf8($category);
104 my $delete_re   = q|authorised_values.pl\?op=delete&searchfield=| . uri_escape_utf8($category);
105 for my $link ( $agent->links() ) {
106     if ( $link->url =~ qr|$add_form_re| ) {
107         $add_form_link_exists = 1;
108     } elsif ( $link->url =~ qr|$delete_re| ) {
109         $delete_form_link_exists = 1;
110     }
111 }
112 is( $add_form_link_exists, 1, 'Add a new category button should be displayed');
113 is( $delete_form_link_exists, 1, '');
114
115 $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl", 'Return to Authorized values page' );
116 $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl?searchfield=" . uri_escape_utf8($category) . "&offset=0", 'Search the values inserted' );
117 my $text = $agent->text() ;
118 #Tests on UTF-8
119 ok ( ( length(Encode::encode('UTF-8', $text)) != length($text) ) , 'UTF-8 are multi-byte. Good') ;
120 ok ($text =~  m/学協会μμ/, 'UTF-8 (Asia) chars are correctly present. Good');
121 ok ($text =~  m/επιμεq/, 'UTF-8 (Greek) chars are correctly present. Good');
122 my @links = $agent->links;
123 my $id_to_del ='';
124 $delete_re = q|op=delete\&searchfield=| . uri_escape_utf8($category) . '\&id=(\d+)';
125 foreach my $dato (@links){
126     my $link = $dato->url;
127     if ($link =~ qr|$delete_re| ) {
128         $id_to_del = $1;
129         last;
130     }
131 }
132 if ($id_to_del) {
133     $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl?op=delete&searchfield=" . uri_escape_utf8($category) . "&id=$id_to_del", 'UTF_8 auth. value deleted' );
134 }else{
135     ok($id_to_del ne undef, "error, link to delete not working");
136 }
137
138 #---------------------------------------- Test with only latin utf-8 (could be taken as Latin-1/ISO 8859-1)
139
140 $category = 'tòmas';
141 $dbh->do(q|DELETE FROM authorised_values WHERE category = ?|, undef, $category);
142 $dbh->do(q|DELETE FROM authorised_value_categories WHERE category_name = ?|, undef, $category);
143
144 $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl", 'Connect to Authorized values page' );
145 $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl?op=add_form", 'Open to create a new category' );
146 $agent->form_name('Aform');
147 $agent->field('category', $category);
148 $agent->click_ok( '', "Create new AV category" );
149
150 $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl?op=add_form&category=$category", 'Open to create a new AV for this category' );
151 $agent->form_name('Aform');
152 $agent->field('authorised_value', 'ràmen');
153 $agent->field('lib_opac', 'autdesc2');
154 $agent->field('lib', 'desc1');
155 $agent->field('branches', '');
156 $agent->click_ok( '', "Create a new value for the category" );
157
158 $expected_base = q|authorised_values.pl|;
159 $agent->base_like(qr|$expected_base|, "check base");
160 $add_form_link_exists = 0;
161 $delete_form_link_exists = 0;
162 $add_form_re = q|authorised_values.pl\?op=add_form&category=|  . uri_escape_utf8($category);
163 $delete_re   = q|authorised_values.pl\?op=delete&searchfield=| . uri_escape_utf8($category);
164 for my $link ( $agent->links() ) {
165     if ( $link->url =~ qr|$add_form_re| ) {
166         $add_form_link_exists = 1;
167     }elsif( $link->url =~ qr|$delete_re| ) {
168         $delete_form_link_exists = 1;
169     }
170 }
171 is( $add_form_link_exists, 1, );
172 is( $delete_form_link_exists, 1, );
173
174 $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl", 'Return to Authorized values page' );
175 $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl?searchfield=tòmas&offset=0", 'Search the values inserted' );
176 my $text2 = $agent->text() ;
177 #Tests on UTF-8
178 ok ( ( length(Encode::encode('UTF-8', $text)) != length($text) ) , 'UTF-8 are multi-byte. Good') ;
179 ok ($text2 =~  m/tòmas/, 'UTF-8 not Latin-1 first test is OK. Good');
180 ok ($text2=~  m/ràmen/, 'UTF-8 not Latin-1 second test is OK. Good');
181 my @links2 = $agent->links;
182 my $id_to_del2 ='';
183 $delete_re   = q|op=delete\&searchfield=| . uri_escape_utf8($category) . q|\&id=(\d+)|;
184 foreach my $dato (@links2){
185     my $link = $dato->url;
186     if ($link =~  qr|$delete_re| ){
187         $id_to_del2 = $1;
188         last;
189     }
190 }
191 if ($id_to_del2) {
192     $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl?op=delete&searchfield=tòmas&id=$id_to_del2", 'UTF_8 auth. value deleted' );
193 }else{
194     ok($id_to_del2 ne undef, "error, link to delete not working");
195 }
196