Bug 13264: Test for utf-8 on authorised values input/display/delete
[koha.git] / t / db_dependent / www / auth_values_input_www.t
1 #!/usr/bin/perl
2
3 # Copyright 2012 C & P Bibliography Services
4 #
5 # This is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # This is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17 #
18
19 use Modern::Perl;
20 use utf8;
21 use Test::More;
22 use Test::WWW::Mechanize;
23 use Data::Dumper;
24 use XML::Simple;
25 use JSON;
26 use File::Basename;
27 use File::Spec;
28 use POSIX;
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
44 my $user     = $ENV{KOHA_USER} || $xml->{config}->{user};
45 my $password = $ENV{KOHA_PASS} || $xml->{config}->{pass};
46 my $intranet = $ENV{KOHA_INTRANET_URL};
47
48 my $mysql_on = ProgProcesses('mysql');
49
50
51 if ($mysql_on < 2) {
52     plan skip_all => "Tests skip. You must start Mysql to do those tests\n";
53 }
54
55 if (not defined $intranet) {
56     plan skip_all => "Tests skip. You must set env. variable KOHA_INTRANET_URL to do tests\n";
57 }
58
59
60 $intranet =~ s#/$##;
61
62 my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
63 my $jsonresponse;
64
65 # -------------------------------------------------- LOAD RECORD
66
67 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' );
68 $agent->form_name('loginform');
69 $agent->field( 'password', $password );
70 $agent->field( 'userid',   $user );
71 $agent->field( 'branch',   '' );
72 $agent->click_ok( '', 'login to staff client' );
73 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' );
74
75 $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl", 'Connect to Authorized values page' );
76 $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl?op=add_form", 'Open to create a new category' );
77 $agent->form_name('Aform');
78 $agent->field('authorised_value', 'επιμεq');
79 $agent->field('lib_opac', 'autdesc2');
80 $agent->field('lib', 'desc1');
81 $agent->field('category', '学協会μμ');
82 $agent->field('branches', '');
83 $agent->click_ok( '', "Create new auth category and value" );
84
85 $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl", 'Return to Authorized values page' );
86 $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl?searchfield=学協会μμ&offset=0", 'Search the values inserted' );
87 my $text = $agent->text() ;
88 #Tests on UTF-8
89 ok ( ( length(Encode::encode_utf8($text)) != length($text) ) , 'UTF-8 are multi-byte. Good') ;
90 ok ($text =~  m/学協会μμ/, 'UTF-8 (Asia) chars are correctly present. Good');
91 ok ($text =~  m/επιμεq/, 'UTF-8 (Greek) chars are correctly present. Good');
92 my @links = $agent->links;
93 my $id_to_del ='';
94 foreach my $dato (@links){
95     my $link = $dato->url;
96     if ($link =~  m/op=delete_confirm\&searchfield=学協会μμ/){
97         $link =~  m/(.*&id=?)(\d{1,})(&.*)/;
98         $id_to_del = $2;
99         last;
100     };
101 }
102 if ($id_to_del) {
103     $agent->get_ok( "$intranet/cgi-bin/koha/admin/authorised_values.pl?op=delete_confirmed&searchfield=学協会μμ&id=$id_to_del", 'UTF_8 auth. value deleted' );
104 }else{
105     ok($id_to_del ne undef, "error, link to delete nor working");
106 }
107
108 done_testing();
109
110 sub ProgProcesses {
111    return scalar grep /$_[0]/, (split /\n/, `ps -aef`);
112 }