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

Jason May jasonmay at bestpractical.com
Mon Feb 13 19:02:59 EST 2012


The branch, 4.0/delete-user-attributes-on-reset has been updated
       via  2c3d8c2474c8e92c035a29bc36eb210db511db76 (commit)
       via  29174af827db048acba5c3a119d154d11e3b9111 (commit)
      from  ef23ab538a49078d64f4f7ba105e92e78acf004b (commit)

Summary of changes:
 lib/RT/User.pm               |   22 ++++++++++++++++++
 share/html/Prefs/MyRT.html   |   20 +++++++++++------
 share/html/Prefs/Search.html |   15 +------------
 t/web/reset_prefs.t          |   49 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 85 insertions(+), 21 deletions(-)

- Log -----------------------------------------------------------------
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 2c3d8c2474c8e92c035a29bc36eb210db511db76
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