Skip to content

Commit

Permalink
Fix invalid enumeration string value display
Browse files Browse the repository at this point in the history
Prior to this, an invalid enumeration string value (e.g. @32@) was
displayed if the corresponding localized value did not exist.

This allows fallback to English language for individual enum elements.

Fixes #16975, #198

Signed-off-by: Damien Regad <dregad@mantisbt.org>
  • Loading branch information
mantis authored and dregad committed Jul 7, 2014
1 parent 4ef0e69 commit 4fbd340
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
8 changes: 7 additions & 1 deletion core/classes/MantisEnum.class.php
Expand Up @@ -79,7 +79,13 @@ public static function getLocalizedLabel( $enumString, $localizedEnumString, $va
return MantisEnum::getLabelForUnknownValue( $value );
}

return MantisEnum::getLabel( $localizedEnumString, $value );
$t_assoc_array = MantisEnum::getAssocArrayIndexedByValues( $localizedEnumString );

if( isset( $t_assoc_array[(int)$value] ) ) {
return $t_assoc_array[(int)$value];
}

return MantisEnum::getLabel( $enumString, $value );
}

/**
Expand Down
15 changes: 11 additions & 4 deletions tests/Mantis/EnumTest.php
Expand Up @@ -37,6 +37,7 @@
*/
class MantisEnumTest extends PHPUnit_Framework_TestCase {
const ACCESS_LEVELS_ENUM = '10:viewer,25:reporter,40:updater,55:developer,70:manager,90:administrator';
const ACCESS_LEVELS_ENUM_EXTRA = '10:viewer,25:reporter,40:updater,55:developer,70:manager,90:administrator,100:missing';
const ACCESS_LEVELS_LOCALIZED_ENUM = '10:viewer_x,25:reporter_x,40:updater_x,55:developer_x,70:manager_x,90:administrator_x,95:extra_x';
const EMPTY_ENUM = '';
const DUPLICATE_VALUES_ENUM = '10:viewer1,10:viewer2';
Expand Down Expand Up @@ -72,9 +73,15 @@ public function testGetLocalizedLabel() {
# Test unknown case
$this->assertEquals( '@5@', MantisEnum::getLocalizedLabel( MantisEnumTest::ACCESS_LEVELS_ENUM, MantisEnumTest::ACCESS_LEVELS_LOCALIZED_ENUM, 5 ) );

# Test the case where the value is in the localized enum but not the standard one. In this case it should be treated
# as unknown.
# Test the case where the value is in the localized enumeration but not the standard one.
# In this case it should be treated as unknown.
$this->assertEquals( '@95@', MantisEnum::getLocalizedLabel( MantisEnumTest::ACCESS_LEVELS_ENUM, MantisEnumTest::ACCESS_LEVELS_LOCALIZED_ENUM, 95 ) );

# Test the case where the value is in the standard enumeration but not in the localized one.
# In this case we should fall back to the standard enumeration (as we do with language strings)
# as the value is a known good value - just that it has not yet been localized.
$this->assertEquals( 'missing', MantisEnum::getLocalizedLabel( MantisEnumTest::ACCESS_LEVELS_ENUM_EXTRA, MantisEnumTest::ACCESS_LEVELS_LOCALIZED_ENUM, 100 ) );

}

/**
Expand Down Expand Up @@ -120,8 +127,8 @@ public function testGetValue() {
$this->assertEquals( 10, MantisEnum::getValue( MantisEnumTest::DUPLICATE_VALUES_ENUM, 'viewer1' ) );
$this->assertEquals( 20, MantisEnum::getValue( MantisEnumTest::NAME_WITH_SPACES_ENUM, 'second label' ) );

# This is not inconsisent with duplicate values behavior, however, it is considered correct since it simplies the code
# and it is not a real scenario.
# This is not inconsistent with duplicate values behaviour, however, it is considered correct
# since it simplifies the code and it is not a real scenario.
$this->assertEquals( 20, MantisEnum::getValue( MantisEnumTest::DUPLICATE_LABELS_ENUM, 'viewer' ) );
}

Expand Down

0 comments on commit 4fbd340

Please sign in to comment.