RemoteContext.php 4.0 KB

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