Bug 21395: (QA follow-up) Remove some introduced issues
[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({
129     source => 'Biblio',
130 });
131
132 my $biblioitem = $builder->build({
133     source => 'Biblioitem',
134     value => {
135         biblionumber => $biblio->{biblionumber}
136     }
137 });
138
139 my $subscriptionFrequency = $builder->build({
140     source => 'SubscriptionFrequency'
141 });
142
143 my $subscriptionNumberpattern = $builder->build({
144     source => 'SubscriptionNumberpattern'
145 });
146
147 my $subscription = $builder->build({
148     source => 'Subscription',
149     value => {
150         biblionumber => $biblio->{biblionumber},
151         periodicity => $subscriptionFrequency->{id},
152         numberpattern => $subscriptionNumberpattern->{id},
153         mana_id => undef
154     }
155 });
156
157 t::lib::Mocks::mock_userenv({ patron => $loggedinuser});
158
159 t::lib::Mocks::mock_preference('language', 'en');
160
161 $post_request = 1;
162 $result = Koha::SharedContent::send_entity('en', $loggedinuser->borrowernumber, $subscription->{subscriptionid}, 'subscription');
163 is($result->{code}, 200, 'send_entity success');
164
165 my $s = Koha::Subscriptions->find($subscription->{subscriptionid});
166 is($s->mana_id, 5, 'Mana id is set');
167
168 $content = { resource_id => $subscription->{mana_id}, resource_type => 'subscription', message => 'My comment'};
169 $result = Koha::SharedContent::comment_entity('resource_comment', $content);
170 is($result->{code}, 200, 'Comment success');
171
172 my $data = Koha::SharedContent::prepare_entity_data(
173     '',
174     $loggedinuser->borrowernumber,
175     $subscription->{subscriptionid},
176     'subscription'
177 );
178
179 is($data->{language}, 'en', 'Language is set to default');
180 is($data->{exportemail}, $library->branchemail, 'Email is set with the userenv branch one');
181 is($data->{title}, $biblio->{title}, 'Shared title');
182 is($data->{sfdescription}, $subscriptionFrequency->{description}, 'Shared sfdescription');
183 is($data->{unit}, $subscriptionFrequency->{unit}, 'Shared unit');
184 is($data->{unitsperissue}, $subscriptionFrequency->{unitsperissue}, 'Shared unitsperissue');
185 is($data->{issuesperunit}, $subscriptionFrequency->{issuesperunit}, 'Shared issuesperunit');
186
187 is($data->{label}, $subscriptionNumberpattern->{label}, 'Shared np label');
188 is($data->{sndescription}, $subscriptionNumberpattern->{description}, 'Shared np description');
189 is($data->{numberingmethod}, $subscriptionNumberpattern->{numberingmethod}, 'Shared numberingmethod');
190 is($data->{label1}, $subscriptionNumberpattern->{label1}, 'Shared label1');
191 is($data->{add1}, $subscriptionNumberpattern->{add1}, 'Shared add1');
192 is($data->{every1}, $subscriptionNumberpattern->{every1}, 'Shared every1');
193 is($data->{whenmorethan1}, $subscriptionNumberpattern->{whenmorethan1}, 'Shared whenmorethan1');
194 is($data->{setto1}, $subscriptionNumberpattern->{setto1}, 'Shared setto1');
195 is($data->{numbering1}, $subscriptionNumberpattern->{numbering1}, 'Shared numbering1');
196 is($data->{issn}, $biblioitem->{issn}, 'Shared ISSN');
197 is($data->{ean}, $biblioitem->{ean}, 'Shared EAN');
198 is($data->{publishercode}, $biblioitem->{publishercode}, 'Shared publishercode');
199
200 sub mock_response {
201     my $response = Test::MockObject->new();
202
203     if ($want_error) {
204         $response->mock('code', sub {
205             return 500;
206         });
207         $response->mock('is_error', sub {
208             return 0;
209         });
210         $response->mock('decoded_content', sub {
211             die 'Error thrown by decoded_content';
212         });
213     } elsif ( $post_request ) {
214         $response->mock('code', sub {
215             return 200;
216         });
217         $response->mock('is_error', sub {
218             return 0;
219         });
220         $response->mock('decoded_content', sub {
221             return '{"code": "200", "msg": "foo", "id": "5"}';
222         });
223     } else {
224         $response->mock('code', sub {
225             return 200;
226         });
227         $response->mock('is_error', sub {
228             return 0;
229         });
230         $response->mock('decoded_content', sub {
231             return '';
232         });
233     }
234 }
235
236 # Increment request.
237 $request = Koha::SharedContent::build_request('increment',
238                                              'subscription',
239                                              12,
240                                              'foo');
241
242 is($request->method, 'POST', 'Increment subscription - Method is post');
243
244 %query = $request->uri->query_form;
245 is($query{id}, 12, 'Check id');
246 is($query{step}, 1, 'Step is default');
247 is($query{resource}, 'subscription', 'Check ressource');
248
249 is($request->uri->path, '/subscription/12.json/increment/foo', 'Path is subscription');
250
251 $schema->storage->txn_rollback;