1#============================================================= -*-perl-*- 2# 3# t/url.t 4# 5# Template script testing URL plugin. 6# 7# Written by Andy Wardley <abw@kfs.org> 8# 9# Copyright (C) 2000 Andy Wardley. All Rights Reserved. 10# 11# This is free software; you can redistribute it and/or modify it 12# under the same terms as Perl itself. 13# 14# $Id$ 15# 16#======================================================================== 17 18use strict; 19use lib qw( ../lib ); 20use Template qw( :status ); 21use Template::Test; 22use Template::Plugin::URL; 23$^W = 1; 24 25skip_all("We can't agree on the right joint for the URL plugin"); 26 27$Template::Test::DEBUG = 0; 28 29my $urls = { 30 product => { 31 map { 32 $_->{ name }, Template::Plugin::URL->new(undef, # no context 33 $_->{ url }, 34 $_->{ args }); 35 } 36 ( 37 { 38 name => 'view', 39 url => '/product', 40 }, 41 { 42 name => 'add', 43 url => '/product', 44 args => { action => 'add' }, 45 }, 46 { 47 name => 'edit', 48 url => '/product', 49 args => { action => 'edit', style => 'editor' }, 50 }, 51 ), 52 }, 53}; 54 55my $vars = { 56 url => $urls, 57 sorted => \&sort_params, 58}; 59 60test_expect(\*DATA, { INTERPOLATE => 1 }, $vars); 61 62# url params are constructed in a non-deterministic order. we obviously 63# can't test against this so we use this devious hack to reorder a 64# query so that its parameters are in alphabetical order. 65# ------------------------------------------------------------------------ 66# later note: in adding support for parameters with multiple values, the 67# sort_params() hacked below got broken so as a temporary solution, I 68# changed teh URL plugin to sort all params by key when generating the 69# URL 70 71sub sort_params { 72 my $query = shift; 73 my ($base, $args) = split(/\?/, $query); 74 my (@args, @keys, %argtab); 75 76 print STDERR "sort_parms(\"$query\")\n" if $Template::Test::DEBUG; 77 78 @args = split('&', $args); 79 @keys = map { (split('=', $_))[0] } @args; 80 @argtab{ @keys } = @args; 81 @keys = sort keys %argtab; 82 @args = map { $argtab{ $_ } } @keys; 83 $args = join('&', @args); 84 $query = join('?', length $base ? ($base, $args) : $args); 85 86 print STDERR "returning [$query]\n" if $Template::Test::DEBUG; 87 88 return $query; 89} 90 91 92#------------------------------------------------------------------------ 93# test input 94#------------------------------------------------------------------------ 95 96__DATA__ 97-- test -- 98[% USE url -%] 99loaded 100[% url %] 101[% url('foo') %] 102[% url(foo='bar') %] 103[% url('bar', wiz='woz') %] 104 105-- expect -- 106loaded 107 108foo 109foo=bar 110bar?wiz=woz 111 112-- test -- 113[% USE url('here') -%] 114[% url %] 115[% url('there') %] 116[% url(any='where') %] 117[% url('every', which='way') %] 118[% sorted( url('every', which='way', you='can') ) %] 119 120-- expect -- 121here 122there 123here?any=where 124every?which=way 125every?which=way;you=can 126 127-- test -- 128[% USE url('there', name='fred') -%] 129[% url %] 130[% url(name='tom') %] 131[% sorted( url(age=24) ) %] 132[% sorted( url(age=42, name='frank') ) %] 133 134-- expect -- 135there?name=fred 136there?name=tom 137there?age=24;name=fred 138there?age=42;name=frank 139 140-- test -- 141[% USE url('/cgi-bin/woz.pl') -%] 142[% url(name="Elrich von Benjy d'Weiro") %] 143 144-- expect -- 145/cgi-bin/woz.pl?name=Elrich%20von%20Benjy%20d%27Weiro 146 147-- test -- 148[% USE url '/script' { one => 1, two => [ 2, 4 ], three => [ 3, 6, 9] } -%] 149[% url %] 150 151-- expect -- 152/script?one=1;three=3;three=6;three=9;two=2;two=4 153 154-- test -- 155[% url.product.view %] 156[% url.product.view(style='compact') %] 157-- expect -- 158/product 159/product?style=compact 160 161-- test -- 162[% url.product.add %] 163[% url.product.add(style='compact') %] 164-- expect -- 165/product?action=add 166/product?action=add;style=compact 167 168-- test -- 169[% url.product.edit %] 170[% url.product.edit(style='compact') %] 171-- expect -- 172/product?action=edit;style=editor 173/product?action=edit;style=compact 174