PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/build/integration/features/bootstrap/BasicStructure.php

https://gitlab.com/wuhang2003/core
PHP | 300 lines | 187 code | 35 blank | 78 comment | 17 complexity | 92e479c25c168ca08092569d842587e9 MD5 | raw file
  1. <?php
  2. use GuzzleHttp\Client;
  3. use GuzzleHttp\Message\ResponseInterface;
  4. require __DIR__ . '/../../vendor/autoload.php';
  5. trait BasicStructure {
  6. /** @var string */
  7. private $currentUser = '';
  8. /** @var string */
  9. private $currentServer = '';
  10. /** @var string */
  11. private $baseUrl = '';
  12. /** @var int */
  13. private $apiVersion = 1;
  14. /** @var ResponseInterface */
  15. private $response = null;
  16. /** @var \GuzzleHttp\Cookie\CookieJar */
  17. private $cookieJar;
  18. /** @var string */
  19. private $requestToken;
  20. public function __construct($baseUrl, $admin, $regular_user_password) {
  21. // Initialize your context here
  22. $this->baseUrl = $baseUrl;
  23. $this->adminUser = $admin;
  24. $this->regularUser = $regular_user_password;
  25. $this->localBaseUrl = $this->baseUrl;
  26. $this->remoteBaseUrl = $this->baseUrl;
  27. $this->currentServer = 'LOCAL';
  28. $this->cookieJar = new \GuzzleHttp\Cookie\CookieJar();
  29. // in case of ci deployment we take the server url from the environment
  30. $testServerUrl = getenv('TEST_SERVER_URL');
  31. if ($testServerUrl !== false) {
  32. $this->baseUrl = $testServerUrl;
  33. $this->localBaseUrl = $testServerUrl;
  34. }
  35. // federated server url from the environment
  36. $testRemoteServerUrl = getenv('TEST_SERVER_FED_URL');
  37. if ($testRemoteServerUrl !== false) {
  38. $this->remoteBaseUrl = $testRemoteServerUrl;
  39. }
  40. }
  41. /**
  42. * @Given /^using api version "([^"]*)"$/
  43. * @param string $version
  44. */
  45. public function usingApiVersion($version) {
  46. $this->apiVersion = $version;
  47. }
  48. /**
  49. * @Given /^As an "([^"]*)"$/
  50. * @param string $user
  51. */
  52. public function asAn($user) {
  53. $this->currentUser = $user;
  54. }
  55. /**
  56. * @Given /^Using server "(LOCAL|REMOTE)"$/
  57. * @param string $server
  58. * @return string Previous used server
  59. */
  60. public function usingServer($server) {
  61. $previousServer = $this->currentServer;
  62. if ($server === 'LOCAL'){
  63. $this->baseUrl = $this->localBaseUrl;
  64. $this->currentServer = 'LOCAL';
  65. return $previousServer;
  66. } else {
  67. $this->baseUrl = $this->remoteBaseUrl;
  68. $this->currentServer = 'REMOTE';
  69. return $previousServer;
  70. }
  71. }
  72. /**
  73. * @When /^sending "([^"]*)" to "([^"]*)"$/
  74. * @param string $verb
  75. * @param string $url
  76. */
  77. public function sendingTo($verb, $url) {
  78. $this->sendingToWith($verb, $url, null);
  79. }
  80. /**
  81. * Parses the xml answer to get ocs response which doesn't match with
  82. * http one in v1 of the api.
  83. * @param ResponseInterface $response
  84. * @return string
  85. */
  86. public function getOCSResponse($response) {
  87. return $response->xml()->meta[0]->statuscode;
  88. }
  89. /**
  90. * This function is needed to use a vertical fashion in the gherkin tables.
  91. * @param array $arrayOfArrays
  92. * @return array
  93. */
  94. public function simplifyArray($arrayOfArrays){
  95. $a = array_map(function($subArray) { return $subArray[0]; }, $arrayOfArrays);
  96. return $a;
  97. }
  98. /**
  99. * @When /^sending "([^"]*)" to "([^"]*)" with$/
  100. * @param string $verb
  101. * @param string $url
  102. * @param \Behat\Gherkin\Node\TableNode $body
  103. */
  104. public function sendingToWith($verb, $url, $body) {
  105. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php" . $url;
  106. $client = new Client();
  107. $options = [];
  108. if ($this->currentUser === 'admin') {
  109. $options['auth'] = $this->adminUser;
  110. } else {
  111. $options['auth'] = [$this->currentUser, $this->regularUser];
  112. }
  113. if ($body instanceof \Behat\Gherkin\Node\TableNode) {
  114. $fd = $body->getRowsHash();
  115. $options['body'] = $fd;
  116. }
  117. try {
  118. $this->response = $client->send($client->createRequest($verb, $fullUrl, $options));
  119. } catch (\GuzzleHttp\Exception\ClientException $ex) {
  120. $this->response = $ex->getResponse();
  121. }
  122. }
  123. public function isExpectedUrl($possibleUrl, $finalPart){
  124. $baseUrlChopped = substr($this->baseUrl, 0, -4);
  125. $endCharacter = strlen($baseUrlChopped) + strlen($finalPart);
  126. return (substr($possibleUrl,0,$endCharacter) == "$baseUrlChopped" . "$finalPart");
  127. }
  128. /**
  129. * @Then /^the OCS status code should be "([^"]*)"$/
  130. * @param int $statusCode
  131. */
  132. public function theOCSStatusCodeShouldBe($statusCode) {
  133. PHPUnit_Framework_Assert::assertEquals($statusCode, $this->getOCSResponse($this->response));
  134. }
  135. /**
  136. * @Then /^the HTTP status code should be "([^"]*)"$/
  137. * @param int $statusCode
  138. */
  139. public function theHTTPStatusCodeShouldBe($statusCode) {
  140. PHPUnit_Framework_Assert::assertEquals($statusCode, $this->response->getStatusCode());
  141. }
  142. /**
  143. * @param ResponseInterface $response
  144. */
  145. private function extracRequestTokenFromResponse(ResponseInterface $response) {
  146. $this->requestToken = substr(preg_replace('/(.*)data-requesttoken="(.*)">(.*)/sm', '\2', $response->getBody()->getContents()), 0, 89);
  147. }
  148. /**
  149. * @Given Logging in using web as :user
  150. * @param string $user
  151. */
  152. public function loggingInUsingWebAs($user) {
  153. $loginUrl = substr($this->baseUrl, 0, -5);
  154. // Request a new session and extract CSRF token
  155. $client = new Client();
  156. $response = $client->get(
  157. $loginUrl,
  158. [
  159. 'cookies' => $this->cookieJar,
  160. ]
  161. );
  162. $this->extracRequestTokenFromResponse($response);
  163. // Login and extract new token
  164. $password = ($user === 'admin') ? 'admin' : '123456';
  165. $client = new Client();
  166. $response = $client->post(
  167. $loginUrl,
  168. [
  169. 'body' => [
  170. 'user' => $user,
  171. 'password' => $password,
  172. 'requesttoken' => $this->requestToken,
  173. ],
  174. 'cookies' => $this->cookieJar,
  175. ]
  176. );
  177. $this->extracRequestTokenFromResponse($response);
  178. }
  179. /**
  180. * @When Sending a :method to :url with requesttoken
  181. * @param string $method
  182. * @param string $url
  183. */
  184. public function sendingAToWithRequesttoken($method, $url) {
  185. $baseUrl = substr($this->baseUrl, 0, -5);
  186. $client = new Client();
  187. $request = $client->createRequest(
  188. $method,
  189. $baseUrl . $url,
  190. [
  191. 'cookies' => $this->cookieJar,
  192. ]
  193. );
  194. $request->addHeader('requesttoken', $this->requestToken);
  195. try {
  196. $this->response = $client->send($request);
  197. } catch (\GuzzleHttp\Exception\ClientException $e) {
  198. $this->response = $e->getResponse();
  199. }
  200. }
  201. /**
  202. * @When Sending a :method to :url without requesttoken
  203. * @param string $method
  204. * @param string $url
  205. */
  206. public function sendingAToWithoutRequesttoken($method, $url) {
  207. $baseUrl = substr($this->baseUrl, 0, -5);
  208. $client = new Client();
  209. $request = $client->createRequest(
  210. $method,
  211. $baseUrl . $url,
  212. [
  213. 'cookies' => $this->cookieJar,
  214. ]
  215. );
  216. try {
  217. $this->response = $client->send($request);
  218. } catch (\GuzzleHttp\Exception\ClientException $e) {
  219. $this->response = $e->getResponse();
  220. }
  221. }
  222. public static function removeFile($path, $filename){
  223. if (file_exists("$path" . "$filename")) {
  224. unlink("$path" . "$filename");
  225. }
  226. }
  227. /**
  228. * @BeforeSuite
  229. */
  230. public static function addFilesToSkeleton(){
  231. for ($i=0; $i<5; $i++){
  232. file_put_contents("../../core/skeleton/" . "textfile" . "$i" . ".txt", "ownCloud test text file\n");
  233. }
  234. if (!file_exists("../../core/skeleton/FOLDER")) {
  235. mkdir("../../core/skeleton/FOLDER", 0777, true);
  236. }
  237. if (!file_exists("../../core/skeleton/PARENT")) {
  238. mkdir("../../core/skeleton/PARENT", 0777, true);
  239. }
  240. file_put_contents("../../core/skeleton/PARENT/" . "parent.txt", "ownCloud test text file\n");
  241. if (!file_exists("../../core/skeleton/PARENT/CHILD")) {
  242. mkdir("../../core/skeleton/PARENT/CHILD", 0777, true);
  243. }
  244. file_put_contents("../../core/skeleton/PARENT/CHILD/" . "child.txt", "ownCloud test text file\n");
  245. }
  246. /**
  247. * @AfterSuite
  248. */
  249. public static function removeFilesFromSkeleton(){
  250. for ($i=0; $i<5; $i++){
  251. self::removeFile("../../core/skeleton/", "textfile" . "$i" . ".txt");
  252. }
  253. if (is_dir("../../core/skeleton/FOLDER")) {
  254. rmdir("../../core/skeleton/FOLDER");
  255. }
  256. self::removeFile("../../core/skeleton/PARENT/CHILD/", "child.txt");
  257. if (is_dir("../../core/skeleton/PARENT/CHILD")) {
  258. rmdir("../../core/skeleton/PARENT/CHILD");
  259. }
  260. self::removeFile("../../core/skeleton/PARENT/", "parent.txt");
  261. if (is_dir("../../core/skeleton/PARENT")) {
  262. rmdir("../../core/skeleton/PARENT");
  263. }
  264. }
  265. }