fix ticketing system error on bootstrap of new install
[freeside.git] / rt / t / web / search_ical.t
1 use strict;
2 use warnings;
3
4 use Data::ICal;
5 use RT::Test tests => 77;
6
7 my $start_obj = RT::Date->new( RT->SystemUser );
8 $start_obj->SetToNow;
9 my $start = $start_obj->iCal( Time => 1);
10
11 my $due_obj = RT::Date->new( RT->SystemUser );
12 $due_obj->SetToNow;
13 $due_obj->AddDays(2);
14 my $due = $due_obj->iCal( Time => 1);
15
16 diag 'Test iCal with date only';
17 {
18     my ($baseurl, $agent) = RT::Test->started_ok;
19
20     my $ticket = RT::Ticket->new(RT->SystemUser);
21
22     for ( 1 .. 5 ) {
23         $ticket->Create(
24                         Subject   => 'Ticket ' . $_,
25                         Queue     => 'General',
26                         Owner     => 'root',
27                         Requestor => 'ical@localhost',
28                         Starts    => $start_obj->ISO,
29                         Due       => $due_obj->ISO,
30                        );
31     }
32
33     ok $agent->login('root', 'password'), 'logged in as root';
34
35     $agent->get_ok('/Search/Build.html');
36     $agent->form_name('BuildQuery');
37     $agent->field('idOp', '>');
38     $agent->field('ValueOfid', '0');
39     $agent->submit('DoSearch');
40     $agent->follow_link_ok({id => 'page-results'});
41
42     for ( 1 .. 5 ) {
43         $agent->content_contains('Ticket ' . $_);
44     }
45
46     $agent->follow_link_ok( { text => 'iCal' } );
47
48     is( $agent->content_type, 'text/calendar', 'content type is text/calendar' );
49
50     for ( 1 .. 5 ) {
51         $agent->content_like(qr/URL\:$baseurl\/Ticket\/Display\.html\?id=$_/);
52     }
53
54     my $ical = Data::ICal->new(data => $agent->content);
55
56     my @entries = $ical->entries;
57     my $ical_count = @{$entries[0]};
58     is( $ical_count, 10, "Got $ical_count ical entries");
59
60     my $prop_ref = $entries[0]->[0]->properties;
61     my $start_as_root = RT::Date->new( RT::CurrentUser->new( 'root' ) );
62     $start_as_root->Unix( $start_obj->Unix );
63     my $start = $start_as_root->ISO( Time => 0, Timezone => 'user' );
64     $start =~ s/-//g;
65     is($prop_ref->{'dtstart'}->[0]->value, $start, "Got start date: $start");
66     like( $prop_ref->{'dtstart'}->[0]->as_string, qr/VALUE=DATE\:/, 'Got DATE value');
67
68     $prop_ref = $entries[0]->[1]->properties;
69     my $due_as_root = RT::Date->new( RT::CurrentUser->new( 'root' ) );
70     $due_as_root->Unix( $due_obj->Unix );
71     my $due = $due_as_root->ISO( Time => 0, Timezone => 'user' );
72     $due =~ s/-//g;
73     is($prop_ref->{'dtend'}->[0]->value, $due, "Got due date: $due");
74     like( $prop_ref->{'dtend'}->[0]->as_string, qr/VALUE=DATE\:/, 'Got DATE value');
75 }
76
77 RT::Test->stop_server;
78
79 diag 'Test iCal with date and time with config option';
80 {
81     RT->Config->Set(TimeInICal =>1);
82     my ($baseurl, $agent) = RT::Test->started_ok;
83
84     ok $agent->login('root', 'password'), 'logged in as root';
85
86     $agent->get_ok('/Search/Build.html');
87     $agent->form_name('BuildQuery');
88     $agent->field('idOp', '>');
89     $agent->field('ValueOfid', '0');
90     $agent->submit('DoSearch');
91     $agent->follow_link_ok({id => 'page-results'});
92
93     for ( 1 .. 5 ) {
94         $agent->content_contains('Ticket ' . $_);
95     }
96
97     my $link = $agent->find_link( text => 'iCal' ); # use $link later
98     $agent->get_ok($link->url);
99
100     is( $agent->content_type, 'text/calendar', 'content type is text/calendar' );
101
102     for ( 1 .. 5 ) {
103         $agent->content_like(qr/URL\:$baseurl\/Ticket\/Display\.html\?id=$_/);
104     }
105
106     my $ical = Data::ICal->new(data => $agent->content);
107
108     my @entries = $ical->entries;
109     my $ical_count = @{$entries[0]};
110     is( $ical_count, 10, "Got $ical_count ical entries");
111
112     my $prop_ref = $entries[0]->[0]->properties;
113     $start =~ s/-//g;
114     is($prop_ref->{'dtstart'}->[0]->value, $start, "Got start date with time: $start");
115     like( $prop_ref->{'dtstart'}->[0]->as_string, qr/VALUE=DATE-TIME\:/, 'Got DATE-TIME value');
116
117     $prop_ref = $entries[0]->[1]->properties;
118     $due =~ s/-//g;
119     is($prop_ref->{'dtend'}->[0]->value, $due, "Got due date with time: $due");
120     like( $prop_ref->{'dtend'}->[0]->as_string, qr/VALUE=DATE-TIME\:/, 'Got DATE-TIME value');
121 }
122
123 RT::Test->stop_server;
124
125 diag 'Test iCal with date and time using query param';
126 {
127     RT->Config->Set(TimeInICal =>0);
128     my ($baseurl, $agent) = RT::Test->started_ok;
129
130     ok $agent->login('root', 'password'), 'logged in as root';
131
132     $agent->get_ok('/Search/Build.html');
133     $agent->form_name('BuildQuery');
134     $agent->field('idOp', '>');
135     $agent->field('ValueOfid', '0');
136     $agent->submit('DoSearch');
137     $agent->follow_link_ok({id => 'page-results'});
138
139     for ( 1 .. 5 ) {
140         $agent->content_contains('Ticket ' . $_);
141     }
142
143     my $link = $agent->find_link( text => 'iCal' );
144     $agent->get_ok($link->url . '?Time=1');
145
146     is( $agent->content_type, 'text/calendar', 'content type is text/calendar' );
147
148     for ( 1 .. 5 ) {
149         $agent->content_like(qr/URL\:$baseurl\/Ticket\/Display\.html\?id=$_/);
150     }
151
152     my $ical = Data::ICal->new(data => $agent->content);
153
154     my @entries = $ical->entries;
155     my $ical_count = @{$entries[0]};
156     is( $ical_count, 10, "Got $ical_count ical entries");
157
158     my $prop_ref = $entries[0]->[0]->properties;
159     $start =~ s/-//g;
160     is($prop_ref->{'dtstart'}->[0]->value, $start, "Got start date with time: $start");
161     like( $prop_ref->{'dtstart'}->[0]->as_string, qr/VALUE=DATE-TIME\:/, 'Got DATE-TIME value');
162
163     $prop_ref = $entries[0]->[1]->properties;
164     $due =~ s/-//g;
165     is($prop_ref->{'dtend'}->[0]->value, $due, "Got due date with time: $due");
166     like( $prop_ref->{'dtend'}->[0]->as_string, qr/VALUE=DATE-TIME\:/, 'Got DATE-TIME value');
167
168     diag 'Test iCal with date and time in single events';
169
170     my $url = $link->url . '?SingleEvent=1&Time=1';
171     $agent->get_ok($url);
172
173     is( $agent->content_type, 'text/calendar', 'content type is text/calendar' );
174
175     for ( 1 .. 5 ) {
176         $agent->content_like(qr/URL\:$baseurl\/Ticket\/Display\.html\?id=$_/);
177     }
178
179     $ical = Data::ICal->new(data => $agent->content);
180
181     @entries = $ical->entries;
182     $ical_count = @{$entries[0]};
183
184     # Only 5 entries in single event mode
185     is( $ical_count, 5, "Got $ical_count ical entries");
186
187     $prop_ref = $entries[0]->[0]->properties;
188     $start =~ s/-//g;
189     is($prop_ref->{'dtstart'}->[0]->value, $start, "Got start date with time: $start");
190     like( $prop_ref->{'dtstart'}->[0]->as_string, qr/VALUE=DATE-TIME\:/, 'Got DATE-TIME value');
191
192     $prop_ref = $entries[0]->[1]->properties;
193     $due =~ s/-//g;
194     is($prop_ref->{'dtend'}->[0]->value, $due, "Got due date with time: $due");
195     like( $prop_ref->{'dtend'}->[0]->as_string, qr/VALUE=DATE-TIME\:/, 'Got DATE-TIME value');
196 }