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

(-)a/t/db_dependent/Items/DelItem.t (-35 lines)
Lines 1-35 Link Here
1
use Modern::Perl;
2
3
# FIXME This file must be removed and the test moved to Koha::Item->delete
4
5
use MARC::Record;
6
use C4::Items;
7
use C4::Biblio;
8
9
use Koha::Items;
10
11
use t::lib::TestBuilder;
12
13
use Test::More tests => 2;
14
15
my $schema = Koha::Database->schema;
16
$schema->storage->txn_begin;
17
my $builder = t::lib::TestBuilder->new;
18
19
my $library = $builder->build({
20
    source => 'Branch',
21
});
22
23
my $biblio = $builder->build_sample_biblio();
24
25
my $item = $builder->build_sample_item(
26
    {
27
        biblionumber => $biblio->biblionumber,
28
        library      => $library->{branchcode}
29
    }
30
);
31
32
my $deleted = $item->delete;
33
is( $deleted, 1, "DelItem should return 1 if the item has been deleted" );
34
my $deleted_item = Koha::Items->find($item->itemnumber);
35
is( $deleted_item, undef, "DelItem with biblionumber parameter - the item should be deleted." );
(-)a/t/db_dependent/Items_DelItemCheck.t (-171 lines)
Lines 1-171 Link Here
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 C4::Circulation;
21
use Koha::Database;
22
use Koha::Items;
23
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
26
27
use Test::More tests => 9;
28
use Test::MockModule;
29
30
BEGIN {
31
    use_ok('C4::Items');
32
}
33
34
my $builder = t::lib::TestBuilder->new();
35
my $schema = Koha::Database->new->schema;
36
# Begin transaction
37
$schema->storage->txn_begin;
38
39
my $branch = $builder->build(
40
    {
41
        source => 'Branch',
42
    }
43
);
44
45
my $module = new Test::MockModule('C4::Context');
46
$module->mock('userenv', sub {
47
    {  flags  => 0,
48
       branch => $branch->{branchcode}
49
    }
50
});
51
52
my $branch2 = $builder->build(
53
    {
54
        source => 'Branch',
55
    }
56
);
57
58
my $category = $builder->build(
59
    {
60
        source => 'Category',
61
    }
62
);
63
64
my $patron = $builder->build(
65
    {
66
        source => 'Borrower',
67
        value  => {
68
            categorycode => $category->{categorycode},
69
            branchcode   => $branch->{branchcode},
70
        },
71
    }
72
);
73
74
my $biblio = $builder->build(
75
    {
76
        source => 'Biblio',
77
        value  => {
78
            branchcode => $branch->{branchcode},
79
        },
80
    }
81
);
82
83
my $item = $builder->build_object(
84
    {
85
        class => 'Koha::Items',
86
        value  => {
87
            biblionumber  => $biblio->{biblionumber},
88
            homebranch    => $branch->{branchcode},
89
            holdingbranch => $branch->{branchcode},
90
            withdrawn    => 0,       # randomly assigned value may block return.
91
            withdrawn_on => undef,
92
        },
93
    }
94
);
95
96
# book_on_loan
97
98
AddIssue( $patron, $item->barcode );
99
100
is(
101
    $item->safe_to_delete,
102
    'book_on_loan',
103
    'Koha::Item->safe_to_delete reports item on loan',
104
);
105
106
is(
107
    $item->safe_delete,
108
    'book_on_loan',
109
    'item that is on loan cannot be deleted',
110
);
111
112
AddReturn( $item->barcode, $branch->{branchcode} );
113
114
# book_reserved is tested in t/db_dependent/Reserves.t
115
116
# not_same_branch
117
t::lib::Mocks::mock_preference('IndependentBranches', 1);
118
$item->set( { homebranch => $branch2->{branchcode}, holdingbranch => $branch2->{branchcode} })->store;
119
120
$item->discard_changes;
121
is(
122
    $item->safe_to_delete,
123
    'not_same_branch',
124
    'Koha::Item->safe_to_delete reports IndependentBranches restriction',
125
);
126
127
is(
128
    $item->safe_delete,
129
    'not_same_branch',
130
    'IndependentBranches prevents deletion at another branch',
131
);
132
133
$item->set( { homebranch => $branch->{branchcode}, holdingbranch => $branch->{branchcode} })->store;
134
135
# linked_analytics
136
137
{ # codeblock to limit scope of $module->mock
138
139
    my $module = Test::MockModule->new('C4::Items');
140
    $module->mock( GetAnalyticsCount => sub { return 1 } );
141
142
    is(
143
        $item->safe_to_delete,
144
        'linked_analytics',
145
        'Koha::Item->safe_to_delete reports linked analytics',
146
    );
147
148
    is(
149
        $item->safe_delete,
150
        'linked_analytics',
151
        'Linked analytics prevents deletion of item',
152
    );
153
154
}
155
156
is(
157
    $item->safe_to_delete,
158
    1,
159
    'Koha::Item->safe_to_delete shows item safe to delete'
160
);
161
162
$item->safe_delete,
163
164
my $test_item = Koha::Items->find( $item->itemnumber );
165
166
is( $test_item, undef,
167
    "Koha::Item->safe_delete should delete item if safe_to_delete returns true"
168
);
169
170
$schema->storage->txn_rollback;
171
(-)a/t/db_dependent/Koha/Item.t (-2 / +109 lines)
Lines 19-30 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 4;
22
use Test::More tests => 5;
23
23
24
use C4::Biblio;
24
use C4::Biblio;
25
use C4::Circulation;
25
26
26
use Koha::Items;
27
use Koha::Items;
27
use Koha::Database;
28
use Koha::Database;
29
use Koha::Old::Items;
28
30
29
use t::lib::TestBuilder;
31
use t::lib::TestBuilder;
30
use t::lib::Mocks;
32
use t::lib::Mocks;
Lines 329-331 subtest 'pickup_locations' => sub { Link Here
329
331
330
    $schema->storage->txn_rollback;
332
    $schema->storage->txn_rollback;
331
};
333
};
332
- 
334
335
subtest 'deletion' => sub {
336
    plan tests => 11;
337
338
    $schema->storage->txn_begin;
339
340
    my $biblio = $builder->build_sample_biblio();
341
342
    my $item = $builder->build_sample_item(
343
        {
344
            biblionumber => $biblio->biblionumber,
345
        }
346
    );
347
348
    is( ref( $item->move_to_deleted ), 'Koha::Schema::Result::Deleteditem', 'Koha::Item->move_to_deleted should return the Deleted item' )
349
      ;    # FIXME This should be Koha::Deleted::Item
350
    is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 1, '->move_to_deleted must have moved the item to deleteditem' );
