Generalize file list steps so a specific ancestor can be used

The "FileListContext" provides steps to interact with and check the
behaviour of a file list. However, the "FileListContext" does not know
the right file list ancestor that has to be used by the file list steps,
so until now the file list steps were explicitly wired to the Files app
and they could be used only in that case.

Instead of duplicating the steps with a slightly different name (for
example, "I rename :fileName1 to :fileName2 in the public shared folder"
instead of "I rename :fileName1 to :fileName2") the steps were
generalized; now contexts that "know" that certain file list ancestor
has to be used by the FileListContext steps performed by certain actor
from that point on (until changed again) set it explicitly. For example,
when the current page is the Files app then the ancestor of the file
list is the main view of the current section of the Files app, but when
the current page is a shared link then the ancestor is set to null
(because there will be just one file list, and thus its ancestor is not
relevant to differentiate between instances)

A helper trait, "FileListAncestorSetter", was introduced to reduce the
boilerplate needed to set the file list ancestor from other contexts.

Signed-off-by: Daniel Calviño Sánchez <danxuliu@gmail.com>
This commit is contained in:
Daniel Calviño Sánchez 2018-02-19 16:58:57 +01:00
parent 3c6a269138
commit d757a19b9c
4 changed files with 115 additions and 3 deletions

View File

@ -0,0 +1,67 @@
<?php
/**
*
* @copyright Copyright (c) 2018, Daniel Calviño Sánchez (danxuliu@gmail.com)
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
/**
* Helper trait to set the ancestor of the file list.
*
* The FileListContext provides steps to interact with and check the behaviour
* of a file list. However, the FileListContext does not know the right file
* list ancestor that has to be used by the file list steps; this has to be set
* from other contexts, for example, when the Files app or the public page for a
* shared folder is opened.
*
* Contexts that "know" that certain file list ancestor has to be used by the
* FileListContext steps should use this trait and call
* "setFileListAncestorForActor" when needed.
*/
trait FileListAncestorSetter {
/**
* @var FileListContext
*/
private $fileListContext;
/**
* @BeforeScenario
*/
public function getSiblingFileListContext(BeforeScenarioScope $scope) {
$environment = $scope->getEnvironment();
$this->fileListContext = $environment->getContext("FileListContext");
}
/**
* Sets the file list ancestor to be used in the file list steps performed
* by the given actor.
*
* @param null|Locator $fileListAncestor the file list ancestor
* @param Actor $actor the actor
*/
private function setFileListAncestorForActor($fileListAncestor, Actor $actor) {
$this->fileListContext->setFileListAncestorForActor($fileListAncestor, $actor);
}
}

View File

@ -25,7 +25,15 @@ use Behat\Behat\Context\Context;
class FileListContext implements Context, ActorAwareInterface { class FileListContext implements Context, ActorAwareInterface {
use ActorAware; /**
* @var Actor
*/
private $actor;
/**
* @var array
*/
private $fileListAncestorsByActor;
/** /**
* @var Locator * @var Locator
@ -35,8 +43,39 @@ class FileListContext implements Context, ActorAwareInterface {
/** /**
* @BeforeScenario * @BeforeScenario
*/ */
public function initializeFileListAncestor() { public function initializeFileListAncestors() {
$this->fileListAncestor = FilesAppContext::currentSectionMainView(); $this->fileListAncestorsByActor = array();
$this->fileListAncestor = null;
}
/**
* @param Actor $actor
*/
public function setCurrentActor(Actor $actor) {
$this->actor = $actor;
if (array_key_exists($actor->getName(), $this->fileListAncestorsByActor)) {
$this->fileListAncestor = $this->fileListAncestorsByActor[$actor->getName()];
} else {
$this->fileListAncestor = null;
}
}
/**
* Sets the file list ancestor to be used in the steps performed by the
* given actor from that point on (until changed again).
*
* This is meant to be called from other contexts, for example, when the
* Files app or the public page for a shared folder are opened.
*
* The FileListAncestorSetter trait can be used to reduce the boilerplate
* needed to set the file list ancestor from other contexts.
*
* @param null|Locator $fileListAncestor the file list ancestor
* @param Actor $actor the actor
*/
public function setFileListAncestorForActor($fileListAncestor, Actor $actor) {
$this->fileListAncestorsByActor[$actor->getName()] = $fileListAncestor;
} }
/** /**

View File

@ -26,6 +26,7 @@ use Behat\Behat\Context\Context;
class FilesAppContext implements Context, ActorAwareInterface { class FilesAppContext implements Context, ActorAwareInterface {
use ActorAware; use ActorAware;
use FileListAncestorSetter;
/** /**
* @return array * @return array
@ -305,6 +306,8 @@ class FilesAppContext implements Context, ActorAwareInterface {
PHPUnit_Framework_Assert::assertStringStartsWith( PHPUnit_Framework_Assert::assertStringStartsWith(
$this->actor->locatePath("/apps/files/"), $this->actor->locatePath("/apps/files/"),
$this->actor->getSession()->getCurrentUrl()); $this->actor->getSession()->getCurrentUrl());
$this->setFileListAncestorForActor(self::currentSectionMainView(), $this->actor);
} }
/** /**

View File

@ -26,6 +26,7 @@ use Behat\Behat\Context\Context;
class FilesSharingAppContext implements Context, ActorAwareInterface { class FilesSharingAppContext implements Context, ActorAwareInterface {
use ActorAware; use ActorAware;
use FileListAncestorSetter;
/** /**
* @return Locator * @return Locator
@ -90,6 +91,8 @@ class FilesSharingAppContext implements Context, ActorAwareInterface {
PHPUnit_Framework_Assert::assertEquals( PHPUnit_Framework_Assert::assertEquals(
$this->actor->getSharedNotebook()["shared link"], $this->actor->getSharedNotebook()["shared link"],
$this->actor->getSession()->getCurrentUrl()); $this->actor->getSession()->getCurrentUrl());
$this->setFileListAncestorForActor(null, $this->actor);
} }
/** /**