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},
49 content => 'content for news 1',
52 my $new_news_item_2 = Koha::AdditionalContent->new(
56 location => 'staff_only',
57 branchcode => $library->{branchcode},
58 title => 'another news',
59 content => 'content for news 2',
63 like( $new_news_item_1->idnew, qr|^\d+$|, 'Adding a new news_item should have set the idnew');
64 is( Koha::AdditionalContents->search->count, $nb_of_news + 2, 'The 2 news should have been added' );
66 my $retrieved_news_item_1 = Koha::AdditionalContents->find( $new_news_item_1->idnew );
67 is( $retrieved_news_item_1->title, $new_news_item_1->title, 'Find a news_item by id should return the correct news_item' );
68 is( $retrieved_news_item_1->content, $new_news_item_1->content, 'The content method return the content of the news');
70 $retrieved_news_item_1->delete;
71 is( Koha::AdditionalContents->search->count, $nb_of_news + 1, 'Delete should have deleted the news_item' );
73 $schema->storage->txn_rollback;
76 subtest '->is_expired' => sub {
80 $schema->storage->txn_begin;
82 my $today = dt_from_string;
83 my $yesterday = dt_from_string->add( days => -1 );
84 my $tomorrow = dt_from_string->add( days => 1 );
85 my $new_today = $builder->build_object({
86 class => 'Koha::AdditionalContents',
88 expirationdate => $today,
91 my $new_expired = $builder->build_object({
92 class => 'Koha::AdditionalContents',
94 expirationdate => $yesterday,
97 my $new_not_expired = $builder->build_object({
98 class => 'Koha::AdditionalContents',
100 expirationdate => $tomorrow,
104 ok($new_expired->is_expired, 'Expired new is expired');
105 ok(!$new_not_expired->is_expired, 'Not expired new is not expired');
106 ok(!$new_today->is_expired, 'Today expiration date means the new is not expired');
108 $schema->storage->txn_rollback;
111 subtest '->library' => sub {
115 $schema->storage->txn_begin;
117 my $library = $builder->build_object({ class => 'Koha::Libraries' });
119 my $new_with_library = $builder->build_object({
120 class => 'Koha::AdditionalContents',
122 branchcode => $library->branchcode
125 my $new_without_library = $builder->build_object({
126 class => 'Koha::AdditionalContents',
132 ok($new_with_library->library, 'News item with library have library relation');
133 is($new_with_library->library->branchcode, $library->branchcode, 'The library linked with new item is right');
135 ok(!$new_without_library->library, 'New item without library does not have library relation');
137 $schema->storage->txn_rollback;
140 subtest '->author' => sub {
143 $schema->storage->txn_begin;
145 my $news_item = $builder->build_object({ class => 'Koha::AdditionalContents' });
146 my $author = $news_item->author;
147 is( ref($author), 'Koha::Patron', 'Koha::AdditionalContent->author returns a Koha::Patron object' );
151 $news_item = Koha::AdditionalContents->find($news_item->idnew);
152 is( ref($news_item), 'Koha::AdditionalContent', 'News are not deleted alongwith the author' );
153 is( $news_item->author, undef, '->author returns undef is the author has been deleted' );
155 $schema->storage->txn_rollback;
158 subtest '->search_for_display' => sub {
162 $schema->storage->txn_begin;
164 Koha::AdditionalContents->search->delete;
166 my $today = dt_from_string;
167 my $yesterday = dt_from_string->add( days => -1 );
168 my $tomorrow = dt_from_string->add( days => 1 );
169 my $library1 = $builder->build_object({ class => 'Koha::Libraries' });
170 my $library2 = $builder->build_object({ class => 'Koha::Libraries' });
172 my $new_expired = $builder->build_object({
173 class => 'Koha::AdditionalContents',
175 expirationdate => $yesterday,
176 published_on => $today,
178 location => 'staff_and_opac',
184 my $new_not_expired = $builder->build_object({
185 class => 'Koha::AdditionalContents',
187 expirationdate => $tomorrow,
188 published_on => $today,
190 location => 'staff_and_opac',
196 my $new_not_active = $builder->build_object({
197 class => 'Koha::AdditionalContents',
199 expirationdate => $tomorrow,
200 published_on => $tomorrow,
202 location => 'staff_and_opac',
208 my $new_slip= $builder->build_object({
209 class => 'Koha::AdditionalContents',
211 expirationdate => $tomorrow,
212 published_on => $today,
214 location => 'staff_only',
216 branchcode => $library1->branchcode,
220 my $new_intra = $builder->build_object({
221 class => 'Koha::AdditionalContents',
223 expirationdate => $tomorrow,
224 published_on => $today,
226 location => 'staff_only',
228 branchcode => $library2->branchcode,
232 my $new_intra2 = $builder->build_object({
233 class => 'Koha::AdditionalContents',
235 expirationdate => $tomorrow,
236 published_on => $today,
238 location => 'staff_only',
245 my $news = Koha::AdditionalContents->search_for_display({ location => 'staff_only' });
246 is($news->count, 1, "There is 1 news for all staff");
248 $news = Koha::AdditionalContents->search_for_display(
249 { location => 'staff_only', library_id => $library1->branchcode } );
250 is( $news->count, 2, "There are 2 news for staff at library1" );
252 $news = Koha::AdditionalContents->search_for_display({ location => 'opac_only' });
253 is($news->count, 0, "There are 0 news for OPAC only");
255 $news = Koha::AdditionalContents->search_for_display({ location => 'staff_and_opac' });
256 is($news->count, 1, "There is 1 news for all staff and all OPAC ");
258 # TODO We should add more tests here
260 $schema->storage->txn_rollback;
263 subtest 'find_best_match' => sub {
265 $schema->storage->txn_begin;
267 my $library01 = $builder->build_object({ class => 'Koha::Libraries' });
268 my $html01 = $builder->build_object({
269 class => 'Koha::AdditionalContents',
270 value => { category => 'html_customizations', location => 'test_best_match', branchcode => undef, lang => 'default' },
272 my $params = { category => 'html_customizations', location => 'test_best_match', lang => 'nl-NL' };
273 is( Koha::AdditionalContents->find_best_match($params)->idnew, $html01->idnew, 'Found all branches, lang default' );
275 my $html02 = $builder->build_object({
276 class => 'Koha::AdditionalContents',
277 value => { category => 'html_customizations', location => 'test_best_match', branchcode => undef, lang => 'nl-NL' },
279 is( Koha::AdditionalContents->find_best_match($params)->idnew, $html02->idnew, 'Found all branches, lang nl-NL' );
281 $params->{ library_id } = $library01->id;
282 $html02->branchcode( $library01->id )->store;
283 is( Koha::AdditionalContents->find_best_match($params)->idnew, $html02->idnew, 'Found library01, lang nl-NL' );
285 # Note: find_best_match is tested further via $libary->opac_info; see t/db_dependent/Koha/Library.t
287 $schema->storage->txn_rollback;