BaseTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Tests\unit\CalDAV\Activity\Provider;
  7. use OCA\DAV\CalDAV\Activity\Provider\Base;
  8. use OCP\Activity\IEvent;
  9. use OCP\Activity\IProvider;
  10. use OCP\IGroupManager;
  11. use OCP\IL10N;
  12. use OCP\IURLGenerator;
  13. use OCP\IUserManager;
  14. use PHPUnit\Framework\MockObject\MockObject;
  15. use Test\TestCase;
  16. class BaseTest extends TestCase {
  17. /** @var IUserManager|MockObject */
  18. protected $userManager;
  19. /** @var IGroupManager|MockObject */
  20. protected $groupManager;
  21. /** @var IURLGenerator|MockObject */
  22. protected $url;
  23. /** @var IProvider|Base|MockObject */
  24. protected $provider;
  25. protected function setUp(): void {
  26. parent::setUp();
  27. $this->userManager = $this->createMock(IUserManager::class);
  28. $this->groupManager = $this->createMock(IGroupManager::class);
  29. $this->url = $this->createMock(IURLGenerator::class);
  30. $this->provider = $this->getMockBuilder(Base::class)
  31. ->setConstructorArgs([
  32. $this->userManager,
  33. $this->groupManager,
  34. $this->url,
  35. ])
  36. ->setMethods(['parse'])
  37. ->getMock();
  38. }
  39. public function dataSetSubjects() {
  40. return [
  41. ['abc', [], 'abc'],
  42. ['{actor} created {calendar}', ['actor' => ['name' => 'abc'], 'calendar' => ['name' => 'xyz']], 'abc created xyz'],
  43. ];
  44. }
  45. /**
  46. * @dataProvider dataSetSubjects
  47. * @param string $subject
  48. * @param array $parameters
  49. * @param string $parsedSubject
  50. */
  51. public function testSetSubjects(string $subject, array $parameters, string $parsedSubject): void {
  52. $event = $this->createMock(IEvent::class);
  53. $event->expects($this->once())
  54. ->method('setRichSubject')
  55. ->with($subject, $parameters)
  56. ->willReturnSelf();
  57. $event->expects($this->never())
  58. ->method('setParsedSubject');
  59. $this->invokePrivate($this->provider, 'setSubjects', [$event, $subject, $parameters]);
  60. }
  61. public function dataGenerateCalendarParameter() {
  62. return [
  63. [['id' => 23, 'uri' => 'foo', 'name' => 'bar'], 'bar'],
  64. [['id' => 42, 'uri' => 'foo', 'name' => 'Personal'], 'Personal'],
  65. [['id' => 42, 'uri' => 'personal', 'name' => 'bar'], 'bar'],
  66. [['id' => 42, 'uri' => 'personal', 'name' => 'Personal'], 't(Personal)'],
  67. ];
  68. }
  69. /**
  70. * @dataProvider dataGenerateCalendarParameter
  71. * @param array $data
  72. * @param string $name
  73. */
  74. public function testGenerateCalendarParameter(array $data, string $name): void {
  75. $l = $this->createMock(IL10N::class);
  76. $l->expects($this->any())
  77. ->method('t')
  78. ->willReturnCallback(function ($string, $args) {
  79. return 't(' . vsprintf($string, $args) . ')';
  80. });
  81. $this->assertEquals([
  82. 'type' => 'calendar',
  83. 'id' => $data['id'],
  84. 'name' => $name,
  85. ], $this->invokePrivate($this->provider, 'generateCalendarParameter', [$data, $l]));
  86. }
  87. public function dataGenerateLegacyCalendarParameter() {
  88. return [
  89. [23, 'c1'],
  90. [42, 'c2'],
  91. ];
  92. }
  93. /**
  94. * @dataProvider dataGenerateLegacyCalendarParameter
  95. * @param int $id
  96. * @param string $name
  97. */
  98. public function testGenerateLegacyCalendarParameter(int $id, string $name): void {
  99. $this->assertEquals([
  100. 'type' => 'calendar',
  101. 'id' => $id,
  102. 'name' => $name,
  103. ], $this->invokePrivate($this->provider, 'generateLegacyCalendarParameter', [$id, $name]));
  104. }
  105. public function dataGenerateGroupParameter() {
  106. return [
  107. ['g1'],
  108. ['g2'],
  109. ];
  110. }
  111. /**
  112. * @dataProvider dataGenerateGroupParameter
  113. * @param string $gid
  114. */
  115. public function testGenerateGroupParameter(string $gid): void {
  116. $this->assertEquals([
  117. 'type' => 'user-group',
  118. 'id' => $gid,
  119. 'name' => $gid,
  120. ], $this->invokePrivate($this->provider, 'generateGroupParameter', [$gid]));
  121. }
  122. }