api.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Tom Needham <tom@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class Test_API extends \Test\TestCase {
  9. // Helps build a response variable
  10. /**
  11. * @param string $message
  12. */
  13. function buildResponse($shipped, $data, $code, $message=null) {
  14. $resp = new OC_OCS_Result($data, $code, $message);
  15. $resp->addHeader('KEY', 'VALUE');
  16. return [
  17. 'shipped' => $shipped,
  18. 'response' => $resp,
  19. 'app' => $this->getUniqueID('testapp_'),
  20. ];
  21. }
  22. // Validate details of the result
  23. /**
  24. * @param OC_OCS_Result $result
  25. */
  26. function checkResult($result, $success) {
  27. // Check response is of correct type
  28. $this->assertInstanceOf('OC_OCS_Result', $result);
  29. // Check if it succeeded
  30. /** @var $result OC_OCS_Result */
  31. $this->assertEquals($success, $result->succeeded());
  32. }
  33. /**
  34. * @return array
  35. */
  36. public function versionDataScriptNameProvider() {
  37. return [
  38. // Valid script name
  39. [
  40. '/master/ocs/v2.php',
  41. true,
  42. ],
  43. // Invalid script names
  44. [
  45. '/master/ocs/v2.php/someInvalidPathName',
  46. false,
  47. ],
  48. [
  49. '/master/ocs/v1.php',
  50. false,
  51. ],
  52. [
  53. '',
  54. false,
  55. ],
  56. ];
  57. }
  58. /**
  59. * @dataProvider versionDataScriptNameProvider
  60. * @param string $scriptName
  61. * @param bool $expected
  62. */
  63. public function testIsV2($scriptName, $expected) {
  64. $request = $this->getMockBuilder('\OCP\IRequest')
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $request
  68. ->expects($this->once())
  69. ->method('getScriptName')
  70. ->will($this->returnValue($scriptName));
  71. $this->assertEquals($expected, $this->invokePrivate(new \OC_API, 'isV2', [$request]));
  72. }
  73. function dataProviderTestOneResult() {
  74. return [
  75. [100, true],
  76. [101, false],
  77. [997, false],
  78. ];
  79. }
  80. /**
  81. * @dataProvider dataProviderTestOneResult
  82. *
  83. * @param $statusCode
  84. * @param $succeeded
  85. */
  86. public function testOneResult($statusCode, $succeeded) {
  87. // Setup some data arrays
  88. $data1 = [
  89. 'users' => [
  90. 'tom' => [
  91. 'key' => 'value',
  92. ],
  93. 'frank' => [
  94. 'key' => 'value',
  95. ],
  96. ]];
  97. // Test merging one success result
  98. $response = $this->buildResponse(true, $data1, $statusCode);
  99. $result = OC_API::mergeResponses([$response]);
  100. $this->assertEquals($response['response'], $result);
  101. $this->checkResult($result, $succeeded);
  102. }
  103. function dataProviderTestMergeResponses() {
  104. return [
  105. // Two shipped success results
  106. [true, 100, true, 100, true],
  107. // Two shipped results, one success and one failure
  108. [true, 100, true, 998, false],
  109. // Two shipped results, both failure
  110. [true, 997, true, 998, false],
  111. // Two third party success results
  112. [false, 100, false, 100, true],
  113. // Two third party results, one success and one failure
  114. [false, 100, false, 998, false],
  115. // Two third party results, both failure
  116. [false, 997, false, 998, false],
  117. // One of each, both success
  118. [false, 100, true, 100, true],
  119. [true, 100, false, 100, true],
  120. // One of each, both failure
  121. [false, 997, true, 998, false],
  122. // One of each, shipped success
  123. [false, 997, true, 100, true],
  124. // One of each, third party success
  125. [false, 100, true, 998, false],
  126. ];
  127. }
  128. /**
  129. * @dataProvider dataProviderTestMergeResponses
  130. *
  131. * Test the merging of multiple responses
  132. * @param $statusCode1
  133. * @param $statusCode2
  134. * @param $succeeded
  135. */
  136. public function testMultipleMergeResponses($shipped1, $statusCode1, $shipped2, $statusCode2, $succeeded){
  137. // Tests that app responses are merged correctly
  138. // Setup some data arrays
  139. $data1 = array(
  140. 'users' => array(
  141. 'tom' => array(
  142. 'key' => 'value',
  143. ),
  144. 'frank' => array(
  145. 'key' => 'value',
  146. ),
  147. ));
  148. $data2 = array(
  149. 'users' => array(
  150. 'tom' => array(
  151. 'key' => 'newvalue',
  152. ),
  153. 'jan' => array(
  154. 'key' => 'value',
  155. ),
  156. ));
  157. // Two shipped success results
  158. $result = OC_API::mergeResponses(array(
  159. $this->buildResponse($shipped1, $data1, $statusCode1, "message1"),
  160. $this->buildResponse($shipped2, $data2, $statusCode2, "message2"),
  161. ));
  162. $this->checkResult($result, $succeeded);
  163. $resultData = $result->getData();
  164. $resultMeta = $result->getMeta();
  165. $resultHeaders = $result->getHeaders();
  166. $resultStatusCode = $result->getStatusCode();
  167. $this->assertArrayHasKey('jan', $resultData['users']);
  168. $this->assertArrayHasKey('KEY', $resultHeaders);
  169. // check if the returned status message matches the selected status code
  170. if ($resultStatusCode === 997) {
  171. $this->assertEquals('message1', $resultMeta['message']);
  172. } elseif ($resultStatusCode === 998) {
  173. $this->assertEquals('message2', $resultMeta['message']);
  174. } elseif ($resultStatusCode === 100) {
  175. $this->assertEquals(null, $resultMeta['message']);
  176. }
  177. }
  178. }