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

(-)a/C4/Items.pm (-4 / +13 lines)
Lines 2648-2659 sub _SearchItems_build_where_fragment { Link Here
2648
2648
2649
=head2 SearchItems
2649
=head2 SearchItems
2650
2650
2651
    my ($items, $total) = SearchItemsByField($filters, $params);
2651
    my ($items, $total) = SearchItems($filter, $params);
2652
2652
2653
Perform a search among items
2653
Perform a search among items
2654
2654
2655
$filters is a reference to an array of filters, where each filter is a hash with
2655
$filter is a reference to a hash which can be a filter, or a combination of filters.
2656
the following keys:
2656
2657
A filter has the following keys:
2657
2658
2658
=over 2
2659
=over 2
2659
2660
Lines 2665-2671 the following keys: Link Here
2665
2666
2666
=back
2667
=back
2667
2668
2668
A logical AND is used to combine filters.
2669
A combination of filters hash the following keys:
2670
2671
=over 2
2672
2673
=item * conjunction: 'AND' or 'OR'
2674
2675
=item * filters: array ref of filters
2676
2677
=back
2669
2678
2670
$params is a reference to a hash that can contain the following parameters:
2679
$params is a reference to a hash that can contain the following parameters:
2671
2680
(-)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 (-1 / +125 lines)
Lines 20-28 use Modern::Perl; Link Here
20
20
21
use MARC::Record;
21
use MARC::Record;
22
use C4::Biblio;
22
use C4::Biblio;
23
use C4::Branch;
23
use Koha::Database;
24
use Koha::Database;
24
25
25
use Test::More tests => 4;
26
use Test::More tests => 5;
26
27
27
BEGIN {
28
BEGIN {
28
    use_ok('C4::Items');
29
    use_ok('C4::Items');
Lines 38-45 subtest 'General Add, Get and Del tests' => sub { Link Here
38
    $dbh->{AutoCommit} = 0;
39
    $dbh->{AutoCommit} = 0;
39
    $dbh->{RaiseError} = 1;
40
    $dbh->{RaiseError} = 1;
40
41
42
41
    # Helper biblio.
43
    # Helper biblio.
42
    diag("Creating biblio instance for testing.");
44
    diag("Creating biblio instance for testing.");
45
    C4::Context->set_preference('marcflavour', 'MARC21');
43
    my ($bibnum, $bibitemnum) = get_biblio();
46
    my ($bibnum, $bibitemnum) = get_biblio();
44
47
45
    # Add an item.
48
    # Add an item.
Lines 76-83 subtest 'GetHiddenItemnumbers tests' => sub { Link Here
76
    $dbh->{RaiseError} = 1;
79
    $dbh->{RaiseError} = 1;
77
80
78
    # Create a new biblio
81
    # Create a new biblio
82
    C4::Context->set_preference('marcflavour', 'MARC21');
79
    my ($biblionumber, $biblioitemnumber) = get_biblio();
83
    my ($biblionumber, $biblioitemnumber) = get_biblio();
80
84
85
    # Add branches if they don't exist
86
    if (not defined GetBranchDetail('CPL')) {
87
        ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'});
88
    }
89
    if (not defined GetBranchDetail('MPL')) {
90
        ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'});
91
    }
92
81
    # Add two items
93
    # Add two items
82
    my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem(
94
    my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem(
83
            { homebranch => 'CPL',
95
            { homebranch => 'CPL',
Lines 175-180 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub { Link Here
175
    $schema->resultset('Systempreference')->update_or_create({ variable => 'item-level_itypes', value => 1 });
187
    $schema->resultset('Systempreference')->update_or_create({ variable => 'item-level_itypes', value => 1 });
176
    ok( $item->effective_itemtype() eq 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is disabled' );
188
    ok( $item->effective_itemtype() eq 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is disabled' );
177
189
190
    $dbh->rollback;
191
};
192
193
subtest 'SearchItems test' => sub {
194
    plan tests => 10;
195
196
    # Start transaction
197
    $dbh->{AutoCommit} = 0;
198
    $dbh->{RaiseError} = 1;
199
200
    C4::Context->set_preference('marcflavour', 'MARC21');
201
    my ($biblionumber) = get_biblio();
202
203
    # Add branches if they don't exist
204
    if (not defined GetBranchDetail('CPL')) {
205
        ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'});
206
    }
207
    if (not defined GetBranchDetail('MPL')) {
208
        ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'});
209
    }
210
211
    my (undef, $initial_items_count) = SearchItems(undef, {rows => 1});
212
213
    # Add two items
214
    my (undef, undef, $item1_itemnumber) = AddItem({
215
        homebranch => 'CPL',
216
        holdingbranch => 'CPL',
217
    }, $biblionumber);
218
    my (undef, undef, $item2_itemnumber) = AddItem({
219
        homebranch => 'MPL',
220
        holdingbranch => 'MPL',
221
    }, $biblionumber);
222
223
    my ($items, $total_results);
224
225
    ($items, $total_results) = SearchItems();
226
    is($total_results, $initial_items_count + 2, "Created 2 new items");
227
    is(scalar @$items, $total_results, "SearchItems() returns all items");
228
229
    ($items, $total_results) = SearchItems(undef, {rows => 1});
230
    is($total_results, $initial_items_count + 2);
231
    is(scalar @$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item");
232
233
    # Search all items where homebranch = 'CPL'
234
    my $filter = {
235
        field => 'homebranch',
236
        query => 'CPL',
237
        operator => '=',
238
    };
239
    ($items, $total_results) = SearchItems($filter);
240
    ok($total_results > 0, "There is at least one CPL item");
241
    my $all_items_are_CPL = 1;
242
    foreach my $item (@$items) {
243
        if ($item->{homebranch} ne 'CPL') {
244
            $all_items_are_CPL = 0;
245
            last;
246
        }
247
    }
248
    ok($all_items_are_CPL, "All items returned by SearchItems are from CPL");
249
250
    # Search all items where homebranch != 'CPL'
251
    $filter = {
252
        field => 'homebranch',
253
        query => 'CPL',
254
        operator => '!=',
255
    };
256
    ($items, $total_results) = SearchItems($filter);
257
    ok($total_results > 0, "There is at least one non-CPL item");
258
    my $all_items_are_not_CPL = 1;
259
    foreach my $item (@$items) {
260
        if ($item->{homebranch} eq 'CPL') {
261
            $all_items_are_not_CPL = 0;
262
            last;
263
        }
264
    }
265
    ok($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
266
267
    # Search all items where biblio title (245$a) is like 'Silence in the %'
268
    $filter = {
269
        field => 'marc:245$a',
270
        query => 'Silence in the %',
271
        operator => 'like',
272
    };
273
    ($items, $total_results) = SearchItems($filter);
274
    ok($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'");
275
276
    # Search all items where biblio title is 'Silence in the library'
277
    # and homebranch is 'CPL'
278
    $filter = {
279
        conjunction => 'AND',
280
        filters => [
281
            {
282
                field => 'marc:245$a',
283
                query => 'Silence in the %',
284
                operator => 'like',
285
            },
286
            {
287
                field => 'homebranch',
288
                query => 'CPL',
289
                operator => '=',
290
            },
291
        ],
292
    };
293
    ($items, $total_results) = SearchItems($filter);
294
    my $found = 0;
295
    foreach my $item (@$items) {
296
        if ($item->{itemnumber} == $item1_itemnumber) {
297
            $found = 1;
298
            last;
299
        }
300
    }
301
    ok($found, "item1 found");
178
302
179
    $dbh->rollback;
303
    $dbh->rollback;
180
};
304
};
(-)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