1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this
3# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4#
5# This Source Code Form is "Incompatible With Secondary Licenses", as
6# defined by the Mozilla Public License, v. 2.0.
7
8# This module represents a test with custom URL parameters.
9# Tests like this are specified in CUSTOM_SEARCH_TESTS in
10# Bugzilla::Test::Search::Constants.
11package Bugzilla::Test::Search::CustomTest;
12use parent qw(Bugzilla::Test::Search::FieldTest);
13use strict;
14use warnings;
15
16use Bugzilla::Test::Search::FieldTest;
17use Bugzilla::Test::Search::OperatorTest;
18
19use Storable qw(dclone);
20
21###############
22# Constructor #
23###############
24
25sub new {
26  my ($class, $test, $search_test) = @_;
27  bless { raw_test => dclone($test), search_test => $search_test }, $class;
28}
29
30#############
31# Accessors #
32#############
33
34sub search_test { return $_[0]->{search_test} }
35sub name { return 'Custom: ' . $_[0]->test->{name} }
36sub test { return $_[0]->{raw_test} }
37
38sub operator_test { die "unimplemented" }
39sub field_object { die "unimplemented" }
40sub main_value { die "unimplenmented" }
41sub test_value { die "unimplemented" }
42# Custom tests don't use transforms.
43sub transformed_value_was_equal { 0 }
44sub debug_value {
45    my ($self) = @_;
46    my $string = '';
47    my $params = $self->search_params;
48    foreach my $param (keys %$params) {
49        $string .= $param . "=" . $params->{$param} . '&';
50    }
51    chop($string);
52    return $string;
53}
54
55# The tests we know are broken for this operator/field combination.
56sub _known_broken { return {} }
57sub contains_known_broken { return undef }
58sub search_known_broken { return undef }
59sub field_not_yet_implemented { return undef }
60sub invalid_field_operator_combination { return undef }
61
62#########################################
63# Accessors: Bugzilla::Search Arguments #
64#########################################
65
66# Converts the f, o, v rows into f0, o0, v0, etc. and translates
67# the values appropriately.
68sub search_params {
69    my ($self) = @_;
70
71    my %params = %{ $self->test->{top_params} || {} };
72    my $counter = 0;
73    foreach my $row (@{ $self->test->{params} }) {
74        $row->{v} = $self->translate_value($row) if exists $row->{v};
75        foreach my $key (keys %$row) {
76            $params{"${key}$counter"} = $row->{$key};
77        }
78        $counter++;
79    }
80
81    return \%params;
82}
83
84sub translate_value {
85    my ($self, $row) = @_;
86    my $as_test = { field => $row->{f}, operator => $row->{o},
87                    value => $row->{v} };
88    my $operator_test = new Bugzilla::Test::Search::OperatorTest($row->{o},
89        $self->search_test);
90    my $field = Bugzilla::Field->check($row->{f});
91    my $field_test = new Bugzilla::Test::Search::FieldTest($operator_test,
92      $field, $as_test);
93    return $field_test->translated_value;
94}
95
96sub search_columns {
97    my ($self) = @_;
98    return ['bug_id', @{ $self->test->{columns} || [] }];
99}
100
1011;
102