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 => 5;
25 use Koha::AdditionalContents;
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({
43 branchcode => $library->{branchcode},
45 content => 'content for news 1',
47 my $new_news_item_2 = Koha::AdditionalContent->new({
48 branchcode => $library->{branchcode},
49 title => 'another news',
50 content => 'content for news 2',
53 like( $new_news_item_1->idnew, qr|^\d+$|, 'Adding a new news_item should have set the idnew');
54 is( Koha::AdditionalContents->search->count, $nb_of_news + 2, 'The 2 news should have been added' );
56 my $retrieved_news_item_1 = Koha::AdditionalContents->find( $new_news_item_1->idnew );
57 is( $retrieved_news_item_1->title, $new_news_item_1->title, 'Find a news_item by id should return the correct news_item' );
58 is( $retrieved_news_item_1->content, $new_news_item_1->content, 'The content method return the content of the news');
60 $retrieved_news_item_1->delete;
61 is( Koha::AdditionalContents->search->count, $nb_of_news + 1, 'Delete should have deleted the news_item' );
63 $schema->storage->txn_rollback;
66 subtest '->is_expired' => sub {
70 $schema->storage->txn_begin;
72 my $today = dt_from_string;
73 my $yesterday = dt_from_string->add( days => -1 );
74 my $tomorrow = dt_from_string->add( days => 1 );
75 my $new_today = $builder->build_object({
76 class => 'Koha::AdditionalContents',
78 expirationdate => $today,
81 my $new_expired = $builder->build_object({
82 class => 'Koha::AdditionalContents',
84 expirationdate => $yesterday,
87 my $new_not_expired = $builder->build_object({
88 class => 'Koha::AdditionalContents',
90 expirationdate => $tomorrow,
94 ok($new_expired->is_expired, 'Expired new is expired');
95 ok(!$new_not_expired->is_expired, 'Not expired new is not expired');
96 ok(!$new_today->is_expired, 'Today expiration date means the new is not expired');
98 $schema->storage->txn_rollback;
101 subtest '->library' => sub {
105 $schema->storage->txn_begin;
107 my $library = $builder->build_object({ class => 'Koha::Libraries' });
109 my $new_with_library = $builder->build_object({
110 class => 'Koha::AdditionalContents',
112 branchcode => $library->branchcode
115 my $new_without_library = $builder->build_object({
116 class => 'Koha::AdditionalContents',
122 ok($new_with_library->library, 'News item with library have library relation');
123 is($new_with_library->library->branchcode, $library->branchcode, 'The library linked with new item is right');
125 ok(!$new_without_library->library, 'New item without library does not have library relation');
127 $schema->storage->txn_rollback;
130 subtest '->author' => sub {
133 my $news_item = $builder->build_object({ class => 'Koha::AdditionalContents' });
134 my $author = $news_item->author;
135 is( ref($author), 'Koha::Patron', 'Koha::AdditionalContent->author returns a Koha::Patron object' );
139 $news_item = Koha::AdditionalContents->find($news_item->idnew);
140 is( ref($news_item), 'Koha::AdditionalContent', 'News are not deleted alongwith the author' );
141 is( $news_item->author, undef, '->author returns undef is the author has been deleted' );
144 subtest '->search_for_display' => sub {
148 $schema->storage->txn_begin;
150 Koha::AdditionalContents->search->delete;
152 my $today = dt_from_string;
153 my $yesterday = dt_from_string->add( days => -1 );
154 my $tomorrow = dt_from_string->add( days => 1 );
155 my $library1 = $builder->build_object({ class => 'Koha::Libraries' });
156 my $library2 = $builder->build_object({ class => 'Koha::Libraries' });
158 my $new_expired = $builder->build_object({
159 class => 'Koha::AdditionalContents',
161 expirationdate => $yesterday,
162 published_on => $today,
164 location => 'staff_and_opac',
170 my $new_not_expired = $builder->build_object({
171 class => 'Koha::AdditionalContents',
173 expirationdate => $tomorrow,
174 published_on => $today,
176 location => 'staff_and_opac',
182 my $new_not_active = $builder->build_object({
183 class => 'Koha::AdditionalContents',
185 expirationdate => $tomorrow,
186 published_on => $tomorrow,
188 location => 'staff_and_opac',
194 my $new_slip= $builder->build_object({
195 class => 'Koha::AdditionalContents',
197 expirationdate => $tomorrow,
198 published_on => $today,
200 location => 'staff_only',
202 branchcode => $library1->branchcode,
206 my $new_intra = $builder->build_object({
207 class => 'Koha::AdditionalContents',
209 expirationdate => $tomorrow,
210 published_on => $today,
212 location => 'staff_only',
214 branchcode => $library2->branchcode,
218 my $new_intra2 = $builder->build_object({
219 class => 'Koha::AdditionalContents',
221 expirationdate => $tomorrow,
222 published_on => $today,
224 location => 'staff_only',
230 my $news = Koha::AdditionalContents->search_for_display;
232 # FIXME Rewrite tests here
234 $schema->storage->txn_rollback;