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