BackendTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Tests\Backend;
  8. use OCA\Files_External\Lib\Backend\Backend;
  9. use OCA\Files_External\Lib\StorageConfig;
  10. class BackendTest extends \Test\TestCase {
  11. public function testJsonSerialization() {
  12. $backend = $this->getMockBuilder(Backend::class)
  13. ->setMethods(['jsonSerializeDefinition'])
  14. ->getMock();
  15. $backend->expects($this->once())
  16. ->method('jsonSerializeDefinition')
  17. ->willReturn(['foo' => 'bar', 'name' => 'abc']);
  18. $backend->setPriority(57);
  19. $backend->addAuthScheme('foopass');
  20. $backend->addAuthScheme('barauth');
  21. $json = $backend->jsonSerialize();
  22. $this->assertEquals('bar', $json['foo']);
  23. $this->assertEquals('abc', $json['name']);
  24. $this->assertEquals($json['name'], $json['backend']);
  25. $this->assertEquals(57, $json['priority']);
  26. $this->assertContains('foopass', array_keys($json['authSchemes']));
  27. $this->assertContains('barauth', array_keys($json['authSchemes']));
  28. }
  29. public function validateStorageProvider() {
  30. return [
  31. [true, true],
  32. [false, false],
  33. ];
  34. }
  35. /**
  36. * @dataProvider validateStorageProvider
  37. */
  38. public function testValidateStorage($expectedSuccess, $definitionSuccess) {
  39. $backend = $this->getMockBuilder(Backend::class)
  40. ->setMethods(['validateStorageDefinition'])
  41. ->getMock();
  42. $backend->expects($this->atMost(1))
  43. ->method('validateStorageDefinition')
  44. ->willReturn($definitionSuccess);
  45. $storageConfig = $this->getMockBuilder(StorageConfig::class)
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->assertEquals($expectedSuccess, $backend->validateStorage($storageConfig));
  49. }
  50. public function testLegacyAuthMechanismCallback() {
  51. $backend = new Backend();
  52. $backend->setLegacyAuthMechanismCallback(function (array $params) {
  53. if (isset($params['ping'])) {
  54. return 'pong';
  55. }
  56. return 'foobar';
  57. });
  58. $this->assertEquals('pong', $backend->getLegacyAuthMechanism(['ping' => true]));
  59. $this->assertEquals('foobar', $backend->getLegacyAuthMechanism(['other' => true]));
  60. $this->assertEquals('foobar', $backend->getLegacyAuthMechanism());
  61. }
  62. }