BackendTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin McCorkell <robin@mccorkell.me.uk>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_External\Tests\Backend;
  26. use OCA\Files_External\Lib\Backend\Backend;
  27. use OCA\Files_External\Lib\StorageConfig;
  28. class BackendTest extends \Test\TestCase {
  29. public function testJsonSerialization() {
  30. $backend = $this->getMockBuilder(Backend::class)
  31. ->setMethods(['jsonSerializeDefinition'])
  32. ->getMock();
  33. $backend->expects($this->once())
  34. ->method('jsonSerializeDefinition')
  35. ->willReturn(['foo' => 'bar', 'name' => 'abc']);
  36. $backend->setPriority(57);
  37. $backend->addAuthScheme('foopass');
  38. $backend->addAuthScheme('barauth');
  39. $json = $backend->jsonSerialize();
  40. $this->assertEquals('bar', $json['foo']);
  41. $this->assertEquals('abc', $json['name']);
  42. $this->assertEquals($json['name'], $json['backend']);
  43. $this->assertEquals(57, $json['priority']);
  44. $this->assertContains('foopass', $json['authSchemes']);
  45. $this->assertContains('barauth', $json['authSchemes']);
  46. }
  47. public function validateStorageProvider() {
  48. return [
  49. [true, true],
  50. [false, false],
  51. ];
  52. }
  53. /**
  54. * @dataProvider validateStorageProvider
  55. */
  56. public function testValidateStorage($expectedSuccess, $definitionSuccess) {
  57. $backend = $this->getMockBuilder(Backend::class)
  58. ->setMethods(['validateStorageDefinition'])
  59. ->getMock();
  60. $backend->expects($this->atMost(1))
  61. ->method('validateStorageDefinition')
  62. ->willReturn($definitionSuccess);
  63. $storageConfig = $this->getMockBuilder(StorageConfig::class)
  64. ->disableOriginalConstructor()
  65. ->getMock();
  66. $this->assertEquals($expectedSuccess, $backend->validateStorage($storageConfig));
  67. }
  68. public function testLegacyAuthMechanismCallback() {
  69. $backend = new Backend();
  70. $backend->setLegacyAuthMechanismCallback(function (array $params) {
  71. if (isset($params['ping'])) {
  72. return 'pong';
  73. }
  74. return 'foobar';
  75. });
  76. $this->assertEquals('pong', $backend->getLegacyAuthMechanism(['ping' => true]));
  77. $this->assertEquals('foobar', $backend->getLegacyAuthMechanism(['other' => true]));
  78. $this->assertEquals('foobar', $backend->getLegacyAuthMechanism());
  79. }
  80. }