AppConfiguration.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. *
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  10. * @author Sergio Bertolin <sbertolin@solidgear.es>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. use Behat\Behat\Hook\Scope\AfterScenarioScope;
  29. use Behat\Behat\Hook\Scope\BeforeScenarioScope;
  30. use PHPUnit\Framework\Assert;
  31. use Psr\Http\Message\ResponseInterface;
  32. require __DIR__ . '/../../vendor/autoload.php';
  33. trait AppConfiguration {
  34. /** @var string */
  35. private $currentUser = '';
  36. /** @var ResponseInterface */
  37. private $response = null;
  38. abstract public function sendingTo($verb, $url);
  39. abstract public function sendingToWith($verb, $url, $body);
  40. abstract public function theOCSStatusCodeShouldBe($statusCode);
  41. abstract public function theHTTPStatusCodeShouldBe($statusCode);
  42. /**
  43. * @Given /^parameter "([^"]*)" of app "([^"]*)" is set to "([^"]*)"$/
  44. * @param string $parameter
  45. * @param string $app
  46. * @param string $value
  47. */
  48. public function serverParameterIsSetTo($parameter, $app, $value) {
  49. $user = $this->currentUser;
  50. $this->currentUser = 'admin';
  51. $this->modifyServerConfig($app, $parameter, $value);
  52. $this->currentUser = $user;
  53. }
  54. /**
  55. * @param string $app
  56. * @param string $parameter
  57. * @param string $value
  58. */
  59. protected function modifyServerConfig($app, $parameter, $value) {
  60. $body = new \Behat\Gherkin\Node\TableNode([['value', $value]]);
  61. $this->sendingToWith('post', "/apps/testing/api/v1/app/{$app}/{$parameter}", $body);
  62. $this->theHTTPStatusCodeShouldBe('200');
  63. if ($this->apiVersion === 1) {
  64. $this->theOCSStatusCodeShouldBe('100');
  65. }
  66. }
  67. /**
  68. * @param string $app
  69. * @param string $parameter
  70. * @param string $value
  71. */
  72. protected function deleteServerConfig($app, $parameter) {
  73. $this->sendingTo('DELETE', "/apps/testing/api/v1/app/{$app}/{$parameter}");
  74. $this->theHTTPStatusCodeShouldBe('200');
  75. if ($this->apiVersion === 1) {
  76. $this->theOCSStatusCodeShouldBe('100');
  77. }
  78. }
  79. protected function setStatusTestingApp($enabled) {
  80. $this->sendingTo(($enabled ? 'post' : 'delete'), '/cloud/apps/testing');
  81. $this->theHTTPStatusCodeShouldBe('200');
  82. if ($this->apiVersion === 1) {
  83. $this->theOCSStatusCodeShouldBe('100');
  84. }
  85. $this->sendingTo('get', '/cloud/apps?filter=enabled');
  86. $this->theHTTPStatusCodeShouldBe('200');
  87. if ($enabled) {
  88. Assert::assertContains('testing', $this->response->getBody()->getContents());
  89. } else {
  90. Assert::assertNotContains('testing', $this->response->getBody()->getContents());
  91. }
  92. }
  93. abstract protected function resetAppConfigs();
  94. /**
  95. * @BeforeScenario
  96. *
  97. * Enable the testing app before the first scenario of the feature and
  98. * reset the configs before each scenario
  99. * @param BeforeScenarioScope $event
  100. */
  101. public function prepareParameters(BeforeScenarioScope $event) {
  102. $user = $this->currentUser;
  103. $this->currentUser = 'admin';
  104. $scenarios = $event->getFeature()->getScenarios();
  105. if ($event->getScenario() === reset($scenarios)) {
  106. $this->setStatusTestingApp(true);
  107. }
  108. $this->resetAppConfigs();
  109. $this->currentUser = $user;
  110. }
  111. /**
  112. * @AfterScenario
  113. *
  114. * Reset the values after the last scenario of the feature and disable the testing app
  115. * @param AfterScenarioScope $event
  116. */
  117. public function undoChangingParameters(AfterScenarioScope $event) {
  118. $scenarios = $event->getFeature()->getScenarios();
  119. if ($event->getScenario() === end($scenarios)) {
  120. $user = $this->currentUser;
  121. $this->currentUser = 'admin';
  122. $this->resetAppConfigs();
  123. $this->setStatusTestingApp(false);
  124. $this->currentUser = $user;
  125. }
  126. }
  127. }