BaseTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  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 OCA\DAV\Tests\unit\CalDAV\Activity\Provider;
  24. use OCA\DAV\CalDAV\Activity\Provider\Base;
  25. use OCP\Activity\IEvent;
  26. use OCP\Activity\IProvider;
  27. use OCP\IL10N;
  28. use OCP\IUser;
  29. use OCP\IUserManager;
  30. use OCP\IGroupManager;
  31. use Test\TestCase;
  32. class BaseTest extends TestCase {
  33. /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  34. protected $userManager;
  35. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  36. protected $groupManager;
  37. /** @var IProvider|Base|\PHPUnit_Framework_MockObject_MockObject */
  38. protected $provider;
  39. protected function setUp() {
  40. parent::setUp();
  41. $this->userManager = $this->createMock(IUserManager::class);
  42. $this->groupManager = $this->createMock(IGroupManager::class);
  43. $this->provider = $this->getMockBuilder(Base::class)
  44. ->setConstructorArgs([
  45. $this->userManager,
  46. $this->groupManager
  47. ])
  48. ->setMethods(['parse'])
  49. ->getMock();
  50. }
  51. public function dataSetSubjects() {
  52. return [
  53. ['abc', [], 'abc'],
  54. ['{actor} created {calendar}', ['actor' => ['name' => 'abc'], 'calendar' => ['name' => 'xyz']], 'abc created xyz'],
  55. ];
  56. }
  57. /**
  58. * @dataProvider dataSetSubjects
  59. * @param string $subject
  60. * @param array $parameters
  61. * @param string $parsedSubject
  62. */
  63. public function testSetSubjects($subject, array $parameters, $parsedSubject) {
  64. $event = $this->createMock(IEvent::class);
  65. $event->expects($this->once())
  66. ->method('setRichSubject')
  67. ->with($subject, $parameters)
  68. ->willReturnSelf();
  69. $event->expects($this->once())
  70. ->method('setParsedSubject')
  71. ->with($parsedSubject)
  72. ->willReturnSelf();
  73. $this->invokePrivate($this->provider, 'setSubjects', [$event, $subject, $parameters]);
  74. }
  75. public function dataGenerateObjectParameter() {
  76. return [
  77. [23, 'c1'],
  78. [42, 'c2'],
  79. ];
  80. }
  81. /**
  82. * @dataProvider dataGenerateObjectParameter
  83. * @param int $id
  84. * @param string $name
  85. */
  86. public function testGenerateObjectParameter($id, $name) {
  87. $this->assertEquals([
  88. 'type' => 'calendar-event',
  89. 'id' => $id,
  90. 'name' => $name,
  91. ], $this->invokePrivate($this->provider, 'generateObjectParameter', [['id' => $id, 'name' => $name]]));
  92. }
  93. public function dataGenerateObjectParameterThrows() {
  94. return [
  95. ['event'],
  96. [['name' => 'event']],
  97. [['id' => 42]],
  98. ];
  99. }
  100. /**
  101. * @dataProvider dataGenerateObjectParameterThrows
  102. * @expectedException \InvalidArgumentException
  103. * @param mixed $eventData
  104. */
  105. public function testGenerateObjectParameterThrows($eventData) {
  106. $this->invokePrivate($this->provider, 'generateObjectParameter', [$eventData]);
  107. }
  108. public function dataGenerateCalendarParameter() {
  109. return [
  110. [['id' => 23, 'uri' => 'foo', 'name' => 'bar'], 'bar'],
  111. [['id' => 42, 'uri' => 'foo', 'name' => 'Personal'], 'Personal'],
  112. [['id' => 42, 'uri' => 'personal', 'name' => 'bar'], 'bar'],
  113. [['id' => 42, 'uri' => 'personal', 'name' => 'Personal'], 't(Personal)'],
  114. ];
  115. }
  116. /**
  117. * @dataProvider dataGenerateCalendarParameter
  118. * @param array $data
  119. * @param string $name
  120. */
  121. public function testGenerateCalendarParameter(array $data, $name) {
  122. $l = $this->createMock(IL10N::class);
  123. $l->expects($this->any())
  124. ->method('t')
  125. ->willReturnCallback(function($string, $args) {
  126. return 't(' . vsprintf($string, $args) . ')';
  127. });
  128. $this->assertEquals([
  129. 'type' => 'calendar',
  130. 'id' => $data['id'],
  131. 'name' => $name,
  132. ], $this->invokePrivate($this->provider, 'generateCalendarParameter', [$data, $l]));
  133. }
  134. public function dataGenerateLegacyCalendarParameter() {
  135. return [
  136. [23, 'c1'],
  137. [42, 'c2'],
  138. ];
  139. }
  140. /**
  141. * @dataProvider dataGenerateLegacyCalendarParameter
  142. * @param int $id
  143. * @param string $name
  144. */
  145. public function testGenerateLegacyCalendarParameter($id, $name) {
  146. $this->assertEquals([
  147. 'type' => 'calendar',
  148. 'id' => $id,
  149. 'name' => $name,
  150. ], $this->invokePrivate($this->provider, 'generateLegacyCalendarParameter', [$id, $name]));
  151. }
  152. public function dataGenerateGroupParameter() {
  153. return [
  154. ['g1'],
  155. ['g2'],
  156. ];
  157. }
  158. /**
  159. * @dataProvider dataGenerateGroupParameter
  160. * @param string $gid
  161. */
  162. public function testGenerateGroupParameter($gid) {
  163. $this->assertEquals([
  164. 'type' => 'group',
  165. 'id' => $gid,
  166. 'name' => $gid,
  167. ], $this->invokePrivate($this->provider, 'generateGroupParameter', [$gid]));
  168. }
  169. public function dataGenerateUserParameter() {
  170. $u1 = $this->createMock(IUser::class);
  171. $u1->expects($this->any())
  172. ->method('getDisplayName')
  173. ->willReturn('User 1');
  174. return [
  175. ['u1', 'User 1', $u1],
  176. ['u2', 'u2', null],
  177. ];
  178. }
  179. /**
  180. * @dataProvider dataGenerateUserParameter
  181. * @param string $uid
  182. * @param string $displayName
  183. * @param IUser|null $user
  184. */
  185. public function testGenerateUserParameter($uid, $displayName, $user) {
  186. $this->userManager->expects($this->once())
  187. ->method('get')
  188. ->with($uid)
  189. ->willReturn($user);
  190. $this->assertEquals([
  191. 'type' => 'user',
  192. 'id' => $uid,
  193. 'name' => $displayName,
  194. ], $this->invokePrivate($this->provider, 'generateUserParameter', [$uid]));
  195. // Test caching (only 1 user manager invocation allowed)
  196. $this->assertEquals([
  197. 'type' => 'user',
  198. 'id' => $uid,
  199. 'name' => $displayName,
  200. ], $this->invokePrivate($this->provider, 'generateUserParameter', [$uid]));
  201. }
  202. }