Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

[Behat] WIP Improve structure and usability of PlatformUI behat Contexts #602

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions Features/Context/ContentActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,29 @@
*/
namespace EzSystems\PlatformUIBundle\Features\Context;

use PHPUnit_Framework_Assert as Assertion;

class ContentActions extends PlatformUI
{
/**
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\ContentEditContext
*/
protected $contentEditContext;

/**
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
*/
protected $dashboardContext;

/**
*/
private function iSeeNotification($message)
{
$this->sleep();
$result = $this->getElementByText($message, '.ez-notification-text');
Assertion::AssertNotNull($result);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instance var not really necessary, if not for consistency maybe?

}

/**
* @Then I am notified that :name has been copied under :destiny
*/
Expand All @@ -25,10 +46,11 @@ public function iSeeCopiedNotification($name, $destiny)
*/
public function moveInto($name, $destiny)
{
$this->onFullView($name);
$this->clickActionBar('Move');
$this->selectFromUniversalDiscovery("eZ Platform/$destiny");
$this->confirmSelection();
$this->contentEditContext->onFullView($name);
$this->waitWhileLoading();
Copy link
Contributor

@joaoinacio joaoinacio Jun 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems that the onFullView() needs to waitWhileLoading afterwards.
If so, adding it there instead would allow removing all waits after using it.

$this->dashboardContext->clickActionBar('Move');
$this->dashboardContext->selectFromUniversalDiscovery("eZ Platform/$destiny");
$this->dashboardContext->confirmSelection();
$destinyName = explode('/', $destiny);
$destinyName = end($destinyName);
$this->iSeeMovedNotification($name, $destinyName);
Expand Down Expand Up @@ -58,9 +80,9 @@ public function iSeeMovedNotification($name, $destiny)
*/
public function removeContent($name)
{
$this->onFullView($name);
$this->contentEditContext->onFullView($name);
$this->waitWhileLoading();
$this->clickActionBar('Send to Trash');
$this->dashboardContext->clickActionBar('Send to Trash');
}

/**
Expand Down
67 changes: 36 additions & 31 deletions Features/Context/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@
*/
namespace EzSystems\PlatformUIBundle\Features\Context;

use EzSystems\BehatBundle\ObjectManager\FieldType;
use Behat\Mink\WebAssert;
use eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext as FieldType;

class Fields extends PlatformUI
{
const NOTIFICATION_CONTENT_PUBLISHED = 'Content has been published';
const NOTIFICATION_PUBLISH_ERROR = 'An error occured while publishing the draft';

/**
* @var eZ\Bundle\EzPublishCoreBundle\Features\Context\FieldTypeContext
*/
protected $fieldtypeContext;

/**
* @var EzSystems\PlatformUIBundle\Features\Context\SubContext\DashboardContext
*/
protected $dashboardContext;

protected function getFieldIdentCss($identifier, $contentId = '')
{
return "ez-field-$contentId-$identifier";
Expand Down Expand Up @@ -49,12 +59,11 @@ protected function getEditLabelCss($type, $fieldIdentifier = '', $contentId = ''

protected function getThisFieldIdent()
{
$fieldManager = $this->getFieldTypeManager();
$identifier = $fieldManager->getThisFieldTypeIdentifier();
$identifier = $this->fieldtypeContext->getThisFieldTypeIdentifier();

// check if we are editing a field for draft or published content
if ($fieldManager->getFieldContentState() == FieldType::CONTENT_PUBLISHED) {
$contentId = $fieldManager->getThisContentId();
if ($this->fieldtypeContext->getFieldContentState() == FieldType::CONTENT_PUBLISHED) {
$contentId = $this->fieldtypeContext->getThisContentId();
} else {
$contentId = '';
}
Expand All @@ -67,14 +76,13 @@ protected function getThisFieldIdent()
*/
public function createAContentOfThisType()
{
$fieldManager = $this->getFieldTypeManager();
$fieldManager->setFieldContentState(FieldType::CONTENT_TYPE_PUBLISHED);
$name = $fieldManager->getThisContentTypeName();
$this->fieldtypeContext->setFieldContentState(FieldType::CONTENT_TYPE_PUBLISHED);
$name = $this->fieldtypeContext->getThisContentTypeName();

$this->clickNavigationZone('Content');
$this->clickNavigationItem('Content structure');
$this->clickActionBar('Create');
$this->clickContentType($name);
$this->dashboardContext->clickNavigationZone('Content');
$this->dashboardContext->clickNavigationItem('Content structure');
$this->dashboardContext->clickActionBar('Create');
$this->dashboardContext->clickContentType($name);
$this->platformStatus = self::WAITING_FOR_PUBLISHING;
}

Expand All @@ -83,14 +91,13 @@ public function createAContentOfThisType()
*/
public function editThisContent()
{
$fieldManager = $this->getFieldTypeManager();
$fieldManager->setFieldContentState(FieldType::CONTENT_PUBLISHED);
$name = $fieldManager->getThisContentName();
$this->fieldtypeContext->setFieldContentState(FieldType::CONTENT_PUBLISHED);
$name = $this->fieldtypeContext->getThisContentName();

$this->clickNavigationZone('Content');
$this->clickNavigationItem('Content structure');
$this->clickOnTreePath($name);
$this->clickActionBar('Edit');
$this->dashboardContext->clickNavigationZone('Content');
$this->dashboardContext->clickNavigationItem('Content structure');
$this->dashboardContext->clickOnTreePath($name);
$this->dashboardContext->clickActionBar('Edit');
$this->platformStatus = self::WAITING_FOR_PUBLISHING;
// assert
$this->iSeeContentEditView();
Expand Down Expand Up @@ -147,7 +154,7 @@ public function uncheckFieldValue()
public function publishContent()
{
if ($this->platformStatus == self::WAITING_FOR_PUBLISHING) {
$this->clickEditActionBar('Publish');
$this->dashboardContext->clickEditActionBar('Publish');
} else {
throw new \Exception('Cannot publish content, application in wrong state');
}
Expand All @@ -161,9 +168,8 @@ public function publishContent()
public function seeRequiredFieldtOfType($label)
{
if ($this->platformStatus == self::WAITING_FOR_PUBLISHING) {
$fieldManager = $this->getFieldTypeManager();
// $type = ...
//$name = $fieldManager->getThisFieldTypeName();
//$name = $fieldtypeContext->getThisFieldTypeName();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleanup comments instead of updating them


$verification = new WebAssert($this->getSession());
//$verification->elementTextContains('css', $this->getEditLabelCss($type, $name), $label . '*');.
Expand All @@ -186,7 +192,7 @@ public function iSeeContentEditView()
*/
public function contentIsPublished()
{
$this->iSeeNotification(self::NOTIFICATION_CONTENT_PUBLISHED);
$this->dashboardContext->iSeeNotification(self::NOTIFICATION_CONTENT_PUBLISHED);
}

/**
Expand All @@ -205,11 +211,11 @@ public function failsWithMessage($message)
public function seeFieldtOfType($type, $label = null)
{
if ($this->platformStatus == self::WAITING_FOR_PUBLISHING) {
$this->clickEditActionBar('Publish');
$this->dashboardContext->clickEditActionBar('Publish');
}
$verification = new WebAssert($this->getSession());
// for view we need the internal field identifier...
$internalName = $this->getFieldTypeManager()->getFieldTypeInternalIdentifier($type);
$internalName = $this->fieldtypeContext->getFieldTypeInternalIdentifier($type);
$verification->elementExists('css', ".ez-fieldview-$internalName .ez-fieldview-value-content");

if ($label != null) {
Expand All @@ -222,13 +228,12 @@ public function seeFieldtOfType($type, $label = null)
*/
public function viewThisContent()
{
$fieldManager = $this->getFieldTypeManager();
$fieldManager->setFieldContentState(FieldType::CONTENT_PUBLISHED);
$name = $fieldManager->getThisContentName();
$this->fieldtypeContext->setFieldContentState(FieldType::CONTENT_PUBLISHED);
$name = $this->fieldtypeContext->getThisContentName();

$this->clickNavigationZone('Content');
$this->clickNavigationItem('Content structure');
$this->clickOnTreePath($name);
$this->dashboardContext->clickNavigationZone('Content');
$this->dashboardContext->clickNavigationItem('Content structure');
$this->dashboardContext->clickOnTreePath($name);
}

/**
Expand Down
111 changes: 63 additions & 48 deletions Features/Context/PlatformUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
*/
namespace EzSystems\PlatformUIBundle\Features\Context;

use EzSystems\BehatBundle\Context\Browser\Context;
use ReflectionClass;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\MinkExtension\Context\RawMinkContext;

class PlatformUI extends Context
class PlatformUI extends RawMinkContext
{
/**
* Default Platform URI.
Expand Down Expand Up @@ -44,9 +46,6 @@ class PlatformUI extends Context
const NOT_WAITING = 0;
const WAITING_FOR_PUBLISHING = 1;

use SubContext\Authentication;
use SubContext\CommonActions;

/**
* PlatformUI relative URL path.
*
Expand Down Expand Up @@ -79,21 +78,13 @@ class PlatformUI extends Context
*/
protected $platformStatus = self::NOT_WAITING;

/**
* Mapping of the new paths of contents after being moved.
*/
protected $newPathsMap = array();

/**
* Initialize class.
*
* @param string $uri
*/
public function __construct($uri = self::PLATFORM_URI, $user = null, $password = null)
{
parent::__construct();
$this->pageIdentifierMap['roles'] = '/ez#/admin/pjax%2Frole';
$this->pageIdentifierMap['users'] = '/ez#/view/%2Fapi%2Fezp%2Fv2%2Fcontent%2Flocations%2F1%2F5/eng-GB';
$this->platformUiUri = $uri;
if ($user != null) {
$this->user = $user;
Expand All @@ -103,6 +94,30 @@ public function __construct($uri = self::PLATFORM_URI, $user = null, $password =
}
}

/**
* @BeforeScenario
Copy link
Contributor

@joaoinacio joaoinacio Jun 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would need some doc.
something in the lines of:

 *
 * Parses property typehints and automatically assigns them the context instances, if any.
 */

*
* Default gather contexts method.
* Uses doc comment type hint to get the contexts and set them
*/
public function gatherContexts(BeforeScenarioScope $scope)
{
$refClass = new ReflectionClass($this);
$refProperties = $refClass->getProperties();
foreach ($refProperties as $refProperty) {
$propertyName = $refProperty->getName();
// get property type hint from doc comment
preg_match_all('#@var\s(.*?)\n#s', $refProperty->getDocComment(), $matches);
foreach ($matches[1] as $typeHint) {
// checks if the type hint implements Behat Context
$implements = @class_implements($typeHint);
if (!empty($implements) && in_array('Behat\Behat\Context\Context', $implements)) {
$this->$propertyName = $scope->getEnvironment()->getContext($typeHint);
}
}
}
}

/**
* @BeforeScenario
*/
Expand All @@ -118,7 +133,6 @@ public function afterScenario()
{
$this->closeConfirmBox();
$this->closeEditView();
$this->iLogout();
}

/**
Expand All @@ -130,11 +144,41 @@ public function beforeStep()
$this->waitWhileLoading();
}

/**
* @AfterStep
*/
public function afterStep()
public function fillFieldWithValue($field, $value = '')
{
$fieldNode = $this->spin(
function () use ($field) {
$fieldNode = $this->getSession()->getPage()->findField($field);
if ($fieldNode == null) {
throw new \Exception('Field not found');
}

return $fieldNode;
}
);

$this->spin(
function () use ($fieldNode, $field, $value) {
// make sure any autofocus elements don't mis-behave when setting value
$fieldNode->blur();
usleep(10 * 1000);
$fieldNode->focus();
usleep(10 * 1000);

// setting value on pre-filled inputs can cause issues, clearing before
$fieldNode->setValue('');
$fieldNode->setValue($value);

// verication that the field was really filled in correctly
$this->sleep();
$check = $this->getSession()->getPage()->findField($field)->getValue();
if ($check != $value) {
throw new \Exception('Failed to set the field value: ' . $check);
}

return true;
}
);
}

/**
Expand Down Expand Up @@ -284,7 +328,7 @@ function () use ($selector, $baseElement, $checkVisibility) {
* @param string $selector CSS selector of the element
* @param string $textSelector Extra CSS selector for text of the element
* @param string $baseElement Element in which the search is based
* @param int $iteration Iteration number, used to control number of executions
*
* @return array
*/
protected function getElementByText($text, $selector, $textSelector = null, $baseElement = null)
Expand Down Expand Up @@ -408,33 +452,4 @@ protected function closeEditView()
} catch (\Exception $e) {
}
}
/**
* Attaches a file to a input field on the HTML.
*
* @param string $file file name relative to mink definitions
* @param string $selector CSS file upload element selector
*/
protected function attachFile($fileName, $selector)
{
if ($this->getMinkParameter('files_path')) {
$fullPath = rtrim(
realpath(
$this->getMinkParameter('files_path')
),
DIRECTORY_SEPARATOR
) . DIRECTORY_SEPARATOR . $fileName;

if (is_file($fullPath)) {
$fileInput = 'input[type="file"]' . $selector;
$field = $this->getSession()->getPage()->find('css', $fileInput);

if (null === $field) {
throw new Exception("File input $selector is not found");
}
$field->attachFile($fullPath);
}
} else {
throw new Exception("File $fileName is not found at the given location: $fullPath");
}
}
}
Loading