MNT Add behat tests

This commit is contained in:
Steve Boyd 2021-11-13 20:33:35 +13:00
parent 363a115300
commit e55c81ec7c
23 changed files with 284 additions and 2 deletions

11
.github/workflows/main.yml vendored Normal file
View File

@ -0,0 +1,11 @@
name: Module CI
on:
push:
pull_request:
jobs:
ci:
uses: silverstripe/github-actions-ci-cd/.github/workflows/ci.yml@0.1.11
with:
run_endtoend: true

View File

@ -1,4 +1,10 @@
version: ~> 1.0 version: ~> 1.0
import: import:
- silverstripe/silverstripe-travis-shared:config/provision/standard-jobs-range-npm.yml - silverstripe/silverstripe-travis-shared:config/provision/standard-jobs-range-behat-npm.yml
env:
global:
- COMPOSER_ROOT_VERSION=2.x-dev
# Required for behat testing
- REQUIRE_EXTRA="silverstripe/widgets:^2 silverstripe/comments:^3 silverstripe/content-widget:^2"

29
behat.yml Normal file
View File

@ -0,0 +1,29 @@
default:
suites:
blog:
paths:
- "%paths.modules.blog%/tests/behat/features"
contexts:
- SilverStripe\Admin\Tests\Behat\Context\AdminContext
- SilverStripe\BehatExtension\Context\BasicContext
- SilverStripe\BehatExtension\Context\EmailContext
- SilverStripe\BehatExtension\Context\LoginContext
- SilverStripe\Framework\Tests\Behaviour\CmsFormsContext
- SilverStripe\Framework\Tests\Behaviour\CmsUiContext
- SilverStripe\Blog\Tests\Behat\Context\FeatureContext
- SilverStripe\Blog\Tests\Behat\Context\FixtureContext
-
SilverStripe\Blog\Tests\Behat\Context\FixtureContext:
- "%paths.modules.blog%/tests/behat/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"
SilverStripe\BehatExtension\Extension:
screenshot_path: "%paths.base%/artifacts/screenshots"
bootstrap_file: vendor/silverstripe/framework/tests/behat/serve-bootstrap.php

View File

@ -28,7 +28,8 @@
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"SilverStripe\\Blog\\": "src/", "SilverStripe\\Blog\\": "src/",
"SilverStripe\\Blog\\Tests\\": "tests/" "SilverStripe\\Blog\\Tests\\": "tests/php/",
"SilverStripe\\Blog\\Tests\\Behat\\Context\\": "tests/behat/src/"
} }
}, },
"license": "BSD-2-Clause", "license": "BSD-2-Clause",

View File

View File

@ -0,0 +1,121 @@
Feature: Create a blog
As a website user
I want to create a blog
Background:
Given the "group" "EDITOR group" has permissions "CMS_ACCESS_LeftAndMain"
And I add an extension "SilverStripe\Widgets\Extensions\WidgetPageExtension" to the "Page" class
And I add an extension "SilverStripe\Comments\Extensions\CommentsExtension" to the "Page" class
Given I take a screenshot after every step
Given I dump the rendered HTML after every step
And an "image" "Uploads/file1.jpg"
# Login then logout to created member
And I am logged in with "EDITOR" permissions
And I go to "/Security/login"
And I press the "Log in as someone else" button
And I am logged in with "ADMIN" permissions
# Create a new blog called "New Blog"
When I go to "/admin/pages"
And I press the "Add new" button
And I select the "Blog" radio button
And I press the "Create" button
# Add widgets
And I click the "Widgets" CMS tab
And I uncheck "Inherit Sidebar From Parent"
And I add the "Content" widget
And I add the "Archive" widget
And I add the "Blog Tags" widget
And I fill in the "Content" widget field "Title" with "My content widget title"
And I fill in the "Content" widget HTML field "Content" with "<p>Content widget content</p>"
And I fill in the "Archive" widget field "Title" with "My archive widget title"
And I fill in the "Blog Tags" widget field "Title" with "My blog tags widget title"
And I press the "Save" button
# Add EDITOR as an Editor
And I click the "Settings" CMS tab
And I click the "Users" CMS tab
And I select "EDITOR" from "Editors"
# Publish and logout
And I press the "Publish" button
And I go to "/Security/login"
And I press the "Log in as someone else" button
Scenario: Create a blog post
Given I log in with "EDITOR@example.org" and "Secret!123"
# Create a new blog post called "New Post"
When I go to "/admin/pages"
And I follow "New Blog"
And I click the "Blog Posts" CMS tab
And I press the "Add new Blog Post" button
And I fill in "Post Title" with "New Post"
# Add a "Featured image"
And I press the "Choose existing" button
# Select file1.jpg - asset-admin FeatureContext is not available here so use css selector
And I click on the ".gallery__files .gallery-item" element
And I press the "Insert" button
# Add categories and tags
And I click the "Post Options" CMS tab
And I add "My Category" to the "Categories" tag field
And I add "My Tag" to the "Tags" tag field
# Publish the blog post
And I press the "Publish" button
# Test the frontend
When I go to "/new-blog"
Then I should see "New Blog"
And I should see "New Post"
# Widgets
And I should see "My content widget title"
And the rendered HTML should contain "<p>Content widget content</p>"
And I should see "My blog tags widget title"
# Hyperlink to "New Post"
Then the rendered HTML should contain "href=\"/new-blog/new-post"
# Category
And the rendered HTML should contain "href=\"/new-blog/category/my-category\""
# Tag
And the rendered HTML should contain "href=\"/new-blog/tag/my-tag\""
# Test that blog post shows in category view
When I go to "/new-blog/category/my-category"
Then I should see "New Post"
# Test that blog post shows in tag view
When I go to "/new-blog/tag/my-tag"
Then I should see "New Post"
# Commenting
When I fill in "Your name" with "My Name"
And I fill in "Email" with "hello@example.com"
And I fill in "Comments" with "My comments"
When I press the "Post" button
Then I should see "New Post"
# Moderation
When I go to "/admin/pages"
And I follow "New Blog"
And I click the "Blog Posts" CMS tab
# Click on the first blog post
And I click on the ".col-Title" element
And I click the "Comments" CMS tab
Then I should see "Approved (1)"
When I click the "Approved (1)" CMS tab
Then I should see "hello@example.com"
When I click on the ".action-menu__toggle" element
And I press the "Spam" button
And I wait for 2 seconds
Then I should not see "hello@example.com"

