1
0

RemoteContext.php 3.5 KB

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