backendservicetest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * @author Robin McCorkell <robin@mccorkell.me.uk>
  4. * @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>
  5. *
  6. * @copyright Copyright (c) 2016, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\Files_External\Tests\Service;
  23. use \OCA\Files_External\Service\BackendService;
  24. class BackendServiceTest extends \Test\TestCase {
  25. /** @var \OCP\IConfig */
  26. protected $config;
  27. /** @var \OCP\IL10N */
  28. protected $l10n;
  29. protected function setUp() {
  30. $this->config = $this->getMock('\OCP\IConfig');
  31. $this->l10n = $this->getMock('\OCP\IL10N');
  32. }
  33. /**
  34. * @param string $class
  35. *
  36. * @return \OCA\Files_External\Lib\Backend\Backend
  37. */
  38. protected function getBackendMock($class) {
  39. $backend = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend')
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. $backend->method('getIdentifier')->will($this->returnValue('identifier:'.$class));
  43. $backend->method('getIdentifierAliases')->will($this->returnValue(['identifier:'.$class]));
  44. return $backend;
  45. }
  46. public function testRegisterBackend() {
  47. $service = new BackendService($this->config, $this->l10n);
  48. $backend = $this->getBackendMock('\Foo\Bar');
  49. $backendAlias = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend')
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $backendAlias->method('getIdentifierAliases')
  53. ->willReturn(['identifier_real', 'identifier_alias']);
  54. $backendAlias->method('getIdentifier')
  55. ->willReturn('identifier_real');
  56. $service->registerBackend($backend);
  57. $service->registerBackend($backendAlias);
  58. $this->assertEquals($backend, $service->getBackend('identifier:\Foo\Bar'));
  59. $this->assertEquals($backendAlias, $service->getBackend('identifier_real'));
  60. $this->assertEquals($backendAlias, $service->getBackend('identifier_alias'));
  61. $backends = $service->getBackends();
  62. $this->assertCount(2, $backends);
  63. $this->assertArrayHasKey('identifier:\Foo\Bar', $backends);
  64. $this->assertArrayHasKey('identifier_real', $backends);
  65. $this->assertArrayNotHasKey('identifier_alias', $backends);
  66. }
  67. public function testUserMountingBackends() {
  68. $this->config->expects($this->exactly(2))
  69. ->method('getAppValue')
  70. ->will($this->returnValueMap([
  71. ['files_external', 'allow_user_mounting', 'yes', 'yes'],
  72. ['files_external', 'user_mounting_backends', '', 'identifier:\User\Mount\Allowed,identifier_alias']
  73. ]));
  74. $service = new BackendService($this->config, $this->l10n);
  75. $backendAllowed = $this->getBackendMock('\User\Mount\Allowed');
  76. $backendAllowed->expects($this->never())
  77. ->method('removeVisibility');
  78. $backendNotAllowed = $this->getBackendMock('\User\Mount\NotAllowed');
  79. $backendNotAllowed->expects($this->once())
  80. ->method('removeVisibility')
  81. ->with(BackendService::VISIBILITY_PERSONAL);
  82. $backendAlias = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend')
  83. ->disableOriginalConstructor()
  84. ->getMock();
  85. $backendAlias->method('getIdentifierAliases')
  86. ->willReturn(['identifier_real', 'identifier_alias']);
  87. $backendAlias->expects($this->never())
  88. ->method('removeVisibility');
  89. $service->registerBackend($backendAllowed);
  90. $service->registerBackend($backendNotAllowed);
  91. $service->registerBackend($backendAlias);
  92. }
  93. public function testGetAvailableBackends() {
  94. $service = new BackendService($this->config, $this->l10n);
  95. $backendAvailable = $this->getBackendMock('\Backend\Available');
  96. $backendAvailable->expects($this->once())
  97. ->method('checkDependencies')
  98. ->will($this->returnValue([]));
  99. $backendNotAvailable = $this->getBackendMock('\Backend\NotAvailable');
  100. $backendNotAvailable->expects($this->once())
  101. ->method('checkDependencies')
  102. ->will($this->returnValue([
  103. $this->getMockBuilder('\OCA\Files_External\Lib\MissingDependency')
  104. ->disableOriginalConstructor()
  105. ->getMock()
  106. ]));
  107. $service->registerBackend($backendAvailable);
  108. $service->registerBackend($backendNotAvailable);
  109. $availableBackends = $service->getAvailableBackends();
  110. $this->assertArrayHasKey('identifier:\Backend\Available', $availableBackends);
  111. $this->assertArrayNotHasKey('identifier:\Backend\NotAvailable', $availableBackends);
  112. }
  113. }