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.

CommentersSorterTest.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Comments\Tests\Unit\Collaboration;
  25. use OCA\Comments\Collaboration\CommentersSorter;
  26. use OCP\Comments\IComment;
  27. use OCP\Comments\ICommentsManager;
  28. use OCP\IConfig;
  29. use Test\TestCase;
  30. class CommentersSorterTest extends TestCase {
  31. /** @var ICommentsManager|\PHPUnit_Framework_MockObject_MockObject */
  32. protected $commentsManager;
  33. /** @var CommentersSorter */
  34. protected $sorter;
  35. protected function setUp(): void {
  36. parent::setUp();
  37. $this->commentsManager = $this->createMock(ICommentsManager::class);
  38. $this->sorter = new CommentersSorter($this->commentsManager);
  39. }
  40. /**
  41. * @dataProvider sortDataProvider
  42. * @param $data
  43. */
  44. public function testSort($data) {
  45. $commentMocks = [];
  46. foreach($data['actors'] as $actorType => $actors) {
  47. foreach ($actors as $actorId => $noOfComments) {
  48. for($i=0;$i<$noOfComments;$i++) {
  49. $mock = $this->createMock(IComment::class);
  50. $mock->expects($this->atLeastOnce())
  51. ->method('getActorType')
  52. ->willReturn($actorType);
  53. $mock->expects($this->atLeastOnce())
  54. ->method('getActorId')
  55. ->willReturn($actorId);
  56. $commentMocks[] = $mock;
  57. }
  58. }
  59. }
  60. $this->commentsManager->expects($this->once())
  61. ->method('getForObject')
  62. ->willReturn($commentMocks);
  63. $workArray = $data['input'];
  64. $this->sorter->sort($workArray, ['itemType' => 'files', 'itemId' => '24']);
  65. $this->assertEquals($data['expected'], $workArray);
  66. }
  67. public function sortDataProvider() {
  68. return [[
  69. [
  70. #1 – sort properly and otherwise keep existing order
  71. 'actors' => ['users' => ['celia' => 3, 'darius' => 7, 'faruk' => 5, 'gail' => 5], 'bots' => ['r2-d2' => 8]],
  72. 'input' => [
  73. 'users' =>
  74. [
  75. ['value' => ['shareWith' => 'alice']],
  76. ['value' => ['shareWith' => 'bob']],
  77. ['value' => ['shareWith' => 'celia']],
  78. ['value' => ['shareWith' => 'darius']],
  79. ['value' => ['shareWith' => 'elena']],
  80. ['value' => ['shareWith' => 'faruk']],
  81. ['value' => ['shareWith' => 'gail']],
  82. ],
  83. 'bots' => [
  84. ['value' => ['shareWith' => 'c-3po']],
  85. ['value' => ['shareWith' => 'r2-d2']],
  86. ]
  87. ],
  88. 'expected' => [
  89. 'users' =>
  90. [
  91. ['value' => ['shareWith' => 'darius']],
  92. ['value' => ['shareWith' => 'faruk']],
  93. ['value' => ['shareWith' => 'gail']],
  94. ['value' => ['shareWith' => 'celia']],
  95. ['value' => ['shareWith' => 'alice']],
  96. ['value' => ['shareWith' => 'bob']],
  97. ['value' => ['shareWith' => 'elena']],
  98. ],
  99. 'bots' => [
  100. ['value' => ['shareWith' => 'r2-d2']],
  101. ['value' => ['shareWith' => 'c-3po']],
  102. ]
  103. ],
  104. ],
  105. [
  106. #2 – no commentors, input equals output
  107. 'actors' => [],
  108. 'input' => [
  109. 'users' =>
  110. [
  111. ['value' => ['shareWith' => 'alice']],
  112. ['value' => ['shareWith' => 'bob']],
  113. ['value' => ['shareWith' => 'celia']],
  114. ['value' => ['shareWith' => 'darius']],
  115. ['value' => ['shareWith' => 'elena']],
  116. ['value' => ['shareWith' => 'faruk']],
  117. ['value' => ['shareWith' => 'gail']],
  118. ],
  119. 'bots' => [
  120. ['value' => ['shareWith' => 'c-3po']],
  121. ['value' => ['shareWith' => 'r2-d2']],
  122. ]
  123. ],
  124. 'expected' => [
  125. 'users' =>
  126. [
  127. ['value' => ['shareWith' => 'alice']],
  128. ['value' => ['shareWith' => 'bob']],
  129. ['value' => ['shareWith' => 'celia']],
  130. ['value' => ['shareWith' => 'darius']],
  131. ['value' => ['shareWith' => 'elena']],
  132. ['value' => ['shareWith' => 'faruk']],
  133. ['value' => ['shareWith' => 'gail']],
  134. ],
  135. 'bots' => [
  136. ['value' => ['shareWith' => 'c-3po']],
  137. ['value' => ['shareWith' => 'r2-d2']],
  138. ]
  139. ],
  140. ],
  141. [
  142. #3 – no nothing
  143. 'actors' => [],
  144. 'input' => [],
  145. 'expected' => [],
  146. ],
  147. ]];
  148. }
  149. }