From 5a03e0a5cf30f4ad5cecd7a90ad3bc2a004770aa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20M=C3=BCller?= <thomas.mueller@tmit.eu>
Date: Fri, 5 Dec 2014 15:28:33 +0100
Subject: [PATCH] adding dependency to owncloud version - with fallback to
 requiremin and requiremax

---
 lib/private/app/dependencyanalyzer.php | 27 ++++++++++++++++++++++
 lib/private/app/platform.php           |  8 +++++++
 tests/data/app/expected-info.json      |  8 ++++++-
 tests/data/app/valid-info.xml          |  1 +
 tests/lib/app/dependencyanalyzer.php   | 32 ++++++++++++++++++++++++++
 5 files changed, 75 insertions(+), 1 deletion(-)

diff --git a/lib/private/app/dependencyanalyzer.php b/lib/private/app/dependencyanalyzer.php
index 2e838872385..795017e2b37 100644
--- a/lib/private/app/dependencyanalyzer.php
+++ b/lib/private/app/dependencyanalyzer.php
@@ -26,6 +26,8 @@ class DependencyAnalyzer {
 	/** @var array  */
 	private $dependencies = array();
 
+	private $appInfo = array();
+
 	/**
 	 * @param array $app
 	 * @param Platform $platform
@@ -49,6 +51,7 @@ class DependencyAnalyzer {
 		$this->analyzeCommands();
 		$this->analyzeLibraries();
 		$this->analyzeOS();
+		$this->analyzeOC();
 		return $this->missing;
 	}
 
@@ -154,7 +157,31 @@ class DependencyAnalyzer {
 		}
 	}
 
+	private function analyzeOC() {
+		$minVersion = null;
+		if (isset($this->dependencies['owncloud']['@attributes']['min-version'])) {
+			$minVersion = $this->dependencies['owncloud']['@attributes']['min-version'];
+		} elseif (isset($this->appInfo['requiremin'])) {
+			$minVersion = $this->appInfo['requiremin'];
+		}
+		$maxVersion = null;
+		if (isset($this->dependencies['oc']['@attributes']['max-version'])) {
+			$maxVersion = $this->dependencies['oc']['@attributes']['max-version'];
+		} elseif (isset($this->appInfo['requiremax'])) {
+			$maxVersion = $this->appInfo['requiremax'];
+		}
 
+		if (!is_null($minVersion)) {
+			if (version_compare($this->platform->getOcVersion(), $minVersion, '<')) {
+				$this->addMissing((string)$this->l->t('ownCloud %s or higher is required.', $minVersion));
+			}
+		}
+		if (!is_null($maxVersion)) {
+			if (version_compare($this->platform->getOcVersion(), $maxVersion, '>')) {
+				$this->addMissing((string)$this->l->t('ownCloud with a version lower than %s is required.', $maxVersion));
+			}
+		}
+	}
 
 	/**
 	 * @param $element
diff --git a/lib/private/app/platform.php b/lib/private/app/platform.php
index 6279bb5f20c..ba96fba00db 100644
--- a/lib/private/app/platform.php
+++ b/lib/private/app/platform.php
@@ -10,6 +10,7 @@
 
 namespace OC\App;
 
+use OC_Util;
 use OCP\IConfig;
 
 /**
@@ -35,6 +36,13 @@ class Platform {
 		return phpversion();
 	}
 
+	/**
+	 * @return string
+	 */
+	public function getOcVersion() {
+		return OC_Util::getVersion();
+	}
+
 	/**
 	 * @return string
 	 */
diff --git a/tests/data/app/expected-info.json b/tests/data/app/expected-info.json
index b899df7a8d6..ab4f103939a 100644
--- a/tests/data/app/expected-info.json
+++ b/tests/data/app/expected-info.json
@@ -60,6 +60,12 @@
 			},
 			"curl"
 		],
-		"os": "Linux"
+		"os": "Linux",
+		"owncloud": {
+			"@attributes" : {
+				"min-version": "7.01",
+				"max-version": "8"
+			}
+		}
 	}
 }
diff --git a/tests/data/app/valid-info.xml b/tests/data/app/valid-info.xml
index 42f4e3edb7f..4b22d55d7bc 100644
--- a/tests/data/app/valid-info.xml
+++ b/tests/data/app/valid-info.xml
@@ -29,5 +29,6 @@
 		<lib max-version="2.0">intl</lib>
 		<lib>curl</lib>
 		<os>Linux</os>
+		<owncloud min-version="7.0.1" max-version="8" />
 	</dependencies>
 </info>
diff --git a/tests/lib/app/dependencyanalyzer.php b/tests/lib/app/dependencyanalyzer.php
index 1cd24193ea6..9c5db96e045 100644
--- a/tests/lib/app/dependencyanalyzer.php
+++ b/tests/lib/app/dependencyanalyzer.php
@@ -51,6 +51,9 @@ class DependencyAnalyzer extends \PHPUnit_Framework_TestCase {
 				}
 				return null;
 			}));
+		$this->platformMock->expects($this->any())
+			->method('getOcVersion')
+			->will( $this->returnValue('8.0.1'));
 
 		$this->l10nMock = $this->getMockBuilder('\OCP\IL10N')
 			->disableOriginalConstructor()
@@ -161,6 +164,35 @@ class DependencyAnalyzer extends \PHPUnit_Framework_TestCase {
 		$this->assertEquals($expectedMissing, $missing);
 	}
 
+	/**
+	 * @dataProvider providesOC
+	 * @param $expectedMissing
+	 * @param $oc
+	 */
+	function testOC($expectedMissing, $oc) {
+		$app = array(
+			'dependencies' => array()
+		);
+		if (!is_null($oc)) {
+			$app['dependencies']['oc'] = $oc;
+		}
+
+		$analyser = new \OC\App\DependencyAnalyzer($app, $this->platformMock, $this->l10nMock);
+		$missing = $analyser->analyze();
+
+		$this->assertTrue(is_array($missing));
+		$this->assertEquals($expectedMissing, $missing);
+	}
+
+	function providesOC() {
+		return array(
+			// no version -> no missing dependency
+			array(array(), null),
+			array(array('ownCloud 9 or higher is required.'), array('@attributes' => array('min-version' => '9'))),
+			array(array('ownCloud with a version lower than 5.1.2 is required.'), array('@attributes' => array('max-version' => '5.1.2'))),
+		);
+	}
+
 	function providesOS() {
 		return array(
 			array(array(), null),
-- 
GitLab