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