|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# This file is part of Koha |
| 4 |
# |
| 5 |
# Koha is free software; you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use Test::More tests => 5; |
| 21 |
use Test::Exception; |
| 22 |
use Test::MockModule; |
| 23 |
|
| 24 |
use Koha::ArticleRequest::Status; |
| 25 |
use Koha::ArticleRequests; |
| 26 |
|
| 27 |
use t::lib::TestBuilder; |
| 28 |
|
| 29 |
my $schema = Koha::Database->new->schema; |
| 30 |
my $builder = t::lib::TestBuilder->new; |
| 31 |
|
| 32 |
subtest 'store() tests' => sub { |
| 33 |
|
| 34 |
plan tests => 8; |
| 35 |
|
| 36 |
$schema->storage->txn_begin; |
| 37 |
|
| 38 |
my $mock_article_request = Test::MockModule->new('Koha::ArticleRequest'); |
| 39 |
my $notify_called; |
| 40 |
$mock_article_request->mock( 'notify', sub { $notify_called = 1; } ); |
| 41 |
|
| 42 |
my $item = $builder->build_sample_item; |
| 43 |
my $article_request = Koha::ArticleRequest->new( |
| 44 |
{ itemnumber => $item->id, biblionumber => $item->biblionumber } ); |
| 45 |
|
| 46 |
throws_ok { $article_request->store; } |
| 47 |
'Koha::Exceptions::MissingParameter', |
| 48 |
'Exception thrown because of missing borrowernumber parameter'; |
| 49 |
|
| 50 |
my $deleted_patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 51 |
my $deleted_patron_id = $deleted_patron->id; |
| 52 |
$deleted_patron->delete; |
| 53 |
|
| 54 |
# use the invalid patron to trigger fk exception |
| 55 |
$article_request->borrowernumber($deleted_patron_id); |
| 56 |
|
| 57 |
throws_ok { $article_request->store; } |
| 58 |
'Koha::Exceptions::Object::FKConstraint', |
| 59 |
'Exception thrown because of missing borrowernumber parameter'; |
| 60 |
|
| 61 |
is( $@->broken_fk, 'borrowernumber', 'Exception build correctly (1/2)' ); |
| 62 |
is( $@->value, $deleted_patron_id, 'Exception build correctly (1/2)' ); |
| 63 |
|
| 64 |
my $patron_can_request = 0; |
| 65 |
|
| 66 |
my $mocked_patron = Test::MockModule->new('Koha::Patron'); |
| 67 |
$mocked_patron->mock( 'can_request_article', sub { return $patron_can_request; } ); |
| 68 |
|
| 69 |
my $patron = $builder->build_object({ class => 'Koha::Patrons' }); |
| 70 |
$article_request->borrowernumber( $patron->id ); |
| 71 |
|
| 72 |
throws_ok |
| 73 |
{ $article_request->store; } |
| 74 |
'Koha::Exceptions::ArticleRequest::LimitReached', |
| 75 |
'Exception thrown because patron reached its limit'; |
| 76 |
|
| 77 |
# allow the patron to place requests |
| 78 |
$patron_can_request = 1; |
| 79 |
$article_request->store->discard_changes; |
| 80 |
|
| 81 |
is( $article_request->status, Koha::ArticleRequest::Status::Requested, 'Status set correctly' ); |
| 82 |
isnt( $article_request->created_on, undef, 'created_on set correctly' ); |
| 83 |
ok( $notify_called, 'Notify was called' ); |
| 84 |
|
| 85 |
$schema->storage->txn_rollback; |
| 86 |
}; |
| 87 |
|
| 88 |
subtest 'set_pending() tests' => sub { |
| 89 |
|
| 90 |
plan tests => 2; |
| 91 |
|
| 92 |
$schema->storage->txn_begin; |
| 93 |
|
| 94 |
my $ar_mock = Test::MockModule->new('Koha::ArticleRequest'); |
| 95 |
$ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } ); |
| 96 |
|
| 97 |
my $ar = $builder->build_object( |
| 98 |
{ |
| 99 |
class => 'Koha::ArticleRequests', |
| 100 |
value => { status => Koha::ArticleRequest::Status::Requested } |
| 101 |
} |
| 102 |
); |
| 103 |
|
| 104 |
$ar->set_pending()->discard_changes; |
| 105 |
|
| 106 |
is( $ar->status, Koha::ArticleRequest::Status::Pending ); |
| 107 |
|
| 108 |
$schema->storage->txn_rollback; |
| 109 |
}; |
| 110 |
|
| 111 |
subtest 'request() tests' => sub { |
| 112 |
|
| 113 |
plan tests => 2; |
| 114 |
|
| 115 |
$schema->storage->txn_begin; |
| 116 |
|
| 117 |
my $ar_mock = Test::MockModule->new('Koha::ArticleRequest'); |
| 118 |
$ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } ); |
| 119 |
|
| 120 |
my $ar = $builder->build_object( |
| 121 |
{ |
| 122 |
class => 'Koha::ArticleRequests', |
| 123 |
value => { status => Koha::ArticleRequest::Status::Pending } |
| 124 |
} |
| 125 |
); |
| 126 |
|
| 127 |
$ar->request()->discard_changes; |
| 128 |
|
| 129 |
is( $ar->status, Koha::ArticleRequest::Status::Requested ); |
| 130 |
|
| 131 |
$schema->storage->txn_rollback; |
| 132 |
}; |
| 133 |
|
| 134 |
subtest 'process() tests' => sub { |
| 135 |
|
| 136 |
plan tests => 2; |
| 137 |
|
| 138 |
$schema->storage->txn_begin; |
| 139 |
|
| 140 |
my $ar_mock = Test::MockModule->new('Koha::ArticleRequest'); |
| 141 |
$ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } ); |
| 142 |
|
| 143 |
my $ar = $builder->build_object( |
| 144 |
{ |
| 145 |
class => 'Koha::ArticleRequests', |
| 146 |
value => { status => Koha::ArticleRequest::Status::Requested } |
| 147 |
} |
| 148 |
); |
| 149 |
|
| 150 |
$ar->process()->discard_changes; |
| 151 |
|
| 152 |
is( $ar->status, Koha::ArticleRequest::Status::Processing ); |
| 153 |
|
| 154 |
$schema->storage->txn_rollback; |
| 155 |
}; |
| 156 |
|
| 157 |
subtest 'complete() tests' => sub { |
| 158 |
|
| 159 |
plan tests => 2; |
| 160 |
|
| 161 |
$schema->storage->txn_begin; |
| 162 |
|
| 163 |
my $ar_mock = Test::MockModule->new('Koha::ArticleRequest'); |
| 164 |
$ar_mock->mock( 'notify', sub { ok( 1, '->notify() called' ); } ); |
| 165 |
|
| 166 |
my $ar = $builder->build_object( |
| 167 |
{ |
| 168 |
class => 'Koha::ArticleRequests', |
| 169 |
value => { status => Koha::ArticleRequest::Status::Requested } |
| 170 |
} |
| 171 |
); |
| 172 |
|
| 173 |
$ar->complete()->discard_changes; |
| 174 |
|
| 175 |
is( $ar->status, Koha::ArticleRequest::Status::Completed ); |
| 176 |
|
| 177 |
$schema->storage->txn_rollback; |
| 178 |
}; |