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

Last change on this file since 587 was 587, checked in by kake, 17 years ago

Encapsulate config data in OpenGuides::Config.

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