Search.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. *
  4. * @copyright Copyright (c) 2018, Daniel Calviño Sánchez (danxuliu@gmail.com)
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. use Behat\Gherkin\Node\TableNode;
  23. use PHPUnit\Framework\Assert;
  24. trait Search {
  25. // BasicStructure trait is expected to be used in the class that uses this
  26. // trait.
  27. /**
  28. * @When /^searching for "([^"]*)"$/
  29. * @param string $query
  30. */
  31. public function searchingFor(string $query) {
  32. $this->searchForInApp($query, '');
  33. }
  34. /**
  35. * @When /^searching for "([^"]*)" in app "([^"]*)"$/
  36. * @param string $query
  37. * @param string $app
  38. */
  39. public function searchingForInApp(string $query, string $app) {
  40. $url = '/index.php/core/search';
  41. $parameters[] = 'query=' . $query;
  42. $parameters[] = 'inApps[]=' . $app;
  43. $url .= '?' . implode('&', $parameters);
  44. $this->sendingAToWithRequesttoken('GET', $url);
  45. }
  46. /**
  47. * @Then /^the list of search results has "(\d+)" results$/
  48. */
  49. public function theListOfSearchResultsHasResults(int $count) {
  50. $this->theHTTPStatusCodeShouldBe(200);
  51. $searchResults = json_decode($this->response->getBody());
  52. Assert::assertEquals($count, count($searchResults));
  53. }
  54. /**
  55. * @Then /^search result "(\d+)" contains$/
  56. *
  57. * @param int $number
  58. * @param TableNode $body
  59. */
  60. public function searchResultXContains(int $number, TableNode $body) {
  61. if (!($body instanceof TableNode)) {
  62. return;
  63. }
  64. $searchResults = json_decode($this->response->getBody(), $asAssociativeArray = true);
  65. $searchResult = $searchResults[$number];
  66. foreach ($body->getRowsHash() as $expectedField => $expectedValue) {
  67. if (!array_key_exists($expectedField, $searchResult)) {
  68. Assert::fail("$expectedField was not found in response");
  69. }
  70. Assert::assertEquals($expectedValue, $searchResult[$expectedField], "Field '$expectedField' does not match ({$searchResult[$expectedField]})");
  71. }
  72. }
  73. }