3 # Copyright 2016 BibLibre Morgane Alonso
5 # This file is part of Koha
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.
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.
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>.
22 use t
::lib
::TestBuilder
;
26 use Test
::More tests
=> 45;
29 use Koha
::Subscriptions
;
31 use HTTP
::Status
qw(:constants :is status_message);
33 use_ok
('Koha::SharedContent');
35 my $schema = Koha
::Database
->new->schema;
36 $schema->storage->txn_begin;
38 my $builder = t
::lib
::TestBuilder
->new();
44 t
::lib
::Mocks
::mock_config
( 'mana_config', 'https://foo.bar');
46 is
(Koha
::SharedContent
::get_sharing_url
(), 'https://foo.bar', 'Mana URL');
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');
52 my $ua = Test
::MockModule
->new('LWP::UserAgent');
53 $ua->mock('request', sub {
54 return mock_response
();
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');
64 $query = {title
=> 'foo', usecomments
=> 1};
65 $result = Koha
::SharedContent
::search_entities
('subscription', $query);
66 is
($result->{code
}, 200, 'search_entities success');
68 $result = Koha
::SharedContent
::get_entity_by_id
('subscription', 23);
69 is
($result->{code
}, 200, 'get_entity_by_id success');
72 title
=> 'The English historical review',
75 publishercode
=> 'Longman'
78 # Search a subscription.
79 my $request = Koha
::SharedContent
::build_request
('get', 'subscription', $params);
80 is
($request->method, 'GET', 'Get subscription - Method is get');
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');
88 is
($request->uri->path, '/subscription.json', 'Path is subscription');
91 $request = Koha
::SharedContent
::build_request
('getwithid', 'report', 26);
92 is
($request->method, 'GET', 'Get with id - Method is get');
94 is
($request->uri->path, '/report/26.json', 'Path is report/26.json');
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',
108 $request = Koha
::SharedContent
::build_request
('post', 'report', $content);
109 is
($request->method, 'POST', 'Share report - Method is post');
111 is
($request->uri->path, '/report.json', 'Path is report.json');
113 # prepare shared data
114 my $library = $builder->build_object({
115 class => 'Koha::Libraries',
118 my $loggedinuser = $builder->build_object({
119 class => 'Koha::Patrons',
124 branchcode
=> $library->branchcode,
128 my $biblio = $builder->build({
132 my $biblioitem = $builder->build({
133 source
=> 'Biblioitem',
135 biblionumber
=> $biblio->{biblionumber
}
139 my $subscriptionFrequency = $builder->build({
140 source
=> 'SubscriptionFrequency'
143 my $subscriptionNumberpattern = $builder->build({
144 source
=> 'SubscriptionNumberpattern'
147 my $subscription = $builder->build({
148 source
=> 'Subscription',
150 biblionumber
=> $biblio->{biblionumber
},
151 periodicity
=> $subscriptionFrequency->{id
},
152 numberpattern
=> $subscriptionNumberpattern->{id
},
157 t
::lib
::Mocks
::mock_userenv
({ patron
=> $loggedinuser});
159 t
::lib
::Mocks
::mock_preference
('language', 'en');
162 $result = Koha
::SharedContent
::send_entity
('en', $loggedinuser->borrowernumber, $subscription->{subscriptionid
}, 'subscription');
163 is
($result->{code
}, 200, 'send_entity success');
165 my $s = Koha
::Subscriptions
->find($subscription->{subscriptionid
});
166 is
($s->mana_id, 5, 'Mana id is set');
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');
172 my $data = Koha
::SharedContent
::prepare_entity_data
(
174 $loggedinuser->borrowernumber,
175 $subscription->{subscriptionid
},
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');
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');
201 my $response = Test
::MockObject
->new();
204 $response->mock('code', sub {
207 $response->mock('is_error', sub {
210 $response->mock('decoded_content', sub {
211 die 'Error thrown by decoded_content';
213 } elsif ( $post_request ) {
214 $response->mock('code', sub {
217 $response->mock('is_error', sub {
220 $response->mock('decoded_content', sub {
221 return '{"code": "200", "msg": "foo", "id": "5"}';
224 $response->mock('code', sub {
227 $response->mock('is_error', sub {
230 $response->mock('decoded_content', sub {
237 $request = Koha
::SharedContent
::build_request
('increment',
242 is
($request->method, 'POST', 'Increment subscription - Method is post');
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');
249 is
($request->uri->path, '/subscription/12.json/increment/foo', 'Path is subscription');
251 $schema->storage->txn_rollback;