API page: http://api.drupal.org/api/drupal/core%21modules%21simpletest%21lib%21Dru...

Enter a descriptive title (above) relating to WebTestBase::assertText, then describe the problem you have found:

Problem/Motivation

It says 'The text version is the equivalent of what a user would see when viewing through a web browser. '. This is not true, or at least after a user submits a form, what user just input could be seen from browser, but user's input won't be seen from 'The text', meaning user's input won't be fond by assertText, event they could be seen.

Steps to reproduce

  1. Install the latest Drupal 8.x using the standard profile and built-in English language.
  2. Enable Language, and Interface Translation module.
  3. Go to '/admin/config/regional/language', click 'Add language', and choose 'German'.
  4. Go to '/admin/config/regional/translate/translate', input 'String contains'-- 'Annnn', 'Translation language'--German, 'Search in'--Both.., Click filter.

We can see that while no result shows this string, string 'Annnn' is still in this input field and apparently we can see it, but in this context, when we run test code, $this->assertText('Annnn', t('Test will fail')); will give false. And $this->assertNoText('Annnn', t('Test will pass')); will give true. So, I think this problem also involves assertNoText

See http://api.drupal.org/api/drupal/core%21modules%21locale%21lib%21Drupal%... for real instance. I'm working on this test. The original author has fond this issue, but didn't bring it in

381.    // assertText() seems to remove the input field where $name always could be
382.    // found, so this is not a false assert. See how assertNoText succeeds
383.    // later.
384.    $this->assertText($name, t('Search found the string.'));

Proposed resolution

The following changes should be applied to the documentation of assertText/assertNoText. Suggestions of comment #1 include:

  • The documentation needs to point out that all tags are removed, including the attributes of some html elements
  • Add a reference to assertRaw/assertNoRaw, which allows to match to all html,including the attributes
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhodgdon’s picture

Title: AssertText/assertNoText doesn't concern user's input, even when they could be seen from browser. » AssertText/assertNoText doesn't really show what you could see in the browser (bad docs)
Issue tags: +Novice, +Needs backport to D7

Thanks for reporting this! I think that, as you've reported, it's a documentation issue:
- assertText/assertNoText do not really show you all the text that a person would see browsing to a page. For instance, form input is a value attribute of an HTML tag, and all tags are stripped out (including attributes), so assertText will not find that. The documentation needs to explain this, and it is currently wrong.
- I think there should be an @see link to assertRaw/assertNoRaw, which you can use for your purposes in this case, since it allows you to match against the full raw HTML (including value attributes of tags).

smiletrl’s picture

Hi, Currently, assertText works good for me:) And I agree that there should be an @see link to assertRaw/assertNoRaw, which would help the clarification much.

Anonymous’s picture

gauravkhambhala’s picture

API Page: http://api.drupal.org/api/drupal/core%21modules%21simpletest%21lib%21Dru... can not be found.

Could you help me get the correct link?

jhodgdon’s picture

What happened is that this issue was filed 2 years ago and the method moved to a different class. So, this situation of a link in an issue to api.drupal.org not working is fairly common in Drupal 8 development, especially for issues as old as this.

To find the new class/method: Go to api.drupal.org and search for "assertText" in Drupal 8. You will find several results; two of them are for methods called assertText and others are for methods called assertText* (with a suffix). Click through to the two methods that are actually called assertText and see which one has the problem; you'll find that it is AssertContentTrait::assertText().

Also note that assertNoText needs a patch; that should be on the same class/trait.

roderik’s picture

Issue tags: +Amsterdam 2014, +Needs issue summary update

Tagging for Amsterdam. Issue summary needs update with what is the actual problem, which jhodgdon explained in #1 - then fixing it (documenting things better).

pieterjd’s picture

Issue summary: View changes

As I am attending the mentored sprint session at DrupalCon Amsterdam, I added a proposed resolution section to the original issue post.

pieterjd’s picture

oenie’s picture

Issue tags: -Amsterdam 2014 +Amsterdam2014

fixing the amsterdam sprint tag to amsterdam2014

jabberwooki’s picture

Status: Active » Needs review
Issue tags: -Amsterdam2014 +drupaldevdays
FileSize
2.09 KB

First patch proposal from the Drupal Dev Days Montpellier 2015.
Hope this might help.

jhodgdon’s picture

Status: Needs review » Needs work

