Bug 31503: (QA follow-up) Fix number of tests in rebase
[koha.git] / t / db_dependent / Koha / AdditionalContents.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 Koha Development team
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 Test::More tests => 6;
23 use Test::Exception;
24
25 use Koha::AdditionalContents;
26 use Koha::Database;
27 use Koha::DateUtils qw( dt_from_string );
28
29 use t::lib::TestBuilder;
30
31 my $schema = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new;
33
34 subtest 'Koha::AdditionalContents basic test' => sub {
35
36     plan tests => 9;
37
38     $schema->storage->txn_begin;
39
40     my $library = $builder->build({ source => 'Branch'});
41     my $nb_of_news = Koha::AdditionalContents->search->count;
42     my $new_news_item_1 = Koha::AdditionalContent->new(
43         {
44             category   => 'news',
45             code       => 'news_1',
46             location   => 'staff_only',
47             branchcode => $library->{branchcode},
48         }
49     )->store;
50     my $content_1 = {
51         title   => 'a news',
52         content => 'content for news 1',
53         lang    => 'default',
54     };
55     $new_news_item_1->translated_contents( [$content_1] );
56     my $new_news_item_2 = Koha::AdditionalContent->new(
57         {
58             category   => 'news',
59             code       => 'news_2',
60             location   => 'staff_only',
61             branchcode => $library->{branchcode},
62         }
63     )->store;
64     my $content_2 = {
65         title   => 'another news',
66         content => 'content for news 2',
67         lang    => 'default',
68     };
69     $new_news_item_2->translated_contents( [$content_2] );
70
71     like( $new_news_item_1->id, qr|^\d+$|, 'Adding a new news_item should have set the id' );
72     is( Koha::AdditionalContents->search->count, $nb_of_news + 2, 'The 2 news should have been added' );
73
74     my $retrieved_news_item_1 = Koha::AdditionalContents->find( $new_news_item_1->id )->translated_contents->next;
75     is(
76         $retrieved_news_item_1->title, $content_1->{title},
77         'Find a news_item by id should return the correct news_item'
78     );
79     is( $retrieved_news_item_1->content, $content_1->{content}, 'The content method return the content of the news' );
80
81     my $default_content = $new_news_item_2->default_localization;
82     is( $default_content->content, $content_2->{content}, 'default_localization return the default content' );
83     my $translated_content = { lang => 'nl-NL', content => 'translated_content' };
84     $new_news_item_2->translated_contents( [ $translated_content, $content_2 ] )->as_list;
85     $default_content = $new_news_item_2->default_localization;
86     is( $default_content->content, $content_2->{content}, 'default_localization still return the default content' );
87     my $retrieved_translated_content = $new_news_item_2->translated_content('en');
88     is(
89         $retrieved_translated_content->content, $content_2->{content},
90         'default content is returned for non-existing translated interface'
91     );
92     $retrieved_translated_content = $new_news_item_2->translated_content('nl-NL');
93     is(
94         $retrieved_translated_content->content, $translated_content->{content},
95         'translated content is returned if it existsOB'
96     );
97
98     $new_news_item_1->delete;
99     is( Koha::AdditionalContents->search->count, $nb_of_news + 1, 'Delete should have deleted the news_item' );
100
101     $schema->storage->txn_rollback;
102 };
103
104 subtest '->is_expired' => sub {
105
106     plan tests => 3;
107
108     $schema->storage->txn_begin;
109
110     my $today = dt_from_string;
111     my $yesterday = dt_from_string->add( days => -1 );
112     my $tomorrow = dt_from_string->add( days => 1 );
113     my $new_today = $builder->build_object({
114         class => 'Koha::AdditionalContents',
115         value => {
116             expirationdate => $today,
117         }
118     });
119     my $new_expired = $builder->build_object({
120         class => 'Koha::AdditionalContents',
121         value => {
122             expirationdate => $yesterday,
123         }
124     });
125     my $new_not_expired = $builder->build_object({
126         class => 'Koha::AdditionalContents',
127         value => {
128             expirationdate => $tomorrow,
129         }
130     });
131
132     ok($new_expired->is_expired, 'Expired new is expired');
133     ok(!$new_not_expired->is_expired, 'Not expired new is not expired');
134     ok(!$new_today->is_expired, 'Today expiration date means the new is not expired');
135
136     $schema->storage->txn_rollback;
137 };
138
139 subtest '->library' => sub {
140
141     plan tests => 3;
142
143     $schema->storage->txn_begin;
144
145     my $library = $builder->build_object({ class => 'Koha::Libraries' });
146
147     my $new_with_library = $builder->build_object({
148         class => 'Koha::AdditionalContents',
149         value => {
150             branchcode => $library->branchcode
151         }
152     });
153     my $new_without_library = $builder->build_object({
154         class => 'Koha::AdditionalContents',
155         value => {
156             branchcode => undef
157         }
158     });
159
160     ok($new_with_library->library, 'News item with library have library relation');
161     is($new_with_library->library->branchcode, $library->branchcode, 'The library linked with new item is right');
162
163     ok(!$new_without_library->library, 'New item without library does not have library relation');
164
165     $schema->storage->txn_rollback;
166 };
167
168 subtest '->author' => sub {
169     plan tests => 3;
170
171     $schema->storage->txn_begin;
172
173     my $news_item = $builder->build_object({ class => 'Koha::AdditionalContents' });
174     my $author = $news_item->author;
175     is( ref($author), 'Koha::Patron', 'Koha::AdditionalContent->author returns a Koha::Patron object' );
176
177     $author->delete;
178
179     $news_item = Koha::AdditionalContents->find( $news_item->id );
180     is( ref($news_item), 'Koha::AdditionalContent', 'News are not deleted alongwith the author' );
181     is( $news_item->author, undef, '->author returns undef is the author has been deleted' );
182
183     $schema->storage->txn_rollback;
184 };
185
186 subtest '->search_for_display' => sub {
187
188     plan tests => 4;
189
190     $schema->storage->txn_begin;
191
192     Koha::AdditionalContents->search->delete;
193
194     my $today = dt_from_string;
195     my $yesterday = dt_from_string->add( days => -1 );
196     my $tomorrow = dt_from_string->add( days => 1 );
197     my $library1 = $builder->build_object({ class => 'Koha::Libraries' });
198     my $library2 = $builder->build_object({ class => 'Koha::Libraries' });
199
200     my $new_expired = $builder->build_object({
201         class => 'Koha::AdditionalContents',
202         value => {
203             expirationdate => $yesterday,
204             published_on => $today,
205             category => 'news',
206             location => 'staff_and_opac',
207             branchcode => undef,
208             number => 1,
209         }
210     });
211     $new_expired->translated_contents( [ { lang => 'default', content => '' } ] );
212     my $new_not_expired = $builder->build_object({
213         class => 'Koha::AdditionalContents',
214         value => {
215             expirationdate => $tomorrow,
216             published_on => $today,
217             category => 'news',
218             location => 'staff_and_opac',
219             branchcode => undef,
220             number => 2,
221         }
222     });
223     $new_not_expired->translated_contents( [ { lang => 'default', content => '' } ] );
224     my $new_not_active = $builder->build_object({
225         class => 'Koha::AdditionalContents',
226         value => {
227             expirationdate => $tomorrow,
228             published_on => $tomorrow,
229             category => 'news',
230             location => 'staff_and_opac',
231             branchcode => undef,
232             number => 3,
233         }
234     });
235     $new_not_active->translated_contents( [ { lang => 'default', content => '' } ] );
236     my $new_slip= $builder->build_object({
237         class => 'Koha::AdditionalContents',
238         value => {
239             expirationdate => $tomorrow,
240             published_on => $today,
241             category => 'news',
242             location => 'staff_only',
243             branchcode => $library1->branchcode,
244             number => 4,
245         }
246     });
247     $new_slip->translated_contents( [ { lang => 'default', content => '' } ] );
248     my $new_intra = $builder->build_object({
249         class => 'Koha::AdditionalContents',
250         value => {
251             expirationdate => $tomorrow,
252             published_on => $today,
253             category => 'news',
254             location => 'staff_only',
255             branchcode => $library2->branchcode,
256             number => 5,
257         }
258     });
259     $new_intra->translated_contents( [ { lang => 'default', content => '' } ] );
260     my $new_intra2 = $builder->build_object({
261         class => 'Koha::AdditionalContents',
262         value => {
263             expirationdate => $tomorrow,
264             published_on => $today,
265             category => 'news',
266             location => 'staff_only',
267             branchcode => undef,
268             number => 5,
269         }
270     });
271     $new_intra2->translated_contents( [ { lang => 'default', content => '' } ] );
272
273     my $news = Koha::AdditionalContents->search_for_display({ location => 'staff_only' });
274     is($news->count, 1, "There is 1 news for all staff");
275
276     $news = Koha::AdditionalContents->search_for_display(
277         { location => 'staff_only', library_id => $library1->branchcode } );
278     is( $news->count, 2, "There are 2 news for staff at library1" );
279
280     $news = Koha::AdditionalContents->search_for_display({ location => 'opac_only' });
281     is($news->count, 0, "There are 0 news for OPAC only");
282
283     $news = Koha::AdditionalContents->search_for_display({ location => 'staff_and_opac' });
284     is($news->count, 1, "There is 1 news for all staff and all OPAC ");
285
286     # TODO We should add more tests here
287
288     $schema->storage->txn_rollback;
289 };
290
291 subtest 'find_best_match' => sub {
292     plan tests => 3;
293     $schema->storage->txn_begin;
294
295     my $library01 = $builder->build_object( { class => 'Koha::Libraries' } );
296     my $html01    = $builder->build_object(
297         {
298             class => 'Koha::AdditionalContents',
299             value => { category => 'html_customizations', location => 'test_best_match', branchcode => undef },
300         }
301     );
302     my ($default_content) = $html01->translated_contents( [ { lang => 'default', content => '' } ] )->as_list;
303     my $params = { category => 'html_customizations', location => 'test_best_match', lang => 'nl-NL' };
304     is(
305         Koha::AdditionalContents->find_best_match($params)->id, $default_content->id,
306         'Found all branches, lang default'
307     );
308
309     my $html02 = $builder->build_object(
310         {
311             class => 'Koha::AdditionalContents',
312             value => { category => 'html_customizations', location => 'test_best_match', branchcode => undef },
313         }
314     );
315     my ($translated_content) = $html02->translated_contents( [ { lang => 'nl-NL', content => '' } ] )->as_list;
316     is(
317         Koha::AdditionalContents->find_best_match($params)->id, $translated_content->id,
318         'Found all branches, lang nl-NL'
319     );
320
321     $params->{library_id} = $library01->id;
322     $html02->branchcode( $library01->id )->store;
323     is(
324         Koha::AdditionalContents->find_best_match($params)->id, $translated_content->id,
325         'Found library01, lang nl-NL'
326     );
327
328     # Note: find_best_match is tested further via $libary->opac_info; see t/db_dependent/Koha/Library.t
329
330     $schema->storage->txn_rollback;
331     }