Bug 17047: SQL reports management with Mana-KB
[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 => 44;
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('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}, '', '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 $loggedinuser = $builder->build({
115     source => 'Borrower',
116     value => {
117         email => '',
118         emailpro => '',
119         B_email => ''
120     }
121 });
122
123 my $library = $builder->build({
124     source => 'Branch',
125 });
126
127 my $biblio = $builder->build({
128     source => 'Biblio',
129 });
130
131 my $biblioitem = $builder->build({
132     source => 'Biblioitem',
133     value => {
134         biblionumber => $biblio->{biblionumber}
135     }
136 });
137
138 my $subscriptionFrequency = $builder->build({
139     source => 'SubscriptionFrequency'
140 });
141
142 my $subscriptionNumberpattern = $builder->build({
143     source => 'SubscriptionNumberpattern'
144 });
145
146 my $subscription = $builder->build({
147     source => 'Subscription',
148     value => {
149         biblionumber => $biblio->{biblionumber},
150         periodicity => $subscriptionFrequency->{id},
151         numberpattern => $subscriptionNumberpattern->{id},
152         mana_id => undef
153     }
154 });
155
156 C4::Context->_new_userenv('xxx');
157 C4::Context->set_userenv(0,0,0,
158     $loggedinuser->{firstname},
159     $loggedinuser->{surname},
160     $library->{branchcode},
161     'Midway Public Library', '', '', '');
162
163 t::lib::Mocks::mock_preference('language', 'en');
164
165 $post_request = 1;
166 $result = Koha::SharedContent::send_entity('en', $loggedinuser->{borrowernumber}, $subscription->{subscriptionid}, 'subscription');
167 is($result->{code}, 200, 'send_entity success');
168
169 my $s = Koha::Subscriptions->find($subscription->{subscriptionid});
170 is($s->mana_id, 5, 'Mana id is set');
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 my $branch = Koha::Libraries->find($library->{branchcode});
181 is($data->{exportemail}, $branch->branchemail, 'Email is set with the userenv branch one');
182 is($data->{title}, $biblio->{title}, 'Shared title');
183 is($data->{sfdescription}, $subscriptionFrequency->{description}, 'Shared sfdescription');
184 is($data->{unit}, $subscriptionFrequency->{unit}, 'Shared unit');
185 is($data->{unitsperissue}, $subscriptionFrequency->{unitsperissue}, 'Shared unitsperissue');
186 is($data->{issuesperunit}, $subscriptionFrequency->{issuesperunit}, 'Shared issuesperunit');
187
188 is($data->{label}, $subscriptionNumberpattern->{label}, 'Shared np label');
189 is($data->{sndescription}, $subscriptionNumberpattern->{description}, 'Shared np description');
190 is($data->{numberingmethod}, $subscriptionNumberpattern->{numberingmethod}, 'Shared numberingmethod');
191 is($data->{label1}, $subscriptionNumberpattern->{label1}, 'Shared label1');
192 is($data->{add1}, $subscriptionNumberpattern->{add1}, 'Shared add1');
193 is($data->{every1}, $subscriptionNumberpattern->{every1}, 'Shared every1');
194 is($data->{whenmorethan1}, $subscriptionNumberpattern->{whenmorethan1}, 'Shared whenmorethan1');
195 is($data->{setto1}, $subscriptionNumberpattern->{setto1}, 'Shared setto1');
196 is($data->{numbering1}, $subscriptionNumberpattern->{numbering1}, 'Shared numbering1');
197 is($data->{issn}, $biblioitem->{issn}, 'Shared ISSN');
198 is($data->{ean}, $biblioitem->{ean}, 'Shared EAN');
199 is($data->{publishercode}, $biblioitem->{publishercode}, 'Shared publishercode');
200
201 sub mock_response {
202     my $response = Test::MockObject->new();
203
204     if ($want_error) {
205         $response->mock('code', sub {
206             return 500;
207         });
208         $response->mock('is_error', sub {
209             return 0;
210         });
211         $response->mock('decoded_content', sub {
212             die 'Error thrown by decoded_content';
213         });
214     } elsif ( $post_request ) {
215         $response->mock('code', sub {
216             return 200;
217         });
218         $response->mock('is_error', sub {
219             return 0;
220         });
221         $response->mock('decoded_content', sub {
222             return '{"code": "200", "msg": "foo", "id": "5"}';
223         });
224     } else {
225         $response->mock('code', sub {
226             return 200;
227         });
228         $response->mock('is_error', sub {
229             return 0;
230         });
231         $response->mock('decoded_content', sub {
232             return '';
233         });
234     }
235 }
236
237 # Increment request.
238 $request = Koha::SharedContent::build_request('increment',
239                                              'subscription',
240                                              12,
241                                              'foo');
242
243 is($request->method, 'POST', 'Increment subscription - Method is post');
244
245 %query = $request->uri->query_form;
246 is($query{id}, 12, 'Check id');
247 is($query{step}, 1, 'Step is default');
248 is($query{resource}, 'subscription', 'Check ressource');
249
250 is($request->uri->path, '/subscription/12.json/increment/foo', 'Path is subscription');
251
252 $schema->storage->txn_rollback;