SearchResultTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Joas Schilling
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Collaboration\Collaborators;
  24. use OC\Collaboration\Collaborators\Search;
  25. use OC\Collaboration\Collaborators\SearchResult;
  26. use OCP\Collaboration\Collaborators\ISearch;
  27. use OCP\Collaboration\Collaborators\SearchResultType;
  28. use OCP\IContainer;
  29. use Test\TestCase;
  30. class SearchResultTest extends TestCase {
  31. /** @var IContainer|\PHPUnit\Framework\MockObject\MockObject */
  32. protected $container;
  33. /** @var ISearch */
  34. protected $search;
  35. protected function setUp(): void {
  36. parent::setUp();
  37. $this->container = $this->createMock(IContainer::class);
  38. $this->search = new Search($this->container);
  39. }
  40. public function dataAddResultSet() {
  41. return [
  42. [[], ['exact' => []]],
  43. [['users' => ['exact' => null, 'loose' => []]], ['exact' => ['users' => []], 'users' => []]],
  44. [['groups' => ['exact' => null, 'loose' => ['l1']]], ['exact' => ['groups' => []], 'groups' => ['l1']]],
  45. [['users' => ['exact' => ['e1'], 'loose' => []]], ['exact' => ['users' => ['e1']], 'users' => []]],
  46. ];
  47. }
  48. /**
  49. * @dataProvider dataAddResultSet
  50. * @param array $toAdd
  51. * @param array $expected
  52. */
  53. public function testAddResultSet(array $toAdd, array $expected) {
  54. $result = new SearchResult();
  55. foreach ($toAdd as $type => $results) {
  56. $result->addResultSet(new SearchResultType($type), $results['loose'], $results['exact']);
  57. }
  58. $this->assertEquals($expected, $result->asArray());
  59. }
  60. public function dataHasResult() {
  61. $result = ['value' => ['shareWith' => 'l1']];
  62. return [
  63. [[],'users', 'n1', false],
  64. [['users' => ['exact' => null, 'loose' => [$result]]], 'users', 'l1', true],
  65. [['users' => ['exact' => null, 'loose' => [$result]]], 'users', 'l2', false],
  66. [['users' => ['exact' => null, 'loose' => [$result]]], 'groups', 'l1', false],
  67. [['users' => ['exact' => [$result], 'loose' => []]], 'users', 'l1', true],
  68. [['users' => ['exact' => [$result], 'loose' => []]], 'users', 'l2', false],
  69. [['users' => ['exact' => [$result], 'loose' => []]], 'groups', 'l1', false],
  70. ];
  71. }
  72. /**
  73. * @dataProvider dataHasResult
  74. * @param array $toAdd
  75. * @param string $type
  76. * @param string $id
  77. * @param bool $expected
  78. */
  79. public function testHasResult(array $toAdd, $type, $id, $expected) {
  80. $result = new SearchResult();
  81. foreach ($toAdd as $addType => $results) {
  82. $result->addResultSet(new SearchResultType($addType), $results['loose'], $results['exact']);
  83. }
  84. $this->assertSame($expected, $result->hasResult(new SearchResultType($type), $id));
  85. }
  86. }