BackendTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin McCorkell <robin@mccorkell.me.uk>
  6. *
  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\Backend;
  23. use \OCA\Files_External\Lib\Backend\Backend;
  24. class BackendTest extends \Test\TestCase {
  25. public function testJsonSerialization() {
  26. $backend = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend')
  27. ->setMethods(['jsonSerializeDefinition'])
  28. ->getMock();
  29. $backend->expects($this->once())
  30. ->method('jsonSerializeDefinition')
  31. ->willReturn(['foo' => 'bar', 'name' => 'abc']);
  32. $backend->setPriority(57);
  33. $backend->addAuthScheme('foopass');
  34. $backend->addAuthScheme('barauth');
  35. $json = $backend->jsonSerialize();
  36. $this->assertEquals('bar', $json['foo']);
  37. $this->assertEquals('abc', $json['name']);
  38. $this->assertEquals($json['name'], $json['backend']);
  39. $this->assertEquals(57, $json['priority']);
  40. $this->assertContains('foopass', $json['authSchemes']);
  41. $this->assertContains('barauth', $json['authSchemes']);
  42. }
  43. public function validateStorageProvider() {
  44. return [
  45. [true, true],
  46. [false, false],
  47. ];
  48. }
  49. /**
  50. * @dataProvider validateStorageProvider
  51. */
  52. public function testValidateStorage($expectedSuccess, $definitionSuccess) {
  53. $backend = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend')
  54. ->setMethods(['validateStorageDefinition'])
  55. ->getMock();
  56. $backend->expects($this->atMost(1))
  57. ->method('validateStorageDefinition')
  58. ->willReturn($definitionSuccess);
  59. $storageConfig = $this->getMockBuilder('\OCA\Files_External\Lib\StorageConfig')
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->assertEquals($expectedSuccess, $backend->validateStorage($storageConfig));
  63. }
  64. public function testLegacyAuthMechanismCallback() {
  65. $backend = new Backend();
  66. $backend->setLegacyAuthMechanismCallback(function(array $params) {
  67. if (isset($params['ping'])) {
  68. return 'pong';
  69. }
  70. return 'foobar';
  71. });
  72. $this->assertEquals('pong', $backend->getLegacyAuthMechanism(['ping' => true]));
  73. $this->assertEquals('foobar', $backend->getLegacyAuthMechanism(['other' => true]));
  74. $this->assertEquals('foobar', $backend->getLegacyAuthMechanism());
  75. }
  76. }