Comments.php 8.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php
/**
 * @author Lukas Reschke <lukas@owncloud.com>
 *
 * @copyright Copyright (c) 2016, ownCloud, Inc.
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

Sergio Bertolin's avatar
Sergio Bertolin committed
22
require __DIR__ . '/../../../../lib/composer/autoload.php';
23

Sergio Bertolin's avatar
Sergio Bertolin committed
24 25 26 27 28 29 30 31
//class CommentsContext implements \Behat\Behat\Context\Context {

trait Comments {
	use Sharing;
	// /** @var string */
	// private $baseUrl;
	// /** @var array */
	// private $response;
32 33 34 35 36
	/** @var int */
	private $commentId;
	/** @var int */
	private $fileId;

Sergio Bertolin's avatar
Sergio Bertolin committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	// /**
	//  * @param string $baseUrl
	//  */
	// public function __construct($baseUrl) {
	// 	$this->baseUrl = $baseUrl;

	// 	// in case of ci deployment we take the server url from the environment
	// 	$testServerUrl = getenv('TEST_SERVER_URL');
	// 	if ($testServerUrl !== false) {
	// 		$this->baseUrl = substr($testServerUrl, 0, -5);
	// 	}
	// }

	// /** @AfterScenario */
	// public function teardownScenario() {
	// 	$client = new \GuzzleHttp\Client();
	// 	try {
	// 		$client->delete(
	// 			$this->baseUrl.'/remote.php/webdav/myFileToComment.txt',
	// 			[
	// 				'auth' => [
	// 					'user0',
	// 					'123456',
	// 				],
	// 				'headers' => [
	// 					'Content-Type' => 'application/json',
	// 				],
	// 			]
	// 		);
	// 	} catch (\GuzzleHttp\Exception\ClientException $e) {
	// 		$e->getResponse();
	// 	}
	// }
70 71

	/**
Joas Schilling's avatar
Joas Schilling committed
72
	 * @param string $path
73 74 75 76
	 * @return int
	 */
	private function getFileIdForPath($path) {
		$url = $this->baseUrl.'/remote.php/webdav/'.$path;
Thomas Müller's avatar
Thomas Müller committed
77 78
		$context = stream_context_create([
			'http' => [
79 80 81 82 83 84 85 86
				'method' => 'PROPFIND',
				'header' => "Authorization: Basic dXNlcjA6MTIzNDU2\r\nContent-Type: application/x-www-form-urlencoded",
				'content' => '<?xml version="1.0"?>
<d:propfind  xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
  <d:prop>
    <oc:fileid />
  </d:prop>
</d:propfind>'
Thomas Müller's avatar
Thomas Müller committed
87 88
			]
		]);
89 90 91 92 93 94 95 96

		$response = file_get_contents($url, false, $context);
		preg_match_all('/\<oc:fileid\>(.*)\<\/oc:fileid\>/', $response, $matches);
		return (int)$matches[1][0];
	}

	/**
	 * @When :user posts a comment with content :content on the file named :fileName it should return :statusCode
Joas Schilling's avatar
Joas Schilling committed
97 98 99 100 101
	 * @param string $user
	 * @param string $content
	 * @param string $fileName
	 * @param int $statusCode
	 * @throws \Exception
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	 */
	public function postsACommentWithContentOnTheFileNamedItShouldReturn($user, $content, $fileName, $statusCode)  {
		$fileId = $this->getFileIdForPath($fileName);
		$this->fileId = (int)$fileId;
		$url = $this->baseUrl.'/remote.php/dav/comments/files/'.$fileId.'/';

		$client = new \GuzzleHttp\Client();
		try {
			$res = $client->post(
				$url,
				[
					'body' => '{"actorId":"user0","actorDisplayName":"user0","actorType":"users","verb":"comment","message":"' . $content . '","creationDateTime":"Thu, 18 Feb 2016 17:04:18 GMT","objectType":"files"}',
					'auth' => [
						$user,
						'123456',
					],
					'headers' => [
						'Content-Type' => 'application/json',
					],
				]
			);
		} catch (\GuzzleHttp\Exception\ClientException $e) {
			$res = $e->getResponse();
		}

		if($res->getStatusCode() !== (int)$statusCode) {
			throw new \Exception("Response status code was not $statusCode (".$res->getStatusCode().")");
		}
	}


	/**
	 * @Then As :user load all the comments of the file named :fileName it should return :statusCode
Joas Schilling's avatar
Joas Schilling committed
135 136 137 138
	 * @param string $user
	 * @param string $fileName
	 * @param int $statusCode
	 * @throws \Exception
139 140 141 142 143 144 145 146 147 148 149 150
	 */
	public function asLoadloadAllTheCommentsOfTheFileNamedItShouldReturn($user, $fileName, $statusCode) {
		$fileId = $this->getFileIdForPath($fileName);
		$url = $this->baseUrl.'/remote.php/dav/comments/files/'.$fileId.'/';

		try {
			$client = new \GuzzleHttp\Client();
			$res = $client->createRequest(
				'REPORT',
				$url,
				[
					'body' => '<?xml version="1.0" encoding="utf-8" ?>
Joas Schilling's avatar
Joas Schilling committed
151
<oc:filter-comments xmlns:oc="http://owncloud.org/ns">
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    <oc:limit>200</oc:limit>
    <oc:offset>0</oc:offset>
</oc:filter-comments>
',
					'auth' => [
						$user,
						'123456',
					],
					'headers' => [
						'Content-Type' => 'application/json',
					],
				]
			);
			$res = $client->send($res);
		} catch (\GuzzleHttp\Exception\ClientException $e) {
			$res = $e->getResponse();
		}

		if($res->getStatusCode() !== (int)$statusCode) {
			throw new \Exception("Response status code was not $statusCode (".$res->getStatusCode().")");
		}

		if($res->getStatusCode() === 207) {
			$service = new Sabre\Xml\Service();
			$this->response = $service->parse($res->getBody()->getContents());
			$this->commentId = (int)$this->response[0]['value'][2]['value'][0]['value'][0]['value'];
		}
	}