351
    $item = $builder->build_sample_item(
352
        {
353
            biblionumber => $biblio->biblionumber,
354
        }
355
    );
356
    $item->delete;
357
    is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 0, '->move_to_deleted must not have moved the item to deleteditem' );
358
359
360
    my $library   = $builder->build_object({ class => 'Koha::Libraries' });
361
    my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
362
    t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
363
364
    my $patron = $builder->build_object({class => 'Koha::Patrons'});
365
    $item = $builder->build_sample_item({ library => $library->branchcode });
366
367
    # book_on_loan
368
    C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
369
370
    is(
371
        $item->safe_to_delete,
372
        'book_on_loan',
373
        'Koha::Item->safe_to_delete reports item on loan',
374
    );
375
376
    is(
377
        $item->safe_delete,
378
        'book_on_loan',
379
        'item that is on loan cannot be deleted',
380
    );
381
382
    AddReturn( $item->barcode, $library->branchcode );
383
384
    # book_reserved is tested in t/db_dependent/Reserves.t
385
386
    # not_same_branch
387
    t::lib::Mocks::mock_preference('IndependentBranches', 1);
388
    my $item_2 = $builder->build_sample_item({ library => $library_2->branchcode });
389
390
    is(
391
        $item_2->safe_to_delete,
392
        'not_same_branch',
393
        'Koha::Item->safe_to_delete reports IndependentBranches restriction',
394
    );
395
396
    is(
397
        $item_2->safe_delete,
398
        'not_same_branch',
399
        'IndependentBranches prevents deletion at another branch',
400
    );
401
402
    # linked_analytics
403
404
    { # codeblock to limit scope of $module->mock
405
406
        my $module = Test::MockModule->new('C4::Items');
407
        $module->mock( GetAnalyticsCount => sub { return 1 } );
408
409
        $item->discard_changes;
410
        is(
411
            $item->safe_to_delete,
412
            'linked_analytics',
413
            'Koha::Item->safe_to_delete reports linked analytics',
414
        );
415
416
        is(
417
            $item->safe_delete,
418
            'linked_analytics',
419
            'Linked analytics prevents deletion of item',
420
        );
421
422
    }
423
424
    is(
425
        $item->safe_to_delete,
426
        1,
427
        'Koha::Item->safe_to_delete shows item safe to delete'
428
    );
429
430
    $item->safe_delete,
431
432
    my $test_item = Koha::Items->find( $item->itemnumber );
433
434
    is( $test_item, undef,
435
        "Koha::Item->safe_delete should delete item if safe_to_delete returns true"
436
    );
437
438
    $schema->storage->txn_rollback;
439
};

Return to bug 23463