Bug 21468: (QA follow-up) Enhance tests
[koha.git] / t / lib / Koha / Plugin / Test.pm
1 package Koha::Plugin::Test;
2
3 ## It's good practice to use Modern::Perl
4 use Modern::Perl;
5
6 use Koha::Exceptions::Exception;
7 use Koha::Plugins::Tab;
8
9 use Mojo::JSON qw(decode_json);
10
11 ## Required for all plugins
12 use base qw(Koha::Plugins::Base);
13
14 our $VERSION = 1.01;
15 our $metadata = {
16     name            => 'Test Plugin',
17     author          => 'Kyle M Hall',
18     description     => 'Test plugin',
19     date_authored   => '2013-01-14',
20     date_updated    => '2013-01-14',
21     minimum_version => '3.11',
22     maximum_version => undef,
23     version         => $VERSION,
24     my_example_tag  => 'find_me',
25 };
26
27 ## This is the minimum code required for a plugin's 'new' method
28 ## More can be added, but none should be removed
29 sub new {
30     my ( $class, $args ) = @_;
31     $args->{'metadata'} = $metadata;
32     my $self = $class->SUPER::new($args);
33     return $self;
34 }
35
36 sub report {
37     my ( $self, $args ) = @_;
38     return "Koha::Plugin::Test::report";
39 }
40
41 sub tool {
42     my ( $self, $args ) = @_;
43     return "Koha::Plugin::Test::tool";
44 }
45
46 sub to_marc {
47     my ( $self, $args ) = @_;
48     return "Koha::Plugin::Test::to_marc";
49 }
50
51 sub intranet_catalog_biblio_enhancements_toolbar_button {
52     my ( $self, $args ) = @_;
53     return "Koha::Plugin::Test::intranet_catalog_biblio_enhancements_toolbar_button";
54 }
55
56 sub intranet_catalog_biblio_enhancements {
57     my ( $self, $args ) = @_;
58     return "Koha::Plugin::Test::intranet_catalog_biblio_enhancements";
59 }
60
61 sub opac_online_payment {
62     my ( $self, $args ) = @_;
63     return "Koha::Plugin::Test::opac_online_payment";
64 }
65
66 sub opac_online_payment_begin {
67     my ( $self, $args ) = @_;
68     return "Koha::Plugin::Test::opac_online_payment_begin";
69 }
70
71 sub opac_online_payment_end {
72     my ( $self, $args ) = @_;
73     return "Koha::Plugin::Test::opac_online_payment_end";
74 }
75
76 sub opac_head {
77     my ( $self, $args ) = @_;
78     return "Koha::Plugin::Test::opac_head";
79 }
80
81 sub opac_js {
82     my ( $self, $args ) = @_;
83     return "Koha::Plugin::Test::opac_js";
84 }
85
86 sub intranet_head {
87     my ( $self, $args ) = @_;
88     return "Koha::Plugin::Test::intranet_head";
89 }
90
91 sub intranet_js {
92     my ( $self, $args ) = @_;
93     return "Koha::Plugin::Test::intranet_js";
94 }
95
96 sub configure {
97     my ( $self, $args ) = @_;
98     return "Koha::Plugin::Test::configure";;
99 }
100
101 sub install {
102     my ( $self, $args ) = @_;
103     return "Koha::Plugin::Test::install";
104 }
105
106 sub upgrade {
107     my ( $self, $args ) = @_;
108     return "Koha::Plugin::Test::upgrade";
109 }
110
111 sub uninstall {
112     my ( $self, $args ) = @_;
113     return "Koha::Plugin::Test::uninstall";
114 }
115
116 sub test_output {
117     my ( $self ) = @_;
118     $self->output( '¡Hola output!', 'json' );
119 }
120
121 sub test_output_html {
122     my ( $self ) = @_;
123     $self->output_html( '¡Hola output_html!' );
124 }
125
126 sub api_namespace {
127     return "testplugin";
128 }
129
130 sub after_hold_create {
131     my ( $self, $param ) = @_;
132     Koha::Exceptions::Exception->throw("after_hold_create called with parameter " . ref($param) );
133 }
134
135 sub after_biblio_action {
136     my ( $self, $params ) = @_;
137     my $action    = $params->{action} // '';
138     my $biblio    = $params->{biblio};
139     my $biblio_id = $params->{biblio_id};
140
141     if ( $action ne 'delete' ) {
142         Koha::Exceptions::Exception->throw("after_biblio_action called with action: $action, ref: " . ref($biblio) );
143     }
144     else {
145         Koha::Exceptions::Exception->throw("after_biblio_action called with action: $action") if $biblio_id;
146     }
147 }
148
149 sub after_item_action {
150     my ( $self, $params ) = @_;
151     my $action  = $params->{action} // '';
152     my $item    = $params->{item};
153     my $item_id = $params->{item_id};
154
155     if ( $action ne 'delete' ) {
156         Koha::Exceptions::Exception->throw("after_item_action called with action: $action, ref: " . ref($item) );
157     }
158     else {
159         Koha::Exceptions::Exception->throw("after_item_action called with action: $action" ) if $item_id;
160     }
161 }
162
163 sub after_circ_action {
164     my ( $self, $params ) = @_;
165
166     my $action   = $params->{action};
167     my $checkout = $params->{payload}->{checkout};
168     my $payload  = $params->{payload};
169
170     my $renewal_library_id = $payload->{renewal_library_id};
171     my $charge             = $payload->{charge};
172     my $item_id            = $payload->{item_id};
173     my $item_type          = $payload->{item_type};
174     my $shelving_location  = $payload->{shelving_location};
175     my $patron_id          = $payload->{patron_id};
176     my $collection_code    = $payload->{collection_code};
177     my $date_due           = $payload->{date_due};
178     my $date_returned      = $payload->{date_returned};
179
180     if ( $action eq 'renewal' ) {
181         Koha::Exceptions::Exception->throw("after_circ_action called with action: $action, ref: " . ref($checkout));
182     }
183     elsif ( $action eq 'checkout') {
184         Koha::Exceptions::Exception->throw("after_circ_action called with action: $action, ref: " . ref($date_due));
185     }
186     elsif ( $action eq 'checkin' ) {
187         Koha::Exceptions::Exception->throw("after_circ_action called with action: $action, ref: " . ref($date_returned));
188     }
189 }
190
191 sub api_routes {
192     my ( $self, $args ) = @_;
193
194     my $spec = qq{
195 {
196   "/patrons/bother": {
197     "get": {
198       "x-mojo-to": "Test::Controller#bother",
199       "operationId": "BotherPatron",
200       "tags": ["patrons"],
201       "produces": [
202         "application/json"
203       ],
204       "responses": {
205         "200": {
206           "description": "A bothered patron",
207           "schema": {
208               "type": "object",
209                 "properties": {
210                   "bothered": {
211                     "description": "If the patron has been bothered",
212                     "type": "boolean"
213                   }
214                 }
215           }
216         },
217         "401": {
218           "description": "An error occurred",
219           "schema": {
220               "type": "object",
221               "properties": {
222                 "error": {
223                   "description": "An explanation for the error",
224                   "type": "string"
225                 }
226               }
227           }
228         }
229       },
230       "x-koha-authorization": {
231         "permissions": {
232           "borrowers": "1"
233         }
234       }
235     }
236   },
237   "/public/patrons/bother": {
238     "get": {
239       "x-mojo-to": "Test::Controller#bother",
240       "operationId": "PubliclyBotherPatron",
241       "tags": ["patrons"],
242       "produces": [
243         "application/json"
244       ],
245       "responses": {
246         "200": {
247           "description": "A bothered patron",
248           "schema": {
249               "type": "object",
250               "properties": {
251                 "bothered": {
252                   "description": "If the patron has been bothered",
253                   "type": "boolean"
254                 }
255               }
256           }
257         },
258         "401": {
259           "description": "Authentication required",
260           "schema": {
261             "type": "object",
262             "properties": {
263               "error": {
264                 "description": "An explanation for the error",
265                 "type": "string"
266               }
267             }
268           }
269         }
270       }
271     }
272   }
273 }
274     };
275
276     return decode_json($spec);
277 }
278
279 sub check_password {
280     my ( $self, $args ) = @_;
281
282     my $password = $args->{'password'};
283     if ( $password && $password =~ m/^\d{4}$/ ) {
284         return { error => 0 };
285     }
286     else {
287         return {
288             error => 1,
289             msg   => "PIN should be four digits"
290         };
291     }
292 }
293
294 sub intranet_catalog_biblio_tab {
295     my @tabs;
296     push @tabs,
297       Koha::Plugins::Tab->new(
298         {
299             title   => 'Tab 1',
300             content => 'This is content for tab 1'
301         }
302       );
303
304     push @tabs,
305       Koha::Plugins::Tab->new(
306         {
307             title   => 'Tab 2',
308             content => 'This is content for tab 2'
309         }
310       );
311
312     return @tabs;
313 }
314
315 sub _private_sub {
316     return "";
317 }