BaseTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\DAV\Tests\unit\CalDAV\Activity\Provider;
  26. use OCA\DAV\CalDAV\Activity\Provider\Base;
  27. use OCP\Activity\IEvent;
  28. use OCP\Activity\IProvider;
  29. use OCP\IGroupManager;
  30. use OCP\IL10N;
  31. use OCP\IUser;
  32. use OCP\IUserManager;
  33. use Test\TestCase;
  34. class BaseTest extends TestCase {
  35. /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  36. protected $userManager;
  37. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  38. protected $groupManager;
  39. /** @var IProvider|Base|\PHPUnit_Framework_MockObject_MockObject */
  40. protected $provider;
  41. protected function setUp(): void {
  42. parent::setUp();
  43. $this->userManager = $this->createMock(IUserManager::class);
  44. $this->groupManager = $this->createMock(IGroupManager::class);
  45. $this->provider = $this->getMockBuilder(Base::class)
  46. ->setConstructorArgs([
  47. $this->userManager,
  48. $this->groupManager
  49. ])
  50. ->setMethods(['parse'])
  51. ->getMock();
  52. }
  53. public function dataSetSubjects() {
  54. return [
  55. ['abc', [], 'abc'],
  56. ['{actor} created {calendar}', ['actor' => ['name' => 'abc'], 'calendar' => ['name' => 'xyz']], 'abc created xyz'],
  57. ];
  58. }
  59. /**
  60. * @dataProvider dataSetSubjects
  61. * @param string $subject
  62. * @param array $parameters
  63. * @param string $parsedSubject
  64. */
  65. public function testSetSubjects($subject, array $parameters, $parsedSubject) {
  66. $event = $this->createMock(IEvent::class);
  67. $event->expects($this->once())
  68. ->method('setRichSubject')
  69. ->with($subject, $parameters)
  70. ->willReturnSelf();
  71. $event->expects($this->once())
  72. ->method('setParsedSubject')
  73. ->with($parsedSubject)
  74. ->willReturnSelf();
  75. $this->invokePrivate($this->provider, 'setSubjects', [$event, $subject, $parameters]);
  76. }
  77. public function dataGenerateObjectParameter() {
  78. return [
  79. [23, 'c1'],
  80. [42, 'c2'],
  81. ];
  82. }
  83. /**
  84. * @dataProvider dataGenerateObjectParameter
  85. * @param int $id
  86. * @param string $name
  87. */
  88. public function testGenerateObjectParameter($id, $name) {
  89. $this->assertEquals([
  90. 'type' => 'calendar-event',
  91. 'id' => $id,
  92. 'name' => $name,
  93. ], $this->invokePrivate($this->provider, 'generateObjectParameter', [['id' => $id, 'name' => $name]]));
  94. }
  95. public function dataGenerateObjectParameterThrows() {
  96. return [
  97. ['event'],
  98. [['name' => 'event']],
  99. [['id' => 42]],
  100. ];
  101. }
  102. /**
  103. * @dataProvider dataGenerateObjectParameterThrows
  104. * @param mixed $eventData
  105. */
  106. public function testGenerateObjectParameterThrows($eventData) {
  107. $this->expectException(\InvalidArgumentException::class);
  108. $this->invokePrivate($this->provider, 'generateObjectParameter', [$eventData]);
  109. }
  110. public function dataGenerateCalendarParameter() {
  111. return [
  112. [['id' => 23, 'uri' => 'foo', 'name' => 'bar'], 'bar'],
  113. [['id' => 42, 'uri' => 'foo', 'name' => 'Personal'], 'Personal'],
  114. [['id' => 42, 'uri' => 'personal', 'name' => 'bar'], 'bar'],
  115. [['id' => 42, 'uri' => 'personal', 'name' => 'Personal'], 't(Personal)'],
  116. ];
  117. }
  118. /**
  119. * @dataProvider dataGenerateCalendarParameter
  120. * @param array $data
  121. * @param string $name
  122. */
  123. public function testGenerateCalendarParameter(array $data, $name) {
  124. $l = $this->createMock(IL10N::class);
  125. $l->expects($this->any())
  126. ->method('t')
  127. ->willReturnCallback(function($string, $args) {
  128. return 't(' . vsprintf($string, $args) . ')';
  129. });
  130. $this->assertEquals([
  131. 'type' => 'calendar',
  132. 'id' => $data['id'],
  133. 'name' => $name,
  134. ], $this->invokePrivate($this->provider, 'generateCalendarParameter', [$data, $l]));
  135. }
  136. public function dataGenerateLegacyCalendarParameter() {
  137. return [
  138. [23, 'c1'],
  139. [42, 'c2'],
  140. ];
  141. }
  142. /**
  143. * @dataProvider dataGenerateLegacyCalendarParameter
  144. * @param int $id
  145. * @param string $name
  146. */
  147. public function testGenerateLegacyCalendarParameter($id, $name) {
  148. $this->assertEquals([
  149. 'type' => 'calendar',
  150. 'id' => $id,
  151. 'name' => $name,
  152. ], $this->invokePrivate($this->provider, 'generateLegacyCalendarParameter', [$id, $name]));
  153. }
  154. public function dataGenerateGroupParameter() {
  155. return [
  156. ['g1'],
  157. ['g2'],
  158. ];
  159. }
  160. /**
  161. * @dataProvider dataGenerateGroupParameter
  162. * @param string $gid
  163. */
  164. public function testGenerateGroupParameter($gid) {
  165. $this->assertEquals([
  166. 'type' => 'user-group',
  167. 'id' => $gid,
  168. 'name' => $gid,
  169. ], $this->invokePrivate($this->provider, 'generateGroupParameter', [$gid]));
  170. }
  171. public function dataGenerateUserParameter() {
  172. $u1 = $this->createMock(IUser::class);
  173. $u1->expects($this->any())
  174. ->method('getDisplayName')
  175. ->willReturn('User 1');
  176. return [
  177. ['u1', 'User 1', $u1],
  178. ['u2', 'u2', null],
  179. ];
  180. }
  181. /**
  182. * @dataProvider dataGenerateUserParameter
  183. * @param string $uid
  184. * @param string $displayName
  185. * @param IUser|null $user
  186. */
  187. public function testGenerateUserParameter($uid, $displayName, $user) {
  188. $this->userManager->expects($this->once())
  189. ->method('get')
  190. ->with($uid)
  191. ->willReturn($user);
  192. $this->assertEquals([
  193. 'type' => 'user',
  194. 'id' => $uid,
  195. 'name' => $displayName,
  196. ], $this->invokePrivate($this->provider, 'generateUserParameter', [$uid]));
  197. // Test caching (only 1 user manager invocation allowed)
  198. $this->assertEquals([
  199. 'type' => 'user',
  200. 'id' => $uid,
  201. 'name' => $displayName,
  202. ], $this->invokePrivate($this->provider, 'generateUserParameter', [$uid]));
  203. }
  204. }