[Rt-commit] rt branch, 4.2/remove-deleted-dashboards-from-menu, created. rt-4.2.13-54-gbc441b8

Jim Brandt jbrandt at bestpractical.com
Fri Sep 2 15:01:32 EDT 2016


The branch, 4.2/remove-deleted-dashboards-from-menu has been created
        at  bc441b84c1f4d631dfc71d867032083406bae8e5 (commit)

- Log -----------------------------------------------------------------
commit e9ac3d7d1ab140987e97cbdb5cd471fdb641889a
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Fri Sep 2 14:51:44 2016 -0400

    Add test that shows warnings when a dashboard is deleted
    
    When a dashboard is deleted, it isn't removed from
    menus, system or personal, it may have been added to.

diff --git a/t/web/dashboards-in-menu.t b/t/web/dashboards-in-menu.t
index 3126d55..8333313 100644
--- a/t/web/dashboards-in-menu.t
+++ b/t/web/dashboards-in-menu.t
@@ -1,7 +1,7 @@
 use strict;
 use warnings;
 
-use RT::Test tests => 31;
+use RT::Test tests => undef;
 my ($baseurl, $m) = RT::Test->started_ok;
 
 my $system_foo = RT::Dashboard->new($RT::SystemUser);
@@ -75,11 +75,29 @@ $m->content_contains( 'Preferences saved for dashboards in menu.',
     'prefs saved' );
 $m->follow_link_ok( { text => 'self bar' }, 'follow self bar link' );
 $m->title_is( 'self bar Dashboard', 'got self bar dashboard page' );
+
+diag "Test deleting dashboard";
+$m->follow_link_ok( { text => 'self foo' }, 'follow self foo link' );
+$m->follow_link_ok( { text => 'Basics' }, 'Click dashboard Basics' );
+$m->form_name('ModifyDashboard');
+$m->click_button(name => 'Delete');
+
+diag "Reset dashboard menu";
 $m->get_ok( $baseurl."/Prefs/DashboardsInMenu.html");
 $m->form_with_fields('Reset');
 $m->click;
 $m->content_contains( 'Preferences saved', 'prefs saved' );
 ok( $m->find_link( text => 'system foo' ), 'got system foo link' );
-ok( !$m->find_link( text => 'self foo' ), 'no self foo link' );
 ok( !$m->find_link( text => 'self bar' ), 'no self bar link' );
 
+diag "Delete system dashboard";
+$m->get_ok( $baseurl . "/Dashboards/index.html" );
+$m->follow_link_ok( { text => 'system foo' }, 'follow self foo link' );
+$m->follow_link_ok( { text => 'Basics' }, 'Click dashboard Basics' );
+$m->form_name('ModifyDashboard');
+$m->click_button(name => 'Delete');
+$m->get_ok( $baseurl . "/Dashboards/index.html" );
+$m->content_lacks('system foo', 'Dashboard is deleted');
+
+undef $m;
+done_testing;

commit bc441b84c1f4d631dfc71d867032083406bae8e5
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Fri Sep 2 14:57:16 2016 -0400

    Remove dashboard from menu if it can't be loaded
    
    If a dashboard can't be loaded, most likely because it was
    deleted, remove it from the user and system dashboard menus.
    
    Fixes: I#29719

diff --git a/lib/RT/Interface/Web/Menu.pm b/lib/RT/Interface/Web/Menu.pm
index 3d22a15..d43a017 100644
--- a/lib/RT/Interface/Web/Menu.pm
+++ b/lib/RT/Interface/Web/Menu.pm
@@ -375,4 +375,52 @@ sub _insert_sibling {
     $parent->child( @_, sort_order => $sort_order );
 }
 
+=head2 RemoveDashboardMenuItems
+
+Remove dashboards from individual user and system dash menus.
+
+Requires a hash with DashboardId and CurrentUser object.
+
+    $menu->RemoveDashboardMenuItem( DashboardId => $id, CurrentUser => $session{CurrentUser}->UserObj );
+
+=cut
+
+sub RemoveDashboardMenuItem {
+    my $self = shift;
+    my %args = @_;
+
+    return unless $args{'DashboardId'} and $args{'CurrentUser'};
+    my $dashboard_id = $args{'DashboardId'};
+    my $current_user = $args{'CurrentUser'};
+
+    # First clear from user's dashboards
+    my $dashboards_in_menu = $current_user->Preferences('DashboardsInMenu', {} );
+
+    my @dashboards = grep { $_ != $dashboard_id } @{$dashboards_in_menu->{'dashboards'}};
+    $dashboards_in_menu->{'dashboards'} = \@dashboards || [];
+
+    my ($ret, $msg) = $current_user->SetPreferences('DashboardsInMenu', $dashboards_in_menu);
+    RT::Logger->warn("Unable to update dashboard for user " . $current_user->Name . ": $msg")
+        unless $ret;
+
+    # Now update the system dashboard
+    my $system = RT::System->new( $current_user );
+    my ($default_dashboards) = $system->Attributes->Named('DashboardsInMenu');
+
+    if ($default_dashboards) {
+        $dashboards_in_menu = $default_dashboards->Content;
+        my @dashboards = grep { $_ != $dashboard_id } @{$dashboards_in_menu->{'dashboards'}};
+
+        # Update only if we removed one
+        if ( @{$dashboards_in_menu->{'dashboards'}} > @dashboards ){
+            $dashboards_in_menu->{'dashboards'} = \@dashboards || [];
+
+            ($ret, $msg) = $default_dashboards->SetContent($dashboards_in_menu);
+            RT::Logger->warn("Unable to update system dashboard menu: $msg")
+                unless $ret;
+        }
+    }
+    return;
+}
+
 1;
diff --git a/share/html/Elements/Tabs b/share/html/Elements/Tabs
index 1b81833..99ee3f8 100644
--- a/share/html/Elements/Tabs
+++ b/share/html/Elements/Tabs
@@ -494,7 +494,9 @@ my $build_main_nav = sub {
         if ( $status ) {
             push @dashboards, $dash;
         } else {
-            $RT::Logger->warning( "Failed to load dashboard $id: $msg" );
+            $RT::Logger->debug( "Failed to load dashboard $id: $msg, removing from menu" );
+            $home->RemoveDashboardMenuItem( DashboardId => $id, CurrentUser => $session{CurrentUser}->UserObj );
+            @{$session{'dashboards_in_menu'}} = grep { $_ != $id } @{$session{'dashboards_in_menu'}};
         }
     }
 

-----------------------------------------------------------------------


More information about the rt-commit mailing list