Skip to content

Commit

Permalink
Item14563: Unit tests for Meta AUTOINC
Browse files Browse the repository at this point in the history
  • Loading branch information
gac410 committed Feb 18, 2018
1 parent 382a435 commit 4213101
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 3 deletions.
98 changes: 98 additions & 0 deletions UnitTestContrib/test/unit/MetaTests.pm
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,104 @@ sub test_single {
return;
}

sub test_Autoname_AUTOINC {
my $this = shift;
my $meta =
Foswiki::Meta->new( $this->{session}, $this->{test_web},
'TestAutoAUTOINC001' );
$this->assert_str_equals( $meta->topic, "TestAutoAUTOINC001" );
$meta->save();
$this->assert_str_equals( $meta->topic, "TestAuto001" );
$this->assert(
Foswiki::Func::topicExists( $this->{test_web}, 'TestAuto001' ) );

# Reset name and inseret another, should create #2
$meta->topic('TestAutoAUTOINC001');
$meta->save();
$this->assert_str_equals( $meta->topic, "TestAuto002" );
$this->assert(
Foswiki::Func::topicExists( $this->{test_web}, 'TestAuto002' ) );

# Remove the first topic
$meta->topic('TestAuto001');
$meta->removeFromStore();
$this->assert(
!Foswiki::Func::topicExists( $this->{test_web}, 'TestAuto001' ) );

# Verify that we don't fill in missing holes. Creates #3`
$meta->topic('TestAutoAUTOINC001');
$meta->save();
$this->assert_str_equals( $meta->topic, "TestAuto003" );
$this->assert(
Foswiki::Func::topicExists( $this->{test_web}, 'TestAuto003' ) );

# Verify that the suffix option works.
$meta->topic('TestAutoAUTOINC001SUFFIX');
$meta->save();
$this->assert_str_equals( $meta->topic, "TestAuto001SUFFIX" );
$this->assert(
Foswiki::Func::topicExists( $this->{test_web}, 'TestAuto001SUFFIX' ) );

$meta->topic('TestAutoAUTOINC001SUFFIX');
$meta->save();
$this->assert_str_equals( $meta->topic, "TestAuto002SUFFIX" );
$this->assert(
Foswiki::Func::topicExists( $this->{test_web}, 'TestAuto002SUFFIX' ) );

$meta->finish();

return;
}

sub test_Autoname_XXXXXXXXXX {
my $this = shift;
my $meta =
Foswiki::Meta->new( $this->{session}, $this->{test_web},
'TestAutoXXXXXXXXXX' );
$this->assert_str_equals( $meta->topic, "TestAutoXXXXXXXXXX" );
$meta->save();
$this->assert_str_equals( 'TestAuto0', $meta->topic );
$this->assert(
Foswiki::Func::topicExists( $this->{test_web}, 'TestAuto0' ) );

# Reset name and inseret another, should create #2
$meta->topic('TestAutoXXXXXXXXXX');
$meta->save();
$this->assert_str_equals( 'TestAuto1', $meta->topic );
$this->assert(
Foswiki::Func::topicExists( $this->{test_web}, 'TestAuto1' ) );

# Remove the first topic
$meta->topic('TestAuto0');
$meta->removeFromStore();
$this->assert(
!Foswiki::Func::topicExists( $this->{test_web}, 'TestAuto0' ) );

# The old XXX method does fill in holes.
$meta->topic('TestAutoXXXXXXXXXX');
$meta->save();
$this->assert_str_equals( 'TestAuto0', $meta->topic );
$this->assert(
Foswiki::Func::topicExists( $this->{test_web}, 'TestAuto0' ) );

# At least 10 X. 9 just creates a topic.
$meta->topic('TestAutoXXXXXXXXX');
$meta->save();
$this->assert_str_equals( 'TestAutoXXXXXXXXX', $meta->topic );
$this->assert(
Foswiki::Func::topicExists( $this->{test_web}, 'TestAutoXXXXXXXXX' ) );

# 11 X, also autoincrement.
$meta->topic('TestAutoXXXXXXXXXXX');
$meta->save();
$this->assert_str_equals( 'TestAuto2', $meta->topic );
$this->assert(
Foswiki::Func::topicExists( $this->{test_web}, 'TestAuto2' ) );
$meta->finish();

return;
}

sub test_multiple {
my $this = shift;
my $meta =
Expand Down
10 changes: 7 additions & 3 deletions core/lib/Foswiki/Meta.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2101,7 +2101,7 @@ sub saveAs {
# doesn't let the topic name to be overridden, and this step changes
# the topic name. So if we lock first, then we will unlock the wrong name.
$this->{_topic} =
expandAUTOINC( $this->{_session}, $this->{_web}, $this->{_topic} );
_expandAUTOINC( $this->{_session}, $this->{_web}, $this->{_topic} );
$this->_atomicLock($cUID);

my $i = $this->{_session}->{store}->getRevisionHistory($this);
Expand Down Expand Up @@ -2645,12 +2645,16 @@ sub removeFromStore {

=begin TML
---++ StaticMethod expandAUTOINC($session, $web, $topic) -> $topic
---++ StaticMethod _expandAUTOINC($session, $web, $topic) -> $topic
Expand AUTOINC\d+ in the topic name to the next topic name available
*CAUTION*: This routine depends on the store being locked until the topic has
been saved into the Store. This is marked as a private method, as it will return
inconsistent results.
=cut

sub expandAUTOINC {
sub _expandAUTOINC {
my ( $session, $web, $topic ) = @_;

# Do not remove, keep as undocumented feature for compatibility with
Expand Down

0 comments on commit 4213101

Please sign in to comment.