Bug 26351: Add plugin hooks to transform item barcodes
[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 item_barcode_transform {
97     my ( $self, $barcode ) = @_;
98     Koha::Exceptions::Exception->throw("item_barcode_transform called with parameter: $barcode");
99 }
100
101 sub configure {
102     my ( $self, $args ) = @_;
103     return "Koha::Plugin::Test::configure";
104 }
105
106 sub install {
107     my ( $self, $args ) = @_;
108     return "Koha::Plugin::Test::install";
109 }
110
111 sub upgrade {
112     my ( $self, $args ) = @_;
113     return "Koha::Plugin::Test::upgrade";
114 }
115
116 sub uninstall {
117     my ( $self, $args ) = @_;
118     return "Koha::Plugin::Test::uninstall";
119 }
120
121 sub test_output {
122     my ( $self ) = @_;
123     $self->output( '¡Hola output!', 'json' );
124 }
125
126 sub test_output_html {
127     my ( $self ) = @_;
128     $self->output_html( '¡Hola output_html!' );
129 }
130
131 sub api_namespace {
132     return "testplugin";
133 }
134
135 sub after_hold_create {
136     my ( $self, $param ) = @_;
137     Koha::Exceptions::Exception->throw("after_hold_create called with parameter " . ref($param) );
138 }
139
140 sub after_biblio_action {
141     my ( $self, $params ) = @_;
142     my $action    = $params->{action} // '';
143     my $biblio    = $params->{biblio};
144     my $biblio_id = $params->{biblio_id};
145
146     if ( $action ne 'delete' ) {
147         Koha::Exceptions::Exception->throw("after_biblio_action called with action: $action, ref: " . ref($biblio) );
148     }
149     else {
150         Koha::Exceptions::Exception->throw("after_biblio_action called with action: $action, id: $biblio_id") if $biblio_id;
151     }
152 }
153
154 sub after_item_action {
155     my ( $self, $params ) = @_;
156     my $action  = $params->{action} // '';
157     my $item    = $params->{item};
158     my $item_id = $params->{item_id};
159
160     if ( $action ne 'delete' ) {
161         my $itemnumber_defined = (defined $item->itemnumber) ? 'yes' : 'no';
162         my $item_id_defined    = (defined $item_id) ? 'yes' : 'no';
163         Koha::Exceptions::Exception->throw("after_item_action called with action: $action, ref: " . ref($item) . " ".
164                                            "item_id defined: $item_id_defined ".
165                                            "itemnumber defined: $itemnumber_defined" );
166     }
167     else {
168         Koha::Exceptions::Exception->throw("after_item_action called with action: $action, id: $item_id" ) if $item_id;
169     }
170 }
171
172 sub after_circ_action {
173     my ( $self, $params ) = @_;
174
175     my $action   = $params->{action};
176     my $checkout = $params->{payload}->{checkout};
177     my $payload  = $params->{payload};
178
179     my $type = $payload->{type};
180
181     if ( $action eq 'renewal' ) {
182         Koha::Exceptions::Exception->throw("after_circ_action called with action: $action, ref: " . ref($checkout));
183     }
184     elsif ( $action eq 'checkout') {
185         Koha::Exceptions::Exception->throw("after_circ_action called with action: $action, ref: " . ref($checkout) . " type: $type");
186     }
187     elsif ( $action eq 'checkin' ) {
188         Koha::Exceptions::Exception->throw("after_circ_action called with action: $action, ref: " . ref($checkout));
189     }
190 }
191
192 sub api_routes {
193     my ( $self, $args ) = @_;
194
195     my $spec = qq{
196 {
197   "/patrons/bother": {
198     "get": {
199       "x-mojo-to": "Test::Controller#bother",
200       "operationId": "BotherPatron",
201       "tags": ["patrons"],
202       "produces": [
203         "application/json"
204       ],
205       "responses": {
206         "200": {
207           "description": "A bothered patron",
208           "schema": {
209               "type": "object",
210                 "properties": {
211                   "bothered": {
212                     "description": "If the patron has been bothered",
213                     "type": "boolean"
214                   }
215                 }
216           }
217         },
218         "401": {
219           "description": "An error occurred",
220           "schema": {
221               "type": "object",
222               "properties": {
223                 "error": {
224                   "description": "An explanation for the error",
225                   "type": "string"
226                 }
227               }
228           }
229         }
230       },
231       "x-koha-authorization": {
232         "permissions": {
233           "borrowers": "1"
234         }
235       }
236     }
237   },
238   "/public/patrons/bother": {
239     "get": {
240       "x-mojo-to": "Test::Controller#bother",
241       "operationId": "PubliclyBotherPatron",
242       "tags": ["patrons"],
243       "produces": [
244         "application/json"
245       ],
246       "responses": {
247         "200": {
248           "description": "A bothered patron",
249           "schema": {
250               "type": "object",
251               "properties": {
252                 "bothered": {
253                   "description": "If the patron has been bothered",
254                   "type": "boolean"
255                 }
256               }
257           }
258         },
259         "401": {
260           "description": "Authentication required",
261           "schema": {
262             "type": "object",
263             "properties": {
264               "error": {
265                 "description": "An explanation for the error",
266                 "type": "string"
267               }
268             }
269           }
270         }
271       }
272     }
273   }
274 }
275     };
276
277     return decode_json($spec);
278 }
279
280 sub check_password {
281     my ( $self, $args ) = @_;
282
283     my $password = $args->{'password'};
284     if ( $password && $password =~ m/^\d{4}$/ ) {
285         return { error => 0 };
286     }
287     else {
288         return {
289             error => 1,
290             msg   => "PIN should be four digits"
291         };
292     }
293 }
294
295 sub intranet_catalog_biblio_tab {
296     my @tabs;
297     push @tabs,
298       Koha::Plugins::Tab->new(
299         {
300             title   => 'Tab 1',
301             content => 'This is content for tab 1'
302         }
303       );
304
305     push @tabs,
306       Koha::Plugins::Tab->new(
307         {
308             title   => 'Tab 2',
309             content => 'This is content for tab 2'
310         }
311       );
312
313     return @tabs;
314 }
315
316 sub test_call_recursive {
317     my ( $self, $value ) = @_;
318
319     return $value x 2;
320 }
321
322 sub _private_sub {
323     return "";
324 }
325
326 1;