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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dww’s picture

Status: Needs review » Needs work

A) 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

mikey_p’s picture

I 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.

dww’s picture

Re: 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.

salvis’s picture

Status: Needs work » Needs review
FileSize
1.59 KB

Please 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 than name, as does the existing assertFieldChecked().

salvis’s picture

Rerolled against HEAD.

mikey_p’s picture

What about assertFieldEnabled() and assertFieldDisabled() ?

dww’s picture

Status: Needs review » Needs work

+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.

salvis’s picture

Status: Needs work » Needs review
FileSize
1.59 KB

Ok, assertFieldEnabled() and assertFieldDisabled(), and re-rolled.

mikey_p’s picture

Status: Needs review » Reviewed & tested by the community

Well 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.

Dries’s picture

The 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() and assertFormElementDisabled()?

webchick’s picture

Status: Reviewed & tested by the community » Needs work
sun’s picture

We 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.

joachim’s picture

Given 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.

joachim’s picture

Version: 7.x-dev » 8.2.x-dev
Issue summary: View changes

Let's resurrect this.

joachim’s picture

> 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.

dawehner’s picture

It 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.

thpoul’s picture

Just a nit.

+++ b/core/modules/simpletest/src/AssertContentTrait.php
@@ -1173,6 +1173,44 @@ protected function assertNoFieldById($id, $value = '', $message = '', $group = '
+    return $this->assertTrue(isset($elements[0]) && empty($elements[0]['disabled']), $message ? $message : t('Field @id is enabled.', array('@id' => $id)), t('Browser'));
...
+    return $this->assertTrue(isset($elements[0]) && !empty($elements[0]['disabled']), $message ? $message : t('Field @id is disabled.', array('@id' => $id)), t('Browser'));

s/@id/%id

joachim’s picture

Thanks for the reviews. I've made those changes, and also changed to use constructFieldXpath() rather than make my own xpath expression.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Thanks a ton!

joachim’s picture

BTW primary credit should go to salvis, I just did rerolls and tweaks.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/simpletest/src/AssertContentTrait.php
@@ -1173,6 +1173,44 @@ protected function assertNoFieldById($id, $value = '', $message = '', $group = '
+   *   messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
...
+   *   messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed

SafeMarkup::format() is deprecated...

   * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
   *   Use \Drupal\Component\Render\FormattableMarkup.

I'm not entirely convinced we should be adding yet more API surface because:

  • There are no usages in core so we're adding unused and untested methods
  • we're (slowly) moving BrowserTestBase
salvis’s picture

Status: Needs work » Reviewed & tested by the community

Thank you for picking this up, joachim!

SafeMarkup::format() is deprecated...

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.

I'm not entirely convinced we should be adding yet more API surface because:

There are no usages in core so we're adding unused and untested methods

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.

we're (slowly) moving BrowserTestBase

I've given up five and a half years ago and just keep hacking core...

I think the RTBC still stands.

Wim Leers’s picture

Title: Add assertions for disabled fields » Add assertion helpers to AssertContentTrait for disabled/enabled fields
Priority: Normal » Minor
Status: Reviewed & tested by the community » Needs work

I'm not entirely convinced we should be adding yet more API surface because:

+1

But more importantly: nothing is using this! I think we should have at least a few tests actually using this, no?

joachim’s picture

> 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.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

rajeevk’s picture

Status: Needs work » Needs review
FileSize
2.07 KB

No changes, just re-rolling patch for 8.4.x

joachim’s picture

+++ b/core/modules/simpletest/src/AssertContentTrait.php
@@ -1168,6 +1168,44 @@ protected function assertNoFieldById($id, $value = '', $message = '', $group = '
+    return $this->assertTrue(isset($elements[0]) && empty($elements[0]['disabled']), $message ? $message : t('Field %name is enabled.', array('%name' => $name)), t('Browser'));
...
+    return $this->assertTrue(isset($elements[0]) && !empty($elements[0]['disabled']), $message ? $message : t('Field %name is disabled.', array('%name' => $name)), t('Browser'));

Should these actually return anything? AFAICT, assertFoo() methods in PHPUnit don't return anything.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

joachim’s picture

Status: Needs review » Needs work

Given 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...

dawehner’s picture

Status: Needs work » Fixed

@joachim
Thank you for following up here. I totally agree, we will not enhance simpletest anymore.

joachim’s picture

Status: Fixed » Closed (outdated)

Outdated, rather than fixed, surely?