Great start, thanks! I have a few suggestions:

  1. +++ b/core/modules/simpletest/src/AssertContentTrait.php
    @@ -485,9 +485,9 @@ protected function assertNoEscaped($raw, $message = '', $group = 'Other') {
    +   * HTML tags in this text will be stripped out of the contents.
    +   * This includes e.g. text inside input elements, so use assertRaw()
    +   * to find text inside / including HTML.
        *
    

    This is factually correct and gives the information that we need, great! But it needs a little editing for English grammar/pronunciation/style... and maybe it is a bit confusing to say the HTML tags are stripped... Maybe we need to change the first line too, so instead of saying "text version" it is clearer?

    How about:

    Passes if the page (with HTML stripped) contains the text.

    Note that stripping HTML tags also removes their attributes, such as the values of text fields.

    [I think we can omit the text that says "use assertRaw..." because we have the @see below]

  2. +++ b/core/modules/simpletest/src/AssertContentTrait.php
    @@ -503,6 +503,8 @@ protected function assertNoEscaped($raw, $message = '', $group = 'Other') {
    +   *
    +   * @see assertRaw()
        */
    

    We need to include the namespaced class name here, not just reference the method name.

  3. +++ b/core/modules/simpletest/src/AssertContentTrait.php
    @@ -511,9 +513,9 @@ protected function assertText($text, $message = '', $group = 'Other') {
    +   * HTML tags in this text will be stripped out of the contents.
    +   * This includes e.g. text inside input elements, so use assertNoRaw()
    +   * to find text inside / including HTML.
        *
    

    Same as in assertText.

  4. +++ b/core/modules/simpletest/src/AssertContentTrait.php
    @@ -529,6 +531,8 @@ protected function assertText($text, $message = '', $group = 'Other') {
    +   *
    +   * @see assertNoRaw()
        */
    

    Needs namespace and class here too.

jabberwooki’s picture

I'm new to namespace concept.
In our current task, does it mean that I have to replace

@see assertRaw()

with

@see core\modules\simpletest\src\AssertContentTrait\assertRaw()

?

jhodgdon’s picture

It's actually:

@see \Drupal\simpletest\AssertContentTrait::assertRaw()

Read about namespaces here:
https://api.drupal.org/api/drupal/core!modules!system!core.api.php/group...
(if you need background information, try some of the links in that text, such as the first one that takes you to PHP.net.

jhodgdon’s picture

You can find the namespace for a class at the top of the file in a line like

namespace Drupal\simpletest\AssertContentTrait;

But in documentation we prefix this with a \
and the method should be separated from the class name with a ::

So in summary, the method assertRaw() on AssertContentTrait would look like:
@see \Drupal\simpletest\AssertContentTrait::assertRaw()

Hope this helps...

jabberwooki’s picture

Status: Needs work » Needs review
FileSize
2.35 KB

@jhodgdon All modifications, as proposed in your comment #11 are done and I've generated the corresponding patch.
Thank you for your patience and your advices to a newbie.

Just a last question.
As mentionned in the link about namespaces you gave, shouldn't the @see mention be written down as follows ?
@see \Drupal\Core\simpletest\AssertContentTrait::assertRaw()

that is, whith ...\Core\..., the simpletest module belonging to Core ?

jhodgdon’s picture

Status: Needs review » Needs work

No. Take a look at the namespace line at the top of this AssertContentTrait file. ;)

Anyway, the patch looks pretty good! A few things to fix:

  1. +++ b/core/modules/simpletest/src/AssertContentTrait.php
    @@ -392,6 +392,8 @@ protected function assertNoLinkByHref($href, $message = '', $group = 'Other') {
    +   *
    +   * @see
        */
    

    This line is incomplete.

  2. +++ b/core/modules/simpletest/src/AssertContentTrait.php
    @@ -503,17 +504,18 @@ protected function assertNoEscaped($raw, $message = '', $group = 'Other') {
    +   * Passes if the page (with HTML stripped) doesn't contains the text.
    

    Grammar: Should be "doesn't contain"

keopx’s picture

Assigned: Unassigned » keopx
keopx’s picture

Assigned: keopx » Unassigned
Status: Needs work » Needs review
FileSize
780 bytes
2.4 KB
jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me, thanks very much!

xjm’s picture

Status: Reviewed & tested by the community » Needs work

Thanks everyone. Just one thing looks out of place here:

+++ b/core/modules/simpletest/src/AssertContentTrait.php
@@ -392,6 +392,8 @@ protected function assertNoLinkByHref($href, $message = '', $group = 'Other') {
+   *
+   * @see \Drupal\simpletest\AssertContentTrait::assertRaw()
    */
   protected function assertRaw($raw, $message = '', $group = 'Other') {

It looks like we're adding an @see on this method to itself?

joshi.rohit100’s picture

Status: Needs work » Needs review
FileSize
2.08 KB
480 bytes
keopx’s picture

Status: Needs review » Reviewed & tested by the community

  • xjm committed e0f69cc on 8.0.x
    Issue #1882788 by jabberwooki, keopx, joshi.rohit100, jhodgdon:...
xjm’s picture

Title: AssertText/assertNoText doesn't really show what you could see in the browser (bad docs) » assertText/assertNoText doesn't really show what you could see in the browser (bad docs)
Status: Reviewed & tested by the community » Fixed

This issue only changes documentation, so per https://www.drupal.org/core/beta-changes, this can be completed any time during the Drupal 8 beta phase. Committed and pushed to 8.0.x. Thanks!

David_Rothstein’s picture

Version: 8.0.x-dev » 7.x-dev
Status: Fixed » Patch (to be ported)

  • xjm committed e0f69cc on 8.1.x
    Issue #1882788 by jabberwooki, keopx, joshi.rohit100, jhodgdon:...

  • xjm committed e0f69cc on 8.3.x
    Issue #1882788 by jabberwooki, keopx, joshi.rohit100, jhodgdon:...

  • xjm committed e0f69cc on 8.3.x
    Issue #1882788 by jabberwooki, keopx, joshi.rohit100, jhodgdon:...
stefan.r’s picture

Issue tags: +Dublin2016

Tagging the backport to 7.x as a novice task

  • xjm committed e0f69cc on 8.4.x
    Issue #1882788 by jabberwooki, keopx, joshi.rohit100, jhodgdon:...

  • xjm committed e0f69cc on 8.4.x
    Issue #1882788 by jabberwooki, keopx, joshi.rohit100, jhodgdon:...