CommentsPropertiesPluginTest.php 9.58 KB
Newer Older
Arthur Schiwon's avatar
Arthur Schiwon committed
1 2
<?php
/**
Lukas Reschke's avatar
Lukas Reschke committed
3
 * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
Thomas Müller's avatar
Thomas Müller committed
4
 * @author Joas Schilling <coding@schilljs.com>
Thomas Müller's avatar
Thomas Müller committed
5
 * @author Thomas Müller <thomas.mueller@tmit.eu>
Arthur Schiwon's avatar
Arthur Schiwon committed
6
 *
Thomas Müller's avatar
Thomas Müller committed
7
 * @copyright Copyright (c) 2017, ownCloud GmbH
Arthur Schiwon's avatar
Arthur Schiwon committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * @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/>
 *
 */

24
namespace OCA\DAV\Tests\unit\Connector\Sabre;
Arthur Schiwon's avatar
Arthur Schiwon committed
25 26 27

use \OCA\DAV\Connector\Sabre\CommentPropertiesPlugin as CommentPropertiesPluginImplementation;

28
class CommentsPropertiesPluginTest extends \Test\TestCase {
Arthur Schiwon's avatar
Arthur Schiwon committed
29 30 31 32 33 34

	/** @var  CommentPropertiesPluginImplementation */
	protected $plugin;
	protected $commentsManager;
	protected $userSession;
	protected $server;
35
	protected $userFolder;
Arthur Schiwon's avatar
Arthur Schiwon committed
36 37 38 39

	public function setUp() {
		parent::setUp();

Thomas Müller's avatar
Thomas Müller committed
40 41
		$this->commentsManager = $this->createMock('\OCP\Comments\ICommentsManager');
		$this->userSession = $this->createMock('\OCP\IUserSession');
Arthur Schiwon's avatar
Arthur Schiwon committed
42

43 44
		$this->userFolder = $this->createMock('\OCP\Files\Folder');

Arthur Schiwon's avatar
Arthur Schiwon committed
45 46 47 48
		$this->server = $this->getMockBuilder('\Sabre\DAV\Server')
			->disableOriginalConstructor()
			->getMock();

49
		$this->plugin = new CommentPropertiesPluginImplementation($this->commentsManager, $this->userSession, $this->userFolder);
Arthur Schiwon's avatar
Arthur Schiwon committed
50 51 52
		$this->plugin->initialize($this->server);
	}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
	public function testHandleGetPropertiesUser() {
		$sabreNode1 = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\File')
			->disableOriginalConstructor()
			->getMock();
		$sabreNode1->expects($this->any())
			->method('getId')
			->will($this->returnValue(111));
		$sabreNode1->expects($this->never())
			->method('getPath');
		$sabreNode2 = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\File')
			->disableOriginalConstructor()
			->getMock();
		$sabreNode2->expects($this->any())
			->method('getId')
			->will($this->returnValue(222));
		$sabreNode2->expects($this->never())
			->method('getPath');

		$sabreNode = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\Directory')
			->disableOriginalConstructor()
			->getMock();
		$sabreNode->expects($this->any())
			->method('getId')
			->will($this->returnValue(123));
		$sabreNode->expects($this->once())
			->method('getChildren')
		->will($this->returnValue([$sabreNode1, $sabreNode2]));
		$sabreNode->expects($this->any())
			->method('getPath')
			->will($this->returnValue('/subdir'));

		// simulate sabre recursive PROPFIND traversal
		$propFindRoot = new \Sabre\DAV\PropFind(
			'/subdir',
			[CommentPropertiesPluginImplementation::PROPERTY_NAME_UNREAD],
			1
		);

		$propFind1 = new \Sabre\DAV\PropFind(
			'/subdir/test.txt',
			[CommentPropertiesPluginImplementation::PROPERTY_NAME_UNREAD],
			0
		);
		$propFind2 = new \Sabre\DAV\PropFind(
			'/subdir/test2.txt',
			[CommentPropertiesPluginImplementation::PROPERTY_NAME_UNREAD],
			0
		);

		// Add some comments there
		$numberOfCommentsForNodes[$sabreNode1->getId()] = 1;
		$numberOfCommentsForNodes[$sabreNode2->getId()] = 4;
		$this->commentsManager->expects($this->once())
			->method('getNumberOfUnreadCommentsForNodes')
			->willReturn($numberOfCommentsForNodes);

		// Now test with user
		$this->userSession->expects($this->any())
			->method('getUser')
			->willReturn($this->createMock('\OCP\IUser'));

		$this->plugin->handleGetProperties(
			$propFindRoot,
			$sabreNode
		);
		$this->plugin->handleGetProperties(
			$propFind1,
			$sabreNode1
		);
		$this->plugin->handleGetProperties(
			$propFind2,
			$sabreNode2
		);

		$result = $propFindRoot->getResultForMultiStatus();
		$result1 = $propFind1->getResultForMultiStatus();
		$result2 = $propFind2->getResultForMultiStatus();
		$this->assertEmpty($result[404]);
		$this->assertEmpty($result1[404]);
		$this->assertEmpty($result2[404]);
		$this->assertEquals(0, $result[200][CommentPropertiesPluginImplementation::PROPERTY_NAME_UNREAD]);
		$this->assertEquals(1, $result1[200][CommentPropertiesPluginImplementation::PROPERTY_NAME_UNREAD]);
		$this->assertEquals(4, $result2[200][CommentPropertiesPluginImplementation::PROPERTY_NAME_UNREAD]);
	}

