AppConfiguration.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. use Behat\Behat\Hook\Scope\AfterScenarioScope;
  8. use Behat\Behat\Hook\Scope\BeforeScenarioScope;
  9. use PHPUnit\Framework\Assert;
  10. use Psr\Http\Message\ResponseInterface;
  11. require __DIR__ . '/../../vendor/autoload.php';
  12. trait AppConfiguration {
  13. /** @var string */
  14. private $currentUser = '';
  15. /** @var ResponseInterface */
  16. private $response = null;
  17. abstract public function sendingTo($verb, $url);
  18. abstract public function sendingToWith($verb, $url, $body);
  19. abstract public function theOCSStatusCodeShouldBe($statusCode);
  20. abstract public function theHTTPStatusCodeShouldBe($statusCode);
  21. /**
  22. * @Given /^parameter "([^"]*)" of app "([^"]*)" is set to "([^"]*)"$/
  23. * @param string $parameter
  24. * @param string $app
  25. * @param string $value
  26. */
  27. public function serverParameterIsSetTo($parameter, $app, $value) {
  28. $user = $this->currentUser;
  29. $this->currentUser = 'admin';
  30. $this->modifyServerConfig($app, $parameter, $value);
  31. $this->currentUser = $user;
  32. }
  33. /**
  34. * @param string $app
  35. * @param string $parameter
  36. * @param string $value
  37. */
  38. protected function modifyServerConfig($app, $parameter, $value) {
  39. $body = new \Behat\Gherkin\Node\TableNode([['value', $value]]);
  40. $this->sendingToWith('post', "/apps/testing/api/v1/app/{$app}/{$parameter}", $body);
  41. $this->theHTTPStatusCodeShouldBe('200');
  42. if ($this->apiVersion === 1) {
  43. $this->theOCSStatusCodeShouldBe('100');
  44. }
  45. }
  46. /**
  47. * @param string $app
  48. * @param string $parameter
  49. * @param string $value
  50. */
  51. protected function deleteServerConfig($app, $parameter) {
  52. $this->sendingTo('DELETE', "/apps/testing/api/v1/app/{$app}/{$parameter}");
  53. $this->theHTTPStatusCodeShouldBe('200');
  54. if ($this->apiVersion === 1) {
  55. $this->theOCSStatusCodeShouldBe('100');
  56. }
  57. }
  58. protected function setStatusTestingApp($enabled) {
  59. $this->sendingTo(($enabled ? 'post' : 'delete'), '/cloud/apps/testing');
  60. $this->theHTTPStatusCodeShouldBe('200');
  61. if ($this->apiVersion === 1) {
  62. $this->theOCSStatusCodeShouldBe('100');
  63. }
  64. $this->sendingTo('get', '/cloud/apps?filter=enabled');
  65. $this->theHTTPStatusCodeShouldBe('200');
  66. if ($enabled) {
  67. Assert::assertStringContainsString('testing', $this->response->getBody()->getContents());
  68. } else {
  69. Assert::assertStringNotContainsString('testing', $this->response->getBody()->getContents());
  70. }
  71. }
  72. abstract protected function resetAppConfigs();
  73. /**
  74. * @BeforeScenario
  75. *
  76. * Enable the testing app before the first scenario of the feature and
  77. * reset the configs before each scenario
  78. * @param BeforeScenarioScope $event
  79. */
  80. public function prepareParameters(BeforeScenarioScope $event) {
  81. $user = $this->currentUser;
  82. $this->currentUser = 'admin';
  83. $scenarios = $event->getFeature()->getScenarios();
  84. if ($event->getScenario() === reset($scenarios)) {
  85. $this->setStatusTestingApp(true);
  86. }
  87. $this->resetAppConfigs();
  88. $this->currentUser = $user;
  89. }
  90. /**
  91. * @AfterScenario
  92. *
  93. * Reset the values after the last scenario of the feature and disable the testing app
  94. * @param AfterScenarioScope $event
  95. */
  96. public function undoChangingParameters(AfterScenarioScope $event) {
  97. $scenarios = $event->getFeature()->getScenarios();
  98. if ($event->getScenario() === end($scenarios)) {
  99. $user = $this->currentUser;
  100. $this->currentUser = 'admin';
  101. $this->resetAppConfigs();
  102. $this->setStatusTestingApp(false);
  103. $this->currentUser = $user;
  104. }
  105. }
  106. }