BaseTest.php 5.5 KB

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