Bug 29625: Add test for get_all_biblios_iterator
[koha.git] / t / db_dependent / Koha / SharedContent.t
1 #!/usr/bin/perl
2
3 # Copyright 2016 BibLibre Morgane Alonso
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use t::lib::TestBuilder;
23 use t::lib::Mocks;
24 use Test::MockModule;
25 use Test::MockObject;
26 use Test::More tests => 45;
27 use Koha::Database;
28 use Koha::Patrons;
29 use Koha::Subscriptions;
30
31 use HTTP::Status qw(:constants :is status_message);
32
33 use_ok('Koha::SharedContent');
34
35 my $schema = Koha::Database->new->schema;
36 $schema->storage->txn_begin;
37
38 my $builder = t::lib::TestBuilder->new();
39
40 my $want_error = 0;
41 my $post_request = 0;
42 my $query = {};
43
44 t::lib::Mocks::mock_config( 'mana_config', 'https://foo.bar');
45
46 is(Koha::SharedContent::get_sharing_url(), 'https://foo.bar', 'Mana URL');
47
48 my $result = Koha::SharedContent::search_entities('report', $query);
49 ok($result->{msg} =~ /Can\'t connect to foo.bar:443/, 'Unable to connect');
50 is($result->{code}, 500, 'Code is 500');
51
52 my $ua = Test::MockModule->new('LWP::UserAgent');
53 $ua->mock('simple_request', sub {
54         return mock_response();
55 });
56
57 $want_error = 1;
58 $query = {query => 'foo', usecomments => 1};
59 $result = Koha::SharedContent::search_entities('report', $query);
60 ok($result->{msg} =~ /^Error thrown by decoded_content/, 'Error in decoded_content');
61 is($result->{code}, 500, 'Code is 500');
62
63 $want_error = 0;
64 $query = {title => 'foo', usecomments => 1};
65 $result = Koha::SharedContent::search_entities('subscription', $query);
66 is($result->{code}, 200, 'search_entities success');
67
68 $result = Koha::SharedContent::get_entity_by_id('subscription', 23);
69 is($result->{code}, 200, 'get_entity_by_id success');
70
71 my $params = {
72     title => 'The English historical review',
73     issn => '0013-8266',
74     ean => '',
75     publishercode => 'Longman'
76 };
77
78 # Search a subscription.
79 my $request = Koha::SharedContent::build_request('get', 'subscription', $params);
80 is($request->method, 'GET', 'Get subscription - Method is get');
81
82 my %query = $request->uri->query_form;
83 is($query{title}, 'The English historical review', 'Check title');
84 is($query{issn}, '0013-8266', 'Check issn');
85 is($query{ean}, undef, 'Check ean');
86 is($query{publishercode}, 'Longman', 'Check publisher');
87
88 is($request->uri->path, '/subscription.json', 'Path is subscription');
89
90 # Get a report by id.
91 $request = Koha::SharedContent::build_request('getwithid', 'report', 26);
92 is($request->method, 'GET', 'Get with id - Method is get');
93
94 is($request->uri->path, '/report/26.json', 'Path is report/26.json');
95
96 # Share a report.
97 my $content = {
98     'kohaversion' => '17.06.00.008',
99     'language' => 'fr-FR',
100     'notes' => 'some notes',
101     'report_group' => '',
102     'exportemail' => 'xx@xx.com',
103     'report_name' => 'A useless report',
104     'savedsql' => 'SELECT * FROM ITEMS',
105     'type' => undef
106 };
107
108 $request = Koha::SharedContent::build_request('post', 'report', $content);
109 is($request->method, 'POST', 'Share report - Method is post');
110
111 is($request->uri->path, '/report.json', 'Path is report.json');
112
113 # prepare shared data
114 my $library = $builder->build_object({
115     class => 'Koha::Libraries',
116 });
117
118 my $loggedinuser = $builder->build_object({
119     class => 'Koha::Patrons',
120     value => {
121         email => '',
122         emailpro => '',
123         B_email => '',
124         branchcode => $library->branchcode,
125     }
126 });
127
128 my $biblio = $builder->build_sample_biblio;
129
130 my $subscriptionFrequency = $builder->build({
131     source => 'SubscriptionFrequency'
132 });
133
134 my $subscriptionNumberpattern = $builder->build({
135     source => 'SubscriptionNumberpattern'
136 });
137
138 my $subscription = $builder->build({
139     source => 'Subscription',
140     value => {
141         biblionumber => $biblio->biblionumber,
142         periodicity => $subscriptionFrequency->{id},
143         numberpattern => $subscriptionNumberpattern->{id},
144         mana_id => undef
145     }
146 });
147
148 t::lib::Mocks::mock_userenv({ patron => $loggedinuser});
149
150 t::lib::Mocks::mock_preference('language', 'en');
151
152 $post_request = 1;
153 $result = Koha::SharedContent::send_entity('en', $loggedinuser->borrowernumber, $subscription->{subscriptionid}, 'subscription');
154 is($result->{code}, 200, 'send_entity success');
155
156 my $s = Koha::Subscriptions->find($subscription->{subscriptionid});
157 is($s->mana_id, 5, 'Mana id is set');
158
159 $content = { resource_id => $subscription->{mana_id}, resource_type => 'subscription', message => 'My comment'};
160 $result = Koha::SharedContent::comment_entity('resource_comment', $content);
161 is($result->{code}, 200, 'Comment success');
162
163 my $data = Koha::SharedContent::prepare_entity_data(
164     '',
165     $loggedinuser->borrowernumber,
166     $subscription->{subscriptionid},
167     'subscription'
168 );
169
170 is($data->{language}, 'en', 'Language is set to default');
171 is($data->{exportemail}, $library->branchemail, 'Email is set with the userenv branch one');
172 is($data->{title}, $biblio->title, 'Shared title');
173 is($data->{sfdescription}, $subscriptionFrequency->{description}, 'Shared sfdescription');
174 is($data->{unit}, $subscriptionFrequency->{unit}, 'Shared unit');
175 is($data->{unitsperissue}, $subscriptionFrequency->{unitsperissue}, 'Shared unitsperissue');
176 is($data->{issuesperunit}, $subscriptionFrequency->{issuesperunit}, 'Shared issuesperunit');
177
178 is($data->{label}, $subscriptionNumberpattern->{label}, 'Shared np label');
179 is($data->{sndescription}, $subscriptionNumberpattern->{description}, 'Shared np description');
180 is($data->{numberingmethod}, $subscriptionNumberpattern->{numberingmethod}, 'Shared numberingmethod');
181 is($data->{label1}, $subscriptionNumberpattern->{label1}, 'Shared label1');
182 is($data->{add1}, $subscriptionNumberpattern->{add1}, 'Shared add1');
183 is($data->{every1}, $subscriptionNumberpattern->{every1}, 'Shared every1');
184 is($data->{whenmorethan1}, $subscriptionNumberpattern->{whenmorethan1}, 'Shared whenmorethan1');
185 is($data->{setto1}, $subscriptionNumberpattern->{setto1}, 'Shared setto1');
186 is($data->{numbering1}, $subscriptionNumberpattern->{numbering1}, 'Shared numbering1');
187 my $biblioitem = $biblio->biblioitem;
188 is($data->{issn}, $biblioitem->issn, 'Shared ISSN');
189 is($data->{ean}, $biblioitem->ean, 'Shared EAN');
190 is($data->{publishercode}, $biblioitem->publishercode, 'Shared publishercode');
191
192 sub mock_response {
193     my $response = Test::MockObject->new();
194
195     if ($want_error) {
196         $response->mock('code', sub {
197             return 500;
198         });
199         $response->mock('is_error', sub {
200             return 0;
201         });
202         $response->mock('decoded_content', sub {
203             die 'Error thrown by decoded_content';
204         });
205     } elsif ( $post_request ) {
206         $response->mock('code', sub {
207             return 200;
208         });
209         $response->mock('is_error', sub {
210             return 0;
211         });
212         $response->mock('decoded_content', sub {
213             return '{"code": "200", "msg": "foo", "id": "5"}';
214         });
215     } else {
216         $response->mock('code', sub {
217             return 200;
218         });
219         $response->mock('is_error', sub {
220             return 0;
221         });
222         $response->mock('decoded_content', sub {
223             return '';
224         });
225     }
226 }
227
228 # Increment request.
229 $request = Koha::SharedContent::build_request('increment',
230                                              'subscription',
231                                              12,
232                                              'foo');
233
234 is($request->method, 'POST', 'Increment subscription - Method is post');
235
236 %query = $request->uri->query_form;
237 is($query{id}, 12, 'Check id');
238 is($query{step}, 1, 'Step is default');
239 is($query{resource}, 'subscription', 'Check ressource');
240
241 is($request->uri->path, '/subscription/12.json/increment/foo', 'Path is subscription');
242
243 $schema->storage->txn_rollback;