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