diff --git a/core/modules/file/lib/Drupal/file/FileStatusMap.php b/core/modules/file/lib/Drupal/file/FileStatusMap.php new file mode 100644 index 0000000..43da305 --- /dev/null +++ b/core/modules/file/lib/Drupal/file/FileStatusMap.php @@ -0,0 +1,51 @@ + t('Temporary'), + // @see core/includes/file.inc + FileStatusMap::FILE_STATUS_PERMANENT => t('Permanent'), + ); + } + +} diff --git a/core/modules/file/lib/Drupal/file/Plugin/views/field/Status.php b/core/modules/file/lib/Drupal/file/Plugin/views/field/Status.php index cced9ea..4f4a0c6 100644 --- a/core/modules/file/lib/Drupal/file/Plugin/views/field/Status.php +++ b/core/modules/file/lib/Drupal/file/Plugin/views/field/Status.php @@ -8,6 +8,7 @@ namespace Drupal\file\Plugin\views\field; use Drupal\Component\Annotation\PluginID; +use Drupal\file\FileStatusMap; use Drupal\views\Plugin\views\field\FieldPluginBase; /** @@ -20,17 +21,7 @@ class Status extends FieldPluginBase { function render($values) { - $value = $this->getValue($values); - return $this->getFileStatusLabel($value); - } - - private function getFileStatusLabel($value) { - $valueToLabelMap = array( - 0 => t('Temporary'), - FILE_STATUS_PERMANENT => t('Permanent'), - ); - - return isset($value) && isset($valueToLabelMap[$value]) ? $valueToLabelMap[$value] : t('Unknown'); + return FileStatusMap::getLabel($this->getValue($values)); } } diff --git a/core/modules/file/lib/Drupal/file/Plugin/views/filter/Status.php b/core/modules/file/lib/Drupal/file/Plugin/views/filter/Status.php index 487efe8..2627b1c 100644 --- a/core/modules/file/lib/Drupal/file/Plugin/views/filter/Status.php +++ b/core/modules/file/lib/Drupal/file/Plugin/views/filter/Status.php @@ -8,6 +8,7 @@ namespace Drupal\file\Plugin\views\filter; use Drupal\Component\Annotation\PluginID; +use Drupal\file\FileStatusMap; use Drupal\views\Plugin\views\filter\InOperator; /** @@ -21,10 +22,7 @@ class Status extends InOperator { public function getValueOptions() { if (!isset($this->value_options)) { - $this->value_options = array( - 0 => t('Temporary'), - FILE_STATUS_PERMANENT => t('Permanent'), - ); + $this->value_options = FileStatusMap::getValueToLabelMap(); } } } diff --git a/core/modules/file/tests/Drupal/file/Tests/FileStatusMapTest.php b/core/modules/file/tests/Drupal/file/Tests/FileStatusMapTest.php new file mode 100644 index 0000000..fd4ea76 --- /dev/null +++ b/core/modules/file/tests/Drupal/file/Tests/FileStatusMapTest.php @@ -0,0 +1,68 @@ + 'FileStatusMap tests', + 'description' => 'Tests the FileStatusMap utility class.', + 'group' => 'File API', + ); + } + + /** + * Tests if the FileStatusMap::FILE_STATUS_PERMANENT equals the expected + * value. + */ + public function testFileStatusPermanentConstant() { + $this->assertEquals(1, FileStatusMap::FILE_STATUS_PERMANENT, "The FileStatusMap::FILE_STATUS_PERMANENT const doesn't equal 1 as expected"); + } + + /** + * Tests if the FileStatusMap::getValueToLabelMap() method returns the map + * as expected. + */ + public function testGetValueToLabelMap() { + $expectedMap = array( + 0 => t('Temporary'), + FileStatusMap::FILE_STATUS_PERMANENT => t('Permanent'), + ); + + $this->assertEquals($expectedMap, FileStatusMap::getValueToLabelMap(), "The FileStatusMap::getValueToLabelMap() method doesn't return the same map as expected."); + } + + /** + * Tests if the FileStatusMap::getLabel() method returns the correct labels + * for a given set of test status values. + */ + public function testGetLabel() { + $this->assertEquals(t('Temporary'), FileStatusMap::getLabel(0), "The FileStatusMap::getLabel() method doesn't return 'Temporary' as the label for a status value of 0 as expected."); + $this->assertEquals(t('Permanent'), FileStatusMap::getLabel(FileStatusMap::FILE_STATUS_PERMANENT), "The FileStatusMap::getLabel() method doesn't return 'Permanent' as the label for a status value of FileStatusMap::FILE_STATUS_PERMANENT as expected."); + $this->assertEquals(t('Unknown'), FileStatusMap::getLabel(-1), "The FileStatusMap::getLabel() method doesn't return 'Unknown' as the label for a status value of -1 as expected."); + } +} + +} + +// @todo Remove this once t() is converted to a service. +namespace { + if (!function_exists('t')) { + function t($string) { + return $string; + } + } +}