RT 4.0.22
[freeside.git] / rt / t / api / menu.t
1 use strict;
2 use warnings;
3
4 use RT::Test tests => undef;
5 use RT::Interface::Web::Menu;
6
7 sub child_path_is($$$) {
8     my ($menu, $child, $expected) = @_;
9     my $c = $menu->child($child->[0], path => $child->[1]);
10     is $c->path, $expected, "'$child->[1]' normalizes to '$expected'";
11     return $c;
12 }
13
14 {
15     package FakeRequest;
16     sub new { bless {}, shift }
17     sub path_info { "" }
18
19     package FakeInterp;
20     require CGI;
21     sub new { bless {}, shift }
22     sub cgi_object { CGI->new }
23 }
24
25 local $HTML::Mason::Commands::r = FakeRequest->new;
26 local $HTML::Mason::Commands::m = FakeInterp->new;
27
28 my $menu = RT::Interface::Web::Menu->new;
29 ok $menu, "Created top level menu";
30
31 child_path_is $menu, [search    => "Search/Simple.html"],   "/Search/Simple.html";
32 child_path_is $menu, [absolute  => "/Prefs/Other.html"],    "/Prefs/Other.html";
33 child_path_is $menu, [scheme    => "http://example.com"],   "http://example.com";
34
35 my $tools =
36     child_path_is $menu,    [tools      => "/Tools/"],              "/Tools/";
37     child_path_is $tools,   [myday      => "MyDay.html"],           "/Tools/MyDay.html";
38     child_path_is $tools,   [activity   => "/Activity.html"],       "/Activity.html";
39     my $ext =
40         child_path_is $tools,   [external   => "http://example.com"],   "http://example.com";
41         child_path_is $ext,     [wiki       => "wiki/"],                "http://example.com/wiki/";
42
43 # Pathological case of multiplying slashes
44 my $home =
45     child_path_is $menu, [home  => "/"], "/";
46     child_path_is $home, [slash => "/"], "/";
47     child_path_is $home, [empty => ""],  "/";
48
49
50
51 sub order_ok($$;$) {
52     my ($menu, $expected, $name) = @_;
53     my @children = $menu->children;
54
55     is scalar @children, scalar @$expected, "correct number of children";
56     is_deeply [map { $_->key } @children], $expected, $name;
57     
58     my $last_child = shift @children; # first child's sort doesn't matter
59     for (@children) {
60         ok $_->sort_order > $last_child->sort_order, sprintf "%s order higher than %s's", $_->key, $last_child->key;
61         $last_child = $_;
62     }
63 }
64
65 $menu = RT::Interface::Web::Menu->new;
66
67 ok $menu->child("foo", title => "foo"), "added child foo";
68 order_ok $menu, [qw(foo)], "sorted";
69
70 ok $menu->child("foo")->add_after("bar", title => "bar"), "added child bar after foo";
71 order_ok $menu, [qw(foo bar)], "sorted after";
72
73 ok $menu->child("bar")->add_before("baz", title => "baz"), "added child baz before bar";
74 order_ok $menu, [qw(foo baz bar)], "sorted before (in between)";
75
76 ok $menu->child("bat", title => "bat", sort_order => 2.2), "added child bat between baz and bar";
77 order_ok $menu, [qw(foo baz bat bar)], "sorted between manually";
78
79 ok $menu->child("bat")->add_before("pre", title => "pre"), "added child pre before bat";
80 order_ok $menu, [qw(foo baz pre bat bar)], "sorted between (before)";
81
82 ok $menu->child("bat")->add_after("post", title => "post"), "added child post after bat";
83 order_ok $menu, [qw(foo baz pre bat post bar)], "sorted between (after)";
84
85 done_testing;