3 # Copyright 2015 Koha Development team
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 Test::More tests => 6;
25 use Koha::AdditionalContents;
27 use Koha::DateUtils qw( dt_from_string );
29 use t::lib::TestBuilder;
31 my $schema = Koha::Database->new->schema;
32 my $builder = t::lib::TestBuilder->new;
34 subtest 'Koha::AdditionalContents basic test' => sub {
38 $schema->storage->txn_begin;
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(
46 location => 'staff_only',
47 branchcode => $library->{branchcode},
52 content => 'content for news 1',
55 $new_news_item_1->translated_contents( [$content_1] );
56 my $new_news_item_2 = Koha::AdditionalContent->new(
60 location => 'staff_only',
61 branchcode => $library->{branchcode},
65 title => 'another news',
66 content => 'content for news 2',
69 $new_news_item_2->translated_contents( [$content_2] );
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' );
74 my $retrieved_news_item_1 = Koha::AdditionalContents->find( $new_news_item_1->id )->translated_contents->next;
76 $retrieved_news_item_1->title, $content_1->{title},
77 'Find a news_item by id should return the correct news_item'
79 is( $retrieved_news_item_1->content, $content_1->{content}, 'The content method return the content of the news' );
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');
89 $retrieved_translated_content->content, $content_2->{content},
90 'default content is returned for non-existing translated interface'
92 $retrieved_translated_content = $new_news_item_2->translated_content('nl-NL');
94 $retrieved_translated_content->content, $translated_content->{content},
95 'translated content is returned if it existsOB'
98 $new_news_item_1->delete;
99 is( Koha::AdditionalContents->search->count, $nb_of_news + 1, 'Delete should have deleted the news_item' );
101 $schema->storage->txn_rollback;
104 subtest '->is_expired' => sub {
108 $schema->storage->txn_begin;
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',
116 expirationdate => $today,
119 my $new_expired = $builder->build_object({
120 class => 'Koha::AdditionalContents',
122 expirationdate => $yesterday,
125 my $new_not_expired = $builder->build_object({
126 class => 'Koha::AdditionalContents',
128 expirationdate => $tomorrow,
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');
136 $schema->storage->txn_rollback;
139 subtest '->library' => sub {
143 $schema->storage->txn_begin;
145 my $library = $builder->build_object({ class => 'Koha::Libraries' });
147 my $new_with_library = $builder->build_object({
148 class => 'Koha::AdditionalContents',
150 branchcode => $library->branchcode
153 my $new_without_library = $builder->build_object({
154 class => 'Koha::AdditionalContents',
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');
163 ok(!$new_without_library->library, 'New item without library does not have library relation');
165 $schema->storage->txn_rollback;
168 subtest '->author' => sub {
171 $schema->storage->txn_begin;
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' );
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' );
183 $schema->storage->txn_rollback;
186 subtest '->search_for_display' => sub {
190 $schema->storage->txn_begin;
192 Koha::AdditionalContents->search->delete;
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' });
200 my $new_expired = $builder->build_object({
201 class => 'Koha::AdditionalContents',
203 expirationdate => $yesterday,
204 published_on => $today,
206 location => 'staff_and_opac',
211 $new_expired->translated_contents( [ { lang => 'default', content => '' } ] );
212 my $new_not_expired = $builder->build_object({
213 class => 'Koha::AdditionalContents',
215 expirationdate => $tomorrow,
216 published_on => $today,
218 location => 'staff_and_opac',
223 $new_not_expired->translated_contents( [ { lang => 'default', content => '' } ] );
224 my $new_not_active = $builder->build_object({
225 class => 'Koha::AdditionalContents',
227 expirationdate => $tomorrow,
228 published_on => $tomorrow,
230 location => 'staff_and_opac',
235 $new_not_active->translated_contents( [ { lang => 'default', content => '' } ] );
236 my $new_slip= $builder->build_object({
237 class => 'Koha::AdditionalContents',
239 expirationdate => $tomorrow,
240 published_on => $today,
242 location => 'staff_only',
243 branchcode => $library1->branchcode,
247 $new_slip->translated_contents( [ { lang => 'default', content => '' } ] );
248 my $new_intra = $builder->build_object({
249 class => 'Koha::AdditionalContents',
251 expirationdate => $tomorrow,
252 published_on => $today,
254 location => 'staff_only',
255 branchcode => $library2->branchcode,
259 $new_intra->translated_contents( [ { lang => 'default', content => '' } ] );
260 my $new_intra2 = $builder->build_object({
261 class => 'Koha::AdditionalContents',
263 expirationdate => $tomorrow,
264 published_on => $today,
266 location => 'staff_only',
271 $new_intra2->translated_contents( [ { lang => 'default', content => '' } ] );
273 my $news = Koha::AdditionalContents->search_for_display({ location => 'staff_only' });
274 is($news->count, 1, "There is 1 news for all staff");
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" );
280 $news = Koha::AdditionalContents->search_for_display({ location => 'opac_only' });
281 is($news->count, 0, "There are 0 news for OPAC only");
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 ");
286 # TODO We should add more tests here
288 $schema->storage->txn_rollback;
291 subtest 'find_best_match' => sub {
293 $schema->storage->txn_begin;
295 my $library01 = $builder->build_object( { class => 'Koha::Libraries' } );
296 my $html01 = $builder->build_object(
298 class => 'Koha::AdditionalContents',
299 value => { category => 'html_customizations', location => 'test_best_match', branchcode => undef },
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' };
305 Koha::AdditionalContents->find_best_match($params)->id, $default_content->id,
306 'Found all branches, lang default'
309 my $html02 = $builder->build_object(
311 class => 'Koha::AdditionalContents',
312 value => { category => 'html_customizations', location => 'test_best_match', branchcode => undef },
315 my ($translated_content) = $html02->translated_contents( [ { lang => 'nl-NL', content => '' } ] )->as_list;
317 Koha::AdditionalContents->find_best_match($params)->id, $translated_content->id,
318 'Found all branches, lang nl-NL'
321 $params->{library_id} = $library01->id;
322 $html02->branchcode( $library01->id )->store;
324 Koha::AdditionalContents->find_best_match($params)->id, $translated_content->id,
325 'Found library01, lang nl-NL'
328 # Note: find_best_match is tested further via $libary->opac_info; see t/db_dependent/Koha/Library.t
330 $schema->storage->txn_rollback;