Bug 22619: Fix null empty behaviour for new rules
[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 => 3;
27
28 use t::lib::Selenium;
29 use t::lib::TestBuilder;
30
31 my $builder = t::lib::TestBuilder->new;
32
33 my $login = $ENV{KOHA_USER} || 'koha';
34
35 my $itemtype      = 'UT_DVD';
36 my $frameworkcode = 'UTFW';     # frameworkcode is only 4 characters max!
37 my $branchcode    = 'UT_BC';
38 my $av_category   = 'AV_CAT_TEST';
39 my $category_code = 'PATRON_CAT';
40 our ($cleanup_needed);
41
42 SKIP: {
43     eval { require Selenium::Remote::Driver; };
44     skip "Selenium::Remote::Driver is needed for selenium tests.", 1 if $@;
45
46     $cleanup_needed = 1;
47
48     my $s        = t::lib::Selenium->new;
49     my $driver   = $s->driver;
50     my $mainpage = $s->base_url . q|mainpage.pl|;
51     $driver->get($mainpage);
52     like( $driver->get_title(), qr(Log in to Koha), );
53     $s->auth;
54     { # Item types
55         # Navigate to the Administration area and create an item type
56         $s->click( { href => '/admin/admin-home.pl', main => 'container-main' } )
57           ;    # Koha administration
58         $s->click( { href => '/admin/itemtypes.pl', main_class => 'main container-fluid' } );  # Item Types
59         $s->click( { href => '/admin/itemtypes.pl?op=add_form', main_class => 'main container-fluid' } )
60           ;    # New item type
61         $s->fill_form(
62             { itemtype => $itemtype, description => "Digital Optical Disc" } );
63         $s->submit_form;
64         $s->click(
65             {
66                 href => '/admin/itemtypes.pl?op=add_form&itemtype=' . $itemtype,
67                 main_class => 'main container-fluid'
68             }
69         );     # New item type
70     };
71
72     { # Circulation/fine rules
73         my $itype = $builder->build_object({ class => "Koha::ItemTypes" });
74         $driver->get($mainpage);
75         $s->click( { href => '/admin/admin-home.pl', main => 'container-main' } )
76           ;    # Koha administration
77         $s->click( { href => '/admin/smart-rules.pl', main_class => 'main container-fluid' } )
78           ;    # Circulation and fines rules
79         my $elt = $driver->find_element('//tr[@id="edit_row"]/td/select[@id="matrixitemtype"]/option[@value="'.$itype->itemtype.'"]');
80         is( $elt->get_text(),$itype->description,"Our new itemtype is in the list");
81         $elt->click();
82         $elt = $driver->find_element('//tr[@id="edit_row"]/td[@class="actions"]/button[@type="submit"]');
83         $elt->click();
84         $elt = $driver->find_elements('//table[@id="default-circulation-rules"]/tbody/tr/td[contains(text(),"'.$itype->description.'")]/following-sibling::td/span[text() = "Unlimited"]');
85         is( @$elt,2,"We have unlimited checkouts");
86         #Clean up
87         Koha::IssuingRules->find({itemtype=>$itype->itemtype})->delete();
88         $itype->delete;
89                # TODO Create more smart rules navigation here
90     };
91
92     { # Biblio frameworks
93         $driver->get($mainpage);
94         $s->click( { href => '/admin/admin-home.pl', main => 'container-main' } )
95           ;    # Koha administration
96         $s->click( { href => '/admin/biblio_framework.pl', main_class => 'main container-fluid' } )
97           ;    # MARC bibliographic framework
98         $s->click(
99             { href => '/admin/biblio_framework.pl?op=add_form', main_class => 'main container-fluid' } )
100           ;    # New framework
101         $s->fill_form(
102             {
103                 frameworkcode => $frameworkcode,
104                 description   => 'just a description'
105             }
106         );
107         $s->submit_form;
108         $s->click( { id => 'frameworkactions' . $frameworkcode } );
109         $s->click(
110             {
111                 href => 'marctagstructure.pl?frameworkcode=' . $frameworkcode,
112                 main_class => 'main container-fluid'
113             }
114         );    # MARC structure # FIXME '/admin/' is missing in the url
115               # TODO Click on OK to create the MARC structure
116     };
117
118     { #Libraries
119         $driver->get($mainpage);
120         $s->click( { href => '/admin/admin-home.pl', main => 'container-main' } )
121           ;    # Koha administration
122         $s->click( { href => '/admin/branches.pl', main_class => 'main container-fluid' } )
123           ;    # Libraries and groups
124         $s->click( { href => '/admin/branches.pl?op=add_form', main_class => 'main container-fluid' } )
125           ;    # New library
126         $s->fill_form( { branchcode => $branchcode, branchname => 'my library' } );
127         $s->submit_form;
128         $s->click(
129             {
130                 href => '/admin/branches.pl?op=add_form&branchcode=' . $branchcode,
131                 main_class => 'main container-fluid'
132             }
133         );     # Edit
134         $s->fill_form( { branchname => 'another branchname' } );
135         $s->submit_form;
136         $s->click(
137             {
138                 id => 'delete_library_'.$branchcode,
139             }
140         );     # Delete
141     };
142
143     { #Authorized values
144         $driver->get($mainpage);
145         $s->click( { href => '/admin/admin-home.pl', main => 'container-main' } ); #Koha administration
146
147         $s->click( { href => '/admin/authorised_values.pl', main_class => 'main container-fluid' } ); #Authorized values
148
149         $s->click( { href => { 'ends-with' => '/admin/authorised_values.pl?op=add_form' }, main_class => 'main container-fluid' } ); # New category
150         $s->fill_form( { category => $av_category } );
151         $s->submit_form;
152
153         $s->click(
154             {
155                 href => '/admin/authorised_values.pl?op=add_form&category=' . $av_category,
156                 main_class => 'main container-fluid'
157             }
158         );    # New authorised value for ...
159         $s->fill_form(
160             {
161                 authorised_value => "$av_category" . "_xxx",
162                 lib              => "This is a description for staff",
163                 lib_opac         => "This is a description for OPAC"
164             }
165         );
166         $s->submit_form;
167
168         my $dbh = C4::Context->dbh;
169         my ( $av_id ) = $dbh->selectrow_array(q|
170             SELECT id FROM authorised_values WHERE category=?|, undef, $av_category );
171         $s->click(
172             {
173                 href => '/admin/authorised_values.pl?op=delete&searchfield=' . $av_category . '&id=' . $av_id,
174                 main_class => 'main container-fluid'
175             }
176         );
177         $s->driver->accept_alert; # Accept the modal "Are you sure you want to delete this authorized value?"
178     };
179
180     { # Patron categories
181         $driver->get($mainpage);
182         $s->click( { href => '/admin/admin-home.pl', main => 'container-main' } ); # Koha administration
183         $s->click( { href => '/admin/categories.pl', main_class => 'main container-fluid' } ); # Patron categories
184         $s->click( { href => '/admin/categories.pl?op=add_form', main_class => 'main container-fluid' } ); # New patron category
185
186         $s->fill_form( { categorycode => $category_code, description => 'Test category', enrolmentperiod => 12, category_type => 'A' } );
187         $s->submit_form;
188
189         $s->click(
190             {
191                 href => '/admin/categories.pl?op=delete_confirm&categorycode=' . $category_code,
192                 main_class => 'main container-fluid'
193             }
194         ); # Delete button
195
196         $s->submit_form; # Delete this category
197
198         # TODO Make sure the category has been deleted
199     };
200
201     $driver->quit();
202 }
203
204 END {
205     cleanup() if $cleanup_needed;
206 };
207
208 sub cleanup {
209     my $dbh = C4::Context->dbh;
210     $dbh->do(q|DELETE FROM itemtypes WHERE itemtype=?|, undef, $itemtype);
211     $dbh->do(q|DELETE FROM biblio_framework WHERE frameworkcode=?|, undef, $frameworkcode);
212     $dbh->do(q|DELETE FROM branches WHERE branchcode=?|, undef, $branchcode);
213     $dbh->do(q|DELETE FROM authorised_value_categories WHERE category_name=?|, undef, $av_category);
214     $dbh->do(q|DELETE FROM categories WHERE categorycode=?|, undef, $category_code);
215 }