File: menu.t

package info (click to toggle)
request-tracker5 5.0.9%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,796 kB
  • sloc: javascript: 191,917; perl: 88,458; sh: 1,433; makefile: 489; python: 37; php: 15
file content (102 lines) | stat: -rw-r--r-- 3,622 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use strict;
use warnings;

use RT::Test tests => undef;
use RT::Interface::Web::Menu;

sub child_path_is($$$) {
    my ($menu, $child, $expected) = @_;
    my $c = $menu->child($child->[0], path => $child->[1]);
    is $c->path, $expected, "'$child->[1]' normalizes to '$expected'";
    return $c;
}

{
    package FakeRequest;
    our $PATH_INFO = "";
    sub new { bless {}, shift }
    sub path_info { $PATH_INFO }

    package FakeInterp;
    our $URI = "";
    require CGI::PSGI;
    sub new { bless {}, shift }
    sub cgi_object { CGI::PSGI->new({ REQUEST_URI => $URI }) }
}

local $HTML::Mason::Commands::r = FakeRequest->new;
local $HTML::Mason::Commands::m = FakeInterp->new;

my $menu = RT::Interface::Web::Menu->new;
ok $menu, "Created top level menu";

child_path_is $menu, [search    => "Search/Simple.html"],   "/Search/Simple.html";
child_path_is $menu, [absolute  => "/Prefs/Other.html"],    "/Prefs/Other.html";
child_path_is $menu, [scheme    => "http://example.com"],   "http://example.com";

my $tools =
    child_path_is $menu,    [tools      => "/Tools/"],              "/Tools/";
    child_path_is $tools,   [myday      => "MyDay.html"],           "/Tools/MyDay.html";
    child_path_is $tools,   [activity   => "/Activity.html"],       "/Activity.html";
    my $ext =
        child_path_is $tools,   [external   => "http://example.com"],   "http://example.com";
        child_path_is $ext,     [wiki       => "wiki/"],                "http://example.com/wiki/";

# Pathological case of multiplying slashes
my $home =
    child_path_is $menu, [home  => "/"], "/";
    child_path_is $home, [slash => "/"], "/";
    child_path_is $home, [empty => ""],  "/";



sub order_ok($$;$) {
    my ($menu, $expected, $name) = @_;
    my @children = $menu->children;

    is scalar @children, scalar @$expected, "correct number of children";
    is_deeply [map { $_->key } @children], $expected, $name;
    
    my $last_child = shift @children; # first child's sort doesn't matter
    for (@children) {
        ok $_->sort_order > $last_child->sort_order, sprintf "%s order higher than %s's", $_->key, $last_child->key;
        $last_child = $_;
    }
}

$menu = RT::Interface::Web::Menu->new;

ok $menu->child("foo", title => "foo"), "added child foo";
order_ok $menu, [qw(foo)], "sorted";

ok $menu->child("foo")->add_after("bar", title => "bar"), "added child bar after foo";
order_ok $menu, [qw(foo bar)], "sorted after";

ok $menu->child("bar")->add_before("baz", title => "baz"), "added child baz before bar";
order_ok $menu, [qw(foo baz bar)], "sorted before (in between)";

ok $menu->child("bat", title => "bat", sort_order => 2.2), "added child bat between baz and bar";
order_ok $menu, [qw(foo baz bat bar)], "sorted between manually";

ok $menu->child("bat")->add_before("pre", title => "pre"), "added child pre before bat";
order_ok $menu, [qw(foo baz pre bat bar)], "sorted between (before)";

ok $menu->child("bat")->add_after("post", title => "post"), "added child post after bat";
order_ok $menu, [qw(foo baz pre bat post bar)], "sorted between (after)";

{
    my $menu = RT::Interface::Web::Menu->new;
    local $FakeRequest::PATH_INFO = "/Tools/index.html";
    $menu->child("tools", title => "Tools", path => "/Tools/");
    ok $menu->child('tools')->active, "Tools is active";
}

{
    local $FakeRequest::PATH_INFO = "/Tools/index.html";
    local $FakeInterp::URI = "/Tools/index.html?foo=bar";
    my $menu = RT::Interface::Web::Menu->new;
    $menu->child("tools", title => "Tools", path => "/Tools?foo=bar");
    ok $menu->child('tools')->active, "Tools is active";
}

done_testing;