PluginTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Georg Ehrke <oc.list@georgehrke.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  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\Schedule;
  26. use OCA\DAV\CalDAV\Schedule\Plugin;
  27. use Sabre\DAV\Server;
  28. use Sabre\DAV\Xml\Property\Href;
  29. use Sabre\VObject\Parameter;
  30. use Sabre\VObject\Property\ICalendar\CalAddress;
  31. use Test\TestCase;
  32. class PluginTest extends TestCase {
  33. /** @var Plugin */
  34. private $plugin;
  35. /** @var Server|\PHPUnit_Framework_MockObject_MockObject */
  36. private $server;
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->server = $this->createMock(Server::class);
  40. $this->plugin = new Plugin();
  41. $this->plugin->initialize($this->server);
  42. }
  43. public function testGetAddressesForPrincipal() {
  44. $href = $this->createMock(Href::class);
  45. $href
  46. ->expects($this->once())
  47. ->method('getHrefs')
  48. ->willReturn(['lukas@nextcloud.com', 'rullzer@nextcloud.com']);
  49. $this->server
  50. ->expects($this->once())
  51. ->method('getProperties')
  52. ->with(
  53. 'MyPrincipal',
  54. [
  55. '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set',
  56. ]
  57. )
  58. ->willReturn([
  59. '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set' => $href
  60. ]);
  61. $result = $this->invokePrivate($this->plugin, 'getAddressesForPrincipal', ['MyPrincipal']);
  62. $this->assertSame(['lukas@nextcloud.com', 'rullzer@nextcloud.com'], $result);
  63. }
  64. public function testGetAddressesForPrincipalEmpty() {
  65. $this->server
  66. ->expects($this->once())
  67. ->method('getProperties')
  68. ->with(
  69. 'MyPrincipal',
  70. [
  71. '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set',
  72. ]
  73. )
  74. ->willReturn(null);
  75. $result = $this->invokePrivate($this->plugin, 'getAddressesForPrincipal', ['MyPrincipal']);
  76. $this->assertSame([], $result);
  77. }
  78. public function testStripOffMailTo() {
  79. $this->assertEquals('test@example.com', $this->invokePrivate($this->plugin, 'stripOffMailTo', ['test@example.com']));
  80. $this->assertEquals('test@example.com', $this->invokePrivate($this->plugin, 'stripOffMailTo', ['mailto:test@example.com']));
  81. }
  82. public function testGetAttendeeRSVP() {
  83. $property1 = $this->createMock(CalAddress::class);
  84. $parameter1 = $this->createMock(Parameter::class);
  85. $property1->expects($this->once())
  86. ->method('offsetGet')
  87. ->with('RSVP')
  88. ->willReturn($parameter1);
  89. $parameter1->expects($this->once())
  90. ->method('getValue')
  91. ->with()
  92. ->willReturn('TRUE');
  93. $property2 = $this->createMock(CalAddress::class);
  94. $parameter2 = $this->createMock(Parameter::class);
  95. $property2->expects($this->once())
  96. ->method('offsetGet')
  97. ->with('RSVP')
  98. ->willReturn($parameter2);
  99. $parameter2->expects($this->once())
  100. ->method('getValue')
  101. ->with()
  102. ->willReturn('FALSE');
  103. $property3 = $this->createMock(CalAddress::class);
  104. $property3->expects($this->once())
  105. ->method('offsetGet')
  106. ->with('RSVP')
  107. ->willReturn(null);
  108. $this->assertTrue($this->invokePrivate($this->plugin, 'getAttendeeRSVP', [$property1]));
  109. $this->assertFalse($this->invokePrivate($this->plugin, 'getAttendeeRSVP', [$property2]));
  110. $this->assertFalse($this->invokePrivate($this->plugin, 'getAttendeeRSVP', [$property3]));
  111. }
  112. }