Skip to content

Commit 3817203

Browse files
committed
Test chartjs images in dashboard emails
1 parent 89b71b7 commit 3817203

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

t/mail/dashboard-chartjs.t

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
use strict;
2+
use warnings;
3+
4+
use RT::Test
5+
tests => undef,
6+
config => qq{Set(\$EmailDashboardJSChartImages, 1);
7+
Set(\@ChromeLaunchArguments, '--no-sandbox');
8+
Set(\$ChromePath, '@{[$ENV{RT_TEST_CHROME_PATH} // '']}');};
9+
10+
plan skip_all => 'Need WWW::Mechanize::Chrome and a chrome-based browser'
11+
unless RT::StaticUtil::RequireModule("WWW::Mechanize::Chrome")
12+
&& ( $ENV{RT_TEST_CHROME_PATH} || WWW::Mechanize::Chrome->find_executable('chromium-browser') );
13+
14+
my $root = RT::Test->load_or_create_user( Name => 'root' );
15+
16+
my ( $baseurl, $m ) = RT::Test->started_ok;
17+
ok( $m->login, 'logged in' );
18+
my $ticket = RT::Ticket->new($RT::SystemUser);
19+
$ticket->Create(
20+
Queue => 'General',
21+
Subject => 'Test ChartJS',
22+
);
23+
ok( $ticket->id, 'created ticket' );
24+
25+
$m->get_ok(q{/Search/Chart.html?Query=Subject LIKE 'test ChartJS'});
26+
$m->submit_form(
27+
form_name => 'SaveSearch',
28+
fields => {
29+
SavedSearchDescription => 'chart foo',
30+
SavedSearchOwner => 'RT::User-' . $root->id,
31+
ChartStyle => 'bar',
32+
},
33+
button => 'SavedSearchSave',
34+
);
35+
36+
# first, create and populate a dashboard
37+
$m->get_ok('/Dashboards/Modify.html?Create=1');
38+
$m->form_name('ModifyDashboard');
39+
$m->field( 'Name' => 'dashboard foo' );
40+
$m->click_button( value => 'Create' );
41+
42+
my ($dashboard_id) = ( $m->uri =~ /id=(\d+)/ );
43+
ok( $dashboard_id, "got an ID for the dashboard, $dashboard_id" );
44+
45+
$m->follow_link_ok( { text => 'Content' } );
46+
47+
# add content, Chart: chart foo, to dashboard body
48+
# we need to get the saved search id from the content before submitting the form.
49+
my $regex = qr/data-type="(\w+)" data-name="RT::User-/ . $root->id . qr/-SavedSearch-(\d+)"/;
50+
my ( $saved_search_type, $saved_search_id ) = $m->content =~ /$regex/;
51+
ok( $saved_search_type, "got a type for the saved search, $saved_search_type" );
52+
ok( $saved_search_id, "got an ID for the saved search, $saved_search_id" );
53+
54+
$m->submit_form_ok(
55+
{
56+
form_name => 'UpdateSearches',
57+
fields => {
58+
dashboard_id => $dashboard_id,
59+
body => $saved_search_type . "-" . "RT::User-" . $root->id . "-SavedSearch-" . $saved_search_id,
60+
},
61+
button => 'UpdateSearches',
62+
},
63+
"add content 'Chart: chart foo' to dashboard body"
64+
);
65+
66+
like( $m->uri, qr/results=[A-Za-z0-9]{32}/, 'URL redirected for results' );
67+
$m->content_contains('Dashboard updated');
68+
69+
$m->follow_link_ok( { text => 'Subscription' } );
70+
$m->form_name('SubscribeDashboard');
71+
$m->field( 'Frequency' => 'daily' );
72+
$m->field( 'Hour' => '06:00' );
73+
$m->click_button( name => 'Save' );
74+
$m->content_contains('Subscribed to dashboard dashboard foo');
75+
76+
RT::Test->run_and_capture(
77+
command => $RT::SbinPath . '/rt-email-dashboards',
78+
all => 1,
79+
);
80+
81+
my @mails = RT::Test->fetch_caught_mails;
82+
is @mails, 1, "got a dashboard mail";
83+
84+
85+
# can't use parse_mail here is because it deletes all attachments
86+
# before we can call bodyhandle :/
87+
use RT::EmailParser;
88+
my $parser = RT::EmailParser->new;
89+
my $mail = $parser->ParseMIMEEntityFromScalar( $mails[0] );
90+
like( $mail->head->get('Subject'), qr/Daily Dashboard: dashboard foo/, 'mail subject' );
91+
92+
my ($mail_image) = grep { $_->mime_type eq 'image/png' } $mail->parts;
93+
ok( $mail_image, 'mail contains image attachment' );
94+
require Imager; # Imager is a dependency of WWW::Mechanize::Chrome
95+
my $imager = Imager->new();
96+
$imager->open( data => $mail_image->bodyhandle->as_string, type => 'png' );
97+
is( $imager->bits, 8, 'image bit depth is 8' ); # Images created by GD::Graph have 4-bit color depth
98+
99+
100+
# The first bar's color is #a6cee3, which is (166, 206, 227),
101+
# on Apple Display preset, it's converted to (174, 205, 225).
102+
103+
my $bar_color = $imager->getpixel( x => 300, y => 200 );
104+
my $srgb_color = Imager::Color->new( 166, 206, 227 );
105+
my $p3_color = Imager::Color->new( 174, 205, 225 );
106+
ok( $bar_color->equals( other => $srgb_color ) || $bar_color->equals( other => $p3_color ),
107+
'image bar color is #a6cee3' );
108+
109+
done_testing;

0 commit comments

Comments
 (0)