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

MNT Behat test LinkField page and LinkField block #162

Conversation

sabina-talipova
Copy link
Contributor

@GuySartorelli
Copy link
Member

GuySartorelli commented Feb 5, 2024

Both of these should be able to be added as TestOnly extensions directly in linkfield. Happy to help get that working if you need it.
Edit: Turns out you can't do that if the extension is affecting the database. 😓

Copy link
Member

@emteknetnz emteknetnz left a comment

Choose a reason for hiding this comment

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

Convert the block to an extension that's applied to ElementContent (not BaseElement) and move these classes to the LinkField PR

Also add relevant namespaces to classes after you've done this

Comment on lines 12 to 14
use SilverStripe\ORM\DataExtension;

class LinkPageExtension extends DataExtension
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
use SilverStripe\ORM\DataExtension;
class LinkPageExtension extends DataExtension
use SilverStripe\ORM\Extension;
use SilverStripe\Dev\TestOnly;
class LinkPageExtension extends Extension implements TestOnly

DataExtension will be removed in a future version

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

Comment on lines 1 to 77
<?php

namespace SilverStripe\FrameworkTest\LinkField\Blocks;

use DNADesign\Elemental\Models\BaseElement;
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
{
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;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<?php
namespace SilverStripe\FrameworkTest\LinkField\Blocks;
use DNADesign\Elemental\Models\BaseElement;
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
{
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;
}
}
<?php
use SilverStripe\Core\Extension;
use SilverStripe\Dev\TestOnly;
use SilverStripe\LinkField\Models\SiteTreeLink;
use SilverStripe\LinkField\Models\EmailLink;
use SilverStripe\LinkField\Models\PhoneLink;
use SilverStripe\LinkField\Form\MultiLinkField;
use SilverStripe\LinkField\Form\LinkField;
use SilverStripe\LinkField\Models\Link;
class ElementContentExtension extends Extension implements TestOnly
{
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 updateCMSFields($fields)
{
$fields->removeByName(['OneLinkID', 'ManyLinks']);
$fields->addFieldsToTab(
'Root.Main',
[
LinkField::create('OneLink')
->setAllowedTypes([
SiteTreeLink::class,
EmailLink::class,
PhoneLink::class
]),
MultiLinkField::create('ManyLinks'),
],
);
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated. Unfortunately, we cannot use TestOnly. Please see this comment. I created new issue for this.

Copy link
Contributor Author

@sabina-talipova sabina-talipova Feb 9, 2024

Choose a reason for hiding this comment

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

@emteknetnz, for using Extension in these test classes I need to rebuild db, since I need to add new relations between classes. For doing this I need to update this block of code, that can be the potential problem for another behat tests in another modules. Do you want me to update FixtureContext as part of this PR? Or if we are planning to get rid of DataExtension in the future then we will make required updates and all test classes will extend DataExtension for now?
Note: As temporary solution I can enforce execution of dev/build as additional step. What would you prefer?

Copy link
Member

@emteknetnz emteknetnz Feb 12, 2024

Choose a reason for hiding this comment

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

Don't worry about refactoring other things right now. For now simply make this a regular Extension and add a step to manually run dev/build

Does this work even with TestOnly though? If it doesn't could you double check with Guy who seems pretty certain that shouldn't need to use non-TestOnly in frameworktest

@sabina-talipova sabina-talipova force-pushed the pulls/1/support-behat-test-linkfield branch from 04f6755 to 7d21d8c Compare February 8, 2024 23:21
Copy link
Member

@emteknetnz emteknetnz left a comment

Choose a reason for hiding this comment

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

Ideally we don't need this PR, and all the code here should be in linkfield as TestOnly extensions.

If you're having trouble getting that to work please double check with Guy about the specifics of getting this to work

@sabina-talipova
Copy link
Contributor Author

sabina-talipova commented Feb 12, 2024

Ideally we don't need this PR, and all the code here should be in linkfield as TestOnly extensions.

If you're having trouble getting that to work please double check with Guy about the specifics of getting this to work

We have already discussed this issue last week and I added new ticket silverstripe/silverstripe-behat-extension#260 to fix this issue. Unfortunately, there are some problems with getting has_one \ has_many properties of mock classes in Behat tests.

Copy link
Member

@emteknetnz emteknetnz left a comment

Choose a reason for hiding this comment

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

These files need to be in the LinkField PR, and this PR should be closed

I tested this locally, while it's true you cannot use TestOnly classes within framework test, if you added them into LinkField they will work

For instance add LinkPageExtension to vendor/silverstripe/linkfield/tests/php/Extensions/LinkFieldExtension.php and change the namespace to SilverStripe\LinkField\Tests\Extensions\LinkPageExtension and make it implement TestOnly, and update the behat test to add this extension. The behat test will work correctly.

use SilverStripe\LinkField\Models\EmailLink;
use SilverStripe\LinkField\Models\PhoneLink;
use SilverStripe\LinkField\Models\SiteTreeLink;
use SilverStripe\ORM\DataExtension;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
use SilverStripe\ORM\DataExtension;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

Comment on lines 47 to 51
LinkField::create('HasOneLink')
->setAllowedTypes([
SiteTreeLink::class,
EmailLink::class,
PhoneLink::class
]),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
LinkField::create('HasOneLink')
->setAllowedTypes([
SiteTreeLink::class,
EmailLink::class,
PhoneLink::class
]),
LinkField::create('HasOneLink')
->setAllowedTypes([
SiteTreeLink::class,
EmailLink::class,
PhoneLink::class
]),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

@sabina-talipova
Copy link
Contributor Author

These files need to be in the LinkField PR, and this PR should be closed

I tested this locally, while it's true you cannot use TestOnly classes within framework test, if you added them into LinkField they will work

For instance add LinkPageExtension to vendor/silverstripe/linkfield/tests/php/Extensions/LinkFieldExtension.php and change the namespace to SilverStripe\LinkField\Tests\Extensions\LinkPageExtension and make it implement TestOnly, and update the behat test to add this extension. The behat test will work correctly.

@emteknetnz, Did all tests run successfully? I still have the same problem with has_one \ has_many.

create-edit-linkfield feature_23

@emteknetnz
Copy link
Member

@sabina-talipova OK don't worry about trying to get these into linkfield module, it really is technically just too hard

There's still a couple of remaining minor changes on this PR

@sabina-talipova sabina-talipova force-pushed the pulls/1/support-behat-test-linkfield branch from 7d21d8c to 7318985 Compare February 13, 2024 00:01
@sabina-talipova sabina-talipova force-pushed the pulls/1/support-behat-test-linkfield branch from 7318985 to 60230da Compare February 13, 2024 00:02
@emteknetnz emteknetnz merged commit f8eac08 into silverstripe:1 Feb 13, 2024
@emteknetnz emteknetnz deleted the pulls/1/support-behat-test-linkfield branch February 13, 2024 01:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants