You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
5.3 KiB

  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OCA\Comments\Tests\Unit\Controller;
  22. use OCA\Comments\Controller\Notifications;
  23. use OCP\Comments\NotFoundException;
  24. use Test\TestCase;
  25. class NotificationsTest extends TestCase {
  26. /** @var \OCA\Comments\Controller\Notifications */
  27. protected $notificationsController;
  28. /** @var \OCP\Comments\ICommentsManager|\PHPUnit_Framework_MockObject_MockObject */
  29. protected $commentsManager;
  30. /** @var \OCP\Files\Folder|\PHPUnit_Framework_MockObject_MockObject */
  31. protected $folder;
  32. /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject */
  33. protected $session;
  34. /** @var \OCP\Notification\IManager|\PHPUnit_Framework_MockObject_MockObject */
  35. protected $notificationManager;
  36. protected function setUp() {
  37. parent::setUp();
  38. $this->commentsManager = $this->getMockBuilder('\OCP\Comments\ICommentsManager')->getMock();
  39. $this->folder = $this->getMockBuilder('\OCP\Files\Folder')->getMock();
  40. $this->session = $this->getMockBuilder('\OCP\IUserSession')->getMock();
  41. $this->notificationManager = $this->getMockBuilder('\OCP\Notification\IManager')->getMock();
  42. $this->notificationsController = new Notifications(
  43. 'comments',
  44. $this->getMockBuilder('\OCP\IRequest')->getMock(),
  45. $this->commentsManager,
  46. $this->folder,
  47. $this->getMockBuilder('\OCP\IURLGenerator')->getMock(),
  48. $this->notificationManager,
  49. $this->session
  50. );
  51. }
  52. public function testViewSuccess() {
  53. $comment = $this->getMockBuilder('\OCP\Comments\IComment')->getMock();
  54. $comment->expects($this->any())
  55. ->method('getObjectType')
  56. ->will($this->returnValue('files'));
  57. $this->commentsManager->expects($this->any())
  58. ->method('get')
  59. ->with('42')
  60. ->will($this->returnValue($comment));
  61. $file = $this->getMockBuilder('\OCP\Files\Node')->getMock();
  62. $file->expects($this->once())
  63. ->method('getParent')
  64. ->will($this->returnValue($this->getMockBuilder('\OCP\Files\Folder')->getMock()));
  65. $this->folder->expects($this->once())
  66. ->method('getById')
  67. ->will($this->returnValue([$file]));
  68. $this->session->expects($this->once())
  69. ->method('getUser')
  70. ->will($this->returnValue($this->getMockBuilder('\OCP\IUser')->getMock()));
  71. $notification = $this->getMockBuilder('\OCP\Notification\INotification')->getMock();
  72. $notification->expects($this->any())
  73. ->method($this->anything())
  74. ->will($this->returnValue($notification));
  75. $this->notificationManager->expects($this->once())
  76. ->method('createNotification')
  77. ->will($this->returnValue($notification));
  78. $this->notificationManager->expects($this->once())
  79. ->method('markProcessed')
  80. ->with($notification);
  81. $response = $this->notificationsController->view('42');
  82. $this->assertInstanceOf('\OCP\AppFramework\Http\RedirectResponse', $response);
  83. }
  84. public function testViewInvalidComment() {
  85. $this->commentsManager->expects($this->any())
  86. ->method('get')
  87. ->with('42')
  88. ->will($this->throwException(new NotFoundException()));
  89. $file = $this->getMockBuilder('\OCP\Files\Node')->getMock();
  90. $file->expects($this->never())
  91. ->method('getParent');
  92. $this->folder->expects($this->never())
  93. ->method('getById');
  94. $this->session->expects($this->never())
  95. ->method('getUser');
  96. $this->notificationManager->expects($this->never())
  97. ->method('createNotification');
  98. $this->notificationManager->expects($this->never())
  99. ->method('markProcessed');
  100. $response = $this->notificationsController->view('42');
  101. $this->assertInstanceOf('\OCP\AppFramework\Http\NotFoundResponse', $response);
  102. }
  103. public function testViewNoFile() {
  104. $comment = $this->getMockBuilder('\OCP\Comments\IComment')->getMock();
  105. $comment->expects($this->any())
  106. ->method('getObjectType')
  107. ->will($this->returnValue('files'));
  108. $this->commentsManager->expects($this->any())
  109. ->method('get')
  110. ->with('42')
  111. ->will($this->returnValue($comment));
  112. $this->folder->expects($this->once())
  113. ->method('getById')
  114. ->will($this->returnValue([]));
  115. $this->session->expects($this->once())
  116. ->method('getUser')
  117. ->will($this->returnValue($this->getMockBuilder('\OCP\IUser')->getMock()));
  118. $notification = $this->getMockBuilder('\OCP\Notification\INotification')->getMock();
  119. $notification->expects($this->any())
  120. ->method($this->anything())
  121. ->will($this->returnValue($notification));
  122. $this->notificationManager->expects($this->once())
  123. ->method('createNotification')
  124. ->will($this->returnValue($notification));
  125. $this->notificationManager->expects($this->once())
  126. ->method('markProcessed')
  127. ->with($notification);
  128. $response = $this->notificationsController->view('42');
  129. $this->assertInstanceOf('\OCP\AppFramework\Http\NotFoundResponse', $response);
  130. }
  131. }