source: trunk/t/15_template.t @ 361

Last change on this file since 361 was 361, checked in by kake, 18 years ago

Test overhaul - no longer require database access info.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 KB
Line 
1use strict;
2use Config::Tiny;
3use Cwd;
4use CGI::Cookie;
5use CGI::Wiki::Formatter::UseMod;
6use OpenGuides::Template;
7use Test::MockObject;
8use Test::More tests => 27;
9
10my $config = Config::Tiny->new;
11$config->{_} = {
12                 template_path         => cwd . '/t/templates',
13                 site_name             => 'CGI::Wiki Test Site',
14                 script_url            => 'http://wiki.example.com/',
15                 script_name           => 'mywiki.cgi',
16                 default_country       => 'United Kingdom',
17                 default_city          => 'London',
18                 contact_email         => 'wiki@example.com',
19                 stylesheet_url        => 'http://wiki.example.com/styles.css',
20                 home_name             => 'Home Page',
21                 formatting_rules_node => 'Rules',
22               };
23
24# White box testing - we know that OpenGuides::Template only actually uses
25# the node_name_to_node_param method of the formatter component of the wiki
26# object passed in, and I CBA to make a proper wiki object here.
27my $fake_wiki = Test::MockObject->new;
28$fake_wiki->mock("formatter",
29                 sub { return CGI::Wiki::Formatter::UseMod->new( munge_urls => 1 ); } );
30
31eval { OpenGuides::Template->output( wiki   => $fake_wiki,
32                                     config => $config ); };
33ok( $@, "->output croaks if no template file supplied" );
34
35eval {
36    OpenGuides::Template->output( wiki     => $fake_wiki,
37                                  config   => $config,
38                                  template => "15_test.tt" );
39};
40is( $@, "", "...but not if one is" );
41
42my $output = OpenGuides::Template->output(
43    wiki     => $fake_wiki,
44    config   => $config,
45    template => "15_test.tt",
46    vars     => { foo => "bar" }
47);
48like( $output, qr/^Content-Type: text\/html/,
49      "Content-Type header included and defaults to text/html" );
50like( $output, qr/FOO: bar/, "variables substituted" );
51
52$output = OpenGuides::Template->output(
53    wiki         => $fake_wiki,
54    config       => $config,
55    template     => "15_test.tt",
56    content_type => ""
57);
58unlike( $output, qr/^Content-Type: text\/html/,
59        "Content-Type header omitted if content_type arg explicitly blank" );
60
61$output = OpenGuides::Template->output(
62    wiki     => $fake_wiki,
63    config   => $config,
64    template => "15_idonotexist.tt"
65);
66like( $output, qr/Failed to process template/, "fails nice on TT error" );
67
68# Test TT variables are auto-set from config.
69$output = OpenGuides::Template->output(
70    wiki     => $fake_wiki,
71    config   => $config,
72    template => "15_test.tt"
73);
74
75like( $output, qr/SITE NAME: CGI::Wiki Test Site/, "site_name var set" );
76like( $output, qr/CGI URL: mywiki.cgi/, "cgi_url var set" );
77like( $output, qr/FULL CGI URL: http:\/\/wiki.example.com\/mywiki.cgi/,
78      "full_cgi_url var set" );
79like( $output, qr/CONTACT EMAIL: wiki\@example.com/, "contact_email var set" );
80like( $output, qr/STYLESHEET: http:\/\/wiki.example.com\/styles.css/,
81      "stylesheet var set" );
82like( $output, qr/HOME LINK: http:\/\/wiki.example.com\/mywiki.cgi/, "home_link var set" );
83like( $output, qr/HOME NAME: Home Page/, "home_name var set" );
84like( $output,
85      qr/FORMATTING RULES LINK: http:\/\/wiki.example.com\/mywiki.cgi\?Rules/,
86      "formatting_rules_link var set" );
87
88# Test openguides_version TT variable.
89like( $output, qr/OPENGUIDES VERSION: 0\.\d\d/,
90      "openguides_version set" );
91
92# Test TT variables auto-set from node name.
93$output = OpenGuides::Template->output(
94    wiki     => $fake_wiki,
95    config   => $config,
96    node     => "Test Node",
97    template => "15_test.tt"
98);
99
100like( $output, qr/NODE NAME: Test Node/, "node_name var set" );
101like( $output, qr/NODE PARAM: Test_Node/, "node_param var set" );
102
103# Test that cookies go in.
104my $cookie = CGI::Cookie->new( -name => "x", -value => "y" );
105$output = OpenGuides::Template->output(
106    wiki     => $fake_wiki,
107    config   => $config,
108    template => "15_test.tt",
109    cookies  => $cookie
110);
111like( $output, qr/Set-Cookie: $cookie/, "cookie in header" );
112
113# Test that home_link is set correctly when script_name is blank.
114$config->{_} = {
115                 template_path         => cwd . '/t/templates',
116                 site_name             => 'CGI::Wiki Test Site',
117                 script_url            => 'http://wiki.example.com/',
118                 script_name           => '',
119               };
120$output = OpenGuides::Template->output(
121    wiki     => $fake_wiki,
122    config   => $config,
123    template => "15_test.tt"
124);
125like( $output, qr/HOME LINK: http:\/\/wiki.example.com/,
126      "home_link var set OK when script_name blank" );
127
128# Test that full_cgi_url comes out right if the trailing '/' is
129# missing from script_url in the config file.
130$config->{_} = {
131                 template_path         => cwd . '/t/templates',
132                 site_name             => 'CGI::Wiki Test Site',
133                 script_url            => 'http://wiki.example.com',
134                 script_name           => 'wiki.cgi',
135               };
136$output = OpenGuides::Template->output(
137    wiki     => $fake_wiki,
138    config   => $config,
139    template => "15_test.tt"
140);
141like( $output, qr/FULL CGI URL: http:\/\/wiki.example.com\/wiki.cgi/,
142      "full_cgi_url OK when trailing '/' missed off script_url" );
143
144# Test that TT vars are picked up from user cookie prefs.
145$cookie = OpenGuides::CGI->make_prefs_cookie(
146    config                 => $config,
147    omit_formatting_link   => 1,
148);
149$ENV{HTTP_COOKIE} = $cookie;
150$output = OpenGuides::Template->output(
151    wiki     => $fake_wiki,
152    config   => $config,
153    template => "15_test.tt"
154);
155like( $output, qr/FORMATTING RULES LINK: /,
156      "formatting_rules_link TT var blank as set in cookie" );
157
158# Test that explicitly supplied vars override vars in cookie.
159$output = OpenGuides::Template->output(
160    wiki     => $fake_wiki,
161    config   => $config,
162    template => "15_test.tt",
163    vars     => { omit_formatting_link => "fish" },
164);
165like( $output, qr/OMIT FORMATTING LINK: fish/,
166      "explicitly supplied TT vars override cookie ones" );
167
168# Test that enable_page_deletion is set correctly in various circumstances.
169$config = Config::Tiny->new;
170$config->{_}->{template_path} = cwd . "/t/templates";
171$config->{_}->{site_name} = "Test Site";
172$config->{_}->{script_url} = "/";
173$config->{_}->{script_name} = "";
174
175$output = OpenGuides::Template->output(
176    wiki     => $fake_wiki,
177    config   => $config,
178    template => "15_test.tt",
179);
180like( $output, qr/ENABLE PAGE DELETION: 0/,
181      "enable_page_deletion var set correctly when not specified in conf" );
182
183$config->{_}->{enable_page_deletion} = "n";
184$output = OpenGuides::Template->output(
185    wiki     => $fake_wiki,
186    config   => $config,
187    template => "15_test.tt",
188);
189like( $output, qr/ENABLE PAGE DELETION: 0/,
190      "enable_page_deletion var set correctly when set to 'n' in conf" );
191
192$config->{_}->{enable_page_deletion} = "y";
193$output = OpenGuides::Template->output(
194    wiki     => $fake_wiki,
195    config   => $config,
196    template => "15_test.tt",
197);
198like( $output, qr/ENABLE PAGE DELETION: 1/,
199      "enable_page_deletion var set correctly when set to 'y' in conf" );
200
201$config->{_}->{enable_page_deletion} = 0;
202$output = OpenGuides::Template->output(
203    wiki     => $fake_wiki,
204    config   => $config,
205    template => "15_test.tt",
206);
207like( $output, qr/ENABLE PAGE DELETION: 0/,
208      "enable_page_deletion var set correctly when set to '0' in conf" );
209
210$config->{_}->{enable_page_deletion} = 1;
211$output = OpenGuides::Template->output(
212    wiki     => $fake_wiki,
213    config   => $config,
214    template => "15_test.tt",
215);
216like( $output, qr/ENABLE PAGE DELETION: 1/,
217      "enable_page_deletion var set correctly when set to '1' in conf" );
Note: See TracBrowser for help on using the repository browser.