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.7KB

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