BackendTest.php 2.8 KB

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