Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Over at #69556: move "cvs access" tab into project, make it generic I added these methods to a class for project module to check that certain fields appear disabled correctly. I thought this would be a good fit for simpletest itself.
I don't know what sort of tests are required for the simpletest system itself. I'd wager that there are form system tests that could use these methods as well, but I haven't had time to look into it.
Comments
Comment #1
dwwA) I thought we stopped wrapping test assertions in t().
B) Seems like the text assertion for assertNoFieldDisabled() should say "Field @name is enabled" (not "disabled").
Otherwise, +1. Seems a lot better to have these assertions available to anyone who wants them than to duplicate them all over the place.
Thanks!
-Derek
Comment #2
mikey_p CreditAttribution: mikey_p commentedI think the patch to remove t() was rolled back? This currently matches the other assertions in drupal_web_test_case at least.
I went with 'not disabled' since that matches the grammar of the method name.
Comment #3
dwwRe: t() -- you're probably right. I haven't been following that closely.
Re: "not disabled". Ugh. The double negative is killing me. ;) Can't we go with
assertFieldEnabled()
and "Field @name is enabled"? I think it'd be a lot easier to use correctly (and debug) this way.Comment #4
salvisPlease excuse me for butting in. I just wrote this because I needed it and wanted to contribute it, and then I found this issue.
I modeled my version closely after assertFieldChecked(), except I reversed the logic and implemented assertFieldEnabled() rather than assertFieldDisabled(). This takes care dww's comments.
The other significant difference to mikey_p's patch in #2 is that I'm using
id
rather thanname
, as does the existing assertFieldChecked().Comment #5
salvisRerolled against HEAD.
Comment #6
mikey_p CreditAttribution: mikey_p commentedWhat about assertFieldEnabled() and assertFieldDisabled() ?
Comment #7
dww+1 to #6. That's what I meant at #3. I think it's safe to break the "No" convention for this, since (en|dis)abled are so obvious and self-documenting.
Comment #8
salvisOk, assertFieldEnabled() and assertFieldDisabled(), and re-rolled.
Comment #9
mikey_p CreditAttribution: mikey_p commentedWell I was going write some test for this, but I don't see any places where all the assertions in simpletest are checked. I could see adding some checks to FormElementTestCase, but I don't think this is required. Otherwise this looks great.
Comment #10
Dries CreditAttribution: Dries commentedThe word 'Fields' is a bit confusing as it is used in many different ways throughout Drupal.
It looks like you want to check whether Form Elements are enabled/disabled. If so, how about we call them
assertFormElementEnabled()
andassertFormElementDisabled()
?Comment #11
webchickComment #12
sunWe already use "Field" everywhere for (form) elements throughout DrupalWebTestCase.
However, we also already know that selecting fields by HTML ID is poor, because most of our HTML IDs are autogenerated. When dealing with form elements, assertion functions should always be based on the "name" attribute, since that is truly unique and won't change all of a sudden.
Overall, however, I don't find these assertion helpers really helpful.
assertFieldAttributesByName()
assertNoFieldAttributesByName()
i.e., not limited to "disabled", would be much more helpful.
Comment #13
joachim CreditAttribution: joachim commentedGiven how important the enabled/disabled case is for testing access control, I think having these two special cases and shortcuts makes good sense and more readable code in the tests too.
Comment #14
joachim CreditAttribution: joachim commentedLet's resurrect this.
Comment #15
joachim CreditAttribution: joachim commented> The word 'Fields' is a bit confusing as it is used in many different ways throughout Drupal.
True, but all the other form element-related assertions use 'Field' too: assertNoFieldById(), assertFieldChecked(), etc. I think it's better to be consistent within the assertion method names.
> Well I was going write some test for this, but I don't see any places where all the assertions in simpletest are checked.
Same here. WebTestBaseTest has testAssertFieldByName(), but doesn't test any of the other AssertField*() assertions.
Here's a reroll of the most recent patch.
Comment #16
dawehnerIt would be great if we don't introduce a method which supports IDs rather than names, given that IDs might vanish or be more or less random now.
Comment #17
thpoul CreditAttribution: thpoul as a volunteer commentedJust a nit.
s/@id/%id
Comment #18
joachim CreditAttribution: joachim commentedThanks for the reviews. I've made those changes, and also changed to use constructFieldXpath() rather than make my own xpath expression.
Comment #19
dawehnerThanks a ton!
Comment #20
joachim CreditAttribution: joachim commentedBTW primary credit should go to salvis, I just did rerolls and tweaks.
Comment #21
alexpottSafeMarkup::format() is deprecated...
I'm not entirely convinced we should be adding yet more API surface because:
Comment #22
salvisThank you for picking this up, joachim!
As long as the methods above and below recommend SafeMarkup::format() there's no point in diverging here.
Replacing references to SafeMarkup::format() should be a separate issue, at least in file scope or better for all of core.
If core has no disabled controls, that's nice, but in the real world you cannot do without them, and if you want to test the state, you simply need these methods.
I've given up five and a half years ago and just keep hacking core...
I think the RTBC still stands.
Comment #23
Wim Leers+1
But more importantly: nothing is using this! I think we should have at least a few tests actually using this, no?
Comment #24
joachim CreditAttribution: joachim commented> I think we should have at least a few tests actually using this, no?
Agreed.
> But more importantly: nothing is using this!
That's just because core doesn't have any forms that are complex enough. Lots of contrib does.
Comment #27
rajeevkNo changes, just re-rolling patch for 8.4.x
Comment #28
joachim CreditAttribution: joachim commentedShould these actually return anything? AFAICT, assertFoo() methods in PHPUnit don't return anything.
Comment #30
joachim CreditAttribution: joachim commentedGiven that Simpletest is now deprecated, I am inclined to mark this as WONTFIX.
PHPUnit browser tests have this functionality already, provided by Mink: https://api.drupal.org/api/drupal/core%21tests%21Drupal%21Tests%21WebAss...
Comment #31
dawehner@joachim
Thank you for following up here. I totally agree, we will not enhance simpletest anymore.
Comment #32
joachim CreditAttribution: joachim commentedOutdated, rather than fixed, surely?