RemoteContext.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. use Behat\Behat\Context\Context;
  7. use PHPUnit\Framework\Assert;
  8. require __DIR__ . '/../../vendor/autoload.php';
  9. /**
  10. * Remote context.
  11. */
  12. class RemoteContext implements Context {
  13. /** @var \OC\Remote\Instance */
  14. protected $remoteInstance;
  15. /** @var \OC\Remote\Credentials */
  16. protected $credentails;
  17. /** @var \OC\Remote\User */
  18. protected $userResult;
  19. protected $remoteUrl;
  20. protected $lastException;
  21. public function __construct($remote) {
  22. require_once __DIR__ . '/../../../../lib/base.php';
  23. $this->remoteUrl = $remote;
  24. }
  25. protected function getApiClient() {
  26. return new \OC\Remote\Api\OCS($this->remoteInstance, $this->credentails, \OC::$server->getHTTPClientService());
  27. }
  28. /**
  29. * @Given /^using remote server "(REMOTE|NON_EXISTING)"$/
  30. *
  31. * @param string $remoteServer "NON_EXISTING" or "REMOTE"
  32. */
  33. public function selectRemoteInstance($remoteServer) {
  34. if ($remoteServer == "REMOTE") {
  35. $baseUri = $this->remoteUrl;
  36. } else {
  37. $baseUri = 'nonexistingnextcloudserver.local';
  38. }
  39. $this->lastException = null;
  40. try {
  41. $this->remoteInstance = new \OC\Remote\Instance($baseUri, \OC::$server->getMemCacheFactory()->createLocal(), \OC::$server->getHTTPClientService());
  42. // trigger the status request
  43. $this->remoteInstance->getProtocol();
  44. } catch (\Exception $e) {
  45. $this->lastException = $e;
  46. }
  47. }
  48. /**
  49. * @Then /^the remote version should be "([^"]*)"$/
  50. * @param string $version
  51. */
  52. public function theRemoteVersionShouldBe($version) {
  53. if ($version === '__current_version__') {
  54. $version = \OC::$server->getConfig()->getSystemValue('version', '0.0.0.0');
  55. }
  56. Assert::assertEquals($version, $this->remoteInstance->getVersion());
  57. }
  58. /**
  59. * @Then /^the remote protocol should be "([^"]*)"$/
  60. * @param string $protocol
  61. */
  62. public function theRemoteProtocolShouldBe($protocol) {
  63. Assert::assertEquals($protocol, $this->remoteInstance->getProtocol());
  64. }
  65. /**
  66. * @Given /^using credentials "([^"]*)", "([^"]*)"/
  67. * @param string $user
  68. * @param string $password
  69. */
  70. public function usingCredentials($user, $password) {
  71. $this->credentails = new \OC\Remote\Credentials($user, $password);
  72. }
  73. /**
  74. * @When /^getting the remote user info for "([^"]*)"$/
  75. * @param string $user
  76. */
  77. public function remoteUserInfo($user) {
  78. $this->lastException = null;
  79. try {
  80. $this->userResult = $this->getApiClient()->getUser($user);
  81. } catch (\Exception $e) {
  82. $this->lastException = $e;
  83. }
  84. }
  85. /**
  86. * @Then /^the remote user should have userid "([^"]*)"$/
  87. * @param string $user
  88. */
  89. public function remoteUserId($user) {
  90. Assert::assertEquals($user, $this->userResult->getUserId());
  91. }
  92. /**
  93. * @Then /^the request should throw a "([^"]*)"$/
  94. * @param string $class
  95. */
  96. public function lastError($class) {
  97. Assert::assertEquals($class, get_class($this->lastException));
  98. }
  99. /**
  100. * @Then /^the capability "([^"]*)" is "([^"]*)"$/
  101. * @param string $key
  102. * @param string $value
  103. */
  104. public function hasCapability($key, $value) {
  105. try {
  106. $capabilities = $this->getApiClient()->getCapabilities();
  107. } catch (\Exception $e) {
  108. Assert::assertInstanceOf($value, $e);
  109. $this->lastException = $e;
  110. return;
  111. }
  112. $current = $capabilities;
  113. $parts = explode('.', $key);
  114. foreach ($parts as $part) {
  115. if ($current !== null) {
  116. $current = isset($current[$part]) ? $current[$part] : null;
  117. }
  118. }
  119. Assert::assertEquals($value, $current);
  120. }
  121. }