Bug 34943: Unit tests
[koha.git] / t / lib / plugins / 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::Exception;
7 use Koha::Plugins::Tab;
8
9 use MARC::Field;
10 use Mojo::JSON qw( decode_json );
11
12 ## Required for all plugins
13 use base qw(Koha::Plugins::Base);
14
15 our $VERSION = "v1.01";
16 our $metadata = {
17     name            => 'Test Plugin',
18     author          => 'Kyle M Hall',
19     description     => 'Test plugin',
20     date_authored   => '2013-01-14',
21     date_updated    => '2013-01-14',
22     minimum_version => '3.11',
23     maximum_version => undef,
24     version         => $VERSION,
25     namespace       => 'test',
26     my_example_tag  => 'find_me',
27 };
28
29 ## This is the minimum code required for a plugin's 'new' method
30 ## More can be added, but none should be removed
31 sub new {
32     my ( $class, $args ) = @_;
33     $args->{'metadata'} = $metadata;
34     my $self = $class->SUPER::new($args);
35     return $self;
36 }
37
38 sub report {
39     my ( $self, $args ) = @_;
40     return "Koha::Plugin::Test::report";
41 }
42
43 sub tool {
44     my ( $self, $args ) = @_;
45     return "Koha::Plugin::Test::tool";
46 }
47
48 sub to_marc {
49     my ( $self, $args ) = @_;
50     return "Koha::Plugin::Test::to_marc";
51 }
52
53 sub intranet_catalog_biblio_enhancements_toolbar_button {
54     my ( $self, $args ) = @_;
55     return "Koha::Plugin::Test::intranet_catalog_biblio_enhancements_toolbar_button";
56 }
57
58 sub intranet_catalog_biblio_enhancements {
59     my ( $self, $args ) = @_;
60     return "Koha::Plugin::Test::intranet_catalog_biblio_enhancements";
61 }
62
63 sub opac_online_payment {
64     my ( $self, $args ) = @_;
65     return "Koha::Plugin::Test::opac_online_payment";
66 }
67
68 sub opac_online_payment_begin {
69     my ( $self, $args ) = @_;
70     return "Koha::Plugin::Test::opac_online_payment_begin";
71 }
72
73 sub opac_online_payment_end {
74     my ( $self, $args ) = @_;
75     return "Koha::Plugin::Test::opac_online_payment_end";
76 }
77
78 sub opac_head {
79     my ( $self, $args ) = @_;
80     return "Koha::Plugin::Test::opac_head";
81 }
82
83 sub opac_js {
84     my ( $self, $args ) = @_;
85     return "Koha::Plugin::Test::opac_js";
86 }
87
88 sub intranet_head {
89     my ( $self, $args ) = @_;
90     return "Koha::Plugin::Test::intranet_head";
91 }
92
93 sub intranet_js {
94     my ( $self, $args ) = @_;
95     return "Koha::Plugin::Test::intranet_js";
96 }
97
98 sub item_barcode_transform {
99     my ( $self, $barcode ) = @_;
100     my $param = $$barcode;
101     if ( Scalar::Util::looks_like_number( $$barcode ) ) {
102         $$barcode = $$barcode * 2
103     }
104     Koha::Exception->throw("item_barcode_transform called with parameter: $param");
105 }
106
107 sub patron_barcode_transform {
108     my ( $self, $barcode ) = @_;
109     $$barcode //= '';
110     Koha::Exception->throw("patron_barcode_transform called with parameter: $$barcode");
111 }
112
113 sub configure {
114     my ( $self, $args ) = @_;
115     return "Koha::Plugin::Test::configure";
116 }
117
118 sub install {
119     my ( $self, $args ) = @_;
120     return "Koha::Plugin::Test::install";
121 }
122
123 sub upgrade {
124     my ( $self, $args ) = @_;
125     return "Koha::Plugin::Test::upgrade";
126 }
127
128 sub uninstall {
129     my ( $self, $args ) = @_;
130     return "Koha::Plugin::Test::uninstall";
131 }
132
133 sub test_output {
134     my ( $self ) = @_;
135     $self->output( '¡Hola output!', 'json' );
136 }
137
138 sub test_output_html {
139     my ( $self ) = @_;
140     $self->output_html( '¡Hola output_html!' );
141 }
142
143 sub api_namespace {
144     return "testplugin";
145 }
146
147 sub after_hold_create {
148     my ( $self, $param ) = @_;
149     Koha::Exception->throw("after_hold_create called with parameter " . ref($param) );
150 }
151
152 sub before_biblio_metadata_store {
153     my ( $self, $record ) = @_;
154
155     $record->insert_fields_ordered(
156         MARC::Field->new(
157             '990', '', '',
158             'a' => 'Arte club'
159         )
160     );
161
162     return $record;
163 }
164
165 sub after_biblio_action {
166     my ( $self, $params ) = @_;
167     my $action    = $params->{action} // '';
168     my $biblio    = $params->{biblio};
169     my $biblio_id = $params->{biblio_id};
170
171     if ( $action ne 'delete' ) {
172         Koha::Exception->throw("after_biblio_action called with action: $action, ref: " . ref($biblio) );
173     }
174     else {
175         Koha::Exception->throw("after_biblio_action called with action: $action, id: $biblio_id") if $biblio_id;
176     }
177 }
178
179 sub after_item_action {
180     my ( $self, $params ) = @_;
181     my $action  = $params->{action} // '';
182     my $item    = $params->{item};
183     my $item_id = $params->{item_id};
184
185     if ( $action ne 'delete' ) {
186         my $itemnumber_defined = (defined $item->itemnumber) ? 'yes' : 'no';
187         my $item_id_defined    = (defined $item_id) ? 'yes' : 'no';
188         Koha::Exception->throw("after_item_action called with action: $action, ref: " . ref($item) . " ".
189                                            "item_id defined: $item_id_defined ".
190                                            "itemnumber defined: $itemnumber_defined" );
191     }
192     else {
193         Koha::Exception->throw("after_item_action called with action: $action, id: $item_id" ) if $item_id;
194     }
195 }
196
197 sub after_authority_action {
198     my ( $self, $params ) = @_;
199     my $action = $params->{action} // q{};
200     my $id = $params->{authority_id} // 0;
201     Koha::Exception->throw("after_authority_action called with action: $action, id: $id");
202 }
203
204 sub after_circ_action {
205     my ( $self, $params ) = @_;
206
207     my $action   = $params->{action};
208     my $checkout = $params->{payload}->{checkout};
209     my $payload  = $params->{payload};
210
211     my $type = $payload->{type};
212
213     if ( $action eq 'renewal' ) {
214         Koha::Exception->throw("after_circ_action called with action: $action, ref: " . ref($checkout));
215     }
216     elsif ( $action eq 'checkout') {
217         Koha::Exception->throw("after_circ_action called with action: $action, ref: " . ref($checkout) . " type: $type");
218     }
219     elsif ( $action eq 'checkin' ) {
220         Koha::Exception->throw("after_circ_action called with action: $action, ref: " . ref($checkout));
221     }
222 }
223
224 sub after_hold_action {
225     my ( $self, $params ) = @_;
226
227     my $action = $params->{action};
228     my $hold   = $params->{payload}->{hold};
229
230     Koha::Exception->throw(
231         "after_hold_action called with action: $action, ref: " . ref($hold) );
232 }
233
234 sub api_routes {
235     my ( $self, $args ) = @_;
236
237     my $spec = qq{
238 {
239   "/patrons/bother": {
240     "get": {
241       "x-mojo-to": "Test::Controller#bother",
242       "operationId": "BotherPatron",
243       "tags": ["patrons"],
244       "produces": [
245         "application/json"
246       ],
247       "responses": {
248         "200": {
249           "description": "A bothered patron",
250           "schema": {
251               "type": "object",
252                 "properties": {
253                   "bothered": {
254                     "description": "If the patron has been bothered",
255                     "type": "boolean"
256                   }
257                 }
258           }
259         },
260         "401": {
261           "description": "An error occurred",
262           "schema": {
263               "type": "object",
264               "properties": {
265                 "error": {
266                   "description": "An explanation for the error",
267                   "type": "string"
268                 }
269               }
270           }
271         }
272       },
273       "x-koha-authorization": {
274         "permissions": {
275           "borrowers": "1"
276         }
277       }
278     }
279   },
280   "/public/patrons/bother": {
281     "get": {
282       "x-mojo-to": "Test::Controller#bother",
283       "operationId": "PubliclyBotherPatron",
284       "tags": ["patrons"],
285       "produces": [
286         "application/json"
287       ],
288       "responses": {
289         "200": {
290           "description": "A bothered patron",
291           "schema": {
292               "type": "object",
293               "properties": {
294                 "bothered": {
295                   "description": "If the patron has been bothered",
296                   "type": "boolean"
297                 }
298               }
299           }
300         },
301         "401": {
302           "description": "Authentication required",
303           "schema": {
304             "type": "object",
305             "properties": {
306               "error": {
307                 "description": "An explanation for the error",
308                 "type": "string"
309               }
310             }
311           }
312         }
313       }
314     }
315   }
316 }
317     };
318
319     return decode_json($spec);
320 }
321
322 sub check_password {
323     my ( $self, $args ) = @_;
324
325     my $password = $args->{'password'};
326     if ( $password && $password =~ m/^\d{4}$/ ) {
327         return { error => 0 };
328     }
329     else {
330         return {
331             error => 1,
332             msg   => "PIN should be four digits"
333         };
334     }
335 }
336
337 sub intranet_catalog_biblio_tab {
338     my @tabs;
339     push @tabs,
340       Koha::Plugins::Tab->new(
341         {
342             title   => 'Tab 1',
343             content => 'This is content for tab 1'
344         }
345       );
346
347     push @tabs,
348       Koha::Plugins::Tab->new(
349         {
350             title   => 'Tab 2',
351             content => 'This is content for tab 2'
352         }
353       );
354
355     return @tabs;
356 }
357
358 sub background_tasks {
359     return {
360         foo => 'MyPlugin::Class::Foo',
361         bar => 'MyPlugin::Class::Bar',
362     };
363 }
364
365 sub after_account_action {
366     my ( $self, $params ) = @_;
367
368     my $action = $params->{action};
369     my $line   = $params->{payload}->{line};
370     my $type   = $params->{payload}->{type};
371
372     Koha::Exception->throw(
373         "after_account_action called with action: $action, type: $type, ref: " . ref($line) );
374 }
375
376 sub after_recall_action {
377     my ( $self, $params ) = @_;
378
379     my $action = $params->{action};
380     my $recall   = $params->{payload}->{recall};
381
382     Koha::Exception->throw(
383         "after_recall_action called with action: $action, ref: " . ref($recall) );
384 }
385
386 sub template_include_paths {
387     my ($self) = @_;
388
389     return [
390         $self->mbf_path('inc'),
391     ];
392 }
393
394 sub ill_table_actions {
395     my ( $self, $table_actions ) = @_;
396
397     push(
398         @{$$table_actions},
399         {
400             button_link_text           => 'Test text',
401             append_column_data_to_link => 1,
402             button_class               => 'test class',
403             button_link                => 'test link'
404         }
405     );
406 }
407
408 sub _private_sub {
409     return "";
410 }
411
412 1;