Sergio Bertolin's avatar
Sergio Bertolin committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	// /**
	//  * @Given As :user sending :verb to :url with
	//  * @param string $user
	//  * @param string $verb
	//  * @param string $url
	//  * @param \Behat\Gherkin\Node\TableNode $body
	//  * @throws \Exception
	//  */
	// public function asUserSendingToWith($user, $verb, $url, \Behat\Gherkin\Node\TableNode $body) {
	// 	$client = new \GuzzleHttp\Client();
	// 	$options = [];
	// 	$options['auth'] = [$user, '123456'];
	// 	$fd = $body->getRowsHash();
	// 	$options['body'] = $fd;
	// 	$client->send($client->createRequest($verb, $this->baseUrl.'/ocs/v1.php/'.$url, $options));
	// }
197 198 199

	/**
	 * @Then As :user delete the created comment it should return :statusCode
Joas Schilling's avatar
Joas Schilling committed
200 201 202
	 * @param string $user
	 * @param int $statusCode
	 * @throws \Exception
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	 */
	public function asDeleteTheCreatedCommentItShouldReturn($user, $statusCode) {
		$url = $this->baseUrl.'/remote.php/dav/comments/files/'.$this->fileId.'/'.$this->commentId;

		$client = new \GuzzleHttp\Client();
		try {
			$res = $client->delete(
				$url,
				[
					'auth' => [
						$user,
						'123456',
					],
					'headers' => [
						'Content-Type' => 'application/json',
					],
				]
			);
		} catch (\GuzzleHttp\Exception\ClientException $e) {
			$res = $e->getResponse();
		}

		if($res->getStatusCode() !== (int)$statusCode) {
			throw new \Exception("Response status code was not $statusCode (".$res->getStatusCode().")");
		}
	}

	/**
	 * @Then the response should contain a property :key with value :value
Joas Schilling's avatar
Joas Schilling committed
232 233 234
	 * @param string $key
	 * @param string $value
	 * @throws \Exception
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
	 */
	public function theResponseShouldContainAPropertyWithValue($key, $value) {
		$keys = $this->response[0]['value'][2]['value'][0]['value'];
		$found = false;
		foreach($keys as $singleKey) {
			if($singleKey['name'] === '{http://owncloud.org/ns}'.substr($key, 3)) {
				if($singleKey['value'] === $value) {
					$found = true;
				}
			}
		}
		if($found === false) {
			throw new \Exception("Cannot find property $key with $value");
		}
	}

	/**
	 * @Then the response should contain only :number comments
Joas Schilling's avatar
Joas Schilling committed
253 254
	 * @param int $number
	 * @throws \Exception
255 256 257 258 259 260 261 262 263
	 */
	public function theResponseShouldContainOnlyComments($number) {
		if(count($this->response) !== (int)$number) {
			throw new \Exception("Found more comments than $number (".count($this->response).")");
		}
	}

	/**
	 * @Then As :user edit the last created comment and set text to :text it should return :statusCode
Joas Schilling's avatar
Joas Schilling committed
264 265 266 267
	 * @param string $user
	 * @param string $text
	 * @param int $statusCode
	 * @throws \Exception
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
	 */
	public function asEditTheLastCreatedCommentAndSetTextToItShouldReturn($user, $text, $statusCode) {
		$client = new \GuzzleHttp\Client();
		$options = [];
		$options['auth'] = [$user, '123456'];
		$options['body'] = '<?xml version="1.0"?>
<d:propertyupdate  xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
  <d:set>
   <d:prop>
      <oc:message>'.$text.'</oc:message>
    </d:prop>
  </d:set>
</d:propertyupdate>';
		try {
			$res = $client->send($client->createRequest('PROPPATCH', $this->baseUrl.'/remote.php/dav/comments/files/' . $this->fileId . '/' . $this->commentId, $options));
		} catch (\GuzzleHttp\Exception\ClientException $e) {
			$res = $e->getResponse();
		}

		if($res->getStatusCode() !== (int)$statusCode) {
			throw new \Exception("Response status code was not $statusCode (".$res->getStatusCode().")");
		}
	}


}