Added feature #13697: Add possibility to retrieve extension version
authorOliver Hader <oliver.hader@typo3.org>
Fri, 26 Mar 2010 22:05:58 +0000 (22:05 +0000)
committerOliver Hader <oliver.hader@typo3.org>
Fri, 26 Mar 2010 22:05:58 +0000 (22:05 +0000)
git-svn-id: https://svn.typo3.org/TYPO3v4/Core/trunk@7199 709f56b5-9817-0410-a4d7-c38de5d9e867

ChangeLog
t3lib/class.t3lib_extmgm.php
tests/t3lib/fixtures/ext_emconf.php [new file with mode: 0644]
tests/t3lib/t3lib_extmgm_testcase.php

index 1f46b18..c1593c5 100755 (executable)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2010-03-26  Oliver Hader  <oliver@typo3.org>
+
+       * Added feature #13697: Add possibility to retrieve extension version (thanks to Tolleiv Nietsch & Oliver Klee)
+
 2010-03-26  Xavier Perseguers  <typo3@perseguers.ch>
 
        * Fixed bug #13775: Missing <tr> in "Admin Tools -> User Admin" (thanks to Stephan Kellermayr)
 2010-03-26  Xavier Perseguers  <typo3@perseguers.ch>
 
        * Fixed bug #13775: Missing <tr> in "Admin Tools -> User Admin" (thanks to Stephan Kellermayr)
index eadd23d..e629cb4 100644 (file)
@@ -229,8 +229,27 @@ final class t3lib_extMgm {
                self::$extensionKeyMap = NULL;
        }
 
                self::$extensionKeyMap = NULL;
        }
 
+       /**
+        * Retrieves the version of an installed extension.
+        * If the extension is not installed, this function returns an empty string.
+        *
+        * @param string $key the key of the extension to look up, must not be empty
+        * @return string the extension version as a string in the format "x.y.z",
+        *                will be an empty string if the extension is not loaded
+        */
+       public static function getExtensionVersion($key) {
+               if (!is_string($key) || empty($key)) {
+                       throw new InvalidArgumentException('Extension key must be a non-empty string.');
+               }
+               if (!self::isLoaded($key)) {
+                       return '';
+               }
 
 
+               $_EXTKEY = $key;
+               include(t3lib_extMgm::extPath($key) . 'ext_emconf.php');
 
 
+               return $EM_CONF[$key]['version'];
+       }
 
 
 
 
 
 
diff --git a/tests/t3lib/fixtures/ext_emconf.php b/tests/t3lib/fixtures/ext_emconf.php
new file mode 100644 (file)
index 0000000..b84cb21
--- /dev/null
@@ -0,0 +1,37 @@
+<?php
+$EM_CONF[$_EXTKEY] = array(
+       'title' => '',
+       'description' => 'This is a fixture extension configuration file used for unit tests.',
+       'category' => '',
+       'shy' => 1,
+       'dependencies' => '',
+       'conflicts' => '',
+       'priority' => '',
+       'loadOrder' => '',
+       'module' => '',
+       'state' => 'stable',
+       'internal' => 1,
+       'uploadfolder' => 0,
+       'createDirs' => '',
+       'modify_tables' => '',
+       'clearCacheOnLoad' => 0,
+       'lockType' => '',
+       'author' => '',
+       'author_email' => '',
+       'author_company' => '',
+       'CGLcompliance' => '',
+       'CGLcompliance_note' => '',
+       'version' => '1.2.3',
+       '_md5_values_when_last_written' => '',
+       'constraints' => array(
+               'depends' => array(
+               ),
+               'conflicts' => array(
+               ),
+               'suggests' => array(
+               ),
+       ),
+       'suggests' => array(
+       ),
+);
+?>
\ No newline at end of file
index 36db259..5eacf33 100644 (file)
@@ -401,5 +401,66 @@ class t3lib_extmgm_testcase extends tx_phpunit_testcase {
                        'newA, newB, fieldX', $GLOBALS['TCA'][$table]['palettes']['generatedFor-fieldA']['showitem']
                );
        }
                        'newA, newB, fieldX', $GLOBALS['TCA'][$table]['palettes']['generatedFor-fieldA']['showitem']
                );
        }
+
+
+       /////////////////////////////////////////
+       // Tests concerning getExtensionVersion
+       /////////////////////////////////////////
+
+       /**
+        * Data provider for negative getExtensionVersion() tests.
+        *
+        * @return array
+        */
+       public function getExtensionVersionFaultyDataProvider() {
+               return array(
+                       array(''),
+                       array(0),
+                       array(new stdClass()),
+                       array(TRUE),
+               );
+       }
+
+       /**
+        * @test
+        * @expectedException InvalidArgumentException
+        * @dataProvider getExtensionVersionFaultyDataProvider
+        */
+       public function getExtensionVersionForFaultyExtensionKeyThrowsException($key) {
+               t3lib_extMgm::getExtensionVersion($key);
+       }
+
+       /**
+        * @test
+        */
+       public function getExtensionVersionForNotLoadedExtensionReturnsEmptyString() {
+               t3lib_extMgm::clearExtensionKeyMap();
+
+               $uniqueSuffix = uniqid('test');
+               $extensionKey = 'unloadedextension' . $uniqueSuffix;
+
+               $this->assertEquals(
+                       '',
+                       t3lib_extMgm::getExtensionVersion($extensionKey)
+               );
+       }
+
+       /**
+        * @test
+        */
+       public function getExtensionVersionForLoadedExtensionReturnsExtensionVersion() {
+               t3lib_extMgm::clearExtensionKeyMap();
+
+               $uniqueSuffix = uniqid('test');
+               $extensionKey = 'unloadedextension' . $uniqueSuffix;
+
+               $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array(
+                       'siteRelPath' => 'typo3_src/tests/t3lib/fixtures/',
+               );
+               $this->assertEquals(
+                       '1.2.3',
+                       t3lib_extMgm::getExtensionVersion($extensionKey)
+               );
+       }
 }
 ?>
\ No newline at end of file
 }
 ?>
\ No newline at end of file