BIN
tests/behat/files/file1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

View File

@ -0,0 +1,105 @@
<?php
namespace SilverStripe\Blog\Tests\Behat\Context;
use SilverStripe\BehatExtension\Context\SilverStripeContext;
use Behat\Mink\Element\NodeElement;
use PHPUnit\Framework\Assert;
class FeatureContext extends SilverStripeContext
{
/**
* Adds a widget to the blog
*
* @Then /^I add the "([^"]+)" widget$/
* @param $widgetTitle e.g. "Content"
*/
public function iAddTheWidget($widgetTitle)
{
$page = $this->getSession()->getPage();
$h3s = $page->findAll('css', '.availableWidgetsHolder h3');
$found = false;
foreach ($h3s as $h3) {
if ($h3->getText() !== $widgetTitle) {
continue;
}
$found = true;
$h3->click();
}
Assert::assertTrue($found, "Widget {$widgetTitle} was not found");
}
/**
* Fills in a field within a widget
*
* @Then /^I fill in the "([^"]+)" widget field "([^"]+)" with "([^"]+)"$/
* @param $widgetTitle e.g. "Content"
* @param $htmlFragment e.g. "Title"
* @param $value e.g. "Lorem ipsum"
*/
public function iFillInTheWidgetField($widgetTitle, $fieldTitle, $value)
{
$page = $this->getSession()->getPage();
$widget = $this->getWidget($widgetTitle);
$field = $widget->findField($fieldTitle);
Assert::assertNotNull($field, "Widget field {$fieldTitle} was not found");
$field->setValue($value);
}
/**
* Adapated from framework CmsFormsContext stepIFillInTheHtmlFieldWith
*
* @When /^I fill in the "([^"]+)" widget HTML field "([^"]+)" with "([^"]+)"$/
*/
public function stepIFillInTheHtmlFieldWith($widgetTitle, $fieldTitle, $value)
{
$widget = $this->getWidget($widgetTitle);
$field = $this->getDescendantHtmlField($widget, $fieldTitle);
$this->getSession()->evaluateScript(sprintf(
"jQuery('#%s').entwine('ss').getEditor().setContent('%s')",
$field->getAttribute('id'),
addcslashes($value, "'")
));
$this->getSession()->evaluateScript(sprintf(
"jQuery('#%s').entwine('ss').getEditor().save()",
$field->getAttribute('id')
));
}
/**
* @return NodeElement|null
*/
private function getWidget($widgetTitle)
{
$ret = null;
$widgets = $this->getSession()->getPage()->findAll('css', '.usedWidgets .Widget');
foreach ($widgets as $widget) {
$h3 = $widget->find('css', 'h3');
if (!$h3 || $h3->getText() !== $widgetTitle) {
continue;
}
$ret = $widget;
break;
}
Assert::assertNotNull($ret, "Widget edit form for {$widgetTitle} was not found");
return $ret;
}
/**
* @param NodeElement $ancestor
* @param string $locator
* @return NodeElement|null
*/
private function getDescendantHtmlField($element, $locator)
{
$textarea = $element->find('css', "textarea.htmleditor[name='{$locator}']");
if (is_null($textarea)) {
$labels = $element->findAll('xpath', "//label[contains(text(), '{$locator}')]");
Assert::assertCount(1, $labels, "Found more than one html field label containing the phrase '{$locator}}'");
$label = array_shift($labels);
$textarea = $element->find('css', '#' . $label->getAttribute('for'));
}
Assert::assertNotNull($textarea, "HTML field {$locator} not found");
return $textarea;
}
}

View File

@ -0,0 +1,9 @@
<?php
namespace SilverStripe\Blog\Tests\Behat\Context;
use SilverStripe\BehatExtension\Context\FixtureContext as BaseFixtureContext;
class FixtureContext extends BaseFixtureContext
{
}