1
0

Search.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018, Daniel Calviño Sánchez (danxuliu@gmail.com)
  4. *
  5. * @author Daniel Calviño Sánchez <danxuliu@gmail.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. use Behat\Gherkin\Node\TableNode;
  24. use PHPUnit\Framework\Assert;
  25. trait Search {
  26. // BasicStructure trait is expected to be used in the class that uses this
  27. // trait.
  28. /**
  29. * @When /^searching for "([^"]*)"$/
  30. * @param string $query
  31. */
  32. public function searchingFor(string $query) {
  33. $this->searchForInApp($query, '');
  34. }
  35. /**
  36. * @When /^searching for "([^"]*)" in app "([^"]*)"$/
  37. * @param string $query
  38. * @param string $app
  39. */
  40. public function searchingForInApp(string $query, string $app) {
  41. $url = '/index.php/core/search';
  42. $parameters[] = 'query=' . $query;
  43. $parameters[] = 'inApps[]=' . $app;
  44. $url .= '?' . implode('&', $parameters);
  45. $this->sendingAToWithRequesttoken('GET', $url);
  46. }
  47. /**
  48. * @Then /^the list of search results has "(\d+)" results$/
  49. */
  50. public function theListOfSearchResultsHasResults(int $count) {
  51. $this->theHTTPStatusCodeShouldBe(200);
  52. $searchResults = json_decode($this->response->getBody());
  53. Assert::assertEquals($count, count($searchResults));
  54. }
  55. /**
  56. * @Then /^search result "(\d+)" contains$/
  57. *
  58. * @param int $number
  59. * @param TableNode $body
  60. */
  61. public function searchResultXContains(int $number, TableNode $body) {
  62. if (!($body instanceof TableNode)) {
  63. return;
  64. }
  65. $searchResults = json_decode($this->response->getBody(), $asAssociativeArray = true);
  66. $searchResult = $searchResults[$number];
  67. foreach ($body->getRowsHash() as $expectedField => $expectedValue) {
  68. if (!array_key_exists($expectedField, $searchResult)) {
  69. Assert::fail("$expectedField was not found in response");
  70. }
  71. Assert::assertEquals($expectedValue, $searchResult[$expectedField], "Field '$expectedField' does not match ({$searchResult[$expectedField]})");
  72. }
  73. }
  74. }