Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Behat feature and (context) to view a list of versions in History Viewer #14

Merged
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
107 changes: 102 additions & 5 deletions tests/Behat/Context/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SilverStripe\VersionedAdmin\Tests\Behat\Context;

use Behat\Mink\Element\NodeElement;
use SilverStripe\BehatExtension\Context\SilverStripeContext;

if (!class_exists(SilverStripeContext::class)) {
Expand All @@ -21,17 +22,113 @@ public function iShouldSeeAListOfVersions()
}

/**
* @When I click on the first version
* @Then I should see a list of versions in descending order
*/
public function iClickOnTheFirstVersion()
public function iShouldSeeAListOfVersionsInDescendingOrder()
{
$page = $this->getSession()->getPage();
$firstVersion = $page->find('css', '.history-viewer .table tbody tr');
assertNotNull($firstVersion, 'I should see a list of versions');
$versions = $page->findAll('css', '.history-viewer .table tbody tr td:nth-of-type(1)');

$firstVersion->click();
$previous = null;
foreach ($versions as $version) {
/** @var NodeElement $version */
if ($previous) {
assert($version->getValue() < $previous);
}
$previous = $version->getValue();
}
}

/**
* @When I click on the first version
*/
public function iClickOnTheFirstVersion()
{
assertNotNull($this->getLatestVersion(), 'I should see a list of versions');
$this->getLatestVersion()->click();

// Wait for the form builder to load
$this->getSession()->wait(3000);
}

/**
* Example: I should see the "Live" badge
* Example: I should not see the "Live" badge
*
* @Then /^I should (not |)see the "([\w\s]+)" badge$/
* @param string $negative
* @param string $text
*/
public function iShouldSeeTheBadge($negative, $text)
{
if ($negative) {
$this->assertElementNotOnPage('.history-viewer .badge');
} else {
$this->assertElementContains('.history-viewer .badge', $text);
}
}

/**
* Example: I should see "ADMIN User" in the author column in version 1
*
* @Then I should see :text in the author column in version :versionNumber
*/
public function iShouldSeeInTheAuthorColumn($text, $versionNumber)
{
$version = $this->getSpecificVersion($versionNumber);
$authorColumn = $version->find('css', 'td:nth-of-type(3)');

$exists = strpos($authorColumn->getText(), $text) !== false;
assertTrue($exists, 'Author column contains ' . $text);
}

/**
* Example: I should see "Saved" in the record column in version 1
*
* @Then I should see :text in the record column in version :versionNumber
*/
public function iShouldSeeInTheRecordColumn($text, $versionNumber)
{
$version = $this->getSpecificVersion($versionNumber);
$recordColumn = $version->find('css', 'td:nth-of-type(2)');

$exists = strpos($recordColumn->getText(), $text) !== false;
assertTrue($exists, 'Record column contains ' . $text);
}

/**
* @Then I should see :text in the version column in version :versionNumber
*/
public function iShouldSeeInTheVersionColumn($text, $versionNumber)
{
$version = $this->getSpecificVersion($versionNumber);
$versionColumn = $version->find('css', 'td');

$exists = strpos($versionColumn->getText(), $text) !== false;
assertTrue($exists, 'Version column contains ' . $text);
}

/**
* Returns the table row that holds information on the most recent version
*/
protected function getLatestVersion()
{
$page = $this->getSession()->getPage();
return $page->find('css', '.history-viewer .table tbody tr');
}

/**
* Returns the table row that holds information on the selected version.
*
* @param int $versionNumber
*/
protected function getSpecificVersion($versionNumber)
{
$versionColumns = $this->getSession()->getPage()->findAll('css', '.history-viewer tbody tr');
foreach ($versionColumns as $version) {
if (strpos($version->getText(), $versionNumber) !== false) {
return $version;
}
}
}
}
52 changes: 52 additions & 0 deletions tests/Behat/features/list-view.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
@javascript
Feature: View a list of versions
As a cms author
I want to view a list past revisions of a versioned DataObject (incl. pages)

Background:
# Test date cannot be in the past or the version list numbers won't be descending
Given the current date is "2100-01-01"
Given a "page" "Home" with "Content"="Background"

Given I am logged in with "ADMIN" permissions
And I go to "/admin/pages"
And I click on "Home" in the tree

Scenario: A list of versions is displayed
Given I click on "History" in the header tabs
Then I should see a list of versions

Scenario: List shows the publish state, publish date and the author
Given I should see an edit page form
When I fill in the "Content" HTML field with "<p>Publish scenario</p>"
And I press the "Publish" button
Then I should see a "Published 'Home' successfully." notice
# This is a workaround @todo remove when https://github.com/silverstripe/silverstripe-cms/issues/2128 is resolved in framework
And I go to "/admin/pages/history/show/1"
Then I should see a list of versions
And I should see "ADMIN User" in the author column in version 1
And I should see "Published" in the record column in version 1
And I should see "01/01/2100" in the record column in version 1
And I should see the "Live" badge

Scenario: List shows the draft state, draft date and the author
Given I should see an edit page form
When I fill in the "Content" HTML field with "<p>Save scenario</p>"
And I press the "Save" button
Then I should see a "Saved 'Home' successfully." notice
# This is a workaround @todo remove when https://github.com/silverstripe/silverstripe-cms/issues/2128 is resolved in framework
And I go to "/admin/pages/history/show/1"
Then I should see a list of versions
And I should see "ADMIN User" in the author column in version 1
And I should see "Saved" in the record column in version 1
And I should see "01/01/2100" in the record column in version 1
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should add And I should not see the "Live" badge to ensure it doesn't show up when nothing is published yet? We can modify the regex for the And I should see the :arg badge step definition to support an optional not

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like the idea, but I am having trouble getting it to work at the moment. Below my approach:

/**
     * Example: I should see the "Live" badge
     * Example: I should not see the "Live" badge
     *
     * @Then /^I should( not? |\s*)see the :text badge
     * @param string $negative
     * @param string $text
     */
    public function iShouldSeeTheBadge($negative, $text)
    {
        if (trim($negative)) {
            $this->assertElementContains('.history-viewer .badge', $text);
        } else {
            $this->assertElementNotContains('.history-viewer .badge', $text);
        }
    }

How important do you reckon this is at this stage?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you need to name the regex capture group for it to be assigned to a variable, e.g.:

@Then /^I should (?P<negative>(?:(not |)))see the :text badge
...
public function iShouldSeeTheBadge($negative, $text)
{

And I should not see the "Live" badge

Scenario: Revisions are ordered descending by date
Given I should see an edit page form
When I fill in the "Content" HTML field with "<p>Order scenario</p>"
And I press the "Publish" button
Then I should see a "Published 'Home' successfully." notice
# This is a workaround @todo remove when https://github.com/silverstripe/silverstripe-cms/issues/2128 is resolved in framework
And I go to "/admin/pages/history/show/1"
Then I should see a list of versions in descending order
7 changes: 6 additions & 1 deletion tests/Behat/features/view-a-version.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ Feature: View a version

Scenario: I can view a selected version of a record
When I click on the first version
And I wait for 3 seconds until I see the "#Form_versionForm" element
Then I should see "Welcome to my website"

Scenario: The form fields shown are readonly
Scenario: Shows readonly version of all core form fields
Given I click on "History" in the header tabs
Then I should see a list of versions

When I click on the first version
And I wait for 3 seconds until I see the "#Form_versionForm" element
Then I should see an "#Form_versionForm_Title[readonly]" element
And I should see an "#Form_versionForm_URLSegment[readonly]" element