Bug 24387: Rename "News" with "Additional contents"
[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 => 5;
23 use Test::Exception;
24
25 use Koha::AdditionalContents;
26 use Koha::Database;
27 use Koha::DateUtils;
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 => 5;
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         branchcode => $library->{branchcode},
44         title => 'a news',
45         content => 'content for news 1',
46     })->store;
47     my $new_news_item_2 = Koha::AdditionalContent->new({
48         branchcode => $library->{branchcode},
49         title => 'another news',
50         content => 'content for news 2',
51     })->store;
52
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' );
55
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');
59
60     $retrieved_news_item_1->delete;
61     is( Koha::AdditionalContents->search->count, $nb_of_news + 1, 'Delete should have deleted the news_item' );
62
63     $schema->storage->txn_rollback;
64 };
65
66 subtest '->is_expired' => sub {
67
68     plan tests => 3;
69
70     $schema->storage->txn_begin;
71
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',
77         value => {
78             expirationdate => $today,
79         }
80     });
81     my $new_expired = $builder->build_object({
82         class => 'Koha::AdditionalContents',
83         value => {
84             expirationdate => $yesterday,
85         }
86     });
87     my $new_not_expired = $builder->build_object({
88         class => 'Koha::AdditionalContents',
89         value => {
90             expirationdate => $tomorrow,
91         }
92     });
93
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');
97
98     $schema->storage->txn_rollback;
99 };
100
101 subtest '->library' => sub {
102
103     plan tests => 3;
104
105     $schema->storage->txn_begin;
106
107     my $library = $builder->build_object({ class => 'Koha::Libraries' });
108
109     my $new_with_library = $builder->build_object({
110         class => 'Koha::AdditionalContents',
111         value => {
112             branchcode => $library->branchcode
113         }
114     });
115     my $new_without_library = $builder->build_object({
116         class => 'Koha::AdditionalContents',
117         value => {
118             branchcode => undef
119         }
120     });
121
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');
124
125     ok(!$new_without_library->library, 'New item without library does not have library relation');
126
127     $schema->storage->txn_rollback;
128 };
129
130 subtest '->author' => sub {
131     plan tests => 3;
132
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' );
136
137     $author->delete;
138
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' );
142 };
143
144 subtest '->search_for_display' => sub {
145
146     plan tests => 13;
147
148     $schema->storage->txn_begin;
149
150     Koha::AdditionalContents->search->delete;
151
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' });
157
158     my $new_expired = $builder->build_object({
159         class => 'Koha::AdditionalContents',
160         value => {
161             expirationdate => $yesterday,
162             published_on => $today,
163             category => 'news',
164             location => 'staff_and_opac',
165             lang => 'default',
166             branchcode => undef,
167             number => 1,
168         }
169     });
170     my $new_not_expired = $builder->build_object({
171         class => 'Koha::AdditionalContents',
172         value => {
173             expirationdate => $tomorrow,
174             published_on => $today,
175             category => 'news',
176             location => 'staff_and_opac',
177             lang => 'default',
178             branchcode => undef,
179             number => 2,
180         }
181     });
182     my $new_not_active = $builder->build_object({
183         class => 'Koha::AdditionalContents',
184         value => {
185             expirationdate => $tomorrow,
186             published_on => $tomorrow,
187             category => 'news',
188             location => 'staff_and_opac',
189             lang => 'default',
190             branchcode => undef,
191             number => 3,
192         }
193     });
194     my $new_slip= $builder->build_object({
195         class => 'Koha::AdditionalContents',
196         value => {
197             expirationdate => $tomorrow,
198             published_on => $today,
199             category => 'news',
200             location => 'staff_only',
201             lang => 'default',
202             branchcode => $library1->branchcode,
203             number => 4,
204         }
205     });
206     my $new_intra = $builder->build_object({
207         class => 'Koha::AdditionalContents',
208         value => {
209             expirationdate => $tomorrow,
210             published_on => $today,
211             category => 'news',
212             location => 'staff_only',
213             lang => 'default',
214             branchcode => $library2->branchcode,
215             number => 5,
216         }
217     });
218     my $new_intra2 = $builder->build_object({
219         class => 'Koha::AdditionalContents',
220         value => {
221             expirationdate => $tomorrow,
222             published_on => $today,
223             category => 'news',
224             location => 'staff_only',
225             lang => 'default',
226             branchcode => undef,
227             number => 5,
228         }
229     });
230     my $news = Koha::AdditionalContents->search_for_display;
231
232     # FIXME Rewrite tests here
233
234     $schema->storage->txn_rollback;
235 };