View | Details | Raw Unified | Return to bug 22544
Collapse All | Expand All

(-)a/t/db_dependent/Koha/News.t (-28 / +239 lines)
Lines 20-58 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 5;
22
use Test::More tests => 5;
23
use Test::Exception;
23
24
24
use Koha::NewsItem;
25
use Koha::News;
25
use Koha::News;
26
use Koha::Database;
26
use Koha::Database;
27
use Koha::DateUtils;
27
28
28
use t::lib::TestBuilder;
29
use t::lib::TestBuilder;
29
30
30
my $schema = Koha::Database->new->schema;
31
my $schema = Koha::Database->new->schema;
31
$schema->storage->txn_begin;
32
33
my $builder = t::lib::TestBuilder->new;
32
my $builder = t::lib::TestBuilder->new;
34
my $library = $builder->build({ source => 'Branch'});
35
my $nb_of_news = Koha::News->search->count;
36
my $new_news_item_1 = Koha::NewsItem->new({
37
    branchcode => $library->{branchcode},
38
    title => 'a news',
39
    content => 'content for news 1',
40
})->store;
41
my $new_news_item_2 = Koha::NewsItem->new({
42
    branchcode => $library->{branchcode},
43
    title => 'another news',
44
    content => 'content for news 2',
45
})->store;
46
47
like( $new_news_item_1->idnew, qr|^\d+$|, 'Adding a new news_item should have set the idnew');
48
is( Koha::News->search->count, $nb_of_news + 2, 'The 2 news should have been added' );
49
50
my $retrieved_news_item_1 = Koha::News->find( $new_news_item_1->idnew );
51
is( $retrieved_news_item_1->title, $new_news_item_1->title, 'Find a news_item by id should return the correct news_item' );
52
is( $retrieved_news_item_1->content, $new_news_item_1->content, 'The content method return the content of the news');
53
54
$retrieved_news_item_1->delete;
55
is( Koha::News->search->count, $nb_of_news + 1, 'Delete should have deleted the news_item' );
56
57
$schema->storage->txn_rollback;
58
33
59
- 
34
subtest 'Koha::News 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::News->search->count;
42
    my $new_news_item_1 = Koha::NewsItem->new({
43
        branchcode => $library->{branchcode},
44
        title => 'a news',
45
        content => 'content for news 1',
46
    })->store;
47
    my $new_news_item_2 = Koha::NewsItem->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::News->search->count, $nb_of_news + 2, 'The 2 news should have been added' );
55
56
    my $retrieved_news_item_1 = Koha::News->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::News->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 $tommorow = dt_from_string->add( days => 1 );
75
    my $new_today = $builder->build_object({
76
        class => 'Koha::News',
77
        value => {
78
            expirationdate => $today,
79
        }
80
    });
81
    my $new_expired = $builder->build_object({
82
        class => 'Koha::News',
83
        value => {
84
            expirationdate => $yesterday,
85
        }
86
    });
