summaryrefslogtreecommitdiff
path: root/rt/t/api/cfsearch.t
blob: 7a460ce2e0c3b83b0824a9b0c5000bee247e44c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use strict;
use warnings;

use RT::Test tests => 18;

my $suffix = '-'. $$;

use_ok 'RT::Users';
use_ok 'RT::CustomField';

my $u1 = RT::User->new( RT->SystemUser );
isa_ok( $u1, 'RT::User' );
ok( $u1->Load('root'), "Loaded user 'root'" );

# create cf
my $cfname = 'TestUserCF'. $suffix;
my $cf = RT::CustomField->new( RT->SystemUser );
isa_ok( $cf, 'RT::CustomField' );

{
    my ($id, $msg) = $cf->Create(
       Name => $cfname,
       LookupType => 'RT::User',
       Type => 'Freeform',
       Description => 'Freeform CF for tests',
    );
    ok( $id, "Created cf '$cfname' - " . $msg );
}

{
  my ($status, $msg) = $cf->AddToObject( $u1 );
  ok( $status, "Added CF to user object - " . $msg);
}

my $cfvalue1 = 'Foo';

{
  my ($id, $msg) = $u1->AddCustomFieldValue(
			  Field => $cfname,
			  Value => $cfvalue1,
			  RecordTransaction => 0 );
  ok( $id, "Adding CF value '$cfvalue1' - " . $msg );
}

# Confirm value is returned.
{
  my $cf_value_ref = QueryCFValue( $cfvalue1, $cf->id );
  is( scalar(@$cf_value_ref), 1, 'Got one value.' );
  is( $cf_value_ref->[0], 'Foo', 'Got Foo back for value.' );
}

{
  my ($id, $msg) = $u1->DeleteCustomFieldValue(
			    Field => $cfname,
			    Value => $cfvalue1,
			    RecordTransaction => 0 );
  ok( $id, "Deleting CF value - " . $msg );
}

my $cfvalue2 = 'Bar';
{
  my ($id, $msg) = $u1->AddCustomFieldValue(
			  Field => $cfname,
			  Value => $cfvalue2,
			  RecordTransaction => 0 );
  ok( $id, "Adding second CF value '$cfvalue2' - " . $msg );
}

# Confirm no value is returned for Foo.
{
  # Calling with $cfvalue1 on purpose to confirm
  # it has been disabled by the delete above.

  my $cf_value_ref = QueryCFValue( $cfvalue1, $cf->id );
  is( scalar(@$cf_value_ref), 0, 'No values returned for Foo.' );
}

# Confirm value is returned for Bar.
{
  my $cf_value_ref = QueryCFValue( $cfvalue2, $cf->id );
  is( scalar(@$cf_value_ref), 1, 'Got one value.' );
  is( $cf_value_ref->[0], 'Bar', 'Got Bar back for value.' );
}


sub QueryCFValue{
  my $cf_value = shift;
  my $cf_id = shift;
  my @cf_value_strs;

  my $users = RT::Users->new(RT->SystemUser);
  isa_ok( $users, 'RT::Users' );

  $users->LimitCustomField(
	  CUSTOMFIELD => $cf_id,
	  OPERATOR => "=",
	  VALUE => $cf_value );

  while ( my $filtered_user = $users->Next() ){
    my $cf_values = $filtered_user->CustomFieldValues($cf->id);
    while (my $cf_value = $cf_values->Next() ){
      push @cf_value_strs, $cf_value->Content;
    }
  }
  return \@cf_value_strs;
}