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