Bug 20045: Fix Selenium tests
[koha.git] / t / db_dependent / selenium / administration_tasks.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 #This selenium test tests the Koha Administration module functionality including adding circ rules, item types and modifying frameworks
19
20 #Note: If you are testing this on kohadevbox with selenium installed in kohadevbox then you need to set the staffClientBaseURL to localhost:8080 and the OPACBaseURL to http://localhost:80
21
22 use Modern::Perl;
23
24 use C4::Context;
25
26 use Test::More tests => 1;
27
28 use t::lib::Selenium;
29
30 my $login = $ENV{KOHA_USER} || 'koha';
31
32 my $itemtype      = 'UT_DVD';
33 my $frameworkcode = 'UTFW';     # frameworkcode is only 4 characters max!
34 my $branchcode    = 'UT_BC';
35 my $av_category   = 'AV_CAT_TEST';
36 my $category_code = 'PATRON_CAT';
37 our ($cleanup_needed);
38
39 SKIP: {
40     eval { require Selenium::Remote::Driver; };
41     skip "Selenium::Remote::Driver is needed for selenium tests.", 1 if $@;
42
43     $cleanup_needed = 1;
44
45     my $s        = t::lib::Selenium->new;
46     my $driver   = $s->driver;
47     my $mainpage = $s->base_url . q|mainpage.pl|;
48     $driver->get($mainpage);
49     like( $driver->get_title(), qr(Log in to Koha), );
50     $s->auth;
51     { # Item types
52         # Navigate to the Administration area and create an item type
53         $s->click( { href => '/admin/admin-home.pl', main => 'container-main' } )
54           ;    # Koha administration
55         $s->click( { href => '/admin/itemtypes.pl', main_class => 'main container-fluid' } );  # Item Types
56         $s->click( { href => '/admin/itemtypes.pl?op=add_form', main => 'doc3' } )
57           ;    # New item type
58         $s->fill_form(
59             { itemtype => $itemtype, description => "Digital Optical Disc" } );
60         $s->submit_form;
61         $s->click(
62             {
63                 href => '/admin/itemtypes.pl?op=add_form&itemtype=' . $itemtype,
64                 main => 'doc3'
65             }
66         );     # New item type
67     };
68
69     { # Circulation/fine rules
70         $driver->get($mainpage);
71         $s->click( { href => '/admin/admin-home.pl', main => 'container-main' } )
72           ;    # Koha administration
73         $s->click( { href => '/admin/smart-rules.pl', main_class => 'main container-fluid' } )
74           ;    # Circulation and fines rules
75                # TODO Create smart navigation here
76     };
77
78     { # Biblio frameworks
79         $driver->get($mainpage);
80         $s->click( { href => '/admin/admin-home.pl', main => 'container-main' } )
81           ;    # Koha administration
82         $s->click( { href => '/admin/biblio_framework.pl', main_class => 'main container-fluid' } )
83           ;    # MARC bibliographic framework
84         $s->click(
85             { href => '/admin/biblio_framework.pl?op=add_form', main => 'doc3' } )
86           ;    # New framework
87         $s->fill_form(
88             {
89                 frameworkcode => $frameworkcode,
90                 description   => 'just a description'
91             }
92         );
93         $s->submit_form;
94         $s->click( { id => 'frameworkactions' . $frameworkcode } );
95         $s->click(
96             {
97                 href => 'marctagstructure.pl?frameworkcode=' . $frameworkcode,
98                 main => 'doc3'
99             }
100         );    # MARC structure # FIXME '/admin/' is missing in the url
101               # TODO Click on OK to create the MARC structure
102     };
103
104     { #Libraries
105         $driver->get($mainpage);
106         $s->click( { href => '/admin/admin-home.pl', main => 'container-main' } )
107           ;    # Koha administration
108         $s->click( { href => '/admin/branches.pl', main_class => 'main container-fluid' } )
109           ;    # Libraries and groups
110         $s->click( { href => '/admin/branches.pl?op=add_form', main => 'doc3' } )
111           ;    # New library
112         $s->fill_form( { branchcode => $branchcode, branchname => 'my library' } );
113         $s->submit_form;
114         $s->click(
115             {
116                 href => '/admin/branches.pl?op=add_form&branchcode=' . $branchcode,
117                 main => 'doc3'
118             }
119         );     # Edit
120         $s->fill_form( { branchname => 'another branchname' } );
121         $s->submit_form;
122         $s->click(
123             {
124                 id => 'delete_library_'.$branchcode,
125             }
126         );     # Delete
127     };
128
129     { #Authorized values
130         $driver->get($mainpage);
131         $s->click( { href => '/admin/admin-home.pl', main => 'container-main' } ); #Koha administration
132
133         $s->click( { href => '/admin/authorised_values.pl', main_class => 'main container-fluid' } ); #Authorized values
134
135         $s->click( { href => { 'ends-with' => '/admin/authorised_values.pl?op=add_form' }, main => 'doc3' } ); # New category
136         $s->fill_form( { category => $av_category } );
137         $s->submit_form;
138
139         $s->click(
140             {
141                 href => '/admin/authorised_values.pl?op=add_form&category=' . $av_category,
142                 main => 'doc3'
143             }
144         );    # New authorised value for ...
145         $s->fill_form(
146             {
147                 authorised_value => "$av_category" . "_xxx",
148                 lib              => "This is a description for staff",
149                 lib_opac         => "This is a description for OPAC"
150             }
151         );
152         $s->submit_form;
153
154         my $dbh = C4::Context->dbh;
155         my ( $av_id ) = $dbh->selectrow_array(q|
156             SELECT id FROM authorised_values WHERE category=?|, undef, $av_category );
157         $s->click(
158             {
159                 href => '/admin/authorised_values.pl?op=delete&searchfield=' . $av_category . '&id=' . $av_id,
160                 main => 'doc3'
161             }
162         );
163         $s->driver->accept_alert; # Accept the modal "Are you sure you want to delete this authorized value?"
164     };
165
166     { # Patron categories
167         $driver->get($mainpage);
168         $s->click( { href => '/admin/admin-home.pl', main => 'container-main' } ); # Koha administration
169         $s->click( { href => '/admin/categories.pl', main_class => 'main container-fluid' } ); # Patron categories
170         $s->click( { href => '/admin/categories.pl?op=add_form', main => 'doc3' } ); # New patron category
171
172         $s->fill_form( { categorycode => $category_code, description => 'Test category', enrolmentperiod => 12, category_type => 'A' } );
173         $s->submit_form;
174
175         $s->click(
176             {
177                 href => '/admin/categories.pl?op=delete_confirm&categorycode=' . $category_code,
178                 main => 'doc3'
179             }
180         ); # Delete button
181
182         $s->submit_form; # Delete this category
183
184         # TODO Make sure the category has been deleted
185     };
186
187     $driver->quit();
188 }
189
190 END {
191     cleanup() if $cleanup_needed;
192 };
193
194 sub cleanup {
195     my $dbh = C4::Context->dbh;
196     $dbh->do(q|DELETE FROM itemtypes WHERE itemtype=?|, undef, $itemtype);
197     $dbh->do(q|DELETE FROM biblio_framework WHERE frameworkcode=?|, undef, $frameworkcode);
198     $dbh->do(q|DELETE FROM branches WHERE branchcode=?|, undef, $branchcode);
199     $dbh->do(q|DELETE FROM authorised_value_categories WHERE category_name=?|, undef, $av_category);
200     $dbh->do(q|DELETE FROM categories WHERE categorycode=?|, undef, $category_code);
201 }