From 6585eaa5df7a4481638a334926743d496f243d65 Mon Sep 17 00:00:00 2001
From: Vincent Petry <pvince81@owncloud.com>
Date: Fri, 26 Sep 2014 12:51:25 +0200
Subject: [PATCH] Added failing unit tests for mount config hooks

---
 apps/files_external/lib/config.php        |  16 +--
 apps/files_external/tests/mountconfig.php | 134 ++++++++++++++++++++++
 lib/private/files/filesystem.php          |   5 +
 3 files changed, 147 insertions(+), 8 deletions(-)

diff --git a/apps/files_external/lib/config.php b/apps/files_external/lib/config.php
index 2a32d239b4..0b5e833a70 100755
--- a/apps/files_external/lib/config.php
+++ b/apps/files_external/lib/config.php
@@ -518,11 +518,11 @@ class OC_Mount_Config {
 		if ($result && $isNew) {
 			\OC_Hook::emit(
 				\OC\Files\Filesystem::CLASSNAME,
-				'add_mount_point',
+				\OC\Files\Filesystem::signal_create_mount,
 				array(
-					'path' => $mountPoint,
-					'type' => $mountType,
-					'applicable' => $applicable
+					\OC\Files\Filesystem::signal_param_path => $mountPoint,
+					\OC\Files\Filesystem::signal_param_mount_type => $mountType,
+					\OC\Files\Filesystem::signal_param_users => $applicable,
 				)
 			);
 		}
@@ -561,11 +561,11 @@ class OC_Mount_Config {
 		self::writeData($isPersonal ? OCP\User::getUser() : NULL, $mountPoints);
 		\OC_Hook::emit(
 			\OC\Files\Filesystem::CLASSNAME,
-			'remove_mount_point',
+			\OC\Files\Filesystem::signal_delete_mount,
 			array(
-				'path' => $mountPoint,
-				'type' => $mountType,
-				'applicable' => $applicable
+				\OC\Files\Filesystem::signal_param_path => $mountPoint,
+				\OC\Files\Filesystem::signal_param_mount_type => $mountType,
+				\OC\Files\Filesystem::signal_param_users => $applicable,
 			)
 		);
 		return true;
diff --git a/apps/files_external/tests/mountconfig.php b/apps/files_external/tests/mountconfig.php
index fc48282384..0fa1e4e46e 100644
--- a/apps/files_external/tests/mountconfig.php
+++ b/apps/files_external/tests/mountconfig.php
@@ -26,6 +26,42 @@ class Test_Mount_Config_Dummy_Storage {
 	}
 }
 
+class Test_Mount_Config_Hook_Test {
+	static $signal;
+	static $params;
+
+	public static function setUpHooks() {
+		self::clear();
+		\OCP\Util::connectHook(
+			\OC\Files\Filesystem::CLASSNAME,
+			\OC\Files\Filesystem::signal_create_mount,
+			'\Test_Mount_Config_Hook_Test', 'createHookCallback');
+		\OCP\Util::connectHook(
+			\OC\Files\Filesystem::CLASSNAME,
+			\OC\Files\Filesystem::signal_delete_mount,
+			'\Test_Mount_Config_Hook_Test', 'deleteHookCallback');
+	}
+
+	public static function clear() {
+		self::$signal = null;
+		self::$params = null;
+	}
+
+	public static function createHookCallback($params) {
+		self::$signal = \OC\Files\Filesystem::signal_create_mount;
+		self::$params = $params;
+	}
+
+	public static function deleteHookCallback($params) {
+		self::$signal = \OC\Files\Filesystem::signal_create_mount;
+		self::$params = $params;
+	}
+
+	public static function getLastCall() {
+		return array(self::$signal, self::$params);
+	}
+}
+
 /**
  * Class Test_Mount_Config
  */
@@ -77,9 +113,11 @@ class Test_Mount_Config extends \PHPUnit_Framework_TestCase {
 		);
 
 		OC_Mount_Config::$skipTest = true;
+		Test_Mount_Config_Hook_Test::setupHooks();
 	}
 
 	public function tearDown() {
+		Test_Mount_Config_Hook_Test::clear();
 		OC_Mount_Config::$skipTest = false;
 
 		\OC_User::deleteUser(self::TEST_USER2);
@@ -337,6 +375,102 @@ class Test_Mount_Config extends \PHPUnit_Framework_TestCase {
 		$this->assertEquals(array_keys($options), array_keys($savedOptions));
 	}
 
+	public function testHooks() {
+		$mountPoint = '/test';
+		$mountType = 'user';
+		$applicable = 'all';
+		$isPersonal = false;
+
+		$mountConfig = array(
+			'host' => 'smbhost',
+			'user' => 'smbuser',
+			'password' => 'smbpassword',
+			'share' => 'smbshare',
+			'root' => 'smbroot'
+		);
+
+		// write config
+		$this->assertTrue(
+			OC_Mount_Config::addMountPoint(
+				$mountPoint,
+				'\OC\Files\Storage\SMB',
+				$mountConfig,
+				$mountType,
+				$applicable,
+				$isPersonal
+			)
+		);
+
+		list($hookName, $params) = Test_Mount_Config_Hook_Test::getLastCall();
+		$this->assertEquals(
+			\OC\Files\Filesystem::signal_create_mount,
+			$hookName
+		);
+		$this->assertEquals(
+			$mountPoint,
+			$params[\OC\Files\Filesystem::signal_param_path]
+		);
+		$this->assertEquals(
+			$mountType,
+			$params[\OC\Files\Filesystem::signal_param_mount_type]
+		);
+		$this->assertEquals(
+			$applicable,
+			$params[\OC\Files\Filesystem::signal_param_mount_users]
+		);
+
+		Test_Mount_Config_Hook_Test::clear();
+
+		// edit
+		$mountConfig['host'] = 'anothersmbhost';
+		$this->assertTrue(
+			OC_Mount_Config::addMountPoint(
+				$mountPoint,
+				'\OC\Files\Storage\SMB',
+				$mountConfig,
+				$mountType,
+				$applicable,
+				$isPersonal
+			)
+		);
+
+		// hook must not be called on edit
+		list($hookName, $params) = Test_Mount_Config_Hook_Test::getLastCall();
+		$this->assertEquals(
+			null,
+			$hookName
+		);
+
+		Test_Mount_Config_Hook_Test::clear();
+
+		$this->assertTrue(
+			OC_Mount_Config::removeMountPoint(
+				'/ext',
+				$mountType,
+				$applicable,
+				$isPersonal
+			)
+		);
+
+		list($hookName, $params) = Test_Mount_Config_Hook_Test::getLastCall();
+		$this->assertEquals(
+			\OC\Files\Filesystem::signal_delete_mount,
+			$hookName
+		);
+		$this->assertEquals(
+			$mountPoint,
+			$params[\OC\Files\Filesystem::signal_param_path]
+		);
+		$this->assertEquals(
+			$mountType,
+			$params[\OC\Files\Filesystem::signal_param_mount_type]
+		);
+		$this->assertEquals(
+			$applicable,
+			$params[\OC\Files\Filesystem::signal_param_mount_users]
+		);
+	}
+
 	/**
 	 * Test password obfuscation
 	 */
diff --git a/lib/private/files/filesystem.php b/lib/private/files/filesystem.php
index db46bcfd1e..cdbbbf3d3c 100644
--- a/lib/private/files/filesystem.php
+++ b/lib/private/files/filesystem.php
@@ -158,6 +158,11 @@ class Filesystem {
 	 */
 	const signal_param_run = 'run';
 
+	const signal_create_mount = 'create_mount';
+	const signal_delete_mount = 'delete_mount';
+	const signal_param_mount_type = 'mounttype';
+	const signal_param_users = 'users';
+
 	/**
 	 * @var \OC\Files\Storage\Loader $loader
 	 */
-- 
GitLab