1
0

LegacyHooksTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Share20;
  24. use OC\Share20\LegacyHooks;
  25. use OC\Share20\Manager;
  26. use OCP\Files\File;
  27. use Symfony\Component\EventDispatcher\EventDispatcher;
  28. use Symfony\Component\EventDispatcher\GenericEvent;
  29. use Test\TestCase;
  30. class LegacyHooksTest extends TestCase {
  31. /** @var LegacyHooks */
  32. private $hooks;
  33. /** @var EventDispatcher */
  34. private $eventDispatcher;
  35. /** @var Manager */
  36. private $manager;
  37. public function setUp() {
  38. parent::setUp();
  39. $this->eventDispatcher = new EventDispatcher();
  40. $this->hooks = new LegacyHooks($this->eventDispatcher);
  41. $this->manager = \OC::$server->getShareManager();
  42. }
  43. public function testPreUnshare() {
  44. $path = $this->createMock(File::class);
  45. $path->method('getId')->willReturn(1);
  46. $share = $this->manager->newShare();
  47. $share->setId(42)
  48. ->setProviderId('prov')
  49. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  50. ->setSharedWith('awesomeUser')
  51. ->setSharedBy('sharedBy')
  52. ->setNode($path)
  53. ->setTarget('myTarget');
  54. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre'])->getMock();
  55. \OCP\Util::connectHook('OCP\Share', 'pre_unshare', $hookListner, 'pre');
  56. $hookListnerExpectsPre = [
  57. 'id' => 42,
  58. 'itemType' => 'file',
  59. 'itemSource' => 1,
  60. 'shareType' => \OCP\Share::SHARE_TYPE_USER,
  61. 'shareWith' => 'awesomeUser',
  62. 'itemparent' => null,
  63. 'uidOwner' => 'sharedBy',
  64. 'fileSource' => 1,
  65. 'fileTarget' => 'myTarget',
  66. ];
  67. $hookListner
  68. ->expects($this->exactly(1))
  69. ->method('pre')
  70. ->with($hookListnerExpectsPre);
  71. $event = new GenericEvent($share);
  72. $this->eventDispatcher->dispatch('OCP\Share::preUnshare', $event);
  73. }
  74. public function testPostUnshare() {
  75. $path = $this->createMock(File::class);
  76. $path->method('getId')->willReturn(1);
  77. $share = $this->manager->newShare();
  78. $share->setId(42)
  79. ->setProviderId('prov')
  80. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  81. ->setSharedWith('awesomeUser')
  82. ->setSharedBy('sharedBy')
  83. ->setNode($path)
  84. ->setTarget('myTarget');
  85. $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  86. \OCP\Util::connectHook('OCP\Share', 'pre_unshare', $hookListner, 'post');
  87. $hookListnerExpectsPost = [
  88. 'id' => 42,
  89. 'itemType' => 'file',
  90. 'itemSource' => 1,
  91. 'shareType' => \OCP\Share::SHARE_TYPE_USER,
  92. 'shareWith' => 'awesomeUser',
  93. 'itemparent' => null,
  94. 'uidOwner' => 'sharedBy',
  95. 'fileSource' => 1,
  96. 'fileTarget' => 'myTarget',
  97. 'deletedShares' => [
  98. [
  99. 'id' => 42,
  100. 'itemType' => 'file',
  101. 'itemSource' => 1,
  102. 'shareType' => \OCP\Share::SHARE_TYPE_USER,
  103. 'shareWith' => 'awesomeUser',
  104. 'itemparent' => null,
  105. 'uidOwner' => 'sharedBy',
  106. 'fileSource' => 1,
  107. 'fileTarget' => 'myTarget',
  108. ],
  109. ],
  110. ];
  111. $hookListner
  112. ->expects($this->exactly(1))
  113. ->method('post')
  114. ->with($hookListnerExpectsPost);
  115. $event = new GenericEvent($share);
  116. $event->setArgument('deletedShares', [$share]);
  117. $this->eventDispatcher->dispatch('OCP\Share::postUnshare', $event);
  118. }
  119. }