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