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

(-)a/C4/Items.pm (-4 / +13 lines)
Lines 2686-2697 sub _SearchItems_build_where_fragment { Link Here
2686
2686
2687
=head2 SearchItems
2687
=head2 SearchItems
2688
2688
2689
    my ($items, $total) = SearchItemsByField($filters, $params);
2689
    my ($items, $total) = SearchItems($filter, $params);
2690
2690
2691
Perform a search among items
2691
Perform a search among items
2692
2692
2693
$filters is a reference to an array of filters, where each filter is a hash with
2693
$filter is a reference to a hash which can be a filter, or a combination of filters.
2694
the following keys:
2694
2695
A filter has the following keys:
2695
2696
2696
=over 2
2697
=over 2
2697
2698
Lines 2703-2709 the following keys: Link Here
2703
2704
2704
=back
2705
=back
2705
2706
2706
A logical AND is used to combine filters.
2707
A combination of filters hash the following keys:
2708
2709
=over 2
2710
2711
=item * conjunction: 'AND' or 'OR'
2712
2713
=item * filters: array ref of filters
2714
2715
=back
2707
2716
2708
$params is a reference to a hash that can contain the following parameters:
2717
$params is a reference to a hash that can contain the following parameters:
2709
2718
(-)a/Koha/Item/Search/Field.pm (+4 lines)
Lines 19-24 sub AddItemSearchField { Link Here
19
    my ( $name, $label, $tagfield, $tagsubfield, $av_category ) =
19
    my ( $name, $label, $tagfield, $tagsubfield, $av_category ) =
20
      @$field{qw(name label tagfield tagsubfield authorised_values_category)};
20
      @$field{qw(name label tagfield tagsubfield authorised_values_category)};
21
21
22
    return unless ($name and $label and $tagfield);
23
22
    my $dbh = C4::Context->dbh;
24
    my $dbh = C4::Context->dbh;
23
    my $query = q{
25
    my $query = q{
24
        INSERT INTO items_search_fields (name, label, tagfield, tagsubfield, authorised_values_category)
26
        INSERT INTO items_search_fields (name, label, tagfield, tagsubfield, authorised_values_category)
Lines 36-41 sub ModItemSearchField { Link Here
36
    my ( $name, $label, $tagfield, $tagsubfield, $av_category ) =
38
    my ( $name, $label, $tagfield, $tagsubfield, $av_category ) =
37
      @$field{qw(name label tagfield tagsubfield authorised_values_category)};
39
      @$field{qw(name label tagfield tagsubfield authorised_values_category)};
38
40
41
    return unless ($name and $label and $tagfield);
42
39
    my $dbh = C4::Context->dbh;
43
    my $dbh = C4::Context->dbh;
40
    my $query = q{
44
    my $query = q{
41
        UPDATE items_search_fields
45
        UPDATE items_search_fields
(-)a/t/db_dependent/Items.t (+123 lines)
Lines 40-45 subtest 'General Add, Get and Del tests' => sub { Link Here
40
    $dbh->{RaiseError} = 1;
40
    $dbh->{RaiseError} = 1;
41
41
42
    # Create a biblio instance for testing
42
    # Create a biblio instance for testing
43
    diag("Creating biblio instance for testing.");
44
    C4::Context->set_preference('marcflavour', 'MARC21');
43
    my ($bibnum, $bibitemnum) = get_biblio();
45
    my ($bibnum, $bibitemnum) = get_biblio();
44
46
45
    # Add an item.
47
    # Add an item.
Lines 76-83 subtest 'GetHiddenItemnumbers tests' => sub { Link Here
76
    $dbh->{RaiseError} = 1;
78
    $dbh->{RaiseError} = 1;
77
79
78
    # Create a new biblio
80
    # Create a new biblio
81
    C4::Context->set_preference('marcflavour', 'MARC21');
79
    my ($biblionumber, $biblioitemnumber) = get_biblio();
82
    my ($biblionumber, $biblioitemnumber) = get_biblio();
80
83
84
    # Add branches if they don't exist
85
    if (not defined GetBranchDetail('CPL')) {
86
        ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'});
87
    }
88
    if (not defined GetBranchDetail('MPL')) {
89
        ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'});
90
    }
91
81
    # Add two items
92
    # Add two items
82
    my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem(
93
    my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem(
83
            { homebranch => 'CPL',
94
            { homebranch => 'CPL',
Lines 213-218 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub { Link Here
213
    C4::Context->set_preference( 'item-level_itypes', 1 );
224
    C4::Context->set_preference( 'item-level_itypes', 1 );
214
    ok( $item->effective_itemtype() eq 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is disabled' );
225
    ok( $item->effective_itemtype() eq 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is disabled' );
215
226
227
    $dbh->rollback;
228
};
229
230
subtest 'SearchItems test' => sub {
231
    plan tests => 10;
232
233
    # Start transaction
234
    $dbh->{AutoCommit} = 0;
235
    $dbh->{RaiseError} = 1;
236
237
    C4::Context->set_preference('marcflavour', 'MARC21');
238
    my ($biblionumber) = get_biblio();
239
240
    # Add branches if they don't exist
241
    if (not defined GetBranchDetail('CPL')) {
242
        ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'});
243
    }
244
    if (not defined GetBranchDetail('MPL')) {
245
        ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'});
246
    }
247
248
    my (undef, $initial_items_count) = SearchItems(undef, {rows => 1});
249
250
    # Add two items
251
    my (undef, undef, $item1_itemnumber) = AddItem({
252
        homebranch => 'CPL',
253
        holdingbranch => 'CPL',
254
    }, $biblionumber);
255
    my (undef, undef, $item2_itemnumber) = AddItem({
256
        homebranch => 'MPL',
257
        holdingbranch => 'MPL',
258
    }, $biblionumber);
259
260
    my ($items, $total_results);
261
262
    ($items, $total_results) = SearchItems();
263
    is($total_results, $initial_items_count + 2, "Created 2 new items");
264
    is(scalar @$items, $total_results, "SearchItems() returns all items");
265
266
    ($items, $total_results) = SearchItems(undef, {rows => 1});
267
    is($total_results, $initial_items_count + 2);
268
    is(scalar @$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item");
269
270
    # Search all items where homebranch = 'CPL'
271
    my $filter = {
272
        field => 'homebranch',
273
        query => 'CPL',
274
        operator => '=',
275
    };
276
    ($items, $total_results) = SearchItems($filter);
277
    ok($total_results > 0, "There is at least one CPL item");
278
    my $all_items_are_CPL = 1;
279
    foreach my $item (@$items) {
280
        if ($item->{homebranch} ne 'CPL') {
281
            $all_items_are_CPL = 0;
282
            last;
283
        }
284
    }
285
    ok($all_items_are_CPL, "All items returned by SearchItems are from CPL");
286
287
    # Search all items where homebranch != 'CPL'
288
    $filter = {
289
        field => 'homebranch',
290
        query => 'CPL',
291
        operator => '!=',
292
    };
293
    ($items, $total_results) = SearchItems($filter);
294
    ok($total_results > 0, "There is at least one non-CPL item");
295
    my $all_items_are_not_CPL = 1;
296
    foreach my $item (@$items) {
297
        if ($item->{homebranch} eq 'CPL') {
298
            $all_items_are_not_CPL = 0;
299
            last;
300
        }
301
    }
302
    ok($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
303
304
    # Search all items where biblio title (245$a) is like 'Silence in the %'
305
    $filter = {
306
        field => 'marc:245$a',
307
        query => 'Silence in the %',
308
        operator => 'like',
309
    };
310
    ($items, $total_results) = SearchItems($filter);
311
    ok($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'");
312
313
    # Search all items where biblio title is 'Silence in the library'
314
    # and homebranch is 'CPL'
315
    $filter = {
316
        conjunction => 'AND',
317
        filters => [
318
            {
319
                field => 'marc:245$a',
320
                query => 'Silence in the %',
321
                operator => 'like',
322
            },
323
            {
324
                field => 'homebranch',
325
                query => 'CPL',
326
                operator => '=',
327
            },
328
        ],
329
    };
330
    ($items, $total_results) = SearchItems($filter);
331
    my $found = 0;
332
    foreach my $item (@$items) {
333
        if ($item->{itemnumber} == $item1_itemnumber) {
334
            $found = 1;
335
            last;
336
        }
337
    }
338
    ok($found, "item1 found");
216
339
217
    $dbh->rollback;
340
    $dbh->rollback;
218
};
341
};
(-)a/t/db_dependent/Koha/Item/Search/Field.t (+39 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
use Test::More tests => 11;
5
6
use C4::Context;
7
8
use_ok('Koha::Item::Search::Field');
9
import Koha::Item::Search::Field qw(AddItemSearchField ModItemSearchField
10
    DelItemSearchField GetItemSearchField GetItemSearchFields);
11
12
my $dbh = C4::Context->dbh;
13
$dbh->{AutoCommit} = 0;
14
15
# Start with empty table.
16
foreach my $field (GetItemSearchFields()) {
17
    DelItemSearchField($field->{name});
18
}
19
is(scalar GetItemSearchFields(), 0, "No existing search field");
20
21
# Missing keys in hashref parameter, so returns undef
22
ok(not (defined AddItemSearchField()), "missing keys => fail");
23
ok(not (defined AddItemSearchField({})), "missing keys => fail");
24
ok(not (defined AddItemSearchField({name => 'foo'})), "missing keys => fail");
25
ok(not (defined AddItemSearchField({name => 'foo', label => 'Foo'})), "missing keys => fail");
26
27
# Success, the field hashref is returned
28
ok('HASH' eq ref AddItemSearchField({name => 'foo', label => 'Foo', tagfield => '001'}), "successful add");
29
30
# Check the table now contains one row.
31
is(scalar GetItemSearchFields(), 1, "Table now contains one row");
32
my $field = GetItemSearchField('foo');
33
is_deeply($field, {name => 'foo', label => 'Foo', tagfield => '001', tagsubfield => undef, authorised_values_category => undef});
34
35
ok((defined ModItemSearchField({name => 'foo', label => 'Foobar', tagfield => '100', 'tagsubfield' => 'a'})), "successful mod");
36
$field = GetItemSearchField('foo');
37
is_deeply($field, {name => 'foo', label => 'Foobar', tagfield => '100', tagsubfield => 'a', authorised_values_category => undef});
38
39
$dbh->rollback;
(-)a/t/db_dependent/SQLHelper.t (-2 / +6 lines)
Lines 10-16 use YAML; Link Here
10
use C4::Debug;
10
use C4::Debug;
11
use C4::SQLHelper qw(:all);
11
use C4::SQLHelper qw(:all);
12
12
13
use Test::More tests => 20;
13
use Test::More tests => 21;
14
14
15
use_ok('C4::SQLHelper');
15
use_ok('C4::SQLHelper');
16
16
Lines 63-65 $status=DeleteInTable("borrowers",{borrowernumber=>$borrtmp}); Link Here
63
ok($status>0 && !($@), "DeleteInTable OK");
63
ok($status>0 && !($@), "DeleteInTable OK");
64
$status=DeleteInTable("branches", {branchcode => 'ZZZZ'});
64
$status=DeleteInTable("branches", {branchcode => 'ZZZZ'});
65
ok($status>0 && !($@), "DeleteInTable (branch) OK");
65
ok($status>0 && !($@), "DeleteInTable (branch) OK");
66
- 
66
67
my @biblio_columns = C4::SQLHelper::GetColumns('biblio');
68
my @expected_columns = qw(biblionumber frameworkcode author title unititle notes
69
    serial seriestitle copyrightdate timestamp datecreated abstract);
70
is_deeply([sort @biblio_columns], [sort @expected_columns], "GetColumns('biblio') returns all columns of biblio table");

Return to bug 11425