Skip to content

Commit

Permalink
Item9563: Refresh prefs cache b4 call afterSave
Browse files Browse the repository at this point in the history
Prefs are cached, so any attempt to read changed topic preferences in
the afterSaveHandler fails because the cached values from before the
save are returned.

The simple fix appears to be to delete the preferences cache for the
object before dispatching the afterSaveHandler.

Updated unit tests to verify the issue.

There is stil some issue remaining - Foswiki::Func returns null
parameters when called in the afterSaveHandler.

git-svn-id: http://svn.foswiki.org/branches/Release01x01@13773 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
GeorgeClark authored and GeorgeClark committed Jan 21, 2012
1 parent 64f238a commit aa8e0c8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
25 changes: 12 additions & 13 deletions UnitTestContrib/test/unit/PluginHandlerTests.pm
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ EOF
$Foswiki::cfg{Plugins}{ $this->{plugin_name} }{Enabled} = 1;
$Foswiki::cfg{Plugins}{ $this->{plugin_name} }{Module} =
"Foswiki::Plugins::$this->{plugin_name}";
$this->{session}->finish();
$this->{session} = new Foswiki(); # default user
$this->createNewFoswikiSession();
eval "\$Foswiki::Plugins::$this->{plugin_name}::tester = \$this;";
$this->checkCalls( 1, 'initPlugin' );
$Foswiki::Plugins::SESSION = $this->{session};
Expand Down Expand Up @@ -189,8 +188,7 @@ sub test_saveHandlers {
};

my $q = Foswiki::Func::getRequestObject();
$this->{session}->finish();
$this->{session} = new Foswiki( $Foswiki::cfg{GuestUserLogin}, $q );
$this->createNewFoswikiSession( $Foswiki::cfg{GuestUserLogin}, $q );

$this->makePlugin( 'saveHandlers', <<'HERE');
sub beforeSaveHandler {
Expand All @@ -215,14 +213,15 @@ sub afterSaveHandler {
$tester->assert($_[4]->isa('Foswiki::Meta'), "OUCH $_[4]");
$tester->assert_str_equals('Wibble', $_[4]->get('WIBBLE')->{wibble});
$tester->assert_matches( qr/B4SAVE/, $_[0]);
Foswiki::Func::pushTopicContext( $this->{test_web}, 'Tropic' );
#SMELL: This fails due to cached preferences
#$tester->assert_str_equals( "AFTER",
# $_[4]->getPreference("BLAH"));
$tester->assert_str_equals( "AFTER",
$_[4]->getPreference("BLAH"));
#SMELL: And for some reason this returns null instead of either BEFORE or AFTER
#Foswiki::Func::getPreferencesValue("BLAH") );
# Foswiki::Func::pushTopicContext( $this->{test_web}, 'Tropic' );
# $tester->assert_str_equals( "AFTER",
# Foswiki::Func::getPreferencesValue("BLAH") );
$called->{afterSaveHandler}++;
}
HERE
Expand All @@ -236,14 +235,15 @@ HERE
$this->checkCalls( 1, 'beforeSaveHandler' );
$this->checkCalls( 1, 'afterSaveHandler' );

$this->createNewFoswikiSession(); # undef, $topicquery );
Foswiki::Func::pushTopicContext( $this->{test_web}, 'Tropic' );

my $newMeta =
Foswiki::Meta->load( $this->{session}, $this->{test_web}, "Tropic" );
$this->assert_matches( qr\B4SAVE\, $newMeta->text() );
$this->assert_str_equals( 'Wibble', $newMeta->get('WIBBLE')->{wibble} );
$this->assert_str_equals( "AFTER", $newMeta->getPreference("BLAH") );

#SMELL: Without this call, getPreferences returns BEFORE
Foswiki::Func::pushTopicContext( $this->{test_web}, 'Tropic' );
$this->assert_str_equals( "AFTER",
Foswiki::Func::getPreferencesValue("BLAH") );

Expand Down Expand Up @@ -686,9 +686,8 @@ sub finishPlugin {
}
HERE

$this->{session}->finish();
$this->createNewFoswikiSession();
$this->checkCalls( 1, 'finishPlugin' );
$this->{session} = new Foswiki();
}

1;
1 change: 1 addition & 0 deletions core/lib/Foswiki/Meta.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1938,6 +1938,7 @@ sub save {
# TWiki:Codev.BugBeforeSaveHandlerBroken
if ( $plugins->haveHandlerFor('afterSaveHandler') ) {
my $text = $this->getEmbeddedStoreForm();
delete $this->{_preferences}; # Make sure handler has changed prefs
my $error = $signal ? $signal->{-text} : undef;
$plugins->dispatch( 'afterSaveHandler', $text, $this->{_topic},
$this->{_web}, $error, $this );
Expand Down

0 comments on commit aa8e0c8

Please sign in to comment.