fix ticketing system error on bootstrap of new install
[freeside.git] / rt / t / web / saved_search_chart.t
1 use strict;
2 use warnings;
3
4 use RT::Test no_plan => 1;
5 my ( $url, $m ) = RT::Test->started_ok;
6 use RT::Attribute;
7
8 my $search = RT::Attribute->new(RT->SystemUser);
9 my $ticket = RT::Ticket->new(RT->SystemUser);
10 my ( $ret, $msg ) = $ticket->Create(
11     Subject   => 'base ticket' . $$,
12     Queue     => 'general',
13     Owner     => 'root',
14     Requestor => 'root@localhost',
15     MIMEObj   => MIME::Entity->build(
16         From    => 'root@localhost',
17         To      => 'rt@localhost',
18         Subject => 'base ticket' . $$,
19         Data    => "",
20     ),
21 );
22 ok( $ret, "ticket created: $msg" );
23
24 ok( $m->login, 'logged in' );
25
26 $m->get_ok( $url . "/Search/Chart.html?Query=" . 'id=1' );
27 my ($owner) = $m->content =~ /value="(RT::User-\d+)"/;
28
29 $m->submit_form(
30     form_name => 'SaveSearch',
31     fields    => {
32         SavedSearchDescription => 'first chart',
33         SavedSearchOwner       => $owner,
34     },
35     button => 'SavedSearchSave',
36 );
37
38 $m->content_contains("Chart first chart saved", 'saved first chart' );
39
40 my ( $search_uri, $id ) = $m->content =~ /value="(RT::User-\d+-SavedSearch-(\d+))"/;
41 $m->submit_form(
42     form_name => 'SaveSearch',
43     fields    => { SavedSearchLoad => $search_uri },
44 );
45
46 $m->content_like( qr/name="SavedSearchDelete"\s+value="Delete"/,
47     'found Delete button' );
48 $m->content_like(
49     qr/name="SavedSearchDescription"\s+value="first chart"/,
50     'found Description input with the value filled'
51 );
52 $m->content_like( qr/name="SavedSearchSave"\s+value="Update"/,
53     'found Update button' );
54 $m->content_unlike( qr/name="SavedSearchSave"\s+value="Save"/,
55     'no Save button' );
56
57 $m->submit_form(
58     form_name => 'SaveSearch',
59     fields    => {
60         Query          => 'id=2',
61         GroupBy        => 'Status',
62         ChartStyle     => 'pie',
63     },
64     button => 'SavedSearchSave',
65 );
66
67 $m->content_contains("Chart first chart updated", 'found updated message' );
68 $m->content_contains("id=2",                      'Query is updated' );
69 $m->content_like( qr/value="Status"\s+selected="selected"/,
70     'GroupBy is updated' );
71 $m->content_like( qr/value="pie"\s+selected="selected"/,
72     'ChartType is updated' );
73 ok( $search->Load($id) );
74 is( $search->SubValue('Query'), 'id=2', 'Query is indeed updated' );
75 is( $search->SubValue('GroupBy'),
76     'Status', 'GroupBy is indeed updated' );
77 is( $search->SubValue('ChartStyle'), 'pie', 'ChartStyle is indeed updated' );
78
79 # finally, let's test delete
80 $m->submit_form(
81     form_name => 'SaveSearch',
82     button    => 'SavedSearchDelete',
83 );
84 $m->content_contains("Chart first chart deleted", 'found deleted message' );
85 $m->content_unlike( qr/value="RT::User-\d+-SavedSearch-\d+"/,
86     'no saved search' );
87
88 for ('A' .. 'F') {
89     $ticket->Create(
90         Subject   => $$ . $_,
91     );
92 }
93
94 for ([A => 'subject="'.$$.'A"'], [BorC => 'subject="'.$$.'B" OR subject="'.$$.'C"']) {
95     $m->get_ok('/Search/Edit.html');
96     $m->form_name('BuildQueryAdvanced');
97     $m->field('Query', $_->[1]);
98     $m->submit;
99
100     # Save the search
101     $m->follow_link_ok({id => 'page-chart'});
102     $m->form_name('SaveSearch');
103     $m->field(SavedSearchDescription => $_->[0]);
104     $m->click_ok('SavedSearchSave');
105     $m->text_contains('Chart ' . $_->[0] . ' saved.');
106
107 }
108
109 $m->form_name('SaveSearch');
110 my @saved_search_ids =
111     $m->current_form->find_input('SavedSearchLoad')->possible_values;
112 shift @saved_search_ids; # first value is blank
113
114 cmp_ok(@saved_search_ids, '==', 2, 'Two saved charts were made');
115
116 # TODO get find_link('page-chart')->URI->params to work...
117 sub page_chart_link_has {
118     my ($m, $id, $msg) = @_;
119
120     $Test::Builder::Level = $Test::Builder::Level + 1;
121
122     (my $dec_id = $id) =~ s/:/%3A/g;
123
124     my $chart_url = $m->find_link(id => 'page-chart')->url;
125     like(
126         $chart_url, qr{SavedChartSearchId=\Q$dec_id\E},
127         $msg || 'Page chart link matches the pattern we expected'
128     );
129 }
130
131 # load the first chart
132 $m->field('SavedSearchLoad' => $saved_search_ids[0]);
133 $m->click('SavedSearchLoadSubmit');
134
135 page_chart_link_has($m, $saved_search_ids[0]);
136
137 $m->form_name('SaveSearch');
138 is($m->form_number(3)->value('SavedChartSearchId'), $saved_search_ids[0]);
139
140 $m->form_name('SaveSearch');
141
142 # now load the second chart
143 $m->field('SavedSearchLoad' => $saved_search_ids[1]);
144 $m->click('SavedSearchLoadSubmit');
145
146 page_chart_link_has($m, $saved_search_ids[1]);
147
148 is(
149     $m->form_number(3)->value('SavedChartSearchId'), $saved_search_ids[1],
150     'Second form is seen as a hidden field'
151 );
152
153 page_chart_link_has($m, $saved_search_ids[1]);