Search.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. use Behat\Gherkin\Node\TableNode;
  7. use PHPUnit\Framework\Assert;
  8. trait Search {
  9. // BasicStructure trait is expected to be used in the class that uses this
  10. // trait.
  11. /**
  12. * @When /^searching for "([^"]*)"$/
  13. * @param string $query
  14. */
  15. public function searchingFor(string $query) {
  16. $this->searchForInApp($query, '');
  17. }
  18. /**
  19. * @When /^searching for "([^"]*)" in app "([^"]*)"$/
  20. * @param string $query
  21. * @param string $app
  22. */
  23. public function searchingForInApp(string $query, string $app) {
  24. $url = '/index.php/core/search';
  25. $parameters[] = 'query=' . $query;
  26. $parameters[] = 'inApps[]=' . $app;
  27. $url .= '?' . implode('&', $parameters);
  28. $this->sendingAToWithRequesttoken('GET', $url);
  29. }
  30. /**
  31. * @Then /^the list of search results has "(\d+)" results$/
  32. */
  33. public function theListOfSearchResultsHasResults(int $count) {
  34. $this->theHTTPStatusCodeShouldBe(200);
  35. $searchResults = json_decode($this->response->getBody());
  36. Assert::assertEquals($count, count($searchResults));
  37. }
  38. /**
  39. * @Then /^search result "(\d+)" contains$/
  40. *
  41. * @param int $number
  42. * @param TableNode $body
  43. */
  44. public function searchResultXContains(int $number, TableNode $body) {
  45. if (!($body instanceof TableNode)) {
  46. return;
  47. }
  48. $searchResults = json_decode($this->response->getBody(), $asAssociativeArray = true);
  49. $searchResult = $searchResults[$number];
  50. foreach ($body->getRowsHash() as $expectedField => $expectedValue) {
  51. if (!array_key_exists($expectedField, $searchResult)) {
  52. Assert::fail("$expectedField was not found in response");
  53. }
  54. Assert::assertEquals($expectedValue, $searchResult[$expectedField], "Field '$expectedField' does not match ({$searchResult[$expectedField]})");
  55. }
  56. }
  57. }