Bug 20589: Remove expanded_facet variable and fix tests
[koha.git] / t / db_dependent / Koha / SharedContent.t
blobc9b7e25c6a27d3634287b016ff1a8d1778f5ddea
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
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;
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();
40 my $want_error = 0;
41 my $post_request = 0;
42 my $query = {};
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();
55 });
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');
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');
68 $result = Koha::SharedContent::get_entity_by_id('subscription', 23);
69 is($result->{code}, 200, 'get_entity_by_id success');
71 my $params = {
72 title => 'The English historical review',
73 issn => '0013-8266',
74 ean => '',
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');
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');
94 is($request->uri->path, '/report/26.json', 'Path is report/26.json');
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
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',
120 value => {
121 email => '',
122 emailpro => '',
123 B_email => '',
124 branchcode => $library->branchcode,
128 my $biblio = $builder->build({
129 source => 'Biblio',
132 my $biblioitem = $builder->build({
133 source => 'Biblioitem',
134 value => {
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',
149 value => {
150 biblionumber => $biblio->{biblionumber},
151 periodicity => $subscriptionFrequency->{id},
152 numberpattern => $subscriptionNumberpattern->{id},
153 mana_id => undef
157 t::lib::Mocks::mock_userenv({ patron => $loggedinuser});
159 t::lib::Mocks::mock_preference('language', 'en');
161 $post_request = 1;
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},
176 'subscription'
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');
200 sub mock_response {
201 my $response = Test::MockObject->new();
203 if ($want_error) {
204 $response->mock('code', sub {
205 return 500;
207 $response->mock('is_error', sub {
208 return 0;
210 $response->mock('decoded_content', sub {
211 die 'Error thrown by decoded_content';
213 } elsif ( $post_request ) {
214 $response->mock('code', sub {
215 return 200;
217 $response->mock('is_error', sub {
218 return 0;
220 $response->mock('decoded_content', sub {
221 return '{"code": "200", "msg": "foo", "id": "5"}';
223 } else {
224 $response->mock('code', sub {
225 return 200;
227 $response->mock('is_error', sub {
228 return 0;
230 $response->mock('decoded_content', sub {
231 return '';
236 # Increment request.
237 $request = Koha::SharedContent::build_request('increment',
238 'subscription',
240 'foo');
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;