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