	public function testHandleGetPropertiesUserLoggedOut() {
		$sabreNode1 = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\File')
			->disableOriginalConstructor()
			->getMock();
		$sabreNode1->expects($this->any())
			->method('getId')
			->will($this->returnValue(111));
		$sabreNode1->expects($this->never())
			->method('getPath');
		$sabreNode2 = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\File')
			->disableOriginalConstructor()
			->getMock();
		$sabreNode2->expects($this->any())
			->method('getId')
			->will($this->returnValue(222));
		$sabreNode2->expects($this->never())
			->method('getPath');

		$sabreNode = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\Directory')
			->disableOriginalConstructor()
			->getMock();
		$sabreNode->expects($this->any())
			->method('getId')
			->will($this->returnValue(123));
		$sabreNode->expects($this->once())
			->method('getChildren')
			->will($this->returnValue([$sabreNode1, $sabreNode2]));
		$sabreNode->expects($this->any())
			->method('getPath')
			->will($this->returnValue('/subdir'));
		// simulate sabre recursive PROPFIND traversal
		$propFindRoot = new \Sabre\DAV\PropFind(
			'/subdir',
			[CommentPropertiesPluginImplementation::PROPERTY_NAME_UNREAD],
			1
		);

		$propFind1 = new \Sabre\DAV\PropFind(
			'/subdir/test.txt',
			[CommentPropertiesPluginImplementation::PROPERTY_NAME_UNREAD],
			0
		);
		$propFind2 = new \Sabre\DAV\PropFind(
			'/subdir/test2.txt',
			[CommentPropertiesPluginImplementation::PROPERTY_NAME_UNREAD],
			0
		);

		$this->plugin->handleGetProperties(
			$propFindRoot,
			$sabreNode
		);
		$this->plugin->handleGetProperties(
			$propFind1,
			$sabreNode1
		);
		$this->plugin->handleGetProperties(
			$propFind2,
			$sabreNode2
		);

		$result = $propFindRoot->getResultForMultiStatus();
		$this->assertEmpty($result[404]);
		$this->assertEquals(0, $result[200][CommentPropertiesPluginImplementation::PROPERTY_NAME_UNREAD]);
	}


Arthur Schiwon's avatar
Arthur Schiwon committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
	public function nodeProvider() {
		$mocks = [];
		foreach(['\OCA\DAV\Connector\Sabre\File', '\OCA\DAV\Connector\Sabre\Directory', '\Sabre\DAV\INode'] as $class) {
			$mocks[] = 	$this->getMockBuilder($class)
				->disableOriginalConstructor()
				->getMock();
		}

		return [
			[$mocks[0], true],
			[$mocks[1], true],
			[$mocks[2], false]
		];
	}

