BaseTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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@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\IUserManager;
  35. use PHPUnit\Framework\MockObject\MockObject;
  36. use Test\TestCase;
  37. class BaseTest extends TestCase {
  38. /** @var IUserManager|MockObject */
  39. protected $userManager;
  40. /** @var IGroupManager|MockObject */
  41. protected $groupManager;
  42. /** @var IURLGenerator|MockObject */
  43. protected $url;
  44. /** @var IProvider|Base|MockObject */
  45. protected $provider;
  46. protected function setUp(): void {
  47. parent::setUp();
  48. $this->userManager = $this->createMock(IUserManager::class);
  49. $this->groupManager = $this->createMock(IGroupManager::class);
  50. $this->url = $this->createMock(IURLGenerator::class);
  51. $this->provider = $this->getMockBuilder(Base::class)
  52. ->setConstructorArgs([
  53. $this->userManager,
  54. $this->groupManager,
  55. $this->url,
  56. ])
  57. ->setMethods(['parse'])
  58. ->getMock();
  59. }
  60. public function dataSetSubjects() {
  61. return [
  62. ['abc', [], 'abc'],
  63. ['{actor} created {calendar}', ['actor' => ['name' => 'abc'], 'calendar' => ['name' => 'xyz']], 'abc created xyz'],
  64. ];
  65. }
  66. /**
  67. * @dataProvider dataSetSubjects
  68. * @param string $subject
  69. * @param array $parameters
  70. * @param string $parsedSubject
  71. */
  72. public function testSetSubjects(string $subject, array $parameters, string $parsedSubject): void {
  73. $event = $this->createMock(IEvent::class);
  74. $event->expects($this->once())
  75. ->method('setRichSubject')
  76. ->with($subject, $parameters)
  77. ->willReturnSelf();
  78. $event->expects($this->never())
  79. ->method('setParsedSubject');
  80. $this->invokePrivate($this->provider, 'setSubjects', [$event, $subject, $parameters]);
  81. }
  82. public function dataGenerateCalendarParameter() {
  83. return [
  84. [['id' => 23, 'uri' => 'foo', 'name' => 'bar'], 'bar'],
  85. [['id' => 42, 'uri' => 'foo', 'name' => 'Personal'], 'Personal'],
  86. [['id' => 42, 'uri' => 'personal', 'name' => 'bar'], 'bar'],
  87. [['id' => 42, 'uri' => 'personal', 'name' => 'Personal'], 't(Personal)'],
  88. ];
  89. }
  90. /**
  91. * @dataProvider dataGenerateCalendarParameter
  92. * @param array $data
  93. * @param string $name
  94. */
  95. public function testGenerateCalendarParameter(array $data, string $name): void {
  96. $l = $this->createMock(IL10N::class);
  97. $l->expects($this->any())
  98. ->method('t')
  99. ->willReturnCallback(function ($string, $args) {
  100. return 't(' . vsprintf($string, $args) . ')';
  101. });
  102. $this->assertEquals([
  103. 'type' => 'calendar',
  104. 'id' => $data['id'],
  105. 'name' => $name,
  106. ], $this->invokePrivate($this->provider, 'generateCalendarParameter', [$data, $l]));
  107. }
  108. public function dataGenerateLegacyCalendarParameter() {
  109. return [
  110. [23, 'c1'],
  111. [42, 'c2'],
  112. ];
  113. }
  114. /**
  115. * @dataProvider dataGenerateLegacyCalendarParameter
  116. * @param int $id
  117. * @param string $name
  118. */
  119. public function testGenerateLegacyCalendarParameter(int $id, string $name): void {
  120. $this->assertEquals([
  121. 'type' => 'calendar',
  122. 'id' => $id,
  123. 'name' => $name,
  124. ], $this->invokePrivate($this->provider, 'generateLegacyCalendarParameter', [$id, $name]));
  125. }
  126. public function dataGenerateGroupParameter() {
  127. return [
  128. ['g1'],
  129. ['g2'],
  130. ];
  131. }
  132. /**
  133. * @dataProvider dataGenerateGroupParameter
  134. * @param string $gid
  135. */
  136. public function testGenerateGroupParameter(string $gid): void {
  137. $this->assertEquals([
  138. 'type' => 'user-group',
  139. 'id' => $gid,
  140. 'name' => $gid,
  141. ], $this->invokePrivate($this->provider, 'generateGroupParameter', [$gid]));
  142. }
  143. }