Skip to content

Commit

Permalink
MNT Add behat test for workflow actions diff (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Jun 14, 2023
1 parent 1a3b4c6 commit ae7ac9a
Show file tree
Hide file tree
Showing 16 changed files with 189 additions and 2 deletions.
25 changes: 25 additions & 0 deletions behat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
default:
suites:
silverstripe-advancedworkflow:
paths:
- '%paths.modules.silverstripe-advancedworkflow%/tests/behat/features'
contexts:
- SilverStripe\Framework\Tests\Behaviour\CmsFormsContext
- SilverStripe\Framework\Tests\Behaviour\CmsUiContext
- SilverStripe\BehatExtension\Context\BasicContext
- SilverStripe\BehatExtension\Context\LoginContext
- Symbiote\AdvancedWorkflow\Tests\Behat\Context\FeatureContext
- Symbiote\AdvancedWorkflow\Tests\Behat\Context\FixtureContext:
# Note: double indent for args is intentional
- "%paths.modules.silverstripe-advancedworkflow%/tests/behat/features/files/"
extensions:
SilverStripe\BehatExtension\MinkExtension:
default_session: facebook_web_driver
javascript_session: facebook_web_driver
facebook_web_driver:
browser: chrome
wd_host: "http://127.0.0.1:9515" #chromedriver port
browser_name: chrome
SilverStripe\BehatExtension\Extension:
bootstrap_file: vendor/silverstripe/cms/tests/behat/serve-bootstrap.php
screenshot_path: '%paths.base%/artifacts/screenshots'
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@
"autoload": {
"psr-4": {
"Symbiote\\AdvancedWorkflow\\": "src/",
"Symbiote\\AdvancedWorkflow\\Tests\\": "tests/"
"Symbiote\\AdvancedWorkflow\\Tests\\": "tests/php/",
"Symbiote\\AdvancedWorkflow\\Tests\\Behat\\": "tests/behat/"
}
},
"replace": {
"silverstripe/advancedworkflow": "self.version"
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
53 changes: 53 additions & 0 deletions tests/behat/Context/FeatureContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Symbiote\AdvancedWorkflow\Tests\Behat\Context;

use Behat\Mink\Element\Element;
use Behat\Mink\Exception\ElementHtmlException;
use SilverStripe\BehatExtension\Context\SilverStripeContext;

if (!class_exists(SilverStripeContext::class)) {
return;
}

class FeatureContext extends SilverStripeContext
{
/**
* Example: Then the workflow diff for the "Title" field should be "About <ins>Us!</ins><del>Us</del>"
*
* @Then /^the workflow diff for the "([^"]+)" field should be "(.*)"$/
*/
public function theFieldDiffShouldBe(string $field, string $diff)
{
$element = $this->assertSession()->elementExists('css', "#workflow-$field");
$actualHtml = $element->getHtml();

$message = sprintf('The diff "%s" for the "%s" field did not match "%s"', $actualHtml, $field, $diff);

$this->assertElement(
(bool) preg_match($this->convertDiffToRegex($diff), $actualHtml),
$message,
$element
);
}

/**
* Allow for arbitrary whitespace before/after HTML tags, and before/after the diff as a whole.
*/
private function convertDiffToRegex(string $diff): string
{
return '/\s*' . str_replace(['\<', '\>'], ['\s*\<', '\>\s*'], preg_quote($diff, '/')) . '\s*/u';
}

/**
* @see Behat\Mink\WebAssert::assertElement()
*/
private function assertElement(bool $condition, string $message, Element $element): void
{
if ($condition) {
return;
}

throw new ElementHtmlException($message, $this->getSession()->getDriver(), $element);
}
}
75 changes: 75 additions & 0 deletions tests/behat/Context/FixtureContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Symbiote\AdvancedWorkflow\Tests\Behat\Context;

use PHPUnit\Framework\Assert;
use SilverStripe\BehatExtension\Context\FixtureContext as BaseFixtureContext;
use SilverStripe\Core\Injector\Injector;
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowDefinition;
use Symbiote\AdvancedWorkflow\Services\WorkflowService;

if (!class_exists(BaseFixtureContext::class)) {
return;
}

/**
* Context used to create fixtures for workflow-related behat tests.
*/
class FixtureContext extends BaseFixtureContext
{
/**
* Create a workflow, typically in the background stage of a feature.
* Example: Given a workflow "My Workflow" using the "Review and Approve" template
*
* @Given /^(?:an|a|the) workflow "([^"]+)" using the "([^"]+)" template$/
*/
public function stepCreateBackgroundWorkflow(string $title, string $template)
{
Assert::assertNotNull(
$this->getWorkflowService()->getNamedTemplate($template),
"Workflow template named '$template' does not exist."
);

if ($existingDefinition = WorkflowDefinition::get()->find('Title', $title)) {
Assert::assertEquals(
$template,
$existingDefinition->Template,
"A workflow named '$title' already exists, but it doesn't use the '$template' template."
);
// If we haven't thrown an exception here, it means the exact workflow we want already exists
return;
}

$definition = WorkflowDefinition::create();
$definition->Title = $title;
$definition->Template = $template;
$definition->write();
}

/**
* Apply a workflow to a record.
* Example: Given the "page" "About Us" has the "My Workflow" workflow
*
* @Given /^(?:an|a|the) "([^"]+)" "([^"]+)" has the "([^"]+)" workflow$/
*/
public function stepPageHasWorkflow(string $type, string $pageName, string $workflowName)
{
$workflow = WorkflowDefinition::get()->find('Title', $workflowName);

Assert::assertNotNull($workflow, "Workflow named '$workflowName' does not exist.");

$class = $this->convertTypeToClass($type);
$fixture = $this->getFixtureFactory()->get($class, $pageName);
if (!$fixture) {
$fixture = $this->getFixtureFactory()->createObject($class, $pageName);
}

$fixture->WorkflowDefinitionID = $workflow->ID;
$fixture->write();
}

private function getWorkflowService(): WorkflowService
{
return Injector::inst()->get(WorkflowService::class);
}
}
33 changes: 33 additions & 0 deletions tests/behat/features/workflow-history.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@javascript
Feature: Workflow Actions history
As a cms author
I want to see the history of workflow actions on a page
So that I can have assurance that correct processes are being followed

Background:
Given a workflow "Two-Step" using the "Review and Approve" template
And a "page" "About Us" has the "Content" "<p>My content</p>"
And the "page" "About Us" has the "Two-Step" workflow
And the "page" "About Us" is published
And the "group" "AUTHOR" has permissions "Access to 'Pages' section"
And I am logged in as a member of "AUTHOR" group
And I go to "/admin/pages"
Then I should see "About Us" in the tree

Scenario: I can see page edits as a diff in the Workflow Actions tab
When I click on "About Us" in the tree
Then I should see an edit page form
When I fill in "Title" with "About Us!"
And I fill in the "Content" HTML field with "<p>my new content</p>"
And I press the "Apply for approval" button
And I wait for 3 seconds
# Form fields should be readonly
Then I should see a "#Form_EditForm_Title.readonly" element
And I should see a "#Form_EditForm_Content.readonly" element
# Save and Apply for approval buttons shouldn't be there anymore
And I should not see "Apply for approval"
And I should not see "Save"
When I click the "Workflow Actions" CMS tab
Then the workflow diff for the "Title" field should be "About <ins>Us!</ins><del>Us</del>"
And the workflow diff for the "Content" field should be "<p><ins>my new</ins><del>My</del> content</p>"

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit ae7ac9a

Please sign in to comment.