[Rt-commit] rt branch, 4.0/delete-user-attributes-on-reset, created. rt-4.0.4-119-gc99611d

Jason May jasonmay at bestpractical.com
Tue Feb 14 11:40:16 EST 2012


The branch, 4.0/delete-user-attributes-on-reset has been created
        at  c99611dcdecf52072a2d33b336717bcddbf6c496 (commit)

- Log -----------------------------------------------------------------
commit 2c91955866dd7e7b9da14c216785251f2e5e8dbb
Author: Jason May <jasonmay at bestpractical.com>
Date:   Wed Dec 7 12:24:38 2011 -0500

    Delete the user's Pref-HomepageSettings attribute on Reset
    
    If the user's Pref-HomepageSettings is there but not empty, it won't
    respect any new changes made to the global homepage settings.
    
    However, the fix is still not visible due to referring to session cache
    checks. The preferences are checked in the session before the database,
    so users will still not see homepage changes.

diff --git a/share/html/Prefs/MyRT.html b/share/html/Prefs/MyRT.html
index cfcf315..46848e1 100644
--- a/share/html/Prefs/MyRT.html
+++ b/share/html/Prefs/MyRT.html
@@ -97,7 +97,9 @@ if ( $ARGS{'UpdateSummaryRows'} ) {
 $ARGS{'SummaryRows'} ||= $user->Preferences('SummaryRows', RT->Config->Get('DefaultSummaryRows'));
 
 if ($ARGS{Reset}) {
-    my ($ok, $msg) = $user->SetPreferences('HomepageSettings', {});
+    my $attr = RT::Attribute->new($session{CurrentUser});
+    $attr->LoadByNameAndObject( Object => $user, Name => 'Pref-HomepageSettings' );
+    my ($ok, $msg) = $attr->Delete;
     push @results, $ok ? loc('Preferences saved.') : $msg;
     delete $session{'my_rt_portlets'};
 }

commit 54149fba710aebc3cbbb9f3ca90d3598ddfe0849
Author: Jason May <jasonmay at bestpractical.com>
Date:   Wed Dec 7 19:18:31 2011 -0500

    Stop caching the default system prefs in the session
    
    This lets users blow away their preferences and track the system
    defaults instead of getting a snapshot of the defaults upon reset.

diff --git a/share/html/Elements/MyRT b/share/html/Elements/MyRT
index 37d8976..22a0e7d 100644
--- a/share/html/Elements/MyRT
+++ b/share/html/Elements/MyRT
@@ -70,7 +70,7 @@ my $user = $session{'CurrentUser'}->UserObj;
 $Portlets ||= $session{'my_rt_portlets'};
 unless ( $Portlets ) {
     my ($default_portlets) = RT::System->new($session{'CurrentUser'})->Attributes->Named('HomepageSettings');
-    $Portlets = $session{'my_rt_portlets'} = $user->Preferences(
+    $Portlets = $user->Preferences(
         HomepageSettings => $default_portlets? $default_portlets->Content: {},
     );
 }
diff --git a/share/html/Prefs/MyRT.html b/share/html/Prefs/MyRT.html
index 46848e1..d0f9786 100644
--- a/share/html/Prefs/MyRT.html
+++ b/share/html/Prefs/MyRT.html
@@ -104,12 +104,15 @@ if ($ARGS{Reset}) {
     delete $session{'my_rt_portlets'};
 }
 
-unless (exists $session{'my_rt_portlets'}) {
+my $portlets;
+if (exists $session{'my_rt_portlets'}) {
+    $portlets = $session{'my_rt_portlets'};
+}
+else {
     my ($default_portlets) = RT::System->new($session{'CurrentUser'})->Attributes->Named('HomepageSettings');
-    my $portlets = $default_portlets ? $default_portlets->Content : {};
-    $session{'my_rt_portlets'} = $user->Preferences('HomepageSettings', $portlets);
+    $default_portlets = $default_portlets ? $default_portlets->Content : {};
+    $portlets = $user->Preferences('HomepageSettings', $default_portlets);
 }
-my $portlets = $session{'my_rt_portlets'};
 
 my %seen;
 my @items = map ["component-$_", $_], grep !$seen{$_}++, @{RT->Config->Get('HomepageComponents')};

commit f441126f6c9f43e875923fda7ab603ecf96121b0
Author: Jason May <jasonmay at bestpractical.com>
Date:   Mon Dec 12 15:47:05 2011 -0500

    Delete the Pref-SummaryRows attribute to reset everything on MyRT.html

diff --git a/share/html/Prefs/MyRT.html b/share/html/Prefs/MyRT.html
index d0f9786..dd3e049 100644
--- a/share/html/Prefs/MyRT.html
+++ b/share/html/Prefs/MyRT.html
@@ -99,8 +99,15 @@ $ARGS{'SummaryRows'} ||= $user->Preferences('SummaryRows', RT->Config->Get('Defa
 if ($ARGS{Reset}) {
     my $attr = RT::Attribute->new($session{CurrentUser});
     $attr->LoadByNameAndObject( Object => $user, Name => 'Pref-HomepageSettings' );
-    my ($ok, $msg) = $attr->Delete;
-    push @results, $ok ? loc('Preferences saved.') : $msg;
+    my ($hp_ok, $msg) = $attr->Delete;
+    push @results, $msg unless $hp_ok;
+
+    $attr->LoadByNameAndObject( Object => $user, Name => 'Pref-SummaryRows' );
+    (my $sr_ok, $msg) = $attr->Delete;
+    push @results, $msg unless $sr_ok;
+
+    push @results, loc('Preferences saved.') if $hp_ok and $sr_ok;
+
     delete $session{'my_rt_portlets'};
 }
 

commit fbf9ae9c00bc6a850f7c100937d7e792b37bb460
Author: Jason May <jasonmay at bestpractical.com>
Date:   Thu Feb 9 18:34:58 2012 -0500

    Provide the option to reset saved search adjustments
    
    This provides a way for users to reset their preferences at the
    saved-search level, which causes the preferences to default to the
    system saved search settings.

diff --git a/share/html/Prefs/Search.html b/share/html/Prefs/Search.html
index 0b97e31..2d02eb1 100644
--- a/share/html/Prefs/Search.html
+++ b/share/html/Prefs/Search.html
@@ -68,6 +68,14 @@
 
 </form>
 
+<&|/Widgets/TitleBox, title => loc("Reset adjustments") &>
+<form method="post" action="Search.html">
+<input type="hidden" name="Reset" value="1" />
+<input type="hidden" name="name" value="<%$ARGS{name}%>" class="hidden" />
+<input type="submit" class="button" value="<% loc('Reset to default') %>">
+</form>
+</&>
+
 <%INIT>
 my @actions;
 my $title = loc("Customize").' ';
@@ -81,6 +89,26 @@ Abort('No search specified')
 
 my $search = $class->new ($session{'CurrentUser'});
 $search->LoadById ($id);
+
+# If we are resetting prefs, do so before attempting to load them
+if ($ARGS{'Reset'}) {
+    my $pref = $class->new($session{'CurrentUser'});
+    my ($pref_ok) = $pref->LoadByNameAndObject(
+        Object => $session{'CurrentUser'}->UserObj,
+        Name   => 'Pref-'.$ARGS{'name'},
+    );
+
+    my ($ok, $msg);
+    if ($pref_ok) {
+        ($ok, $msg) = $pref->Delete;
+    }
+    else {
+        # The pref doesn't exist yet, but say it was reset anyway
+        $ok = 1;
+    }
+    push @actions, $ok ? loc('Preferences saved.') : $msg;
+}
+
 $title .= loc ($search->Description, loc ('"N"'));
 my $user = $session{'CurrentUser'}->UserObj;
 my $SearchArg = $user->Preferences($search, $search->Content);

commit ef23ab538a49078d64f4f7ba105e92e78acf004b
Author: Jason May <jasonmay at bestpractical.com>
Date:   Fri Feb 10 15:22:46 2012 -0500

    Add tests for resetting preferences

diff --git a/share/html/Prefs/Search.html b/share/html/Prefs/Search.html
index 2d02eb1..204f164 100644
--- a/share/html/Prefs/Search.html
+++ b/share/html/Prefs/Search.html
@@ -69,10 +69,10 @@
 </form>
 
 <&|/Widgets/TitleBox, title => loc("Reset adjustments") &>
-<form method="post" action="Search.html">
+<form method="post" name="ResetSearchOptions" action="Search.html">
 <input type="hidden" name="Reset" value="1" />
 <input type="hidden" name="name" value="<%$ARGS{name}%>" class="hidden" />
-<input type="submit" class="button" value="<% loc('Reset to default') %>">
+<input type="submit" class="button" name="ResetSearchOptions" value="<% loc('Reset to default') %>">
 </form>
 </&>
 
diff --git a/t/web/reset_prefs.t b/t/web/reset_prefs.t
new file mode 100644
index 0000000..9ad777f
--- /dev/null
+++ b/t/web/reset_prefs.t
@@ -0,0 +1,57 @@
+#!/usr/bin/perl -w
+use strict;
+
+use RT::Test tests => undef;
+my ($baseurl, $m) = RT::Test->started_ok;
+$m->login;
+my $url = $m->rt_base_url;
+
+#TODO Test resetting RT at a glance
+
+# find a saved search
+my $search;
+{
+    my $system = RT::System->new(RT->SystemUser);
+    while (my $attribute = $system->Attributes->Next) {
+        if ($attribute->Name =~ /^Search - /) {
+            $search = $attribute;
+            last;
+        }
+    }
+}
+
+my $search_name = 'RT::Attribute-'.$search->id;
+my $pref_name = 'Pref-'.$search_name;
+my $uri = URI->new($url.'Prefs/Search.html');
+$uri->query_form(
+    OrderBy => 'Priority|Due', # something different from the default
+    name    => $search_name,
+);
+
+$m->get($uri);
+
+require RT::User;
+my $user = RT::User->new(RT->SystemUser);
+$user->Load('root');
+
+require RT::Attribute;
+my $pref = RT::Attribute->new($user);
+
+my ($pref_created, $pref_exists);
+($pref_exists) = $pref->LoadByNameAndObject(Name => $pref_name, Object => $user);
+ok(!$pref_exists, 'Preference does not exist yet');
+
+$m->form_name('BuildQuery');
+$m->click_button(name => 'Save');
+
+($pref_created) = $pref->LoadByNameAndObject(Name => $pref_name, Object => $user);
+ok($pref_created, 'Preference was successfully created upon saving');
+
+$m->form_name('ResetSearchOptions');
+$m->click_button(name => 'ResetSearchOptions');
+
+$m->form_name('BuildQuery');
+isnt($m->value('OrderBy', 2), 'Due', 'Custom prefs were cleared');
+
+undef $m;
+done_testing();

commit 29174af827db048acba5c3a119d154d11e3b9111
Author: Jason May <jasonmay at bestpractical.com>
Date:   Mon Feb 13 18:59:25 2012 -0500

    Extract pref deletion logic into a RT::User method for reuse

diff --git a/lib/RT/User.pm b/lib/RT/User.pm
index cbc1a8f..67ee2d5 100644
--- a/lib/RT/User.pm
+++ b/lib/RT/User.pm
@@ -1320,6 +1320,28 @@ sub SetPreferences {
     }
 }
 
+=head2 SetPreferences NAME/OBJ VALUE
+
+Delete user preferences associated with given object or name.
+
+=cut
+
+sub DeletePreferences {
+    my $self = shift;
+    my $name = _PrefName( shift );
+
+    return (0, $self->loc("No permission to set preferences"))
+        unless $self->CurrentUserCanModify('Preferences');
+
+    my $attr = RT::Attribute->new( $self->CurrentUser );
+    $attr->LoadByNameAndObject( Object => $self, Name => $name );
+    if ( $attr->Id ) {
+        return $attr->Delete;
+    }
+
+    return (0, $self->loc("Preferences were not found"));
+}
+
 =head2 Stylesheet
 
 Returns a list of valid stylesheets take from preferences.
diff --git a/share/html/Prefs/MyRT.html b/share/html/Prefs/MyRT.html
index dd3e049..b5cd452 100644
--- a/share/html/Prefs/MyRT.html
+++ b/share/html/Prefs/MyRT.html
@@ -97,16 +97,22 @@ if ( $ARGS{'UpdateSummaryRows'} ) {
 $ARGS{'SummaryRows'} ||= $user->Preferences('SummaryRows', RT->Config->Get('DefaultSummaryRows'));
 
 if ($ARGS{Reset}) {
+    my $user = $session{CurrentUser}->UserObj;
     my $attr = RT::Attribute->new($session{CurrentUser});
-    $attr->LoadByNameAndObject( Object => $user, Name => 'Pref-HomepageSettings' );
-    my ($hp_ok, $msg) = $attr->Delete;
-    push @results, $msg unless $hp_ok;
 
-    $attr->LoadByNameAndObject( Object => $user, Name => 'Pref-SummaryRows' );
-    (my $sr_ok, $msg) = $attr->Delete;
-    push @results, $msg unless $sr_ok;
+    my $reset_ok = 1;
+    for my $pref_name ('HomepageSettings', 'SummaryRows') {
+        my $prefs = $user->Preferences($pref_name);
+        if ($prefs) {
+            my ($ok, $msg) = $user->DeletePreferences($pref_name);
+            if (!$ok)  {
+                push @results, $msg unless $ok;
+                $reset_ok = 0;
+            }
+        }
+    }
 
-    push @results, loc('Preferences saved.') if $hp_ok and $sr_ok;
+    push @results, loc('Preferences saved.') if $reset_ok;
 
     delete $session{'my_rt_portlets'};
 }
diff --git a/share/html/Prefs/Search.html b/share/html/Prefs/Search.html
index 204f164..0efb291 100644
--- a/share/html/Prefs/Search.html
+++ b/share/html/Prefs/Search.html
@@ -92,20 +92,7 @@ $search->LoadById ($id);
 
 # If we are resetting prefs, do so before attempting to load them
 if ($ARGS{'Reset'}) {
-    my $pref = $class->new($session{'CurrentUser'});
-    my ($pref_ok) = $pref->LoadByNameAndObject(
-        Object => $session{'CurrentUser'}->UserObj,
-        Name   => 'Pref-'.$ARGS{'name'},
-    );
-
-    my ($ok, $msg);
-    if ($pref_ok) {
-        ($ok, $msg) = $pref->Delete;
-    }
-    else {
-        # The pref doesn't exist yet, but say it was reset anyway
-        $ok = 1;
-    }
+    my ($ok, $msg) = $session{'CurrentUser'}->UserObj->DeletePreferences($ARGS{name});
     push @actions, $ok ? loc('Preferences saved.') : $msg;
 }
 

commit c99611dcdecf52072a2d33b336717bcddbf6c496
Author: Jason May <jasonmay at bestpractical.com>
Date:   Mon Feb 13 19:01:10 2012 -0500

    Add mysteriously failing tests for the pref reset logic
    
    I think the unexpected behavior boils down to mech failing to get the
    values of a list input ("body-Selected").

diff --git a/t/web/reset_prefs.t b/t/web/reset_prefs.t
index 9ad777f..3c07180 100644
--- a/t/web/reset_prefs.t
+++ b/t/web/reset_prefs.t
@@ -53,5 +53,54 @@ $m->click_button(name => 'ResetSearchOptions');
 $m->form_name('BuildQuery');
 isnt($m->value('OrderBy', 2), 'Due', 'Custom prefs were cleared');
 
+$m->get($url.'Prefs/MyRT.html');
+
+diag("Verifying Dashboards are not a default");
+{
+    lacks_dashboards_ok('body-Selected', "'Dashboards' is not a default pref");
+}
+{ undef $m; done_testing(); exit } # XXX
+
+diag("Adding a component to body prefs");
+{
+    $m->form_name('SelectionBox-body');
+    $m->field('body-Available' => 'component-Dashboards');
+    $m->click_button(name => 'Add');
+    has_dashboards_ok('body-Selected', 'Dashboards are now in the prefs');
+}
+
+diag("Resetting the body prefs");
+{
+    $m->submit_form(fields => {Reset => 1});
+
+    lacks_dashboard_ok('body-Selected', 'Dashboards are no longer in the prefs');
+}
+
 undef $m;
 done_testing();
+
+sub _has_dashboards {
+    my $input_name = shift;
+    $m->form_name('SelectionBox-body');
+    my ($input) = $m->find_all_inputs(name => $input_name);
+    my @selected_values = $input->value_names;
+    
+    my $has_dashboards = grep { $_ eq 'component-Dashboards' } @selected_values;
+    return $input->possible_values;
+}
+
+sub lacks_dashboards_ok {
+    my ($input_name, $message) = @_;
+    local $Test::Builder::Level = $Test::Builder::Level + 1;
+
+    my $has_dashboards = _has_dashboards($input_name);
+    ok(!$has_dashboards, $message);
+}
+
+sub has_dashboards_ok {
+    my ($input_name, $message) = @_;
+    local $Test::Builder::Level = $Test::Builder::Level + 1;
+
+    my $has_dashboards = _has_dashboards($input_name);
+    ok($has_dashboards, $message);
+}

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


More information about the Rt-commit mailing list