Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[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 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 => 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         {
44             category   => 'news',
45             code       => 'news_1',
46             location   => 'staff_only',
47             branchcode => $library->{branchcode},
48             title      => 'a news',
49             content    => 'content for news 1',
50         }
51     )->store;
52     my $new_news_item_2 = Koha::AdditionalContent->new(
53         {
54             category   => 'news',
55             code       => 'news_2',
56             location   => 'staff_only',
57             branchcode => $library->{branchcode},
58             title      => 'another news',
59             content    => 'content for news 2',
60         }
61     )->store;
62
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' );
65
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');
69
70     $retrieved_news_item_1->delete;
71     is( Koha::AdditionalContents->search->count, $nb_of_news + 1, 'Delete should have deleted the news_item' );
72
73     $schema->storage->txn_rollback;
74 };
75
76 subtest '->is_expired' => sub {
77
78     plan tests => 3;
79
80     $schema->storage->txn_begin;
81
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',
87         value => {
88             expirationdate => $today,
89         }
90     });
91     my $new_expired = $builder->build_object({
92         class => 'Koha::AdditionalContents',
93         value => {
94             expirationdate => $yesterday,
95         }
96     });
97     my $new_not_expired = $builder->build_object({
98         class => 'Koha::AdditionalContents',
99         value => {
100             expirationdate => $tomorrow,
101         }
102     });
103
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');
107
108     $schema->storage->txn_rollback;
109 };
110
111 subtest '->library' => sub {
112
113     plan tests => 3;
114
115     $schema->storage->txn_begin;
116
117     my $library = $builder->build_object({ class => 'Koha::Libraries' });
118
119     my $new_with_library = $builder->build_object({
120         class => 'Koha::AdditionalContents',
121         value => {
122             branchcode => $library->branchcode
123         }
124     });
125     my $new_without_library = $builder->build_object({
126         class => 'Koha::AdditionalContents',
127         value => {
128             branchcode => undef
129         }
130     });
131
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');
134
135     ok(!$new_without_library->library, 'New item without library does not have library relation');
136
137     $schema->storage->txn_rollback;
138 };
139
140 subtest '->author' => sub {
141     plan tests => 3;
142
143     $schema->storage->txn_begin;
144
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' );
148
149     $author->delete;
150
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' );
154
155     $schema->storage->txn_rollback;
156 };
157
158 subtest '->search_for_display' => sub {
159
160     plan tests => 4;
161
162     $schema->storage->txn_begin;
163
164     Koha::AdditionalContents->search->delete;
165
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' });
171
172     my $new_expired = $builder->build_object({
173         class => 'Koha::AdditionalContents',
174         value => {
175             expirationdate => $yesterday,
176             published_on => $today,
177             category => 'news',
178             location => 'staff_and_opac',
179             lang => 'default',
180             branchcode => undef,
181             number => 1,
182         }
183     });
184     my $new_not_expired = $builder->build_object({
185         class => 'Koha::AdditionalContents',
186         value => {
187             expirationdate => $tomorrow,
188             published_on => $today,
189             category => 'news',
190             location => 'staff_and_opac',
191             lang => 'default',
192             branchcode => undef,
193             number => 2,
194         }
195     });
196     my $new_not_active = $builder->build_object({
197         class => 'Koha::AdditionalContents',
198         value => {
199             expirationdate => $tomorrow,
200             published_on => $tomorrow,
201             category => 'news',
202             location => 'staff_and_opac',
203             lang => 'default',
204             branchcode => undef,
205             number => 3,
206         }
207     });
208     my $new_slip= $builder->build_object({
209         class => 'Koha::AdditionalContents',
210         value => {
211             expirationdate => $tomorrow,
212             published_on => $today,
213             category => 'news',
214             location => 'staff_only',
215             lang => 'default',
216             branchcode => $library1->branchcode,
217             number => 4,
218         }
219     });
220     my $new_intra = $builder->build_object({
221         class => 'Koha::AdditionalContents',
222         value => {
223             expirationdate => $tomorrow,
224             published_on => $today,
225             category => 'news',
226             location => 'staff_only',
227             lang => 'default',
228             branchcode => $library2->branchcode,
229             number => 5,
230         }
231     });
232     my $new_intra2 = $builder->build_object({
233         class => 'Koha::AdditionalContents',
234         value => {
235             expirationdate => $tomorrow,
236             published_on => $today,
237             category => 'news',
238             location => 'staff_only',
239             lang => 'default',
240             branchcode => undef,
241             number => 5,
242         }
243     });
244
245     my $news = Koha::AdditionalContents->search_for_display({ location => 'staff_only' });
246     is($news->count, 1, "There is 1 news for all staff");
247
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" );
251
252     $news = Koha::AdditionalContents->search_for_display({ location => 'opac_only' });
253     is($news->count, 0, "There are 0 news for OPAC only");
254
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 ");
257
258     # TODO We should add more tests here
259
260     $schema->storage->txn_rollback;
261 };