SearchResultTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\ISearchPlugin;
  28. use OCP\Collaboration\Collaborators\SearchResultType;
  29. use OCP\IContainer;
  30. use OCP\Share;
  31. use Test\TestCase;
  32. class SearchResultTest extends TestCase {
  33. /** @var IContainer|\PHPUnit_Framework_MockObject_MockObject */
  34. protected $container;
  35. /** @var ISearch */
  36. protected $search;
  37. public function setUp() {
  38. parent::setUp();
  39. $this->container = $this->createMock(IContainer::class);
  40. $this->search = new Search($this->container);
  41. }
  42. public function dataAddResultSet() {
  43. return [
  44. [[], ['exact' => []]],
  45. [['users' => ['exact' => null, 'loose' => []]], ['exact' => ['users' => []], 'users' => []]],
  46. [['groups' => ['exact' => null, 'loose' => ['l1']]], ['exact' => ['groups' => []], 'groups' => ['l1']]],
  47. [['users' => ['exact' => ['e1'], 'loose' => []]], ['exact' => ['users' => ['e1']], 'users' => []]],
  48. ];
  49. }
  50. /**
  51. * @dataProvider dataAddResultSet
  52. * @param array $toAdd
  53. * @param array $expected
  54. */
  55. public function testAddResultSet(array $toAdd, array $expected) {
  56. $result = new SearchResult();
  57. foreach ($toAdd as $type => $results) {
  58. $result->addResultSet(new SearchResultType($type), $results['loose'], $results['exact']);
  59. }
  60. $this->assertEquals($expected, $result->asArray());
  61. }
  62. public function dataHasResult() {
  63. $result = ['value' => ['shareWith' => 'l1']];
  64. return [
  65. [[],'users', 'n1', false],
  66. [['users' => ['exact' => null, 'loose' => [$result]]], 'users', 'l1', true],
  67. [['users' => ['exact' => null, 'loose' => [$result]]], 'users', 'l2', false],
  68. [['users' => ['exact' => null, 'loose' => [$result]]], 'groups', 'l1', false],
  69. [['users' => ['exact' => [$result], 'loose' => []]], 'users', 'l1', true],
  70. [['users' => ['exact' => [$result], 'loose' => []]], 'users', 'l2', false],
  71. [['users' => ['exact' => [$result], 'loose' => []]], 'groups', 'l1', false],
  72. ];
  73. }
  74. /**
  75. * @dataProvider dataHasResult
  76. * @param array $toAdd
  77. * @param string $type
  78. * @param string $id
  79. * @param bool $expected
  80. */
  81. public function testHasResult(array $toAdd, $type, $id, $expected) {
  82. $result = new SearchResult();
  83. foreach ($toAdd as $addType => $results) {
  84. $result->addResultSet(new SearchResultType($addType), $results['loose'], $results['exact']);
  85. }
  86. $this->assertSame($expected, $result->hasResult(new SearchResultType($type), $id));
  87. }
  88. }