1
0

APITest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test;
  8. use OCP\IRequest;
  9. class APITest extends \Test\TestCase {
  10. // Helps build a response variable
  11. /**
  12. * @param string $message
  13. */
  14. public function buildResponse($shipped, $data, $code, $message = null) {
  15. $resp = new \OC\OCS\Result($data, $code, $message);
  16. $resp->addHeader('KEY', 'VALUE');
  17. return [
  18. 'shipped' => $shipped,
  19. 'response' => $resp,
  20. 'app' => $this->getUniqueID('testapp_'),
  21. ];
  22. }
  23. // Validate details of the result
  24. /**
  25. * @param \OC\OCS\Result $result
  26. */
  27. public function checkResult($result, $success) {
  28. // Check response is of correct type
  29. $this->assertInstanceOf(\OC\OCS\Result::class, $result);
  30. // Check if it succeeded
  31. /** @var $result \OC\OCS\Result */
  32. $this->assertEquals($success, $result->succeeded());
  33. }
  34. /**
  35. * @return array
  36. */
  37. public function versionDataScriptNameProvider() {
  38. return [
  39. // Valid script name
  40. [
  41. '/master/ocs/v2.php',
  42. true,
  43. ],
  44. // Invalid script names
  45. [
  46. '/master/ocs/v2.php/someInvalidPathName',
  47. false,
  48. ],
  49. [
  50. '/master/ocs/v1.php',
  51. false,
  52. ],
  53. [
  54. '',
  55. false,
  56. ],
  57. ];
  58. }
  59. /**
  60. * @dataProvider versionDataScriptNameProvider
  61. * @param string $scriptName
  62. * @param bool $expected
  63. */
  64. public function testIsV2($scriptName, $expected) {
  65. $request = $this->getMockBuilder(IRequest::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $request
  69. ->expects($this->once())
  70. ->method('getScriptName')
  71. ->willReturn($scriptName);
  72. $this->assertEquals($expected, $this->invokePrivate(new \OC_API, 'isV2', [$request]));
  73. }
  74. }