SearchResultTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Collaboration\Collaborators;
  7. use OC\Collaboration\Collaborators\Search;
  8. use OC\Collaboration\Collaborators\SearchResult;
  9. use OCP\Collaboration\Collaborators\ISearch;
  10. use OCP\Collaboration\Collaborators\SearchResultType;
  11. use OCP\IContainer;
  12. use Test\TestCase;
  13. class SearchResultTest extends TestCase {
  14. /** @var IContainer|\PHPUnit\Framework\MockObject\MockObject */
  15. protected $container;
  16. /** @var ISearch */
  17. protected $search;
  18. protected function setUp(): void {
  19. parent::setUp();
  20. $this->container = $this->createMock(IContainer::class);
  21. $this->search = new Search($this->container);
  22. }
  23. public function dataAddResultSet() {
  24. return [
  25. [[], ['exact' => []]],
  26. [['users' => ['exact' => null, 'loose' => []]], ['exact' => ['users' => []], 'users' => []]],
  27. [['groups' => ['exact' => null, 'loose' => ['l1']]], ['exact' => ['groups' => []], 'groups' => ['l1']]],
  28. [['users' => ['exact' => ['e1'], 'loose' => []]], ['exact' => ['users' => ['e1']], 'users' => []]],
  29. ];
  30. }
  31. /**
  32. * @dataProvider dataAddResultSet
  33. * @param array $toAdd
  34. * @param array $expected
  35. */
  36. public function testAddResultSet(array $toAdd, array $expected): void {
  37. $result = new SearchResult();
  38. foreach ($toAdd as $type => $results) {
  39. $result->addResultSet(new SearchResultType($type), $results['loose'], $results['exact']);
  40. }
  41. $this->assertEquals($expected, $result->asArray());
  42. }
  43. public function dataHasResult() {
  44. $result = ['value' => ['shareWith' => 'l1']];
  45. return [
  46. [[],'users', 'n1', false],
  47. [['users' => ['exact' => null, 'loose' => [$result]]], 'users', 'l1', true],
  48. [['users' => ['exact' => null, 'loose' => [$result]]], 'users', 'l2', false],
  49. [['users' => ['exact' => null, 'loose' => [$result]]], 'groups', 'l1', false],
  50. [['users' => ['exact' => [$result], 'loose' => []]], 'users', 'l1', true],
  51. [['users' => ['exact' => [$result], 'loose' => []]], 'users', 'l2', false],
  52. [['users' => ['exact' => [$result], 'loose' => []]], 'groups', 'l1', false],
  53. ];
  54. }
  55. /**
  56. * @dataProvider dataHasResult
  57. * @param array $toAdd
  58. * @param string $type
  59. * @param string $id
  60. * @param bool $expected
  61. */
  62. public function testHasResult(array $toAdd, $type, $id, $expected): void {
  63. $result = new SearchResult();
  64. foreach ($toAdd as $addType => $results) {
  65. $result->addResultSet(new SearchResultType($addType), $results['loose'], $results['exact']);
  66. }
  67. $this->assertSame($expected, $result->hasResult(new SearchResultType($type), $id));
  68. }
  69. }