diff --git a/apps/encryption/lib/Hooks/UserHooks.php b/apps/encryption/lib/Hooks/UserHooks.php
index 488158929005a91825edb4c5437f83e353dacc18..bf3f3f42b47b0aef1e86ecf6418225b9a8318e08 100644
--- a/apps/encryption/lib/Hooks/UserHooks.php
+++ b/apps/encryption/lib/Hooks/UserHooks.php
@@ -68,7 +68,7 @@ class UserHooks implements IHook {
 	/**
 	 * @var IUserSession
 	 */
-	private $user;
+	private $userSession;
 	/**
 	 * @var Util
 	 */
@@ -93,7 +93,7 @@ class UserHooks implements IHook {
 	 * @param IUserManager $userManager
 	 * @param ILogger $logger
 	 * @param Setup $userSetup
-	 * @param IUserSession $user
+	 * @param IUserSession $userSession
 	 * @param Util $util
 	 * @param Session $session
 	 * @param Crypt $crypt
@@ -103,7 +103,7 @@ class UserHooks implements IHook {
 								IUserManager $userManager,
 								ILogger $logger,
 								Setup $userSetup,
-								IUserSession $user,
+								IUserSession $userSession,
 								Util $util,
 								Session $session,
 								Crypt $crypt,
@@ -113,7 +113,7 @@ class UserHooks implements IHook {
 		$this->userManager = $userManager;
 		$this->logger = $logger;
 		$this->userSetup = $userSetup;
-		$this->user = $user;
+		$this->userSession = $userSession;
 		$this->util = $util;
 		$this->session = $session;
 		$this->recovery = $recovery;
@@ -253,7 +253,7 @@ class UserHooks implements IHook {
 		}
 
 		// Get existing decrypted private key
-		$user = $this->user->getUser();
+		$user = $this->userSession->getUser();
 
 		// current logged in user changes his own password
 		if ($user && $params['uid'] === $user->getUID()) {
@@ -265,7 +265,7 @@ class UserHooks implements IHook {
 
 			// Save private key
 			if ($encryptedPrivateKey) {
-				$this->keyManager->setPrivateKey($this->user->getUser()->getUID(),
+				$this->keyManager->setPrivateKey($user->getUID(),
 					$this->crypt->generateHeader() . $encryptedPrivateKey);
 			} else {
 				$this->logger->error('Encryption could not update users encryption password');
@@ -275,8 +275,8 @@ class UserHooks implements IHook {
 			// private key has not changed, only the passphrase
 			// used to decrypt it has changed
 		} else { // admin changed the password for a different user, create new keys and re-encrypt file keys
-			$user = $params['uid'];
-			$this->initMountPoints($user);
+			$userId = $params['uid'];
+			$this->initMountPoints($userId);
 			$recoveryPassword = isset($params['recoveryPassword']) ? $params['recoveryPassword'] : null;
 
 			$recoveryKeyId = $this->keyManager->getRecoveryKeyId();
@@ -296,9 +296,9 @@ class UserHooks implements IHook {
 			// ...encryption was activated for the first time (no keys exists)
 			// ...the user doesn't have any files
 			if (
-				($this->recovery->isRecoveryEnabledForUser($user) && $recoveryPassword)
-				|| !$this->keyManager->userHasKeys($user)
-				|| !$this->util->userHasFiles($user)
+				($this->recovery->isRecoveryEnabledForUser($userId) && $recoveryPassword)
+				|| !$this->keyManager->userHasKeys($userId)
+				|| !$this->util->userHasFiles($userId)
 			) {
 
 				// backup old keys
@@ -309,16 +309,16 @@ class UserHooks implements IHook {
 				$keyPair = $this->crypt->createKeyPair();
 
 				// Save public key
-				$this->keyManager->setPublicKey($user, $keyPair['publicKey']);
+				$this->keyManager->setPublicKey($userId, $keyPair['publicKey']);
 
 				// Encrypt private key with new password
-				$encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $newUserPassword, $user);
+				$encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $newUserPassword, $userId);
 
 				if ($encryptedKey) {
-					$this->keyManager->setPrivateKey($user, $this->crypt->generateHeader() . $encryptedKey);
+					$this->keyManager->setPrivateKey($userId, $this->crypt->generateHeader() . $encryptedKey);
 
 					if ($recoveryPassword) { // if recovery key is set we can re-encrypt the key files
-						$this->recovery->recoverUsersFiles($recoveryPassword, $user);
+						$this->recovery->recoverUsersFiles($recoveryPassword, $userId);
 					}
 				} else {
 					$this->logger->error('Encryption Could not update users encryption password');
diff --git a/apps/encryption/lib/Recovery.php b/apps/encryption/lib/Recovery.php
index 17533e7b11428618a04f441fa31415cece0f7670..f9cdef53f130984fa416689dc957f1acfacf4335 100644
--- a/apps/encryption/lib/Recovery.php
+++ b/apps/encryption/lib/Recovery.php
@@ -46,10 +46,6 @@ class Recovery {
 	 * @var Crypt
 	 */
 	protected $crypt;
-	/**
-	 * @var ISecureRandom
-	 */
-	private $random;
 	/**
 	 * @var KeyManager
 	 */
@@ -58,10 +54,6 @@ class Recovery {
 	 * @var IConfig
 	 */
 	private $config;
-	/**
-	 * @var IStorage
-	 */
-	private $keyStorage;
 	/**
 	 * @var View
 	 */
@@ -72,29 +64,23 @@ class Recovery {
 	private $file;
 
 	/**
-	 * @param IUserSession $user
+	 * @param IUserSession $userSession
 	 * @param Crypt $crypt
-	 * @param ISecureRandom $random
 	 * @param KeyManager $keyManager
 	 * @param IConfig $config
-	 * @param IStorage $keyStorage
 	 * @param IFile $file
 	 * @param View $view
 	 */
-	public function __construct(IUserSession $user,
+	public function __construct(IUserSession $userSession,
 								Crypt $crypt,
-								ISecureRandom $random,
 								KeyManager $keyManager,
 								IConfig $config,
-								IStorage $keyStorage,
 								IFile $file,
 								View $view) {
-		$this->user = ($user && $user->isLoggedIn()) ? $user->getUser() : false;
+		$this->user = ($userSession && $userSession->isLoggedIn()) ? $userSession->getUser() : false;
 		$this->crypt = $crypt;
-		$this->random = $random;
 		$this->keyManager = $keyManager;
 		$this->config = $config;
-		$this->keyStorage = $keyStorage;
 		$this->view = $view;
 		$this->file = $file;
 	}
@@ -169,7 +155,7 @@ class Recovery {
 	 * @return bool
 	 */
 	public function isRecoveryEnabledForUser($user = '') {
-		$uid = empty($user) ? $this->user->getUID() : $user;
+		$uid = $user === '' ? $this->user->getUID() : $user;
 		$recoveryMode = $this->config->getUserValue($uid,
 			'encryption',
 			'recoveryEnabled',
diff --git a/apps/encryption/tests/Controller/SettingsControllerTest.php b/apps/encryption/tests/Controller/SettingsControllerTest.php
index b12652b51c918a597e6861cc6f5d971ba7f73f15..b50f7cd0b612e93f3ab5ce1e32a27bdd9a8a89ad 100644
--- a/apps/encryption/tests/Controller/SettingsControllerTest.php
+++ b/apps/encryption/tests/Controller/SettingsControllerTest.php
@@ -34,8 +34,10 @@ use OCP\AppFramework\Http;
 use OCP\IL10N;
 use OCP\IRequest;
 use OCP\ISession;
+use OCP\IUser;
 use OCP\IUserManager;
 use OCP\IUserSession;
+use PHPUnit\Framework\MockObject\MockObject;
 use Test\TestCase;
 
 class SettingsControllerTest extends TestCase {
@@ -63,6 +65,8 @@ class SettingsControllerTest extends TestCase {
 
 	/** @var \OCA\Encryption\Session|\PHPUnit_Framework_MockObject_MockObject */
 	private $sessionMock;
+	/** @var MockObject|IUser */
+	private $user;
 
 	/** @var \OCP\ISession|\PHPUnit_Framework_MockObject_MockObject */
 	private $ocSessionMock;
@@ -94,28 +98,17 @@ class SettingsControllerTest extends TestCase {
 		$this->cryptMock = $this->getMockBuilder(Crypt::class)
 			->disableOriginalConstructor()->getMock();
 
-		$this->userSessionMock = $this->getMockBuilder(IUserSession::class)
-			->disableOriginalConstructor()
-			->setMethods([
-				'isLoggedIn',
-				'getUID',
-				'login',
-				'logout',
-				'setUser',
-				'getUser',
-				'canChangePassword',
-			])
-			->getMock();
-
 		$this->ocSessionMock = $this->getMockBuilder(ISession::class)->disableOriginalConstructor()->getMock();
 
-		$this->userSessionMock->expects($this->any())
+		$this->user = $this->createMock(IUser::class);
+		$this->user->expects($this->any())
 			->method('getUID')
 			->willReturn('testUserUid');
 
+		$this->userSessionMock = $this->createMock(IUserSession::class);
 		$this->userSessionMock->expects($this->any())
-			->method($this->anything())
-			->will($this->returnSelf());
+			->method('getUser')
+			->willReturn($this->user);
 
 		$this->sessionMock = $this->getMockBuilder(Session::class)
 			->disableOriginalConstructor()->getMock();
@@ -146,7 +139,9 @@ class SettingsControllerTest extends TestCase {
 		$oldPassword = 'old';
 		$newPassword = 'new';
 
-		$this->userSessionMock->expects($this->once())->method('getUID')->willReturn('uid');
+		$this->user->expects($this->any())
+			->method('getUID')
+			->willReturn('uid');
 
 		$this->userManagerMock
 			->expects($this->exactly(2))
diff --git a/apps/encryption/tests/Hooks/UserHooksTest.php b/apps/encryption/tests/Hooks/UserHooksTest.php
index b14a1f6a559e86b903c3adfa2c1c3b008a607693..2295b90625dd8f4b714c57c133658d4b980d9aa1 100644
--- a/apps/encryption/tests/Hooks/UserHooksTest.php
+++ b/apps/encryption/tests/Hooks/UserHooksTest.php
@@ -41,6 +41,7 @@ use OCP\ILogger;
 use OCP\IUser;
 use OCP\IUserManager;
 use OCP\IUserSession;
+use PHPUnit\Framework\MockObject\MockObject;
 use Test\TestCase;
 
 /**
@@ -79,6 +80,10 @@ class UserHooksTest extends TestCase {
 	 * @var \PHPUnit_Framework_MockObject_MockObject
 	 */
 	private $userSessionMock;
+	/**
+	 * @var MockObject|IUser
+	 */
+	private $user;
 	/**
 	 * @var \PHPUnit_Framework_MockObject_MockObject
 	 */
@@ -343,24 +348,15 @@ class UserHooksTest extends TestCase {
 			->disableOriginalConstructor()
 			->getMock();
 
-		$this->userSessionMock = $this->getMockBuilder(IUserSession::class)
-			->disableOriginalConstructor()
-			->setMethods([
-				'isLoggedIn',
-				'getUID',
-				'login',
-				'logout',
-				'setUser',
-				'getUser',
-				'canChangePassword'
-			])
-			->getMock();
-
-		$this->userSessionMock->expects($this->any())->method('getUID')->will($this->returnValue('testUser'));
+		$this->user = $this->createMock(IUser::class);
+		$this->user->expects($this->any())
+			->method('getUID')
+			->willReturn('testUser');
 
+		$this->userSessionMock = $this->createMock(IUserSession::class);
 		$this->userSessionMock->expects($this->any())
-			->method($this->anything())
-			->will($this->returnSelf());
+			->method('getUser')
+			->willReturn($this->user);
 
 		$utilMock = $this->getMockBuilder(Util::class)
 			->disableOriginalConstructor()
diff --git a/apps/encryption/tests/RecoveryTest.php b/apps/encryption/tests/RecoveryTest.php
index 0eb9a777ec3980186ea925597abedaff0e3b2b55..9b737cdb82f5109317e6db5bff49fb47cf5f74bc 100644
--- a/apps/encryption/tests/RecoveryTest.php
+++ b/apps/encryption/tests/RecoveryTest.php
@@ -36,8 +36,10 @@ use OCA\Encryption\Recovery;
 use OCP\Encryption\IFile;
 use OCP\Encryption\Keys\IStorage;
 use OCP\IConfig;
+use OCP\IUser;
 use OCP\IUserSession;
 use OCP\Security\ISecureRandom;
+use PHPUnit\Framework\MockObject\MockObject;
 use Test\TestCase;
 
 class RecoveryTest extends TestCase {
@@ -54,6 +56,10 @@ class RecoveryTest extends TestCase {
 	 * @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject
 	 */
 	private $userSessionMock;
+	/**
+	 * @var MockObject|IUser
+	 */
+	private $user;
 	/**
 	 * @var \OCA\Encryption\KeyManager|\PHPUnit_Framework_MockObject_MockObject
 	 */
@@ -257,32 +263,22 @@ class RecoveryTest extends TestCase {
 	protected function setUp() {
 		parent::setUp();
 
+		$this->user = $this->createMock(IUser::class);
+		$this->user->expects($this->any())
+			->method('getUID')
+			->willReturn('admin');
 
-		$this->userSessionMock = $this->getMockBuilder(IUserSession::class)
-			->disableOriginalConstructor()
-			->setMethods([
-				'isLoggedIn',
-				'getUID',
-				'login',
-				'logout',
-				'setUser',
-				'getUser'
-			])
-			->getMock();
-
-		$this->userSessionMock->expects($this->any())->method('getUID')->will($this->returnValue('admin'));
-
+		$this->userSessionMock = $this->createMock(IUserSession::class);
+		$this->userSessionMock->expects($this->any())
+			->method('getUser')
+			->willReturn($this->user);
 		$this->userSessionMock->expects($this->any())
-			->method($this->anything())
-			->will($this->returnSelf());
+			->method('isLoggedIn')
+			->willReturn(true);
 
 		$this->cryptMock = $this->getMockBuilder(Crypt::class)->disableOriginalConstructor()->getMock();
-		/** @var \OCP\Security\ISecureRandom $randomMock */
-		$randomMock = $this->createMock(ISecureRandom::class);
 		$this->keyManagerMock = $this->getMockBuilder(KeyManager::class)->disableOriginalConstructor()->getMock();
 		$this->configMock = $this->createMock(IConfig::class);
-		/** @var \OCP\Encryption\Keys\IStorage $keyStorageMock */
-		$keyStorageMock = $this->createMock(IStorage::class);
 		$this->fileMock = $this->createMock(IFile::class);
 		$this->viewMock = $this->createMock(View::class);
 
@@ -296,10 +292,8 @@ class RecoveryTest extends TestCase {
 
 		$this->instance = new Recovery($this->userSessionMock,
 			$this->cryptMock,
-			$randomMock,
 			$this->keyManagerMock,
 			$this->configMock,
-			$keyStorageMock,
 			$this->fileMock,
 			$this->viewMock);
 	}
diff --git a/apps/encryption/tests/UtilTest.php b/apps/encryption/tests/UtilTest.php
index 1777d1f8f1f1012f718fb72111ffae3ba24ab22f..17721d81eafba1e33530d9d87649e54b50aa359b 100644
--- a/apps/encryption/tests/UtilTest.php
+++ b/apps/encryption/tests/UtilTest.php
@@ -36,8 +36,10 @@ use OCP\Files\Mount\IMountPoint;
 use OCP\Files\Storage;
 use OCP\IConfig;
 use OCP\ILogger;
+use OCP\IUser;
 use OCP\IUserManager;
 use OCP\IUserSession;
+use PHPUnit\Framework\MockObject\MockObject;
 use Test\TestCase;
 
 class UtilTest extends TestCase {
@@ -91,27 +93,20 @@ class UtilTest extends TestCase {
 			->getMock();
 		/** @var \OCP\ILogger $loggerMock */
 		$loggerMock = $this->createMock(ILogger::class);
-		/** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject $userSessionMock */
-		$userSessionMock = $this->getMockBuilder(IUserSession::class)
-			->disableOriginalConstructor()
-			->setMethods([
-				'isLoggedIn',
-				'getUID',
-				'login',
-				'logout',
-				'setUser',
-				'getUser'
-			])
-			->getMock();
-
-		$userSessionMock->method('isLoggedIn')->will($this->returnValue(true));
 
-		$userSessionMock->method('getUID')->will($this->returnValue('admin'));
+		$user = $this->createMock(IUser::class);
+		$user->expects($this->any())
+			->method('getUID')
+			->willReturn('admin');
 
+		/** @var IUserSession|MockObject $userSessionMock */
+		$userSessionMock = $this->createMock(IUserSession::class);
 		$userSessionMock->expects($this->any())
-			->method($this->anything())
-			->will($this->returnSelf());
-
+			->method('getUser')
+			->willReturn($user);
+		$userSessionMock->expects($this->any())
+			->method('isLoggedIn')
+			->willReturn(true);
 
 		$this->configMock = $this->createMock(IConfig::class);