2

I am trying to create a test case against a patch which will be used for automated testing in D8 release.

However, I am trying to do it to check if the page I am on it, has any multiple escaped HTML element. The page where I am trying is following,

<table class="responsive-enabled">
  <thead>
    <tr>
      <th>Uninstall</th>
      <th>Name</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr class="odd">
      <td align="center">
        <div class="form-item form-type-checkbox form-item-uninstall-block form-disabled form-no-label">
          <label class="visually-hidden" for="edit-uninstall-block">Uninstall Block module</label>
          <input disabled="disabled" type="checkbox" id="edit-uninstall-block" name="uninstall[block]" value="1" class="form-checkbox" />
        </div>
      </td>
      <td>&lt;label for=&quot;edit-uninstall-block&quot; class=&quot;module-name table-filter-text-source&quot;&gt;Block&lt;/label&gt;</td>
      <td class="description">Controls the visual building blocks a page is constructed with. Blocks are boxes of content rendered into an area, or region, of a web page.&lt;div class=&quot;admin-requirements&quot;&gt;To uninstall Block, the following module must be uninstalled first: Custom Block&lt;/div&gt;</td>
    </tr>
    <tr class="even">
      <td align="center">
        <div class="form-item form-type-checkbox form-item-uninstall-breakpoint form-disabled form-no-label">
          <label class="visually-hidden" for="edit-uninstall-breakpoint">Uninstall Breakpoint module</label>
          <input disabled="disabled" type="checkbox" id="edit-uninstall-breakpoint" name="uninstall[breakpoint]" value="1" class="form-checkbox" />
        </div>
      </td>
      <td>&lt;label for=&quot;edit-uninstall-breakpoint&quot; class=&quot;module-name table-filter-text-source&quot;&gt;Breakpoint&lt;/label&gt;</td>
      <td class="description">Manage breakpoints and breakpoint groups for responsive designs.&lt;div class=&quot;admin-requirements&quot;&gt;To uninstall Breakpoint, the following module must be uninstalled first: Toolbar&lt;/div&gt;</td>
    </tr>
    <tr class="odd">
      <td align="center">
        <div class="form-item form-type-checkbox form-item-uninstall-ckeditor form-no-label">
          <label class="visually-hidden" for="edit-uninstall-ckeditor">Uninstall CKEditor module</label>
          <input type="checkbox" id="edit-uninstall-ckeditor" name="uninstall[ckeditor]" value="1" class="form-checkbox" />
        </div>
      </td>
      <td>&lt;label for=&quot;edit-uninstall-ckeditor&quot; class=&quot;module-name table-filter-text-source&quot;&gt;CKEditor&lt;/label&gt;</td>
      <td class="description">WYSIWYG editing for rich text fields using CKEditor.</td>
    </tr>
    <tr class="even">
      <td align="center">
        <div class="form-item form-type-checkbox form-item-uninstall-color form-no-label">
          <label class="visually-hidden" for="edit-uninstall-color">Uninstall Color module</label>
          <input type="checkbox" id="edit-uninstall-color" name="uninstall[color]" value="1" class="form-checkbox" />
        </div>
      </td>
      <td>&lt;label for=&quot;edit-uninstall-color&quot; class=&quot;module-name table-filter-text-source&quot;&gt;Color&lt;/label&gt;</td>
      <td class="description">Allows administrators to change the color scheme of compatible themes.</td>
    </tr>
  </tbody>
</table> 

From the code you can understand that it's the module's uninstall page. So in the RAW HTML we can see it has the double escaped HTML elements. Like < became &lt;.

The test file I am writing is the following,

<?php

/**
 * @file
 * Definition of Drupal\system\Tests\Module\UninstallPageLoad.
 */

namespace Drupal\system\Tests\Module;

use Drupal\Component\Utility\String;
use Drupal\simpletest\WebTestBase;

/**
 * Tests the module uninstall page load for multiple HTML escaping.
 *
 * @group Module
 */
class UninstallPageLoad extends WebTestBase {

  /**
   * Loads module uninstall page and checks HTML escaping
   */
  function testUninstallPageHtmlEscape() {
    // Create new admin user and test page load.


    $account = $this->drupalCreateUser(array('administer modules'));
    $this->drupalLogin($account);
    $this->drupalGet('admin/modules/uninstall');
    $this->assertResponse(200, 'Module Uninstall page loaded.');

    // Test the page if it has multiple HTML sanitizations.
    $this->assertNoRaw('&lt;label', 'Page should not have htmlentities');
  }
}

While executing the file I think I should get a failed result as the page do have many double escaped HTML characters. But I am getting a success.

EDIT: After Core debug

I think now I know the reason why this fails, When I added a debug code in core/modules/simpletest/src/AssertContentTrait.php in the method assertNoRaw it gave me the following RAW HTML.

<table class="responsive-enabled">
  <thead>
    <tr>
      <th>Uninstall</th>
      <th>Name</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr class="odd">
      <td colspan="3" class="empty message">No modules are available to uninstall.</td>
    </tr>
  </tbody>
</table>

So no modules are loading in this page. So now the question is how to replicate the page output same as theme_system_modules_uninstall from core/modules/system/system.admin.inc, line 290 page.

Can any one help me with this?

  • assertNoRaw() uses a basic strpos()...I just ran strpos($your_html_string, '&lt;label'); and got the correct result of 599. I would check what $this->getRawContent() contains (which is what assertNoRaw() checks the test string against), it sounds like that value is not actually the HTML code in your question – Clive Aug 5 '14 at 14:51
  • You are right Clive. I also did the same thing. It showed the same result as you said. But in the tests it's always false. I think I have to check in the core class where the value is checked. I also added some dummy content to the '#type' => 'table' and run the test against those contents but with no success. I'll post more once I debug the core class. – Aneek Mukhopadhyay Aug 5 '14 at 15:55
  • Try the 'stupid' test first - replace your search string with a single character that you know is in the HTML, and run the test for that. If it still doesn't work, you can be reasonably sure the raw content isn't what you're expecting – Clive Aug 5 '14 at 15:58
  • @Clive, please have a look at the edited section in the question. – Aneek Mukhopadhyay Aug 5 '14 at 16:10
2

To get output on the page, you first need to install some non-required modules.

A test uses the testing profile, which installs no modules except those that are required (system, user, field, entity), and you can't uninstall them.

So all you need to do is add a public static $modules = array('node') or any other non-required module to have some output show up there.

Note: Simpletest by default stores out the output of every page request you make as a static page, with a "Verbose output" link (only shown in the UI). Looking at that is a lot easier than debugging some code somewhere.

  • Okay, I will check on that. But the main question still remains as how can I use the rendered HTML from theme_system_modules_uninstall function into this test case? The issue I'm working is drupal.org/node/2305831 – Aneek Mukhopadhyay Aug 6 '14 at 5:55
  • I checked with the other test cases and I saw the page in "Verbose message" generated the faulty HTML. So I got a chance to add my test logic and it worked. Thanks for sharing the information about the testing profile. I've seen that test cases use their own testing modules but was not sure about is this a desired behavior. Thanks :) – Aneek Mukhopadhyay Aug 6 '14 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.