AppConfiguration.php 3.8 KB

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