EncryptionControllerTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Tests\Settings\Controller;
  22. use OC\DB\Connection;
  23. use OC\Files\View;
  24. use OC\Settings\Controller\EncryptionController;
  25. use OCP\IConfig;
  26. use OCP\IL10N;
  27. use OCP\ILogger;
  28. use OCP\IRequest;
  29. use OCP\IUserManager;
  30. use Test\TestCase;
  31. /**
  32. * Class EncryptionControllerTest
  33. *
  34. * @package Tests\Settings\Controller
  35. */
  36. class EncryptionControllerTest extends TestCase {
  37. /** @var IRequest */
  38. private $request;
  39. /** @var IL10N */
  40. private $l10n;
  41. /** @var IConfig */
  42. private $config;
  43. /** @var Connection */
  44. private $connection;
  45. /** @var IUserManager */
  46. private $userManager;
  47. /** @var View */
  48. private $view;
  49. /** @var ILogger */
  50. private $logger;
  51. /** @var EncryptionController */
  52. private $encryptionController;
  53. public function setUp() {
  54. parent::setUp();
  55. $this->request = $this->getMockBuilder('\\OCP\\IRequest')
  56. ->disableOriginalConstructor()->getMock();
  57. $this->l10n = $this->getMockBuilder('\\OCP\\IL10N')
  58. ->disableOriginalConstructor()->getMock();
  59. $this->l10n->expects($this->any())
  60. ->method('t')
  61. ->will($this->returnCallback(function($message, array $replace) {
  62. return vsprintf($message, $replace);
  63. }));
  64. $this->config = $this->getMockBuilder('\\OCP\\IConfig')
  65. ->disableOriginalConstructor()->getMock();
  66. $this->connection = $this->getMockBuilder('\\OC\\DB\\Connection')
  67. ->disableOriginalConstructor()->getMock();
  68. $this->userManager = $this->getMockBuilder('\\OCP\\IUserManager')
  69. ->disableOriginalConstructor()->getMock();
  70. $this->view = $this->getMockBuilder('\\OC\\Files\\View')
  71. ->disableOriginalConstructor()->getMock();
  72. $this->logger = $this->getMockBuilder('\\OCP\\ILogger')
  73. ->disableOriginalConstructor()->getMock();
  74. $this->encryptionController = $this->getMockBuilder('\\OC\\Settings\\Controller\\EncryptionController')
  75. ->setConstructorArgs([
  76. 'settings',
  77. $this->request,
  78. $this->l10n,
  79. $this->config,
  80. $this->connection,
  81. $this->userManager,
  82. $this->view,
  83. $this->logger,
  84. ])
  85. ->setMethods(['getMigration'])
  86. ->getMock();
  87. }
  88. public function testStartMigrationSuccessful() {
  89. // we need to be able to autoload the class we're mocking
  90. \OC_App::registerAutoloading('encryption', \OC_App::getAppPath('encryption'));
  91. $migration = $this->getMockBuilder('\\OCA\\Encryption\\Migration')
  92. ->disableOriginalConstructor()->getMock();
  93. $this->encryptionController
  94. ->expects($this->once())
  95. ->method('getMigration')
  96. ->with($this->config, $this->view, $this->connection, $this->logger)
  97. ->will($this->returnValue($migration));
  98. $migration
  99. ->expects($this->once())
  100. ->method('reorganizeSystemFolderStructure');
  101. $migration
  102. ->expects($this->once())
  103. ->method('updateDB');
  104. $backend = $this->getMockBuilder('\OCP\UserInterface')
  105. ->getMock();
  106. $this->userManager
  107. ->expects($this->once())
  108. ->method('getBackends')
  109. ->will($this->returnValue([$backend]));
  110. $backend
  111. ->expects($this->once())
  112. ->method('getUsers')
  113. ->will($this->returnValue(['User 1', 'User 2']));
  114. $migration
  115. ->expects($this->exactly(2))
  116. ->method('reorganizeFolderStructureForUser')
  117. ->withConsecutive(
  118. ['User 1'],
  119. ['User 2']
  120. );
  121. $migration
  122. ->expects($this->once())
  123. ->method('finalCleanUp');
  124. $expected = [
  125. 'data' => [
  126. 'message' => 'Migration Completed',
  127. ],
  128. 'status' => 'success',
  129. ];
  130. $this->assertSame($expected, $this->encryptionController->startMigration());
  131. }
  132. public function testStartMigrationException() {
  133. $this->encryptionController
  134. ->expects($this->once())
  135. ->method('getMigration')
  136. ->with($this->config, $this->view, $this->connection, $this->logger)
  137. ->will($this->throwException(new \Exception('My error message')));
  138. $expected = [
  139. 'data' => [
  140. 'message' => 'A problem occurred, please check your log files (Error: My error message)',
  141. ],
  142. 'status' => 'error',
  143. ];
  144. $this->assertSame($expected, $this->encryptionController->startMigration());
  145. }
  146. }