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

(-)a/t/db_dependent/Koha/News.t (-29 / +211 lines)
Lines 19-58 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 6;
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'});
33
35
my $nb_of_news = Koha::News->search->count;
34
subtest 'Koha::News basic test' => sub {
36
my $new_news_item_1 = Koha::NewsItem->new({
35
37
    branchcode => $library->{branchcode},
36
    plan tests => 5;
38
    title => 'a news',
37
39
    content => 'content for news 1',
38
    $schema->storage->txn_begin;
40
})->store;
39
41
my $new_news_item_2 = Koha::NewsItem->new({
40
    my $library = $builder->build({ source => 'Branch'});
42
    branchcode => $library->{branchcode},
41
    my $nb_of_news = Koha::News->search->count;
43
    title => 'another news',
42
    my $new_news_item_1 = Koha::NewsItem->new({
44
    content => 'content for news 2',
43
        branchcode => $library->{branchcode},
45
})->store;
44
        title => 'a news',
46
45
        content => 'content for news 1',
47
like( $new_news_item_1->idnew, qr|^\d+$|, 'Adding a new news_item should have set the idnew');
46
    })->store;
48
is( Koha::News->search->count, $nb_of_news + 2, 'The 2 news should have been added' );
47
    my $new_news_item_2 = Koha::NewsItem->new({
49
48
        branchcode => $library->{branchcode},
50
my $retrieved_news_item_1 = Koha::News->find( $new_news_item_1->idnew );
49
        title => 'another news',
51
is( $retrieved_news_item_1->title, $new_news_item_1->title, 'Find a news_item by id should return the correct news_item' );
50
        content => 'content for news 2',
52
is( $retrieved_news_item_1->content, $new_news_item_1->content, 'The content method return the content of the news');
51
    })->store;
53
52
54
$retrieved_news_item_1->delete;
53
    like( $new_news_item_1->idnew, qr|^\d+$|, 'Adding a new news_item should have set the idnew');
55
is( Koha::News->search->count, $nb_of_news + 1, 'Delete should have deleted the news_item' );
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
};
56
129
57
subtest '->author' => sub {
130
subtest '->author' => sub {
58
    plan tests => 3;
131
    plan tests => 3;
Lines 68-72 subtest '->author' => sub { Link Here
68
    is( $news_item->author, undef, '->author returns undef is the author has been deleted' );
141
    is( $news_item->author, undef, '->author returns undef is the author has been deleted' );
69
};
142
};
70
143
71
$schema->storage->txn_rollback;
144
subtest '->search_for_display' => sub {
72
145
146
    plan tests => 12;
147
148
    $schema->storage->txn_begin;
149
150
    Koha::News->search->delete;
151
152
    my $today = dt_from_string;
153
    my $yesterday = dt_from_string->add( days => -1 );
154
    my $tommorow = 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::News',
160
        value => {
161
            expirationdate => $yesterday,
162
            timestamp => $today,
163
            lang => '',
164
            branchcode => undef,
165
            number => 1,
166
        }
167
    });
168
    my $new_not_expired = $builder->build_object({
169
        class => 'Koha::News',
170
        value => {
171
            expirationdate => $tommorow,
172
            timestamp => $today,
173
            lang => '',
174
            branchcode => undef,
175
            number => 2,
176
        }
177
    });
178
    my $new_not_active = $builder->build_object({
179
        class => 'Koha::News',
180
        value => {
181
            expirationdate => $tommorow,
182
            timestamp => $tommorow,
183
            lang => '',
184
            branchcode => undef,
185
            number => 3,
186
        }
187
    });
188
    my $new_slip= $builder->build_object({
189
        class => 'Koha::News',
190
        value => {
191
            expirationdate => $tommorow,
192
            timestamp => $today,
193
            lang => 'slip',
194
            branchcode => $library1->branchcode,
195
            number => 4,
196
        }
197
    });
198
    my $new_intra = $builder->build_object({
199
        class => 'Koha::News',
200
        value => {
201
            expirationdate => $tommorow,
202
            timestamp => $today,
203
            lang => 'koha',
204
            branchcode => $library2->branchcode,
205
            number => 5,
206
        }
207
    });
208
    my $new_intra2 = $builder->build_object({
209
        class => 'Koha::News',
210
        value => {
211
            expirationdate => $tommorow,
212
            timestamp => $today,
213
            lang => 'koha',
214
            branchcode => undef,
215
            number => 5,
216
        }
217
    });
218
    my $news = Koha::News->search_for_display;
219
220
    is($news->count, 4, 'Active and not expired news');
221
    is($news->next->number, 2, 'News items are returned in correct order');
222
223
    $news = Koha::News->search_for_display({ type => 'slip'});
224
    is($news->count, 2, 'Slip and all type returned');
225
226
    $news = Koha::News->search_for_display({ type => 'koha'});
227
    is($news->count, 3, 'Intranet and all');
228
229
    $new_not_expired->lang('OpacNavRight_en')->store;
230
    $news = Koha::News->search_for_display({ type => 'OpacNavRight', lang => 'en'});
231
    is($news->count, 1, 'OpacNavRight');
232
    is($news->next->idnew, $new_not_expired->idnew, 'Returned the right new item');
233
234
    $new_intra->lang('')->store;
235
    $news = Koha::News->search_for_display({ type => 'opac', lang => 'en'});
236
    is($news->count, 1, 'Only all type is returned');
237
    $new_not_expired->lang('en')->store;
238
    $news = Koha::News->search_for_display({ type => 'opac', lang => 'en'});
239
    is($news->count, 2, 'Opac en and all is returned');
240
241
    $news = Koha::News->search_for_display({ library_id => $library1->branchcode });
242
    is($news->count, 3, 'Filtering by library returns right number of news items');
243
244
    $news = Koha::News->search_for_display({ library_id => $library2->branchcode});
245
    is($news->count, 3, 'Filtering by library returns right number of news items');
246
247
    $new_intra->branchcode($library1->branchcode)->store;
248
    $news = Koha::News->search_for_display({ library_id => $library2->branchcode});
249
    is($news->count, 2, 'Filtering by library returns right number of news items');
250
251
    throws_ok { Koha::News->search_for_display({type => 'opac'}) } 'Koha::Exceptions::BadParameter',
252
        'Exception raised when type is opac and no language given';
253
254
    $schema->storage->txn_rollback;
255
};
73
- 

Return to bug 22544