Skip to content

Commit

Permalink
NEW LinkField Behat tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sabina Talipova committed Feb 5, 2024
1 parent 03ffff8 commit 7fde492
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 1 deletion.
31 changes: 31 additions & 0 deletions behat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Run linkfield behat tests with this command
# Note that linkfield behat tests require CMS module
# ========================================================================= #
# chromedriver
# vendor/bin/behat @linkfield
# ========================================================================= #
default:
suites:
linkfield:
paths:
- '%paths.modules.linkfield%/tests/behat/features'
contexts:
- SilverStripe\Framework\Tests\Behaviour\FeatureContext
- SilverStripe\Framework\Tests\Behaviour\CmsFormsContext
- SilverStripe\Framework\Tests\Behaviour\CmsUiContext
- SilverStripe\BehatExtension\Context\BasicContext
- SilverStripe\BehatExtension\Context\LoginContext
- SilverStripe\BehatExtension\Context\FixtureContext:
- '%paths.modules.linkfield%/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

SilverStripe\BehatExtension\Extension:
screenshot_path: '%paths.base%/artifacts/screenshots'
bootstrap_file: vendor/silverstripe/framework/tests/behat/serve-bootstrap.php
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"autoload": {
"psr-4": {
"SilverStripe\\LinkField\\": "src/",
"SilverStripe\\LinkField\\Tests\\": "tests/php/"
"SilverStripe\\LinkField\\Tests\\": "tests/php/",
"SilverStripe\\LinkField\\Tests\\Behat\\": "tests/behat/"
}
},
"config": {
Expand Down
78 changes: 78 additions & 0 deletions tests/behat/Blocks/LinksListBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace SilverStripe\LinkField\Tests\Behat\Blocks;

use DNADesign\Elemental\Models\BaseElement;
use SilverStripe\Dev\TestOnly;
use SilverStripe\LinkField\Form\LinkField;
use SilverStripe\LinkField\Form\MultiLinkField;
use SilverStripe\LinkField\Models\Link;
use SilverStripe\LinkField\Models\EmailLink;
use SilverStripe\LinkField\Models\PhoneLink;
use SilverStripe\LinkField\Models\SiteTreeLink;

class LinksListBlock extends BaseElement //implements TestOnly
{
private static string $icon = 'font-icon-block-file-list';

private static string $table_name = 'LinksListBlock';

private static string $singular_name = 'Links List Block';

private static string $plural_name = 'Links List Blocks';

private static string $description = 'Displays a set of links which can be internal page links, external urls or '
. 'file links.';

private static bool $inline_editable = true;

private static array $has_one = [
'OneLink' => Link::class,
];

private static $has_many = [
'ManyLinks' => Link::class . '.Owner',
];

private static array $owns = [
'OneLink',
'ManyLinks',
];

private static array $cascade_deletes = [
'OneLink',
'ManyLinks',
];

private static array $cascade_duplicates = [
'OneLink',
'ManyLinks',
];

public function getType(): string
{
return _t(self::class . '.BlockType', 'Links List');
}

public function getCMSFields()
{
$fields = parent::getCMSFields();

$fields->removeByName(['OneLinkID', 'ManyLinks']);

$fields->addFieldsToTab(
'Root.Main',
[
LinkField::create('OneLink')
->setAllowedTypes([
SiteTreeLink::class,
EmailLink::class,
PhoneLink::class
]),
MultiLinkField::create('ManyLinks'),
],
);

return $fields;
}
}
61 changes: 61 additions & 0 deletions tests/behat/PageTypes/LinkPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace SilverStripe\LinkField\Tests\Behat\PageTypes;

use Page;
use SilverStripe\Dev\TestOnly;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\LinkField\Models\Link;
use SilverStripe\LinkField\Form\LinkField;
use SilverStripe\LinkField\Form\MultiLinkField;
use SilverStripe\LinkField\Models\EmailLink;
use SilverStripe\LinkField\Models\PhoneLink;
use SilverStripe\LinkField\Models\SiteTreeLink;

class LinkPage extends SiteTree implements TestOnly
{
private static array $has_one = [
'HasOneLink' => Link::class,
];

private static $has_many = [
'HasManyLinks' => Link::class . '.Owner',
];

private static array $owns = [
'HasOneLink',
'HasManyLinks',
];

private static array $cascade_deletes = [
'HasOneLink',
'HasManyLinks',
];

private static array $cascade_duplicates = [
'HasOneLink',
'HasManyLinks',
];

public function getCMSFields()
{
$fields = parent::getCMSFields();

$fields->removeByName(['Content', 'HasOneLinkID', 'HasManyLinks']);

$fields->addFieldsToTab(
'Root.Main',
[
LinkField::create('HasOneLink')
->setAllowedTypes([
SiteTreeLink::class,
EmailLink::class,
PhoneLink::class
]),
MultiLinkField::create('HasManyLinks'),
],
);

return $fields;
}
}
134 changes: 134 additions & 0 deletions tests/behat/features/create-edit-linkfield.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
Feature: Create Links in LinkField and MultiLinkField
As a content editor
I want to add links to pages, files, external URLs, email addresses and phone numbers

Background:
Given a "SilverStripe\LinkField\Tests\Behat\PageTypes\LinkPage" "Link Page"
And a "page" "About Us"
And the "group" "EDITOR" has permissions "Access to 'Pages' section"
And I am logged in as a member of "EDITOR" group
And I go to "/admin/pages"
And I should see "Link Page"
And I click on "Link Page" in the tree

Scenario: I click on the link fields and see the list of allowed link types with icons for LinkFields
Given I should see the "#Form_EditForm_HasManyLinks" element
And I should see the "#Form_EditForm_HasOneLink" element

# Test limited list of link types are present in correct order in the dropdown

When I click on the "[data-field-id='Form_EditForm_HasOneLink'] button" element
Then I should see the "[data-field-id='Form_EditForm_HasOneLink'] .dropdown-menu.show" element

And I should see the "[data-field-id='Form_EditForm_HasOneLink'] .dropdown-item:nth-of-type(1) .font-icon-page" element
And I should see "Page on this site" in the "[data-field-id='Form_EditForm_HasOneLink'] .dropdown-item:nth-of-type(1)" element

And I should see the "[data-field-id='Form_EditForm_HasOneLink'] .dropdown-item:nth-of-type(2) .font-icon-p-mail" element
And I should see "Link to email address" in the "[data-field-id='Form_EditForm_HasOneLink'] .dropdown-item:nth-of-type(2)" element

And I should see the "[data-field-id='Form_EditForm_HasOneLink'] .dropdown-item:nth-of-type(3) .font-icon-mobile" element
And I should see "Phone number" in the "[data-field-id='Form_EditForm_HasOneLink'] .dropdown-item:nth-of-type(3)" element

# Test full list of link types are present in correct order in the dropdown

When I click on the "[data-field-id='Form_EditForm_HasManyLinks'] button" element
Then I should see the "[data-field-id='Form_EditForm_HasManyLinks'] .dropdown-menu.show" element

And I should see the "[data-field-id='Form_EditForm_HasManyLinks'] .dropdown-item:nth-of-type(1) .font-icon-page" element
And I should see "Page on this site" in the "[data-field-id='Form_EditForm_HasManyLinks'] .dropdown-item:nth-of-type(1)" element

And I should see the "[data-field-id='Form_EditForm_HasManyLinks'] .dropdown-item:nth-of-type(2) .font-icon-image" element
And I should see "Link to a file" in the "[data-field-id='Form_EditForm_HasManyLinks'] .dropdown-item:nth-of-type(2)" element

And I should see the "[data-field-id='Form_EditForm_HasManyLinks'] .dropdown-item:nth-of-type(3) .font-icon-external-link" element
And I should see "Link to external URL" in the "[data-field-id='Form_EditForm_HasManyLinks'] .dropdown-item:nth-of-type(3)" element

And I should see the "[data-field-id='Form_EditForm_HasManyLinks'] .dropdown-item:nth-of-type(4) .font-icon-p-mail" element
And I should see "Link to email address" in the "[data-field-id='Form_EditForm_HasManyLinks'] .dropdown-item:nth-of-type(4)" element

And I should see the "[data-field-id='Form_EditForm_HasManyLinks'] .dropdown-item:nth-of-type(5) .font-icon-mobile" element
And I should see "Phone number" in the "[data-field-id='Form_EditForm_HasManyLinks'] .dropdown-item:nth-of-type(5)" element

# Test that user can create email link in LinkField

When I click on the "[data-field-id='Form_EditForm_HasOneLink'] button" element
Then I should see "Link to email address" in the "[data-field-id='Form_EditForm_HasOneLink'] .dropdown-item:nth-of-type(2)" element
When I click on the "[data-field-id='Form_EditForm_HasOneLink'] .dropdown-item:nth-of-type(2)" element
And I wait for 2 seconds
Then I should see "Link to email address" in the ".modal-header" element
And I wait for 2 seconds
Then I fill in "Form_LinkForm_0_Title" with "Email link"
And I fill in "Form_LinkForm_0_Email" with "[email protected]"
And I should not see "Open in new window" in the ".modal-content" element
And I press the "Create link" button
And I wait for 2 seconds

# Create SiteTreeLink in MultiLinkField

When I click on the "[data-field-id='Form_EditForm_HasManyLinks'] button" element
Then I should see "Page on this site" in the "[data-field-id='Form_EditForm_HasManyLinks'] .dropdown-menu.show" element
When I click on the "[data-field-id='Form_EditForm_HasManyLinks'] .dropdown-item:nth-of-type(1)" element
Then I should see "Page on this site" in the ".modal-header" element
And I wait for 2 seconds
Then I fill in "Form_LinkForm_0_Title" with "About Us"
And I select "About Us" in the "#Form_LinkForm_0_PageID_Holder" tree dropdown
And I fill in "Form_LinkForm_0_QueryString" with "option=value"
And I check "Open in new window"
And I press the "Create link" button
And I wait for 2 seconds


# Create ExternalLink in MultiLinkField

When I click on the "[data-field-id='Form_EditForm_HasManyLinks'] button" element
Then I should see "Link to external URL" in the "[data-field-id='Form_EditForm_HasManyLinks'] .dropdown-item:nth-of-type(3)" element
When I click on the "[data-field-id='Form_EditForm_HasManyLinks'] .dropdown-item:nth-of-type(3)" element
And I wait for 2 seconds
Then I should see "Link to external URL" in the ".modal-header" element
And I wait for 2 seconds
Then I fill in "Form_LinkForm_0_Title" with "External URL"
Then I fill in "Form_LinkForm_0_ExternalUrl" with "https://www.silverstripe.org"
And I check "Open in new window"
And I press the "Create link" button
And I wait for 2 seconds

# Test that all links are created

# Link ID 1
Then I should see "Email link" in the "[data-field-id='Form_EditForm_HasOneLink']" element
And I should see "[email protected]" in the "[data-field-id='Form_EditForm_HasOneLink'] .link-picker__link" element
And I should see "Draft" in the "[data-field-id='Form_EditForm_HasOneLink']" element

# Link ID 2
And I should see "About Us" in the "[data-field-id='Form_EditForm_HasManyLinks'] .link-picker__link--is-first" element
And I should see "about-us" in the "[data-field-id='Form_EditForm_HasManyLinks'] .link-picker__link--is-first" element
And I should see "Draft" in the "[data-field-id='Form_EditForm_HasManyLinks'] .link-picker__link--is-first" element

# Link ID 3
And I should see "External URL" in the "[data-field-id='Form_EditForm_HasManyLinks'] .link-picker__link--is-last" element
And I should see "https://www.silverstripe.org" in the "[data-field-id='Form_EditForm_HasManyLinks'] .link-picker__link--is-last" element
And I should see "Draft" in the "[data-field-id='Form_EditForm_HasManyLinks'] .link-picker__link--is-last" element

# Test that user can publish the page with links

When I press the "Publish" button
And I wait for 2 seconds
And I should not see "Draft" in the "[data-field-id='Form_EditForm_HasOneLink']" element
And I should not see "Draft" in the "[data-field-id='Form_EditForm_HasManyLinks']" element

# Test that user can edit the links

When I click on the "[data-field-id='Form_EditForm_HasManyLinks'] .link-picker__link--is-first button" element
Then I should see "Page on this site" in the ".modal-header" element
Then I fill in "Form_LinkForm_2_Title" with "All about us"
And I press the "Update link" button
And I wait for 2 seconds
And I should see "All about us" in the "[data-field-id='Form_EditForm_HasManyLinks'] .link-picker__link--is-first" element
And I should see "Modified" in the "[data-field-id='Form_EditForm_HasManyLinks'] .link-picker__link--is-first" element

# Test that user can reorder the links

And I drag the "[data-field-id='Form_EditForm_HasManyLinks'] .link-picker__link--is-first" element to the "[data-field-id='Form_EditForm_HasManyLinks'] .link-picker__link--is-last" element
And I wait for 3 seconds
And I should see "All about us" in the "[data-field-id='Form_EditForm_HasManyLinks'] .link-picker__link--is-last" element
And I should see "External URL" in the "[data-field-id='Form_EditForm_HasManyLinks'] .link-picker__link--is-first" element
Binary file added tests/behat/features/files/file1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 7fde492

Please sign in to comment.