	/**
	 * @dataProvider nodeProvider
	 * @param $node
	 * @param $expectedSuccessful
	 */
225
	public function testHandleGetPropertiesCorrectNode($node, $expectedSuccessful) {
Arthur Schiwon's avatar
Arthur Schiwon committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
		$propFind = $this->getMockBuilder('\Sabre\DAV\PropFind')
			->disableOriginalConstructor()
			->getMock();

		if($expectedSuccessful) {
			$propFind->expects($this->exactly(3))
				->method('handle');
		} else {
			$propFind->expects($this->never())
				->method('handle');
		}

		$this->plugin->handleGetProperties($propFind, $node);
	}

	public function baseUriProvider() {
		return [
			['owncloud/remote.php/webdav/', '4567', 'owncloud/remote.php/dav/comments/files/4567'],
244 245
			['owncloud/remote.php/files/', '4567', 'owncloud/remote.php/dav/comments/files/4567'],
			['owncloud/wicked.php/files/', '4567', null]
Arthur Schiwon's avatar
Arthur Schiwon committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
		];
	}

	/**
	 * @dataProvider baseUriProvider
	 * @param $baseUri
	 * @param $fid
	 * @param $expectedHref
	 */
	public function testGetCommentsLink($baseUri, $fid, $expectedHref) {
		$node = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\File')
			->disableOriginalConstructor()
			->getMock();
		$node->expects($this->any())
			->method('getId')
			->will($this->returnValue($fid));

		$this->server->expects($this->once())
			->method('getBaseUri')
			->will($this->returnValue($baseUri));

		$href = $this->plugin->getCommentsLink($node);
		$this->assertSame($expectedHref, $href);
	}

	public function userProvider() {
		return [
Thomas Müller's avatar
Thomas Müller committed
273
			[$this->createMock('\OCP\IUser')],
Arthur Schiwon's avatar
Arthur Schiwon committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
			[null]
		];
	}

	/**
	 * @dataProvider userProvider
	 * @param $user
	 */
	public function testGetUnreadCount($user) {
		$node = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\File')
			->disableOriginalConstructor()
			->getMock();
		$node->expects($this->any())
			->method('getId')
			->will($this->returnValue('4567'));

		$this->userSession->expects($this->once())
			->method('getUser')
			->will($this->returnValue($user));

294
		$numberOfCommentsForNodes[$node->getId()] = 42;
Arthur Schiwon's avatar
Arthur Schiwon committed
295
		$this->commentsManager->expects($this->any())
296 297
			->method('getNumberOfUnreadCommentsForNodes')
			->willReturn($numberOfCommentsForNodes);
Arthur Schiwon's avatar
Arthur Schiwon committed
298 299 300 301 302 303 304 305 306

		$unread = $this->plugin->getUnreadCount($node);
		if(is_null($user)) {
			$this->assertNull($unread);
		} else {
			$this->assertSame($unread, 42);
		}
	}

Piotr M's avatar
Piotr M committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	/**
	 * @dataProvider userProvider
	 * @param $user
	 */
	public function testGetUnreadCountWithZeroUnread($user) {
		$node = $this->getMockBuilder('\OCA\DAV\Connector\Sabre\File')
			->disableOriginalConstructor()
			->getMock();
		$node->expects($this->any())
			->method('getId')
			->will($this->returnValue('4567'));

		$this->userSession->expects($this->once())
			->method('getUser')
			->will($this->returnValue($user));

		$numberOfCommentsForNodes = [];
		$this->commentsManager->expects($this->any())
			->method('getNumberOfUnreadCommentsForNodes')
			->willReturn($numberOfCommentsForNodes);

		$unread = $this->plugin->getUnreadCount($node);
		if(is_null($user)) {
			$this->assertNull($unread);
		} else {
			$this->assertSame($unread, 0);
		}
	}

Arthur Schiwon's avatar
Arthur Schiwon committed
336
}