diff --git a/build/acceptance/config/behat.yml b/build/acceptance/config/behat.yml index 2ac1c077537..186a57c2023 100644 --- a/build/acceptance/config/behat.yml +++ b/build/acceptance/config/behat.yml @@ -6,6 +6,7 @@ default: paths: - %paths.base%/../features contexts: + - ActorContext - NextcloudTestServerContext - FeatureContext diff --git a/build/acceptance/features/bootstrap/FeatureContext.php b/build/acceptance/features/bootstrap/FeatureContext.php index b309a7e04b0..0598cbae959 100644 --- a/build/acceptance/features/bootstrap/FeatureContext.php +++ b/build/acceptance/features/bootstrap/FeatureContext.php @@ -23,6 +23,8 @@ use Behat\Behat\Context\Context; -class FeatureContext implements Context { +class FeatureContext implements Context, ActorAwareInterface { + + use ActorAware; } diff --git a/build/acceptance/features/core/Actor.php b/build/acceptance/features/core/Actor.php new file mode 100644 index 00000000000..0fea9be20c0 --- /dev/null +++ b/build/acceptance/features/core/Actor.php @@ -0,0 +1,92 @@ +. + * + */ + +/** + * An actor in a test scenario. + * + * Every Actor object is intended to be used only in a single test scenario. + * An Actor can control its web browser thanks to the Mink Session received when + * it was created, so in each scenario each Actor must have its own Mink + * Session; the same Mink Session can be used by different Actors in different + * scenarios, but never by different Actors in the same scenario. + * + * The test servers used in an scenario can change between different test runs, + * so an Actor stores the base URL for the current test server being used; in + * most cases the tests are specified using relative paths that can be converted + * to the appropriate absolute URL using locatePath() in the step + * implementation. + */ +class Actor { + + /** + * @var \Behat\Mink\Session + */ + private $session; + + /** + * @var string + */ + private $baseUrl; + + /** + * Creates a new Actor. + * + * @param \Behat\Mink\Session $session the Mink Session used to control its + * web browser. + * @param string $baseUrl the base URL used when solving relative URLs. + */ + public function __construct(\Behat\Mink\Session $session, $baseUrl) { + $this->session = $session; + $this->baseUrl = $baseUrl; + } + + /** + * Sets the base URL. + * + * @param string $baseUrl the base URL used when solving relative URLs. + */ + public function setBaseUrl($baseUrl) { + $this->baseUrl = $baseUrl; + } + + /** + * Returns the Mink Session used to control its web browser. + * + * @return \Behat\Mink\Session the Mink Session used to control its web + * browser. + */ + public function getSession() { + return $this->session; + } + + /** + * Returns the full path for the given relative path based on the base URL. + * + * @param string relativePath the relative path. + * @return string the full path. + */ + public function locatePath($relativePath) { + return $this->baseUrl . $relativePath; + } + +} diff --git a/build/acceptance/features/core/ActorAware.php b/build/acceptance/features/core/ActorAware.php new file mode 100644 index 00000000000..f1d355c1b0e --- /dev/null +++ b/build/acceptance/features/core/ActorAware.php @@ -0,0 +1,38 @@ +. + * + */ + +trait ActorAware { + + /** + * @var Actor + */ + private $actor; + + /** + * @param Actor $actor + */ + public function setCurrentActor(Actor $actor) { + $this->actor = $actor; + } + +} diff --git a/build/acceptance/features/core/ActorAwareInterface.php b/build/acceptance/features/core/ActorAwareInterface.php new file mode 100644 index 00000000000..9363bc3e607 --- /dev/null +++ b/build/acceptance/features/core/ActorAwareInterface.php @@ -0,0 +1,31 @@ +. + * + */ + +interface ActorAwareInterface { + + /** + * @param Actor $actor + */ + public function setCurrentActor(Actor $actor); + +} diff --git a/build/acceptance/features/core/ActorContext.php b/build/acceptance/features/core/ActorContext.php new file mode 100644 index 00000000000..e655911af67 --- /dev/null +++ b/build/acceptance/features/core/ActorContext.php @@ -0,0 +1,127 @@ +. + * + */ + +use Behat\Behat\Hook\Scope\BeforeStepScope; +use Behat\MinkExtension\Context\RawMinkContext; + +/** + * Behat context to set the actor used in sibling contexts. + * + * This helper context provides a step definition ("I act as XXX") to change the + * current actor of the scenario, which makes possible to use different browser + * sessions in the same scenario. + * + * Sibling contexts that want to have access to the current actor of the + * scenario must implement the ActorAwareInterface; this can be done just by + * using the ActorAware trait. + * + * Besides updating the current actor in sibling contexts the ActorContext also + * propagates its inherited "base_url" Mink parameter to the Actors as needed. + * + * Every actor used in the scenarios must have a corresponding Mink session + * declared in "behat.yml" with the same name as the actor. All used sessions + * are stopped after each scenario is run. + */ +class ActorContext extends RawMinkContext { + + /** + * @var array + */ + private $actors; + + /** + * @var Actor + */ + private $currentActor; + + /** + * Sets a Mink parameter. + * + * When the "base_url" parameter is set its value is propagated to all the + * Actors. + * + * @param string $name the name of the parameter. + * @param string $value the value of the parameter. + */ + public function setMinkParameter($name, $value) { + parent::setMinkParameter($name, $value); + + if ($name === "base_url") { + foreach ($this->actors as $actor) { + $actor->setBaseUrl($value); + } + } + } + + /** + * @BeforeScenario + * + * Initializes the Actors for the new Scenario with the default Actor. + * + * Other Actors are added (and their Mink Sessions started) only when they + * are used in an "I act as XXX" step. + */ + public function initializeActors() { + $this->actors = array(); + + $this->actors["default"] = new Actor($this->getSession(), $this->getMinkParameter("base_url")); + + $this->currentActor = $this->actors["default"]; + } + + /** + * @BeforeStep + */ + public function setCurrentActorInSiblingActorAwareContexts(BeforeStepScope $scope) { + $environment = $scope->getEnvironment(); + + foreach ($environment->getContexts() as $context) { + if ($context instanceof ActorAwareInterface) { + $context->setCurrentActor($this->currentActor); + } + } + } + + /** + * @Given I act as :actorName + */ + public function iActAs($actorName) { + if (!array_key_exists($actorName, $this->actors)) { + $this->actors[$actorName] = new Actor($this->getSession($actorName), $this->getMinkParameter("base_url")); + } + + $this->currentActor = $this->actors[$actorName]; + } + + /** + * @AfterScenario + * + * Stops all the Mink Sessions used in the last Scenario. + */ + public function cleanUpSessions() { + foreach ($this->actors as $actor) { + $actor->getSession()->stop(); + } + } + +}