RemoteContext.php 4.1 KB

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