Problem/Motivation

Currently, we have two methods to create flags in FlagTestBase, createFlag() and createFlagFromForm(). While the latter provides full options, it is slow as it relies on the form. The createFlag() method, on the other hand, is limited.

Proposed resolution

Create a new method in FlagTestBase, createFlagFromArray(). This method provides sensible defaults, which can be overridden by a parameter.

Refactor createFlag() to use createFlagFromArray().

Remaining tasks

Create, test patch.

User interface changes

None.

API changes

Additional methods will be available on FlagTestBase.

Data model changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

socketwench created an issue. See original summary.

socketwench’s picture

Status: Active » Needs review
FileSize
3.21 KB

Also added createGlobalFlag() to simplify global flag tests.

socketwench’s picture

martin107’s picture

I think this change is worthwhile.

A few minor things

1) entiyManager is deprecated

the replacement is \Drupal::service('entity_type.bundle.info')

2) I want to lock down a few parameter input type that we are touching.
In short we should prefix inputs like $edit with array.
This could be done to other methods in the class .. but hey that is for another issue.

3) When I first glanced at createFlagFromArray() my first impression was ok cool that is a pattern I recognise.
I wonder if it is whitelisting key values pairs or if I am free to add values outside the default.

I want the annotations to try and answer that question without the next reader to having to inspect the code, so I added an extra sentence.

  • socketwench committed 1cdc240 on 8.x-4.x
    Issue #2701399 by martin107, socketwench: Changed FlagTestBase::...
joachim’s picture

  1. +++ b/src/Tests/FlagTestBase.php
    @@ -72,35 +72,77 @@ abstract class FlagTestBase extends WebTestBase {
    +  protected function createFlag($entity_type = 'node', array $bundles = [], $link_type = 'reload') {
    ...
    +  protected function createGlobalFlag($entity_type = 'node', array $bundles = [], $link_type = 'reload') {
    

    Logically, if we have both of these, then it should be create(Global|Personal)Flag.

  2. +++ b/src/Tests/FlagTestBase.php
    @@ -72,35 +72,77 @@ abstract class FlagTestBase extends WebTestBase {
    +  protected function createFlagFromArray(array $edit) {
    

    I considered adding something like this when I was working on tests last week.

    What made me reconsider was the thought that: a) it doesn't save on that much code, and b) it means that when you see createFlagFromArray([STUFF]) in a test, you then have to go and look in the base class to see what the defaults are for the properties not in STUFF. I am concerned that will make tests harder to read and understand.

socketwench’s picture

Status: Needs review » Fixed

entiyManager is deprecated

That's true, but there's an existing issue to fix this that needs review. But, *shrug* no reason to not fix it here.

Logically, if we have both of these, then it should be create(Global|Personal)Flag.

I thought about that, but that would require even more refactoring of existing code. The other option would be to just add a $global param to createFlag(). Honestly, I don't think it's that big of a deal.

I am concerned that will make tests harder to read and understand.

I think it will make it easier, since there'll be less boilerplate in most of the tests. That was going to be another issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.