storagesservicetest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * @author Vincent Petry <pvince81@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 OCA\Files_external\Tests\Service;
  22. use \OC\Files\Filesystem;
  23. use \OCA\Files_external\NotFoundException;
  24. use \OCA\Files_external\Lib\StorageConfig;
  25. abstract class StoragesServiceTest extends \Test\TestCase {
  26. /**
  27. * @var StoragesService
  28. */
  29. protected $service;
  30. /**
  31. * Data directory
  32. *
  33. * @var string
  34. */
  35. protected $dataDir;
  36. /**
  37. * Hook calls
  38. *
  39. * @var array
  40. */
  41. protected static $hookCalls;
  42. public function setUp() {
  43. self::$hookCalls = array();
  44. $config = \OC::$server->getConfig();
  45. $this->dataDir = $config->getSystemValue(
  46. 'datadirectory',
  47. \OC::$SERVERROOT . '/data/'
  48. );
  49. \OC_Mount_Config::$skipTest = true;
  50. \OCP\Util::connectHook(
  51. Filesystem::CLASSNAME,
  52. Filesystem::signal_create_mount,
  53. get_class($this), 'createHookCallback');
  54. \OCP\Util::connectHook(
  55. Filesystem::CLASSNAME,
  56. Filesystem::signal_delete_mount,
  57. get_class($this), 'deleteHookCallback');
  58. }
  59. public function tearDown() {
  60. \OC_Mount_Config::$skipTest = false;
  61. self::$hookCalls = array();
  62. }
  63. /**
  64. * Creates a StorageConfig instance based on array data
  65. *
  66. * @param array data
  67. *
  68. * @return StorageConfig storage config instance
  69. */
  70. protected function makeStorageConfig($data) {
  71. $storage = new StorageConfig();
  72. if (isset($data['id'])) {
  73. $storage->setId($data['id']);
  74. }
  75. $storage->setMountPoint($data['mountPoint']);
  76. $storage->setBackendClass($data['backendClass']);
  77. $storage->setBackendOptions($data['backendOptions']);
  78. if (isset($data['applicableUsers'])) {
  79. $storage->setApplicableUsers($data['applicableUsers']);
  80. }
  81. if (isset($data['applicableGroups'])) {
  82. $storage->setApplicableGroups($data['applicableGroups']);
  83. }
  84. if (isset($data['priority'])) {
  85. $storage->setPriority($data['priority']);
  86. }
  87. if (isset($data['mountOptions'])) {
  88. $storage->setMountOptions($data['mountOptions']);
  89. }
  90. return $storage;
  91. }
  92. /**
  93. * @expectedException \OCA\Files_external\NotFoundException
  94. */
  95. public function testNonExistingStorage() {
  96. $storage = new StorageConfig(255);
  97. $storage->setMountPoint('mountpoint');
  98. $storage->setBackendClass('\OC\Files\Storage\SMB');
  99. $this->service->updateStorage($storage);
  100. }
  101. public function testDeleteStorage() {
  102. $storage = new StorageConfig(255);
  103. $storage->setMountPoint('mountpoint');
  104. $storage->setBackendClass('\OC\Files\Storage\SMB');
  105. $storage->setBackendOptions(['password' => 'testPassword']);
  106. $newStorage = $this->service->addStorage($storage);
  107. $this->assertEquals(1, $newStorage->getId());
  108. $newStorage = $this->service->removeStorage(1);
  109. $caught = false;
  110. try {
  111. $this->service->getStorage(1);
  112. } catch (NotFoundException $e) {
  113. $caught = true;
  114. }
  115. $this->assertTrue($caught);
  116. }
  117. /**
  118. * @expectedException \OCA\Files_external\NotFoundException
  119. */
  120. public function testDeleteUnexistingStorage() {
  121. $this->service->removeStorage(255);
  122. }
  123. public static function createHookCallback($params) {
  124. self::$hookCalls[] = array(
  125. 'signal' => Filesystem::signal_create_mount,
  126. 'params' => $params
  127. );
  128. }
  129. public static function deleteHookCallback($params) {
  130. self::$hookCalls[] = array(
  131. 'signal' => Filesystem::signal_delete_mount,
  132. 'params' => $params
  133. );
  134. }
  135. /**
  136. * Asserts hook call
  137. *
  138. * @param array $callData hook call data to check
  139. * @param string $signal signal name
  140. * @param string $mountPath mount path
  141. * @param string $mountType mount type
  142. * @param string $applicable applicable users
  143. */
  144. protected function assertHookCall($callData, $signal, $mountPath, $mountType, $applicable) {
  145. $this->assertEquals($signal, $callData['signal']);
  146. $params = $callData['params'];
  147. $this->assertEquals(
  148. $mountPath,
  149. $params[Filesystem::signal_param_path]
  150. );
  151. $this->assertEquals(
  152. $mountType,
  153. $params[Filesystem::signal_param_mount_type]
  154. );
  155. $this->assertEquals(
  156. $applicable,
  157. $params[Filesystem::signal_param_users]
  158. );
  159. }
  160. }