1
0

RemoteContext.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author John Molakvoæ <skjnldsv@protonmail.com>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  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
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. use Behat\Behat\Context\Context;
  26. use PHPUnit\Framework\Assert;
  27. require __DIR__ . '/../../vendor/autoload.php';
  28. /**
  29. * Remote context.
  30. */
  31. class RemoteContext implements Context {
  32. /** @var \OC\Remote\Instance */
  33. protected $remoteInstance;
  34. /** @var \OC\Remote\Credentials */
  35. protected $credentails;
  36. /** @var \OC\Remote\User */
  37. protected $userResult;
  38. protected $remoteUrl;
  39. protected $lastException;
  40. public function __construct($remote) {
  41. require_once __DIR__ . '/../../../../lib/base.php';
  42. $this->remoteUrl = $remote;
  43. }
  44. protected function getApiClient() {
  45. return new \OC\Remote\Api\OCS($this->remoteInstance, $this->credentails, \OC::$server->getHTTPClientService());
  46. }
  47. /**
  48. * @Given /^using remote server "(REMOTE|NON_EXISTING)"$/
  49. *
  50. * @param string $remoteServer "NON_EXISTING" or "REMOTE"
  51. */
  52. public function selectRemoteInstance($remoteServer) {
  53. if ($remoteServer == "REMOTE") {
  54. $baseUri = $this->remoteUrl;
  55. } else {
  56. $baseUri = 'nonexistingnextcloudserver.local';
  57. }
  58. $this->lastException = null;
  59. try {
  60. $this->remoteInstance = new \OC\Remote\Instance($baseUri, \OC::$server->getMemCacheFactory()->createLocal(), \OC::$server->getHTTPClientService());
  61. // trigger the status request
  62. $this->remoteInstance->getProtocol();
  63. } catch (\Exception $e) {
  64. $this->lastException = $e;
  65. }
  66. }
  67. /**
  68. * @Then /^the remote version should be "([^"]*)"$/
  69. * @param string $version
  70. */
  71. public function theRemoteVersionShouldBe($version) {
  72. if ($version === '__current_version__') {
  73. $version = \OC::$server->getConfig()->getSystemValue('version', '0.0.0.0');
  74. }
  75. Assert::assertEquals($version, $this->remoteInstance->getVersion());
  76. }
  77. /**
  78. * @Then /^the remote protocol should be "([^"]*)"$/
  79. * @param string $protocol
  80. */
  81. public function theRemoteProtocolShouldBe($protocol) {
  82. Assert::assertEquals($protocol, $this->remoteInstance->getProtocol());
  83. }
  84. /**
  85. * @Given /^using credentials "([^"]*)", "([^"]*)"/
  86. * @param string $user
  87. * @param string $password
  88. */
  89. public function usingCredentials($user, $password) {
  90. $this->credentails = new \OC\Remote\Credentials($user, $password);
  91. }
  92. /**
  93. * @When /^getting the remote user info for "([^"]*)"$/
  94. * @param string $user
  95. */
  96. public function remoteUserInfo($user) {
  97. $this->lastException = null;
  98. try {
  99. $this->userResult = $this->getApiClient()->getUser($user);
  100. } catch (\Exception $e) {
  101. $this->lastException = $e;
  102. }
  103. }
  104. /**
  105. * @Then /^the remote user should have userid "([^"]*)"$/
  106. * @param string $user
  107. */
  108. public function remoteUserId($user) {
  109. Assert::assertEquals($user, $this->userResult->getUserId());
  110. }
  111. /**
  112. * @Then /^the request should throw a "([^"]*)"$/
  113. * @param string $class
  114. */
  115. public function lastError($class) {
  116. Assert::assertEquals($class, get_class($this->lastException));
  117. }
  118. /**
  119. * @Then /^the capability "([^"]*)" is "([^"]*)"$/
  120. * @param string $key
  121. * @param string $value
  122. */
  123. public function hasCapability($key, $value) {
  124. try {
  125. $capabilities = $this->getApiClient()->getCapabilities();
  126. } catch (\Exception $e) {
  127. Assert::assertInstanceOf($value, $e);
  128. $this->lastException = $e;
  129. return;
  130. }
  131. $current = $capabilities;
  132. $parts = explode('.', $key);
  133. foreach ($parts as $part) {
  134. if ($current !== null) {
  135. $current = isset($current[$part]) ? $current[$part] : null;
  136. }
  137. }
  138. Assert::assertEquals($value, $current);
  139. }
  140. }