You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BasicStructure.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. <?php
  2. /**
  3. *
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Sergio Bertolin <sbertolin@solidgear.es>
  15. * @author Sergio Bertolín <sbertolin@solidgear.es>
  16. * @author Thomas Müller <thomas.mueller@tmit.eu>
  17. *
  18. * @license GNU AGPL version 3 or any later version
  19. *
  20. * This program is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License as
  22. * published by the Free Software Foundation, either version 3 of the
  23. * License, or (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32. *
  33. */
  34. use Behat\Gherkin\Node\TableNode;
  35. use GuzzleHttp\Client;
  36. use GuzzleHttp\Cookie\CookieJar;
  37. use GuzzleHttp\Exception\ClientException;
  38. use PHPUnit\Framework\Assert;
  39. use Psr\Http\Message\ResponseInterface;
  40. require __DIR__ . '/../../vendor/autoload.php';
  41. trait BasicStructure {
  42. use Auth;
  43. use Avatar;
  44. use Download;
  45. use Mail;
  46. /** @var string */
  47. private $currentUser = '';
  48. /** @var string */
  49. private $currentServer = '';
  50. /** @var string */
  51. private $baseUrl = '';
  52. /** @var int */
  53. private $apiVersion = 1;
  54. /** @var ResponseInterface */
  55. private $response = null;
  56. /** @var CookieJar */
  57. private $cookieJar;
  58. /** @var string */
  59. private $requestToken;
  60. protected $adminUser;
  61. protected $regularUser;
  62. protected $localBaseUrl;
  63. protected $remoteBaseUrl;
  64. public function __construct($baseUrl, $admin, $regular_user_password) {
  65. // Initialize your context here
  66. $this->baseUrl = $baseUrl;
  67. $this->adminUser = $admin;
  68. $this->regularUser = $regular_user_password;
  69. $this->localBaseUrl = $this->baseUrl;
  70. $this->remoteBaseUrl = $this->baseUrl;
  71. $this->currentServer = 'LOCAL';
  72. $this->cookieJar = new CookieJar();
  73. // in case of ci deployment we take the server url from the environment
  74. $testServerUrl = getenv('TEST_SERVER_URL');
  75. if ($testServerUrl !== false) {
  76. $this->baseUrl = $testServerUrl;
  77. $this->localBaseUrl = $testServerUrl;
  78. }
  79. // federated server url from the environment
  80. $testRemoteServerUrl = getenv('TEST_SERVER_FED_URL');
  81. if ($testRemoteServerUrl !== false) {
  82. $this->remoteBaseUrl = $testRemoteServerUrl;
  83. }
  84. }
  85. /**
  86. * @Given /^using api version "(\d+)"$/
  87. * @param string $version
  88. */
  89. public function usingApiVersion($version) {
  90. $this->apiVersion = (int)$version;
  91. }
  92. /**
  93. * @Given /^As an "([^"]*)"$/
  94. * @param string $user
  95. */
  96. public function asAn($user) {
  97. $this->currentUser = $user;
  98. }
  99. /**
  100. * @Given /^Using server "(LOCAL|REMOTE)"$/
  101. * @param string $server
  102. * @return string Previous used server
  103. */
  104. public function usingServer($server) {
  105. $previousServer = $this->currentServer;
  106. if ($server === 'LOCAL') {
  107. $this->baseUrl = $this->localBaseUrl;
  108. $this->currentServer = 'LOCAL';
  109. return $previousServer;
  110. } else {
  111. $this->baseUrl = $this->remoteBaseUrl;
  112. $this->currentServer = 'REMOTE';
  113. return $previousServer;
  114. }
  115. }
  116. /**
  117. * @When /^sending "([^"]*)" to "([^"]*)"$/
  118. * @param string $verb
  119. * @param string $url
  120. */
  121. public function sendingTo($verb, $url) {
  122. $this->sendingToWith($verb, $url, null);
  123. }
  124. /**
  125. * Parses the xml answer to get ocs response which doesn't match with
  126. * http one in v1 of the api.
  127. *
  128. * @param ResponseInterface $response
  129. * @return string
  130. */
  131. public function getOCSResponse($response) {
  132. return simplexml_load_string($response->getBody())->meta[0]->statuscode;
  133. }
  134. /**
  135. * This function is needed to use a vertical fashion in the gherkin tables.
  136. *
  137. * @param array $arrayOfArrays
  138. * @return array
  139. */
  140. public function simplifyArray($arrayOfArrays) {
  141. $a = array_map(function ($subArray) {
  142. return $subArray[0];
  143. }, $arrayOfArrays);
  144. return $a;
  145. }
  146. /**
  147. * @When /^sending "([^"]*)" to "([^"]*)" with$/
  148. * @param string $verb
  149. * @param string $url
  150. * @param TableNode $body
  151. */
  152. public function sendingToWith($verb, $url, $body) {
  153. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php" . $url;
  154. $client = new Client();
  155. $options = [];
  156. if ($this->currentUser === 'admin') {
  157. $options['auth'] = $this->adminUser;
  158. } elseif (strpos($this->currentUser, 'anonymous') !== 0) {
  159. $options['auth'] = [$this->currentUser, $this->regularUser];
  160. }
  161. $options['headers'] = [
  162. 'OCS_APIREQUEST' => 'true'
  163. ];
  164. if ($body instanceof TableNode) {
  165. $fd = $body->getRowsHash();
  166. $options['form_params'] = $fd;
  167. }
  168. // TODO: Fix this hack!
  169. if ($verb === 'PUT' && $body === null) {
  170. $options['form_params'] = [
  171. 'foo' => 'bar',
  172. ];
  173. }
  174. try {
  175. $this->response = $client->request($verb, $fullUrl, $options);
  176. } catch (ClientException $ex) {
  177. $this->response = $ex->getResponse();
  178. }
  179. }
  180. /**
  181. * @When /^sending "([^"]*)" with exact url to "([^"]*)"$/
  182. * @param string $verb
  183. * @param string $url
  184. */
  185. public function sendingToDirectUrl($verb, $url) {
  186. $this->sendingToWithDirectUrl($verb, $url, null);
  187. }
  188. public function sendingToWithDirectUrl($verb, $url, $body) {
  189. $fullUrl = substr($this->baseUrl, 0, -5) . $url;
  190. $client = new Client();
  191. $options = [];
  192. if ($this->currentUser === 'admin') {
  193. $options['auth'] = $this->adminUser;
  194. } elseif (strpos($this->currentUser, 'anonymous') !== 0) {
  195. $options['auth'] = [$this->currentUser, $this->regularUser];
  196. }
  197. if ($body instanceof TableNode) {
  198. $fd = $body->getRowsHash();
  199. $options['form_params'] = $fd;
  200. }
  201. try {
  202. $this->response = $client->request($verb, $fullUrl, $options);
  203. } catch (ClientException $ex) {
  204. $this->response = $ex->getResponse();
  205. }
  206. }
  207. public function isExpectedUrl($possibleUrl, $finalPart) {
  208. $baseUrlChopped = substr($this->baseUrl, 0, -4);
  209. $endCharacter = strlen($baseUrlChopped) + strlen($finalPart);
  210. return (substr($possibleUrl, 0, $endCharacter) == "$baseUrlChopped" . "$finalPart");
  211. }
  212. /**
  213. * @Then /^the OCS status code should be "([^"]*)"$/
  214. * @param int $statusCode
  215. */
  216. public function theOCSStatusCodeShouldBe($statusCode) {
  217. Assert::assertEquals($statusCode, $this->getOCSResponse($this->response));
  218. }
  219. /**
  220. * @Then /^the HTTP status code should be "([^"]*)"$/
  221. * @param int $statusCode
  222. */
  223. public function theHTTPStatusCodeShouldBe($statusCode) {
  224. Assert::assertEquals($statusCode, $this->response->getStatusCode());
  225. }
  226. /**
  227. * @Then /^the Content-Type should be "([^"]*)"$/
  228. * @param string $contentType
  229. */
  230. public function theContentTypeShouldbe($contentType) {
  231. Assert::assertEquals($contentType, $this->response->getHeader('Content-Type')[0]);
  232. }
  233. /**
  234. * @param ResponseInterface $response
  235. */
  236. private function extracRequestTokenFromResponse(ResponseInterface $response) {
  237. $this->requestToken = substr(preg_replace('/(.*)data-requesttoken="(.*)">(.*)/sm', '\2', $response->getBody()->getContents()), 0, 89);
  238. }
  239. /**
  240. * @Given Logging in using web as :user
  241. * @param string $user
  242. */
  243. public function loggingInUsingWebAs($user) {
  244. $loginUrl = substr($this->baseUrl, 0, -5) . '/login';
  245. // Request a new session and extract CSRF token
  246. $client = new Client();
  247. $response = $client->get(
  248. $loginUrl,
  249. [
  250. 'cookies' => $this->cookieJar,
  251. ]
  252. );
  253. $this->extracRequestTokenFromResponse($response);
  254. // Login and extract new token
  255. $password = ($user === 'admin') ? 'admin' : '123456';
  256. $client = new Client();
  257. $response = $client->post(
  258. $loginUrl,
  259. [
  260. 'form_params' => [
  261. 'user' => $user,
  262. 'password' => $password,
  263. 'requesttoken' => $this->requestToken,
  264. ],
  265. 'cookies' => $this->cookieJar,
  266. ]
  267. );
  268. $this->extracRequestTokenFromResponse($response);
  269. }
  270. /**
  271. * @When Sending a :method to :url with requesttoken
  272. * @param string $method
  273. * @param string $url
  274. * @param TableNode|array|null $body
  275. */
  276. public function sendingAToWithRequesttoken($method, $url, $body = null) {
  277. $baseUrl = substr($this->baseUrl, 0, -5);
  278. $options = [
  279. 'cookies' => $this->cookieJar,
  280. 'headers' => [
  281. 'requesttoken' => $this->requestToken
  282. ],
  283. ];
  284. if ($body instanceof TableNode) {
  285. $fd = $body->getRowsHash();
  286. $options['form_params'] = $fd;
  287. } elseif ($body) {
  288. $options = array_merge($options, $body);
  289. }
  290. $client = new Client();
  291. try {
  292. $this->response = $client->request(
  293. $method,
  294. $baseUrl . $url,
  295. $options
  296. );
  297. } catch (ClientException $e) {
  298. $this->response = $e->getResponse();
  299. }
  300. }
  301. /**
  302. * @When Sending a :method to :url without requesttoken
  303. * @param string $method
  304. * @param string $url
  305. */
  306. public function sendingAToWithoutRequesttoken($method, $url) {
  307. $baseUrl = substr($this->baseUrl, 0, -5);
  308. $client = new Client();
  309. try {
  310. $this->response = $client->request(
  311. $method,
  312. $baseUrl . $url,
  313. [
  314. 'cookies' => $this->cookieJar
  315. ]
  316. );
  317. } catch (ClientException $e) {
  318. $this->response = $e->getResponse();
  319. }
  320. }
  321. public static function removeFile($path, $filename) {
  322. if (file_exists("$path" . "$filename")) {
  323. unlink("$path" . "$filename");
  324. }
  325. }
  326. /**
  327. * @Given User :user modifies text of :filename with text :text
  328. * @param string $user
  329. * @param string $filename
  330. * @param string $text
  331. */
  332. public function modifyTextOfFile($user, $filename, $text) {
  333. self::removeFile($this->getDataDirectory() . "/$user/files", "$filename");
  334. file_put_contents($this->getDataDirectory() . "/$user/files" . "$filename", "$text");
  335. }
  336. private function getDataDirectory() {
  337. // Based on "runOcc" from CommandLine trait
  338. $args = ['config:system:get', 'datadirectory'];
  339. $args = array_map(function ($arg) {
  340. return escapeshellarg($arg);
  341. }, $args);
  342. $args[] = '--no-ansi --no-warnings';
  343. $args = implode(' ', $args);
  344. $descriptor = [
  345. 0 => ['pipe', 'r'],
  346. 1 => ['pipe', 'w'],
  347. 2 => ['pipe', 'w'],
  348. ];
  349. $process = proc_open('php console.php ' . $args, $descriptor, $pipes, $ocPath = '../..');
  350. $lastStdOut = stream_get_contents($pipes[1]);
  351. proc_close($process);
  352. return trim($lastStdOut);
  353. }
  354. /**
  355. * @Given file :filename is created :times times in :user user data
  356. * @param string $filename
  357. * @param string $times
  358. * @param string $user
  359. */
  360. public function fileIsCreatedTimesInUserData($filename, $times, $user) {
  361. for ($i = 0; $i < $times; $i++) {
  362. file_put_contents($this->getDataDirectory() . "/$user/files" . "$filename-$i", "content-$i");
  363. }
  364. }
  365. public function createFileSpecificSize($name, $size) {
  366. $file = fopen("work/" . "$name", 'w');
  367. fseek($file, $size - 1, SEEK_CUR);
  368. fwrite($file, 'a'); // write a dummy char at SIZE position
  369. fclose($file);
  370. }
  371. public function createFileWithText($name, $text) {
  372. $file = fopen("work/" . "$name", 'w');
  373. fwrite($file, $text);
  374. fclose($file);
  375. }
  376. /**
  377. * @Given file :filename of size :size is created in local storage
  378. * @param string $filename
  379. * @param string $size
  380. */
  381. public function fileIsCreatedInLocalStorageWithSize($filename, $size) {
  382. $this->createFileSpecificSize("local_storage/$filename", $size);
  383. }
  384. /**
  385. * @Given file :filename with text :text is created in local storage
  386. * @param string $filename
  387. * @param string $text
  388. */
  389. public function fileIsCreatedInLocalStorageWithText($filename, $text) {
  390. $this->createFileWithText("local_storage/$filename", $text);
  391. }
  392. /**
  393. * @When Sleep for :seconds seconds
  394. * @param int $seconds
  395. */
  396. public function sleepForSeconds($seconds) {
  397. sleep((int)$seconds);
  398. }
  399. /**
  400. * @BeforeSuite
  401. */
  402. public static function addFilesToSkeleton() {
  403. for ($i = 0; $i < 5; $i++) {
  404. file_put_contents("../../core/skeleton/" . "textfile" . "$i" . ".txt", "Nextcloud test text file\n");
  405. }
  406. if (!file_exists("../../core/skeleton/FOLDER")) {
  407. mkdir("../../core/skeleton/FOLDER", 0777, true);
  408. }
  409. if (!file_exists("../../core/skeleton/PARENT")) {
  410. mkdir("../../core/skeleton/PARENT", 0777, true);
  411. }
  412. file_put_contents("../../core/skeleton/PARENT/" . "parent.txt", "Nextcloud test text file\n");
  413. if (!file_exists("../../core/skeleton/PARENT/CHILD")) {
  414. mkdir("../../core/skeleton/PARENT/CHILD", 0777, true);
  415. }
  416. file_put_contents("../../core/skeleton/PARENT/CHILD/" . "child.txt", "Nextcloud test text file\n");
  417. }
  418. /**
  419. * @AfterSuite
  420. */
  421. public static function removeFilesFromSkeleton() {
  422. for ($i = 0; $i < 5; $i++) {
  423. self::removeFile("../../core/skeleton/", "textfile" . "$i" . ".txt");
  424. }
  425. if (is_dir("../../core/skeleton/FOLDER")) {
  426. rmdir("../../core/skeleton/FOLDER");
  427. }
  428. self::removeFile("../../core/skeleton/PARENT/CHILD/", "child.txt");
  429. if (is_dir("../../core/skeleton/PARENT/CHILD")) {
  430. rmdir("../../core/skeleton/PARENT/CHILD");
  431. }
  432. self::removeFile("../../core/skeleton/PARENT/", "parent.txt");
  433. if (is_dir("../../core/skeleton/PARENT")) {
  434. rmdir("../../core/skeleton/PARENT");
  435. }
  436. }
  437. /**
  438. * @BeforeScenario @local_storage
  439. */
  440. public static function removeFilesFromLocalStorageBefore() {
  441. $dir = "./work/local_storage/";
  442. $di = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS);
  443. $ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST);
  444. foreach ($ri as $file) {
  445. $file->isDir() ? rmdir($file) : unlink($file);
  446. }
  447. }
  448. /**
  449. * @AfterScenario @local_storage
  450. */
  451. public static function removeFilesFromLocalStorageAfter() {
  452. $dir = "./work/local_storage/";
  453. $di = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS);
  454. $ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST);
  455. foreach ($ri as $file) {
  456. $file->isDir() ? rmdir($file) : unlink($file);
  457. }
  458. }
  459. /**
  460. * @Given /^cookies are reset$/
  461. */
  462. public function cookiesAreReset() {
  463. $this->cookieJar = new CookieJar();
  464. }
  465. /**
  466. * @Then The following headers should be set
  467. * @param TableNode $table
  468. * @throws \Exception
  469. */
  470. public function theFollowingHeadersShouldBeSet(TableNode $table) {
  471. foreach ($table->getTable() as $header) {
  472. $headerName = $header[0];
  473. $expectedHeaderValue = $header[1];
  474. $returnedHeader = $this->response->getHeader($headerName)[0];
  475. if ($returnedHeader !== $expectedHeaderValue) {
  476. throw new \Exception(
  477. sprintf(
  478. "Expected value '%s' for header '%s', got '%s'",
  479. $expectedHeaderValue,
  480. $headerName,
  481. $returnedHeader
  482. )
  483. );
  484. }
  485. }
  486. }
  487. }