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