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.

SystemTagPluginTest.php 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Vincent Petry <vincent@nextcloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\DAV\Tests\unit\SystemTag;
  29. use OC\SystemTag\SystemTag;
  30. use OCA\DAV\SystemTag\SystemTagNode;
  31. use OCA\DAV\SystemTag\SystemTagsByIdCollection;
  32. use OCA\DAV\SystemTag\SystemTagsObjectMappingCollection;
  33. use OCP\IGroupManager;
  34. use OCP\IUser;
  35. use OCP\IUserSession;
  36. use OCP\SystemTag\ISystemTag;
  37. use OCP\SystemTag\ISystemTagManager;
  38. use OCP\SystemTag\TagAlreadyExistsException;
  39. use Sabre\DAV\Tree;
  40. use Sabre\HTTP\RequestInterface;
  41. use Sabre\HTTP\ResponseInterface;
  42. class SystemTagPluginTest extends \Test\TestCase {
  43. public const ID_PROPERTYNAME = \OCA\DAV\SystemTag\SystemTagPlugin::ID_PROPERTYNAME;
  44. public const DISPLAYNAME_PROPERTYNAME = \OCA\DAV\SystemTag\SystemTagPlugin::DISPLAYNAME_PROPERTYNAME;
  45. public const USERVISIBLE_PROPERTYNAME = \OCA\DAV\SystemTag\SystemTagPlugin::USERVISIBLE_PROPERTYNAME;
  46. public const USERASSIGNABLE_PROPERTYNAME = \OCA\DAV\SystemTag\SystemTagPlugin::USERASSIGNABLE_PROPERTYNAME;
  47. public const CANASSIGN_PROPERTYNAME = \OCA\DAV\SystemTag\SystemTagPlugin::CANASSIGN_PROPERTYNAME;
  48. public const GROUPS_PROPERTYNAME = \OCA\DAV\SystemTag\SystemTagPlugin::GROUPS_PROPERTYNAME;
  49. /**
  50. * @var \Sabre\DAV\Server
  51. */
  52. private $server;
  53. /**
  54. * @var \Sabre\DAV\Tree
  55. */
  56. private $tree;
  57. /**
  58. * @var \OCP\SystemTag\ISystemTagManager
  59. */
  60. private $tagManager;
  61. /**
  62. * @var IGroupManager
  63. */
  64. private $groupManager;
  65. /**
  66. * @var IUserSession
  67. */
  68. private $userSession;
  69. /**
  70. * @var IUser
  71. */
  72. private $user;
  73. /**
  74. * @var \OCA\DAV\SystemTag\SystemTagPlugin
  75. */
  76. private $plugin;
  77. protected function setUp(): void {
  78. parent::setUp();
  79. $this->tree = $this->getMockBuilder(Tree::class)
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. $this->server = new \Sabre\DAV\Server($this->tree);
  83. $this->tagManager = $this->getMockBuilder(ISystemTagManager::class)
  84. ->getMock();
  85. $this->groupManager = $this->getMockBuilder(IGroupManager::class)
  86. ->getMock();
  87. $this->user = $this->getMockBuilder(IUser::class)
  88. ->getMock();
  89. $this->userSession = $this->getMockBuilder(IUserSession::class)
  90. ->getMock();
  91. $this->userSession
  92. ->expects($this->any())
  93. ->method('getUser')
  94. ->willReturn($this->user);
  95. $this->userSession
  96. ->expects($this->any())
  97. ->method('isLoggedIn')
  98. ->willReturn(true);
  99. $this->plugin = new \OCA\DAV\SystemTag\SystemTagPlugin(
  100. $this->tagManager,
  101. $this->groupManager,
  102. $this->userSession
  103. );
  104. $this->plugin->initialize($this->server);
  105. }
  106. public function getPropertiesDataProvider() {
  107. return [
  108. [
  109. new SystemTag(1, 'Test', true, true),
  110. [],
  111. [
  112. self::ID_PROPERTYNAME,
  113. self::DISPLAYNAME_PROPERTYNAME,
  114. self::USERVISIBLE_PROPERTYNAME,
  115. self::USERASSIGNABLE_PROPERTYNAME,
  116. self::CANASSIGN_PROPERTYNAME,
  117. ],
  118. [
  119. self::ID_PROPERTYNAME => '1',
  120. self::DISPLAYNAME_PROPERTYNAME => 'Test',
  121. self::USERVISIBLE_PROPERTYNAME => 'true',
  122. self::USERASSIGNABLE_PROPERTYNAME => 'true',
  123. self::CANASSIGN_PROPERTYNAME => 'true',
  124. ]
  125. ],
  126. [
  127. new SystemTag(1, 'Test', true, false),
  128. [],
  129. [
  130. self::ID_PROPERTYNAME,
  131. self::DISPLAYNAME_PROPERTYNAME,
  132. self::USERVISIBLE_PROPERTYNAME,
  133. self::USERASSIGNABLE_PROPERTYNAME,
  134. self::CANASSIGN_PROPERTYNAME,
  135. ],
  136. [
  137. self::ID_PROPERTYNAME => '1',
  138. self::DISPLAYNAME_PROPERTYNAME => 'Test',
  139. self::USERVISIBLE_PROPERTYNAME => 'true',
  140. self::USERASSIGNABLE_PROPERTYNAME => 'false',
  141. self::CANASSIGN_PROPERTYNAME => 'false',
  142. ]
  143. ],
  144. [
  145. new SystemTag(1, 'Test', true, false),
  146. ['group1', 'group2'],
  147. [
  148. self::ID_PROPERTYNAME,
  149. self::GROUPS_PROPERTYNAME,
  150. ],
  151. [
  152. self::ID_PROPERTYNAME => '1',
  153. self::GROUPS_PROPERTYNAME => 'group1|group2',
  154. ]
  155. ],
  156. [
  157. new SystemTag(1, 'Test', true, true),
  158. ['group1', 'group2'],
  159. [
  160. self::ID_PROPERTYNAME,
  161. self::GROUPS_PROPERTYNAME,
  162. ],
  163. [
  164. self::ID_PROPERTYNAME => '1',
  165. // groups only returned when userAssignable is false
  166. self::GROUPS_PROPERTYNAME => '',
  167. ]
  168. ],
  169. ];
  170. }
  171. /**
  172. * @dataProvider getPropertiesDataProvider
  173. */
  174. public function testGetProperties(ISystemTag $systemTag, $groups, $requestedProperties, $expectedProperties) {
  175. $this->user->expects($this->any())
  176. ->method('getUID')
  177. ->willReturn('admin');
  178. $this->groupManager
  179. ->expects($this->any())
  180. ->method('isAdmin')
  181. ->with('admin')
  182. ->willReturn(true);
  183. $node = $this->getMockBuilder(SystemTagNode::class)
  184. ->disableOriginalConstructor()
  185. ->getMock();
  186. $node->expects($this->any())
  187. ->method('getSystemTag')
  188. ->willReturn($systemTag);
  189. $this->tagManager->expects($this->any())
  190. ->method('canUserAssignTag')
  191. ->willReturn($systemTag->isUserAssignable());
  192. $this->tagManager->expects($this->any())
  193. ->method('getTagGroups')
  194. ->willReturn($groups);
  195. $this->tree->expects($this->any())
  196. ->method('getNodeForPath')
  197. ->with('/systemtag/1')
  198. ->willReturn($node);
  199. $propFind = new \Sabre\DAV\PropFind(
  200. '/systemtag/1',
  201. $requestedProperties,
  202. 0
  203. );
  204. $this->plugin->handleGetProperties(
  205. $propFind,
  206. $node
  207. );
  208. $result = $propFind->getResultForMultiStatus();
  209. $this->assertEmpty($result[404]);
  210. $this->assertEquals($expectedProperties, $result[200]);
  211. }
  212. public function testGetPropertiesForbidden() {
  213. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  214. $systemTag = new SystemTag(1, 'Test', true, false);
  215. $requestedProperties = [
  216. self::ID_PROPERTYNAME,
  217. self::GROUPS_PROPERTYNAME,
  218. ];
  219. $this->user->expects($this->once())
  220. ->method('getUID')
  221. ->willReturn('admin');
  222. $this->groupManager
  223. ->expects($this->once())
  224. ->method('isAdmin')
  225. ->with('admin')
  226. ->willReturn(false);
  227. $node = $this->getMockBuilder(SystemTagNode::class)
  228. ->disableOriginalConstructor()
  229. ->getMock();
  230. $node->expects($this->any())
  231. ->method('getSystemTag')
  232. ->willReturn($systemTag);
  233. $this->tree->expects($this->any())
  234. ->method('getNodeForPath')
  235. ->with('/systemtag/1')
  236. ->willReturn($node);
  237. $propFind = new \Sabre\DAV\PropFind(
  238. '/systemtag/1',
  239. $requestedProperties,
  240. 0
  241. );
  242. $this->plugin->handleGetProperties(
  243. $propFind,
  244. $node
  245. );
  246. }
  247. public function testUpdatePropertiesAdmin() {
  248. $systemTag = new SystemTag(1, 'Test', true, false);
  249. $this->user->expects($this->any())
  250. ->method('getUID')
  251. ->willReturn('admin');
  252. $this->groupManager
  253. ->expects($this->any())
  254. ->method('isAdmin')
  255. ->with('admin')
  256. ->willReturn(true);
  257. $node = $this->getMockBuilder(SystemTagNode::class)
  258. ->disableOriginalConstructor()
  259. ->getMock();
  260. $node->expects($this->any())
  261. ->method('getSystemTag')
  262. ->willReturn($systemTag);
  263. $this->tree->expects($this->any())
  264. ->method('getNodeForPath')
  265. ->with('/systemtag/1')
  266. ->willReturn($node);
  267. $node->expects($this->once())
  268. ->method('update')
  269. ->with('Test changed', false, true);
  270. $this->tagManager->expects($this->once())
  271. ->method('setTagGroups')
  272. ->with($systemTag, ['group1', 'group2']);
  273. // properties to set
  274. $propPatch = new \Sabre\DAV\PropPatch([
  275. self::DISPLAYNAME_PROPERTYNAME => 'Test changed',
  276. self::USERVISIBLE_PROPERTYNAME => 'false',
  277. self::USERASSIGNABLE_PROPERTYNAME => 'true',
  278. self::GROUPS_PROPERTYNAME => 'group1|group2',
  279. ]);
  280. $this->plugin->handleUpdateProperties(
  281. '/systemtag/1',
  282. $propPatch
  283. );
  284. $propPatch->commit();
  285. // all requested properties removed, as they were processed already
  286. $this->assertEmpty($propPatch->getRemainingMutations());
  287. $result = $propPatch->getResult();
  288. $this->assertEquals(200, $result[self::DISPLAYNAME_PROPERTYNAME]);
  289. $this->assertEquals(200, $result[self::USERASSIGNABLE_PROPERTYNAME]);
  290. $this->assertEquals(200, $result[self::USERVISIBLE_PROPERTYNAME]);
  291. }
  292. public function testUpdatePropertiesForbidden() {
  293. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  294. $systemTag = new SystemTag(1, 'Test', true, false);
  295. $this->user->expects($this->any())
  296. ->method('getUID')
  297. ->willReturn('admin');
  298. $this->groupManager
  299. ->expects($this->any())
  300. ->method('isAdmin')
  301. ->with('admin')
  302. ->willReturn(false);
  303. $node = $this->getMockBuilder(SystemTagNode::class)
  304. ->disableOriginalConstructor()
  305. ->getMock();
  306. $node->expects($this->any())
  307. ->method('getSystemTag')
  308. ->willReturn($systemTag);
  309. $this->tree->expects($this->any())
  310. ->method('getNodeForPath')
  311. ->with('/systemtag/1')
  312. ->willReturn($node);
  313. $node->expects($this->never())
  314. ->method('update');
  315. $this->tagManager->expects($this->never())
  316. ->method('setTagGroups');
  317. // properties to set
  318. $propPatch = new \Sabre\DAV\PropPatch([
  319. self::GROUPS_PROPERTYNAME => 'group1|group2',
  320. ]);
  321. $this->plugin->handleUpdateProperties(
  322. '/systemtag/1',
  323. $propPatch
  324. );
  325. $propPatch->commit();
  326. }
  327. public function createTagInsufficientPermissionsProvider() {
  328. return [
  329. [true, false, ''],
  330. [false, true, ''],
  331. [true, true, 'group1|group2'],
  332. ];
  333. }
  334. /**
  335. * @dataProvider createTagInsufficientPermissionsProvider
  336. */
  337. public function testCreateNotAssignableTagAsRegularUser($userVisible, $userAssignable, $groups) {
  338. $this->expectException(\Sabre\DAV\Exception\BadRequest::class);
  339. $this->expectExceptionMessage('Not sufficient permissions');
  340. $this->user->expects($this->once())
  341. ->method('getUID')
  342. ->willReturn('admin');
  343. $this->groupManager
  344. ->expects($this->once())
  345. ->method('isAdmin')
  346. ->with('admin')
  347. ->willReturn(false);
  348. $requestData = [
  349. 'name' => 'Test',
  350. 'userVisible' => $userVisible,
  351. 'userAssignable' => $userAssignable,
  352. ];
  353. if (!empty($groups)) {
  354. $requestData['groups'] = $groups;
  355. }
  356. $requestData = json_encode($requestData);
  357. $node = $this->getMockBuilder(SystemTagsByIdCollection::class)
  358. ->disableOriginalConstructor()
  359. ->getMock();
  360. $this->tagManager->expects($this->never())
  361. ->method('createTag');
  362. $this->tagManager->expects($this->never())
  363. ->method('setTagGroups');
  364. $this->tree->expects($this->any())
  365. ->method('getNodeForPath')
  366. ->with('/systemtags')
  367. ->willReturn($node);
  368. $request = $this->getMockBuilder(RequestInterface::class)
  369. ->disableOriginalConstructor()
  370. ->getMock();
  371. $response = $this->getMockBuilder(ResponseInterface::class)
  372. ->disableOriginalConstructor()
  373. ->getMock();
  374. $request->expects($this->once())
  375. ->method('getPath')
  376. ->willReturn('/systemtags');
  377. $request->expects($this->once())
  378. ->method('getBodyAsString')
  379. ->willReturn($requestData);
  380. $request->expects($this->once())
  381. ->method('getHeader')
  382. ->with('Content-Type')
  383. ->willReturn('application/json');
  384. $this->plugin->httpPost($request, $response);
  385. }
  386. public function testCreateTagInByIdCollectionAsRegularUser() {
  387. $systemTag = new SystemTag(1, 'Test', true, false);
  388. $requestData = json_encode([
  389. 'name' => 'Test',
  390. 'userVisible' => true,
  391. 'userAssignable' => true,
  392. ]);
  393. $node = $this->getMockBuilder(SystemTagsByIdCollection::class)
  394. ->disableOriginalConstructor()
  395. ->getMock();
  396. $this->tagManager->expects($this->once())
  397. ->method('createTag')
  398. ->with('Test', true, true)
  399. ->willReturn($systemTag);
  400. $this->tree->expects($this->any())
  401. ->method('getNodeForPath')
  402. ->with('/systemtags')
  403. ->willReturn($node);
  404. $request = $this->getMockBuilder(RequestInterface::class)
  405. ->disableOriginalConstructor()
  406. ->getMock();
  407. $response = $this->getMockBuilder(ResponseInterface::class)
  408. ->disableOriginalConstructor()
  409. ->getMock();
  410. $request->expects($this->once())
  411. ->method('getPath')
  412. ->willReturn('/systemtags');
  413. $request->expects($this->once())
  414. ->method('getBodyAsString')
  415. ->willReturn($requestData);
  416. $request->expects($this->once())
  417. ->method('getHeader')
  418. ->with('Content-Type')
  419. ->willReturn('application/json');
  420. $request->expects($this->once())
  421. ->method('getUrl')
  422. ->willReturn('http://example.com/dav/systemtags');
  423. $response->expects($this->once())
  424. ->method('setHeader')
  425. ->with('Content-Location', 'http://example.com/dav/systemtags/1');
  426. $this->plugin->httpPost($request, $response);
  427. }
  428. public function createTagProvider() {
  429. return [
  430. [true, false, ''],
  431. [false, false, ''],
  432. [true, false, 'group1|group2'],
  433. ];
  434. }
  435. /**
  436. * @dataProvider createTagProvider
  437. */
  438. public function testCreateTagInByIdCollection($userVisible, $userAssignable, $groups) {
  439. $this->user->expects($this->once())
  440. ->method('getUID')
  441. ->willReturn('admin');
  442. $this->groupManager
  443. ->expects($this->once())
  444. ->method('isAdmin')
  445. ->with('admin')
  446. ->willReturn(true);
  447. $systemTag = new SystemTag(1, 'Test', true, false);
  448. $requestData = [
  449. 'name' => 'Test',
  450. 'userVisible' => $userVisible,
  451. 'userAssignable' => $userAssignable,
  452. ];
  453. if (!empty($groups)) {
  454. $requestData['groups'] = $groups;
  455. }
  456. $requestData = json_encode($requestData);
  457. $node = $this->getMockBuilder(SystemTagsByIdCollection::class)
  458. ->disableOriginalConstructor()
  459. ->getMock();
  460. $this->tagManager->expects($this->once())
  461. ->method('createTag')
  462. ->with('Test', $userVisible, $userAssignable)
  463. ->willReturn($systemTag);
  464. if (!empty($groups)) {
  465. $this->tagManager->expects($this->once())
  466. ->method('setTagGroups')
  467. ->with($systemTag, explode('|', $groups))
  468. ->willReturn($systemTag);
  469. } else {
  470. $this->tagManager->expects($this->never())
  471. ->method('setTagGroups');
  472. }
  473. $this->tree->expects($this->any())
  474. ->method('getNodeForPath')
  475. ->with('/systemtags')
  476. ->willReturn($node);
  477. $request = $this->getMockBuilder(RequestInterface::class)
  478. ->disableOriginalConstructor()
  479. ->getMock();
  480. $response = $this->getMockBuilder(ResponseInterface::class)
  481. ->disableOriginalConstructor()
  482. ->getMock();
  483. $request->expects($this->once())
  484. ->method('getPath')
  485. ->willReturn('/systemtags');
  486. $request->expects($this->once())
  487. ->method('getBodyAsString')
  488. ->willReturn($requestData);
  489. $request->expects($this->once())
  490. ->method('getHeader')
  491. ->with('Content-Type')
  492. ->willReturn('application/json');
  493. $request->expects($this->once())
  494. ->method('getUrl')
  495. ->willReturn('http://example.com/dav/systemtags');
  496. $response->expects($this->once())
  497. ->method('setHeader')
  498. ->with('Content-Location', 'http://example.com/dav/systemtags/1');
  499. $this->plugin->httpPost($request, $response);
  500. }
  501. public function nodeClassProvider() {
  502. return [
  503. ['\OCA\DAV\SystemTag\SystemTagsByIdCollection'],
  504. ['\OCA\DAV\SystemTag\SystemTagsObjectMappingCollection'],
  505. ];
  506. }
  507. public function testCreateTagInMappingCollection() {
  508. $this->user->expects($this->once())
  509. ->method('getUID')
  510. ->willReturn('admin');
  511. $this->groupManager
  512. ->expects($this->once())
  513. ->method('isAdmin')
  514. ->with('admin')
  515. ->willReturn(true);
  516. $systemTag = new SystemTag(1, 'Test', true, false);
  517. $requestData = json_encode([
  518. 'name' => 'Test',
  519. 'userVisible' => true,
  520. 'userAssignable' => false,
  521. ]);
  522. $node = $this->getMockBuilder(SystemTagsObjectMappingCollection::class)
  523. ->disableOriginalConstructor()
  524. ->getMock();
  525. $this->tagManager->expects($this->once())
  526. ->method('createTag')
  527. ->with('Test', true, false)
  528. ->willReturn($systemTag);
  529. $this->tree->expects($this->any())
  530. ->method('getNodeForPath')
  531. ->with('/systemtags-relations/files/12')
  532. ->willReturn($node);
  533. $node->expects($this->once())
  534. ->method('createFile')
  535. ->with(1);
  536. $request = $this->getMockBuilder(RequestInterface::class)
  537. ->disableOriginalConstructor()
  538. ->getMock();
  539. $response = $this->getMockBuilder(ResponseInterface::class)
  540. ->disableOriginalConstructor()
  541. ->getMock();
  542. $request->expects($this->once())
  543. ->method('getPath')
  544. ->willReturn('/systemtags-relations/files/12');
  545. $request->expects($this->once())
  546. ->method('getBodyAsString')
  547. ->willReturn($requestData);
  548. $request->expects($this->once())
  549. ->method('getHeader')
  550. ->with('Content-Type')
  551. ->willReturn('application/json');
  552. $request->expects($this->once())
  553. ->method('getBaseUrl')
  554. ->willReturn('http://example.com/dav/');
  555. $response->expects($this->once())
  556. ->method('setHeader')
  557. ->with('Content-Location', 'http://example.com/dav/systemtags/1');
  558. $this->plugin->httpPost($request, $response);
  559. }
  560. public function testCreateTagToUnknownNode() {
  561. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  562. $node = $this->getMockBuilder(SystemTagsObjectMappingCollection::class)
  563. ->disableOriginalConstructor()
  564. ->getMock();
  565. $this->tree->expects($this->any())
  566. ->method('getNodeForPath')
  567. ->will($this->throwException(new \Sabre\DAV\Exception\NotFound()));
  568. $this->tagManager->expects($this->never())
  569. ->method('createTag');
  570. $node->expects($this->never())
  571. ->method('createFile');
  572. $request = $this->getMockBuilder(RequestInterface::class)
  573. ->disableOriginalConstructor()
  574. ->getMock();
  575. $response = $this->getMockBuilder(ResponseInterface::class)
  576. ->disableOriginalConstructor()
  577. ->getMock();
  578. $request->expects($this->once())
  579. ->method('getPath')
  580. ->willReturn('/systemtags-relations/files/12');
  581. $this->plugin->httpPost($request, $response);
  582. }
  583. /**
  584. * @dataProvider nodeClassProvider
  585. */
  586. public function testCreateTagConflict($nodeClass) {
  587. $this->expectException(\Sabre\DAV\Exception\Conflict::class);
  588. $this->user->expects($this->once())
  589. ->method('getUID')
  590. ->willReturn('admin');
  591. $this->groupManager
  592. ->expects($this->once())
  593. ->method('isAdmin')
  594. ->with('admin')
  595. ->willReturn(true);
  596. $requestData = json_encode([
  597. 'name' => 'Test',
  598. 'userVisible' => true,
  599. 'userAssignable' => false,
  600. ]);
  601. $node = $this->getMockBuilder($nodeClass)
  602. ->disableOriginalConstructor()
  603. ->getMock();
  604. $this->tagManager->expects($this->once())
  605. ->method('createTag')
  606. ->with('Test', true, false)
  607. ->will($this->throwException(new TagAlreadyExistsException('Tag already exists')));
  608. $this->tree->expects($this->any())
  609. ->method('getNodeForPath')
  610. ->with('/systemtags')
  611. ->willReturn($node);
  612. $request = $this->getMockBuilder(RequestInterface::class)
  613. ->disableOriginalConstructor()
  614. ->getMock();
  615. $response = $this->getMockBuilder(ResponseInterface::class)
  616. ->disableOriginalConstructor()
  617. ->getMock();
  618. $request->expects($this->once())
  619. ->method('getPath')
  620. ->willReturn('/systemtags');
  621. $request->expects($this->once())
  622. ->method('getBodyAsString')
  623. ->willReturn($requestData);
  624. $request->expects($this->once())
  625. ->method('getHeader')
  626. ->with('Content-Type')
  627. ->willReturn('application/json');
  628. $this->plugin->httpPost($request, $response);
  629. }
  630. }