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