Comments.php 6.96 KB
Newer Older
1 2 3 4
<?php
/**
 * @author Lukas Reschke <lukas@owncloud.com>
 *
5
 * @copyright Copyright (c) 2017, ownCloud, Gmbh.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * @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
//class CommentsContext implements \Behat\Behat\Context\Context {

trait Comments {
	use Sharing;
28

29
	/** @var int */
30 31 32
	private $lastCommentId;
	/** @var int */
	private $lastFileId;
33 34

	/**
Joas Schilling's avatar
Joas Schilling committed
35
	 * @param string $path
36 37
	 * @return int
	 */
38 39 40 41
	private function getFileIdForPath($user, $path) {
		$propertiesTable = new \Behat\Gherkin\Node\TableNode([["{http://owncloud.org/ns}fileid"]]);
		$this->asGetsPropertiesOfFolderWith($user, 'file', $path, $propertiesTable);
		return (int) $this->response['{http://owncloud.org/ns}fileid'];
42 43 44
	}

	/**
45
	 * @When /^user "([^"]*)" comments with content "([^"]*)" on (file|folder) "([^"]*)"$/
Joas Schilling's avatar
Joas Schilling committed
46 47
	 * @param string $user
	 * @param string $content
48 49
	 * @param string $type
	 * @param string $path
Joas Schilling's avatar
Joas Schilling committed
50
	 * @throws \Exception
51
	 */
52 53
	public function postsAComment($user, $content, $type, $path) {
		$fileId = $this->getFileIdForPath($user, $path);
54
		$this->lastFileId = $fileId;
55
		$commentsPath = '/comments/files/' . $fileId . '/';
56
		try {
57 58 59 60 61 62 63 64 65 66 67 68 69 70
			$this->response = $this->makeDavRequest($user,
								  "POST",
								  $commentsPath,
								  ['Content-Type' => 'application/json',
								   ],
								    null,
								   "uploads",
								   '{"actorId":"user0",
								    "actorDisplayName":"user0",
								    "actorType":"users",
								    "verb":"comment",
								    "message":"' . $content . '",
								    "creationDateTime":"Thu, 18 Feb 2016 17:04:18 GMT",
								    "objectType":"files"}');
71 72 73
			$responseHeaders =  $this->response->getHeaders();
			$commentUrl = $responseHeaders['Content-Location'][0];
			$this->lastCommentId = substr($commentUrl, strrpos($commentUrl,'/')+1);
74 75
		} catch (\GuzzleHttp\Exception\ClientException $ex) {
			$this->response = $ex->getResponse();
76 77 78
		}
	}

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	/**
	 * @Then /^user "([^"]*)" should have the following comments on (file|folder) "([^"]*)"$/
	 * @param string $user
	 * @param string $type
	 * @param string $path
	 * @param \Behat\Gherkin\Node\TableNode|null $expectedElements
	 */
	public function checkComments($user, $type, $path, $expectedElements){
		$fileId = $this->getFileIdForPath($user, $path);
		$commentsPath = '/comments/files/' . $fileId . '/';

		$properties = '<oc:limit>200</oc:limit><oc:offset>0</oc:offset>';

		try{
			$elementList = $this->reportElementComments($user,$commentsPath,$properties);
			print_r($elementList);
		} catch (\GuzzleHttp\Exception\ClientException $e) {
			$this->response = $e->getResponse();
			return 1;
		}

		if ($expectedElements instanceof \Behat\Gherkin\Node\TableNode) {
			$elementRows = $expectedElements->getRows();
			foreach($elementRows as $expectedElement) {
				$commentFound = false;
				foreach ($elementList as $id => $answer) {
					if  (($answer[200]['{http://owncloud.org/ns}actorDisplayName'] === $expectedElement[0]) and
						($answer[200]['{http://owncloud.org/ns}message'] === $expectedElement[1])){
						$commentFound = true;
					}
				}
				PHPUnit_Framework_Assert::assertTrue($commentFound, "Comment not found");
			}
		}
	}
114 115

	/**
116
	 * @Then /^user "([^"]*)" should have (\d+) comments on (file|folder) "([^"]*)"$/
Joas Schilling's avatar
Joas Schilling committed
117
	 * @param string $user
118 119 120
	 * @param string $numberOfComments
	 * @param string $type
	 * @param string $path
121
	 */
122 123 124 125 126 127 128
	public function checkNumberOfComments($user, $numberOfComments, $type, $path) {
		$fileId = $this->getFileIdForPath($user, $path);
		$commentsPath = '/comments/files/' . $fileId . '/';
		$properties = '<oc:limit>200</oc:limit><oc:offset>0</oc:offset>';
		try{
			$elementList = $this->reportElementComments($user,$commentsPath,$properties);
			PHPUnit_Framework_Assert::assertEquals($numberOfComments, count($elementList));
129
		} catch (\GuzzleHttp\Exception\ClientException $e) {
130
			$this->response = $e->getResponse();
131
		}
132
	}
133

134 135 136 137 138 139 140 141 142 143 144 145
	public function deleteComment($user, $id){
		$commentsPath = '/comments/files/' . $this->lastFileId . '/' . $this->lastCommentId;
		try {
			$this->response = $this->makeDavRequest($user,
													"DELETE",
													$commentsPath,
													[],
													null,
													"uploads",
													null);
		} catch (\GuzzleHttp\Exception\ClientException $ex) {
			$this->response = $ex->getResponse();
146 147 148
		}
	}

149 150 151 152 153 154 155 156 157
	/**
	 * @Then user :user deletes the last created comment
	 * @param string $user
	 * @throws \Exception
	 */
	public function userDeletesLastComment($user) {
		$this->deleteComment($user, $this->lastCommentId);
	}

158 159
	/**
	 * @Then the response should contain a property :key with value :value
Joas Schilling's avatar
Joas Schilling committed
160 161 162
	 * @param string $key
	 * @param string $value
	 * @throws \Exception
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
	 */
	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
181 182
	 * @param int $number
	 * @throws \Exception
183 184 185 186 187 188 189 190 191
	 */
	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
192 193 194 195
	 * @param string $user
	 * @param string $text
	 * @param int $statusCode
	 * @throws \Exception
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
	 */
	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().")");
		}
	}

}