87
    my $new_not_expired = $builder->build_object({
88
        class => 'Koha::News',
89
        value => {
90
            expirationdate => $tommorow,
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::News',
111
        value => {
112
            branchcode => $library->branchcode
113
        }
114
    });
115
    my $new_without_library = $builder->build_object({
116
        class => 'Koha::News',
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
132
    plan tests => 3;
133
134
    $schema->storage->txn_begin;
135
136
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
137
138
    my $new_with_author = $builder->build_object({
139
        class => 'Koha::News',
140
        value => {
141
            borrowernumber => $patron->borrowernumber
142
        }
143
    });
144
    my $new_without_author = $builder->build_object({
145
        class => 'Koha::News',
146
        value => {
147
            borrowernumber => undef
148
        }
149
    });
150
151
    ok($new_with_author->author, 'News item with author have author relation');
152
    is($new_with_author->author->borrowernumber, $patron->borrowernumber, 'The patron linked with new item is right');
153
154
    ok(!$new_without_author->author, 'New item without author does not have author relation');
155
156
    $schema->storage->txn_rollback;
157
};
158
159
subtest '->search_for_display' => sub {
160
161
    plan tests => 12;
162
163
    $schema->storage->txn_begin;
164
165
    Koha::News->search->delete;
166
167
    my $today = dt_from_string;
168
    my $yesterday = dt_from_string->add( days => -1 );
169
    my $tommorow = dt_from_string->add( days => 1 );
170
    my $library1 = $builder->build_object({ class => 'Koha::Libraries' });
171
    my $library2 = $builder->build_object({ class => 'Koha::Libraries' });
172
173
    my $new_expired = $builder->build_object({
174
        class => 'Koha::News',
175
        value => {
176
            expirationdate => $yesterday,
177
            timestamp => $today,
178
            lang => '',
179
            branchcode => undef,
180
            number => 1,
181
        }
182
    });
183
    my $new_not_expired = $builder->build_object({
184
        class => 'Koha::News',
185
        value => {
186
            expirationdate => $tommorow,
187
            timestamp => $today,
188
            lang => '',
189
            branchcode => undef,
190
            number => 2,
191
        }
192
    });
193
    my $new_not_active = $builder->build_object({
194
        class => 'Koha::News',
195
        value => {
196
            expirationdate => $tommorow,
197
            timestamp => $tommorow,
198
            lang => '',
199
            branchcode => undef,
200
            number => 3,
201
        }
202
    });
203
    my $new_slip= $builder->build_object({
204
        class => 'Koha::News',
205
        value => {
206
            expirationdate => $tommorow,
207
            timestamp => $today,
208
            lang => 'slip',
209
            branchcode => $library1->branchcode,
210
            number => 4,
211
        }
212
    });
213
    my $new_intra = $builder->build_object({
214
        class => 'Koha::News',
215
        value => {
216
            expirationdate => $tommorow,
217
            timestamp => $today,
218
            lang => 'koha',
219
            branchcode => $library2->branchcode,
220
            number => 5,
221
        }
222
    });
223
    my $new_intra2 = $builder->build_object({
224
        class => 'Koha::News',
225
        value => {
226
            expirationdate => $tommorow,
227
            timestamp => $today,
228
            lang => 'koha',
229
            branchcode => undef,
230
            number => 5,
231
        }
232
    });
233
    my $news = Koha::News->search_for_display;
234
235
    is($news->count, 4, 'Active and not expired news');
236
    is($news->next->number, 2, 'News items are returned in correct order');
237
238
    $news = Koha::News->search_for_display({ type => 'slip'});
239
    is($news->count, 2, 'Slip and all type returned');
240
241
    $news = Koha::News->search_for_display({ type => 'koha'});
242
    is($news->count, 3, 'Intranet and all');
243
244
    $new_not_expired->lang('OpacNavRight_en')->store;
245
    $news = Koha::News->search_for_display({ type => 'OpacNavRight', lang => 'en'});
246
    is($news->count, 1, 'OpacNavRight');
247
    is($news->next->idnew, $new_not_expired->idnew, 'Returned the right new item');
248
249
    $new_intra->lang('')->store;
250
    $news = Koha::News->search_for_display({ type => 'opac', lang => 'en'});
251
    is($news->count, 1, 'Only all type is returned');
252
    $new_not_expired->lang('en')->store;
253
    $news = Koha::News->search_for_display({ type => 'opac', lang => 'en'});
254
    is($news->count, 2, 'Opac en and all is returned');
255
256
    $news = Koha::News->search_for_display({ library_id => $library1->branchcode });
257
    is($news->count, 3, 'Filtering by library returns right number of news items');
258
259
    $news = Koha::News->search_for_display({ library_id => $library2->branchcode});
260
    is($news->count, 3, 'Filtering by library returns right number of news items');
261
262
    $new_intra->branchcode($library1->branchcode)->store;
263
    $news = Koha::News->search_for_display({ library_id => $library2->branchcode});
264
    is($news->count, 2, 'Filtering by library returns right number of news items');
265
266
    throws_ok { Koha::News->search_for_display({type => 'opac'}) } 'Koha::Exceptions::BadParameter',
267
        'Exception raised when type is opac and no language given';
268
269
    $schema->storage->txn_rollback;
270
};

Return to bug 22544