|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/env perl |
|
|
2 |
|
| 3 |
use Modern::Perl; |
| 4 |
use Test::More tests => 14; |
| 5 |
use Test::MockModule; |
| 6 |
|
| 7 |
use C4::Context; |
| 8 |
use C4::Circulation; |
| 9 |
use C4::Biblio; |
| 10 |
use C4::Items; |
| 11 |
use C4::Members; |
| 12 |
|
| 13 |
use Koha::Database; |
| 14 |
|
| 15 |
my $dbh = C4::Context->dbh; |
| 16 |
my $schema = Koha::Database->new()->schema(); |
| 17 |
|
| 18 |
# Start transaction |
| 19 |
$dbh->{RaiseError} = 1; |
| 20 |
$schema->storage->txn_begin(); |
| 21 |
|
| 22 |
$dbh->do(q{INSERT INTO itemtypes (itemtype) VALUES ('GTI_I_TEST')}); |
| 23 |
$schema->resultset('Category')->create({ categorycode => 'GTI_C_TEST' }); |
| 24 |
$schema->resultset('Branch')->create({ branchcode => 'GTI_B_1', branchname => 'GTI_B_1' }); |
| 25 |
$schema->resultset('Branch')->create({ branchcode => 'GTI_B_2', branchname => 'GTI_B_2' }); |
| 26 |
|
| 27 |
my $c4_context = Test::MockModule->new('C4::Context'); |
| 28 |
$c4_context->mock('userenv', sub { |
| 29 |
{ branch => 'GTI_B_1' } |
| 30 |
}); |
| 31 |
C4::Context->set_preference('item-level_itypes', '0'); |
| 32 |
|
| 33 |
my $biblionumber = create_biblio('Test 1', 'GTI_I_TEST'); |
| 34 |
AddItem({ |
| 35 |
barcode => 'GTI_BARCODE_001', |
| 36 |
homebranch => 'GTI_B_1', |
| 37 |
ccode => 'GTI_CCODE', |
| 38 |
}, $biblionumber); |
| 39 |
|
| 40 |
$biblionumber = create_biblio('Test 2', 'GTI_I_TEST'); |
| 41 |
AddItem({ |
| 42 |
barcode => 'GTI_BARCODE_002', |
| 43 |
homebranch => 'GTI_B_2', |
| 44 |
}, $biblionumber); |
| 45 |
|
| 46 |
my $borrowernumber = AddMember( |
| 47 |
userid => 'gti.test', |
| 48 |
categorycode => 'GTI_C_TEST', |
| 49 |
branchcode => 'GTI_B_1' |
| 50 |
); |
| 51 |
my $borrower = GetMember(borrowernumber => $borrowernumber); |
| 52 |
|
| 53 |
AddIssue($borrower, 'GTI_BARCODE_001'); |
| 54 |
AddIssue($borrower, 'GTI_BARCODE_002'); |
| 55 |
|
| 56 |
# |
| 57 |
# Start of tests |
| 58 |
# |
| 59 |
|
| 60 |
my @issues = GetTopIssues({count => 10, itemtype => 'GTI_I_TEST'}); |
| 61 |
is(scalar @issues, 2); |
| 62 |
is($issues[0]->{title}, 'Test 1'); |
| 63 |
is($issues[1]->{title}, 'Test 2'); |
| 64 |
|
| 65 |
@issues = GetTopIssues({count => 1, itemtype => 'GTI_I_TEST'}); |
| 66 |
is(scalar @issues, 1); |
| 67 |
is($issues[0]->{title}, 'Test 1'); |
| 68 |
|
| 69 |
@issues = GetTopIssues({count => 10, branch => 'GTI_B_2'}); |
| 70 |
is(scalar @issues, 1); |
| 71 |
is($issues[0]->{title}, 'Test 2'); |
| 72 |
|
| 73 |
@issues = GetTopIssues({count => 10, ccode => 'GTI_CCODE'}); |
| 74 |
is(scalar @issues, 1); |
| 75 |
is($issues[0]->{title}, 'Test 1'); |
| 76 |
|
| 77 |
@issues = GetTopIssues({count => 10, itemtype => 'GTI_I_TEST', newness => 1}); |
| 78 |
is(scalar @issues, 2); |
| 79 |
is($issues[0]->{title}, 'Test 1'); |
| 80 |
is($issues[1]->{title}, 'Test 2'); |
| 81 |
|
| 82 |
$dbh->do(q{ |
| 83 |
UPDATE biblio |
| 84 |
SET datecreated = DATE_SUB(datecreated, INTERVAL 2 DAY) |
| 85 |
WHERE biblionumber = ? |
| 86 |
}, undef, $biblionumber); |
| 87 |
|
| 88 |
@issues = GetTopIssues({count => 10, itemtype => 'GTI_I_TEST', newness => 1}); |
| 89 |
is(scalar @issues, 1); |
| 90 |
is($issues[0]->{title}, 'Test 1'); |
| 91 |
|
| 92 |
# |
| 93 |
# End of tests |
| 94 |
# |
| 95 |
|
| 96 |
$schema->storage->txn_rollback(); |
| 97 |
|
| 98 |
sub create_biblio { |
| 99 |
my ($title, $itemtype) = @_; |
| 100 |
|
| 101 |
my ($title_tag, $title_subfield) = GetMarcFromKohaField('biblio.title', ''); |
| 102 |
my ($it_tag, $it_subfield) = GetMarcFromKohaField('biblioitems.itemtype', ''); |
| 103 |
|
| 104 |
my $record = MARC::Record->new(); |
| 105 |
$record->append_fields( |
| 106 |
MARC::Field->new($title_tag, ' ', ' ', $title_subfield => $title), |
| 107 |
MARC::Field->new($it_tag, ' ', ' ', $it_subfield => $itemtype), |
| 108 |
); |
| 109 |
|
| 110 |
return AddBiblio($record, ''); |
| 111 |
} |